game_machine 0.0.11 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (274) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +12 -1
  3. data/Gemfile.lock +32 -47
  4. data/Rakefile +0 -27
  5. data/bin/bundle_run.sh +1 -0
  6. data/bin/game_machine +29 -27
  7. data/config/cluster.conf +6 -5
  8. data/config/default.conf +164 -0
  9. data/config/game_machine.sql +33 -0
  10. data/config/game_messages.proto +87 -25
  11. data/config/gamecloud.conf +140 -0
  12. data/config/messages.proto +46 -53
  13. data/config/test.conf +149 -0
  14. data/game_machine.gemspec +10 -5
  15. data/games/boot.rb +3 -0
  16. data/games/example/data/game_data.yml +4 -4
  17. data/games/example/lib/aggressive_npc.rb +1 -1
  18. data/games/example/lib/game.rb +1 -2
  19. data/games/example/lib/player_register.rb +1 -1
  20. data/games/routes.rb +9 -0
  21. data/games/tutorial/boot.rb +12 -0
  22. data/games/tutorial/item_manager.rb +256 -0
  23. data/games/tutorial/object_store.rb +55 -0
  24. data/games/tutorial/seed.rb +52 -0
  25. data/games/tutorial/sql_store.rb +30 -0
  26. data/java/project/build.gradle +134 -0
  27. data/java/project/component.erb +719 -0
  28. data/java/{gradle.properties → project/gradle.properties} +1 -1
  29. data/java/project/gradle/wrapper/gradle-wrapper.jar +0 -0
  30. data/java/{gradle → project/gradle}/wrapper/gradle-wrapper.properties +2 -2
  31. data/java/{gradlew → project/gradlew} +0 -0
  32. data/java/{gradlew.bat → project/gradlew.bat} +0 -0
  33. data/java/project/local_lib/AdminUi.jar +0 -0
  34. data/java/{local_lib/protostuff-compiler-1.0.7-jarjar.jar → project/local_lib/protostuff-compiler-1.0.8-jarjar.jar} +0 -0
  35. data/java/project/local_lib/sigar/libsigar-amd64-freebsd-6.so +0 -0
  36. data/java/project/local_lib/sigar/libsigar-amd64-linux.so +0 -0
  37. data/java/project/local_lib/sigar/libsigar-amd64-solaris.so +0 -0
  38. data/java/project/local_lib/sigar/libsigar-ia64-hpux-11.sl +0 -0
  39. data/java/project/local_lib/sigar/libsigar-ia64-linux.so +0 -0
  40. data/java/project/local_lib/sigar/libsigar-pa-hpux-11.sl +0 -0
  41. data/java/project/local_lib/sigar/libsigar-ppc-aix-5.so +0 -0
  42. data/java/project/local_lib/sigar/libsigar-ppc-linux.so +0 -0
  43. data/java/project/local_lib/sigar/libsigar-ppc64-aix-5.so +0 -0
  44. data/java/project/local_lib/sigar/libsigar-ppc64-linux.so +0 -0
  45. data/java/project/local_lib/sigar/libsigar-s390x-linux.so +0 -0
  46. data/java/project/local_lib/sigar/libsigar-sparc-solaris.so +0 -0
  47. data/java/project/local_lib/sigar/libsigar-sparc64-solaris.so +0 -0
  48. data/java/project/local_lib/sigar/libsigar-universal-macosx.dylib +0 -0
  49. data/java/project/local_lib/sigar/libsigar-universal64-macosx.dylib +0 -0
  50. data/java/project/local_lib/sigar/libsigar-x86-freebsd-5.so +0 -0
  51. data/java/project/local_lib/sigar/libsigar-x86-freebsd-6.so +0 -0
  52. data/java/project/local_lib/sigar/libsigar-x86-linux.so +0 -0
  53. data/java/project/local_lib/sigar/libsigar-x86-solaris.so +0 -0
  54. data/java/project/local_lib/sigar/sigar-amd64-winnt.dll +0 -0
  55. data/java/project/local_lib/sigar/sigar-x86-winnt.dll +0 -0
  56. data/java/project/local_lib/sigar/sigar-x86-winnt.lib +0 -0
  57. data/java/project/model.erb +99 -0
  58. data/java/{settings.gradle → project/settings.gradle} +0 -0
  59. data/java/project/src/main/java/com/game_machine/authentication/DefaultAuthenticator.java +28 -0
  60. data/java/project/src/main/java/com/game_machine/authentication/PlayerAuthenticator.java +6 -0
  61. data/java/project/src/main/java/com/game_machine/authentication/PublicAuthenticator.java +20 -0
  62. data/java/{src → project/src}/main/java/com/game_machine/core/ActorFactory.java +0 -0
  63. data/java/{src → project/src}/main/java/com/game_machine/core/ActorUtil.java +13 -0
  64. data/java/project/src/main/java/com/game_machine/core/AuthorizedPlayers.java +23 -0
  65. data/java/project/src/main/java/com/game_machine/core/ClientMessageDecoder.java +36 -0
  66. data/java/project/src/main/java/com/game_machine/core/ClientMessageEncoder.java +19 -0
  67. data/java/project/src/main/java/com/game_machine/core/CloudClient.java +298 -0
  68. data/java/{src → project/src}/main/java/com/game_machine/core/CommandProxy.java +0 -0
  69. data/java/project/src/main/java/com/game_machine/core/Commands.java +20 -0
  70. data/java/project/src/main/java/com/game_machine/core/DatastoreCommands.java +43 -0
  71. data/java/project/src/main/java/com/game_machine/core/DbConnectionPool.java +72 -0
  72. data/java/project/src/main/java/com/game_machine/core/DefaultMovementVerifier.java +56 -0
  73. data/java/{src → project/src}/main/java/com/game_machine/core/EntitySerializer.java +0 -0
  74. data/java/project/src/main/java/com/game_machine/core/EntityTracking.java +119 -0
  75. data/java/{src → project/src}/main/java/com/game_machine/core/EventStreamHandler.java +1 -1
  76. data/java/project/src/main/java/com/game_machine/core/GameActor.java +73 -0
  77. data/java/project/src/main/java/com/game_machine/core/GameMachineLoader.java +43 -0
  78. data/java/project/src/main/java/com/game_machine/core/GameMessageActor.java +44 -0
  79. data/java/project/src/main/java/com/game_machine/core/Grid.java +255 -0
  80. data/java/{src → project/src}/main/java/com/game_machine/core/GridValue.java +0 -0
  81. data/java/project/src/main/java/com/game_machine/core/Hashring.java +66 -0
  82. data/java/{src → project/src}/main/java/com/game_machine/core/IActorFactory.java +0 -0
  83. data/java/project/src/main/java/com/game_machine/core/LocalLinkedBuffer.java +20 -0
  84. data/java/project/src/main/java/com/game_machine/core/MessageGateway.java +120 -0
  85. data/java/project/src/main/java/com/game_machine/core/MessagePersister.java +26 -0
  86. data/java/project/src/main/java/com/game_machine/core/MonoProxy.java +39 -0
  87. data/java/project/src/main/java/com/game_machine/core/MovementVerifier.java +7 -0
  88. data/java/{src → project/src}/main/java/com/game_machine/core/NetMessage.java +10 -6
  89. data/java/project/src/main/java/com/game_machine/core/PersistentMessage.java +9 -0
  90. data/java/project/src/main/java/com/game_machine/core/PlayerCommands.java +31 -0
  91. data/java/project/src/main/java/com/game_machine/core/TcpServer.java +100 -0
  92. data/java/project/src/main/java/com/game_machine/core/TcpServerHandler.java +54 -0
  93. data/java/project/src/main/java/com/game_machine/core/TcpServerInitializer.java +32 -0
  94. data/java/project/src/main/java/com/game_machine/core/UdpClient.java +86 -0
  95. data/java/{src → project/src}/main/java/com/game_machine/core/UdpServer.java +18 -27
  96. data/java/{src → project/src}/main/java/com/game_machine/core/UdpServerHandler.java +23 -26
  97. data/java/project/src/main/java/com/game_machine/core/Vector3.java +159 -0
  98. data/java/project/src/main/java/com/game_machine/orm/models/PlayerItem.java +118 -0
  99. data/java/project/src/main/java/com/game_machine/orm/models/TestObject.java +110 -0
  100. data/java/project/src/main/java/com/game_machine/tutorial/LootGenerator.java +26 -0
  101. data/java/{src → project/src}/main/resources/game_machine.java.stg +3 -1
  102. data/java/project/src/main/resources/logback.properties +13 -0
  103. data/java/project/src/main/resources/logback.xml +76 -0
  104. data/java/{src → project/src}/main/resources/protostuff.properties +0 -0
  105. data/java/src/main/java/game/MyGameActor.java +26 -0
  106. data/lib/game_machine.rb +17 -16
  107. data/lib/game_machine/actor.rb +1 -1
  108. data/lib/game_machine/actor/base.rb +8 -31
  109. data/lib/game_machine/actor/builder.rb +5 -6
  110. data/lib/game_machine/actor/game_actor.rb +55 -0
  111. data/lib/game_machine/actor/reloadable.rb +6 -1
  112. data/lib/game_machine/akka.rb +26 -32
  113. data/lib/game_machine/app_config.rb +39 -26
  114. data/lib/game_machine/application.rb +56 -62
  115. data/lib/game_machine/client_manager.rb +14 -8
  116. data/lib/game_machine/cloud_updater.rb +51 -0
  117. data/lib/game_machine/cluster_monitor.rb +3 -3
  118. data/lib/game_machine/commands.rb +1 -1
  119. data/lib/game_machine/commands/misc_commands.rb +4 -8
  120. data/lib/game_machine/commands/player_commands.rb +8 -0
  121. data/lib/game_machine/console.rb +1 -0
  122. data/lib/game_machine/console/build.rb +57 -24
  123. data/lib/game_machine/console/bundle.rb +95 -0
  124. data/lib/game_machine/console/deploy.rb +30 -0
  125. data/lib/game_machine/console/install.rb +70 -36
  126. data/lib/game_machine/console/server.rb +2 -69
  127. data/lib/game_machine/data_store.rb +111 -15
  128. data/lib/game_machine/data_stores/couchbase.rb +8 -3
  129. data/lib/game_machine/data_stores/gamecloud.rb +93 -0
  130. data/lib/game_machine/data_stores/jdbc.rb +98 -0
  131. data/lib/game_machine/default_handlers.rb +2 -0
  132. data/lib/game_machine/default_handlers/team_handler.rb +51 -0
  133. data/lib/game_machine/default_handlers/zone_manager.rb +30 -0
  134. data/lib/game_machine/endpoints.rb +0 -4
  135. data/lib/game_machine/endpoints/udp_incoming.rb +13 -5
  136. data/lib/game_machine/endpoints/udp_outgoing.rb +15 -9
  137. data/lib/game_machine/game_systems.rb +0 -2
  138. data/lib/game_machine/game_systems/agents/controller.rb +2 -2
  139. data/lib/game_machine/game_systems/entity_tracking.rb +0 -3
  140. data/lib/game_machine/game_systems/region_manager.rb +3 -2
  141. data/lib/game_machine/game_systems/region_service.rb +2 -2
  142. data/lib/game_machine/game_systems/remote_echo.rb +10 -0
  143. data/lib/game_machine/game_systems/team_manager.rb +2 -11
  144. data/lib/game_machine/grid.rb +5 -18
  145. data/lib/game_machine/handlers/authentication.rb +1 -9
  146. data/lib/game_machine/handlers/game.rb +27 -2
  147. data/lib/game_machine/handlers/player_authentication.rb +87 -0
  148. data/lib/game_machine/handlers/request.rb +9 -11
  149. data/lib/game_machine/hocon_config.rb +81 -0
  150. data/lib/game_machine/java_lib.rb +14 -1
  151. data/lib/game_machine/logger.rb +10 -23
  152. data/lib/game_machine/models.rb +1 -0
  153. data/lib/game_machine/mono_server.rb +6 -1
  154. data/lib/game_machine/object_db.rb +12 -6
  155. data/lib/game_machine/protobuf.rb +1 -1
  156. data/lib/game_machine/protobuf/game_messages.rb +13 -3
  157. data/lib/game_machine/protobuf/generate.rb +107 -5
  158. data/lib/game_machine/restart_watcher.rb +1 -1
  159. data/lib/game_machine/routes.rb +23 -0
  160. data/lib/game_machine/scheduler.rb +1 -1
  161. data/lib/game_machine/securerandom.rb +2 -0
  162. data/lib/game_machine/system_stats.rb +28 -7
  163. data/lib/game_machine/version.rb +1 -1
  164. data/lib/game_machine/wavefront_ext.rb +47 -0
  165. data/lib/game_machine/write_behind_cache.rb +24 -9
  166. data/mono/server/Makefile +1 -1
  167. data/mono/server/Newtonsoft.Json.dll +0 -0
  168. data/mono/server/build.bat +1 -1
  169. data/mono/server/callable.cs +9 -0
  170. data/mono/server/echo.cs +17 -0
  171. data/mono/server/message_router.cs +16 -23
  172. data/mono/server/messages.cs +1792 -417
  173. data/mono/server/protobuf-net.dll +0 -0
  174. data/mono/server/server.cs +120 -0
  175. data/mono/server/server.exe +0 -0
  176. data/pathfinding/astar.cpp +149 -0
  177. data/pathfinding/build.sh +6 -0
  178. data/pathfinding/build.txt +16 -0
  179. data/pathfinding/crowd.cpp +194 -0
  180. data/pathfinding/include/astar.h +49 -0
  181. data/pathfinding/include/common.h +5 -0
  182. data/pathfinding/include/crowd.h +43 -0
  183. data/pathfinding/include/micropather.h +511 -0
  184. data/pathfinding/include/navmesh.h +114 -0
  185. data/pathfinding/include/pathfinder.h +24 -0
  186. data/pathfinding/main.cpp +108 -17
  187. data/pathfinding/micropather.cpp +1062 -0
  188. data/pathfinding/navmesh.cpp +408 -0
  189. data/pathfinding/overrides/DetourCrowd.cpp +1446 -0
  190. data/pathfinding/overrides/DetourNavMeshQuery.cpp +3551 -0
  191. data/pathfinding/overrides/DetourNavMeshQuery.h +538 -0
  192. data/pathfinding/pathfinder.cpp +117 -0
  193. data/pathfinding/{bin → premake}/premake4 +0 -0
  194. data/pathfinding/premake/premake4.exe +0 -0
  195. data/pathfinding/premake4.lua +12 -3
  196. data/spec/actor/actor_spec.rb +0 -7
  197. data/spec/client_manager_spec.rb +1 -1
  198. data/spec/couchproxy_spec.rb +38 -0
  199. data/spec/entity_persistence_spec.rb +129 -0
  200. data/spec/game_systems/team_manager_spec.rb +2 -2
  201. data/spec/hashring_spec.rb +17 -39
  202. data/spec/java_grid_spec.rb +0 -2
  203. data/spec/misc_spec.rb +111 -0
  204. data/spec/mono_spec.rb +50 -3
  205. data/spec/reliable_message_spec.rb +38 -0
  206. data/spec/spec_helper.rb +4 -4
  207. data/spec/spec_helper_minimal.rb +10 -0
  208. data/web/app.rb +108 -86
  209. data/web/config/trinidad.yml +1 -0
  210. data/web/views/add_player.erb +25 -0
  211. data/web/views/index.erb +0 -0
  212. data/web/views/layout.erb +48 -0
  213. data/web/views/login.erb +25 -0
  214. data/web/views/players.erb +24 -0
  215. metadata +209 -94
  216. data/config/config.example.yml +0 -100
  217. data/config/regions.example.yml +0 -9
  218. data/games/example/lib/authentication_handler.rb +0 -69
  219. data/games/models.rb +0 -3
  220. data/games/models/clan_member.rb +0 -8
  221. data/games/models/clan_profile.rb +0 -9
  222. data/games/models/player.rb +0 -7
  223. data/games/plugins.rb +0 -1
  224. data/games/plugins/team_handler.rb +0 -49
  225. data/games/preload.rb +0 -13
  226. data/java/.gitignore +0 -1
  227. data/java/build.gradle +0 -95
  228. data/java/component.erb +0 -396
  229. data/java/gradle/wrapper/gradle-wrapper.jar +0 -0
  230. data/java/src/main/java/com/game_machine/core/GameMachineLoader.java +0 -25
  231. data/java/src/main/java/com/game_machine/core/Grid.java +0 -195
  232. data/java/src/main/resources/logback.xml +0 -14
  233. data/java/src/main/resources/logging.properties +0 -3
  234. data/lib/game_machine/actor/mono_actor.rb +0 -89
  235. data/lib/game_machine/auth_handlers/base.rb +0 -21
  236. data/lib/game_machine/auth_handlers/public.rb +0 -34
  237. data/lib/game_machine/endpoints/mono_gateway.rb +0 -87
  238. data/lib/game_machine/endpoints/tcp.rb +0 -51
  239. data/lib/game_machine/endpoints/tcp_handler.rb +0 -75
  240. data/lib/game_machine/endpoints/udp.rb +0 -88
  241. data/lib/game_machine/game_loader.rb +0 -46
  242. data/lib/game_machine/game_systems/region_settings.rb +0 -13
  243. data/lib/game_machine/hashring.rb +0 -48
  244. data/lib/game_machine/settings.rb +0 -11
  245. data/mono/server/actor.cs +0 -37
  246. data/mono/server/iactor.cs +0 -11
  247. data/mono/server/message_util.cs +0 -29
  248. data/mono/server/proxy_client.cs +0 -73
  249. data/mono/server/proxy_server.cs +0 -30
  250. data/mono/server/test_actor.cs +0 -33
  251. data/pathfinding/include/pathfind.h +0 -167
  252. data/pathfinding/pathfind.cpp +0 -174
  253. data/pathfinding/pathfinder.cs +0 -66
  254. data/script/server.sh +0 -109
  255. data/script/watch.sh +0 -11
  256. data/spec/commands/navigation_commands_spec.rb +0 -51
  257. data/spec/game_systems/entity_tracking_spec.rb +0 -64
  258. data/spec/navigation/detour_navmesh_spec.rb +0 -34
  259. data/spec/navigation/detour_path_spec.rb +0 -25
  260. data/spec/udp_server_spec.rb +0 -10
  261. data/web/controllers/auth_controller.rb +0 -19
  262. data/web/controllers/base_controller.rb +0 -16
  263. data/web/controllers/index_controller.rb +0 -7
  264. data/web/controllers/log_controller.rb +0 -47
  265. data/web/controllers/messages_controller.rb +0 -59
  266. data/web/controllers/player_register_controller.rb +0 -15
  267. data/web/views/game_messages.haml +0 -45
  268. data/web/views/index.haml +0 -6
  269. data/web/views/layout.haml +0 -41
  270. data/web/views/logs.haml +0 -32
  271. data/web/views/player_register.haml +0 -22
  272. data/web/views/player_registered.haml +0 -2
  273. data/web/views/register_layout.haml +0 -22
  274. data/web/views/restart.haml +0 -35
Binary file
@@ -0,0 +1,120 @@
1
+ using System;
2
+ using System.Net;
3
+ using System.Net.Sockets;
4
+ using System.Text;
5
+ using System.Threading;
6
+ using System.Threading.Tasks;
7
+ using System.IO;
8
+ using ProtoBuf;
9
+ using GameMachine;
10
+ using NLog;
11
+ using MonoMessage = GameMachine.Messages.MonoMessage;
12
+ using Entity = GameMachine.Messages.Entity;
13
+
14
+ namespace GameMachine
15
+ {
16
+ public class DataPacket
17
+ {
18
+ public Socket sock;
19
+ public byte[] buf;
20
+ public EndPoint ep;
21
+
22
+ public DataPacket(Socket sock, byte[] buf, EndPoint ep)
23
+ {
24
+ this.sock = sock;
25
+ this.buf = buf;
26
+ this.ep = ep;
27
+ }
28
+ }
29
+
30
+ public class Server
31
+ {
32
+
33
+ public static int port = 24320;
34
+ public static Logger logger = LogManager.GetLogger("GameMachine");
35
+ private Socket server;
36
+ private MessageRouter router;
37
+
38
+ static void Main(string[] args)
39
+ {
40
+ Server server = new Server();
41
+ server.Run();
42
+
43
+ Server.logger.Info("Server Starting");
44
+ Console.WriteLine("Press any key to exit.");
45
+ Console.ReadLine();
46
+ }
47
+
48
+ public void Run()
49
+ {
50
+ router = new MessageRouter();
51
+ server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
52
+ server.Bind(new IPEndPoint(IPAddress.Loopback, port));
53
+
54
+ for (int i=0; i<5; i++)
55
+ {
56
+ BeginReceive();
57
+ }
58
+ }
59
+
60
+ private void BeginReceive()
61
+ {
62
+ byte[] buffer = new byte[4096];
63
+ EndPoint ep = (EndPoint)new IPEndPoint(IPAddress.Loopback, 0);
64
+ DataPacket packet = new DataPacket(server, buffer, ep);
65
+ server.BeginReceiveFrom(packet.buf, 0, packet.buf.Length, SocketFlags.None, ref packet.ep, new AsyncCallback(Received), packet);
66
+ }
67
+
68
+ private void Received(IAsyncResult iar)
69
+ {
70
+ try
71
+ {
72
+ DataPacket packet = (DataPacket)iar.AsyncState;
73
+ int msgLen = packet.sock.EndReceiveFrom(iar, ref packet.ep);
74
+ byte[] localMsg = new byte[msgLen];
75
+ Array.Copy(packet.buf, localMsg, msgLen);
76
+
77
+ server.BeginReceiveFrom(packet.buf, 0, packet.buf.Length, SocketFlags.None, ref packet.ep, new AsyncCallback(Received), packet);
78
+
79
+ Task.Factory.StartNew(() => {
80
+ MonoMessage monoMessage = ByteArrayToMonoMessage(localMsg);
81
+ monoMessage.entity = router.Route(monoMessage.klass, monoMessage.entity);
82
+ byte[] bytes = MonoMessageToByteArray(monoMessage);
83
+ server.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, packet.ep, new AsyncCallback(Sent), packet.ep);
84
+ });
85
+
86
+ } catch (ObjectDisposedException e)
87
+ {
88
+ Console.WriteLine(e.Message);
89
+ }
90
+ }
91
+
92
+ public void Sent(IAsyncResult ar)
93
+ {
94
+ try
95
+ {
96
+ server.EndSend(ar);
97
+ } catch (Exception ex)
98
+ {
99
+ Console.WriteLine(ex.Message);
100
+ }
101
+ }
102
+
103
+ public static MonoMessage ByteArrayToMonoMessage(byte[] bytes)
104
+ {
105
+ MonoMessage monoMessage;
106
+ MemoryStream stream = new MemoryStream(bytes);
107
+ monoMessage = Serializer.Deserialize<MonoMessage>(stream);
108
+ return monoMessage;
109
+ }
110
+
111
+ public static byte[] MonoMessageToByteArray(MonoMessage monoMessage)
112
+ {
113
+ byte[] data;
114
+ MemoryStream stream = new MemoryStream();
115
+ Serializer.Serialize(stream, monoMessage);
116
+ data = stream.ToArray();
117
+ return data;
118
+ }
119
+ }
120
+ }
Binary file
@@ -0,0 +1,149 @@
1
+ #include <map>
2
+
3
+ #include "astar.h"
4
+ using namespace std;
5
+
6
+ Astar::Astar(int a, int b) : width(a), height(b), gmap(width*height+1), pather(0)
7
+ {
8
+ pather = new MicroPather( this, 100);
9
+ }
10
+
11
+ void Astar::setPassable(int x, int y)
12
+ {
13
+ gmap[ y*width + x] = '\0';
14
+ }
15
+
16
+ void Astar::setBlocked(int x, int y)
17
+ {
18
+ gmap[ y*width + x] = 'D';
19
+ }
20
+
21
+ int Astar::Passable( int nx, int ny )
22
+ {
23
+ if ( nx >= 0 && nx < width
24
+ && ny >= 0 && ny < height )
25
+ {
26
+ int index = ny*width+nx;
27
+ char c = gmap[ index ];
28
+ if ( c == '\0' )
29
+ return 1;
30
+ else if ( c == 'D' )
31
+ return 2;
32
+ }
33
+ return 0;
34
+ }
35
+
36
+ void Astar::ClearPath()
37
+ {
38
+ path.resize( 0 );
39
+ pather->Reset();
40
+ }
41
+
42
+ int Astar::findPath(int startx, int starty, int endx, int endy)
43
+ {
44
+ int result = 0;
45
+ float totalCost;
46
+
47
+ if (Passable(startx,starty) && Passable(endx,endy))
48
+ {
49
+ result = pather->Solve( XYToNode( startx, starty ), XYToNode( endx, endy ), &path, &totalCost );
50
+ if ( result == MicroPather::SOLVED ) {
51
+ return path.size();
52
+ }
53
+ }
54
+ return result;
55
+ }
56
+
57
+ void Astar::NodeToXY( void* node, int* x, int* y )
58
+ {
59
+ intptr_t index = (intptr_t)node;
60
+ *y = index / width;
61
+ *x = index - *y * width;
62
+ }
63
+
64
+ void* Astar::XYToNode( int x, int y )
65
+ {
66
+ return (void*) ( y*width + x );
67
+ }
68
+
69
+ int Astar::getYAt(int i)
70
+ {
71
+ int y;
72
+ intptr_t index = (intptr_t)path[i];
73
+ y = index / width;
74
+ return y;
75
+ }
76
+
77
+ int Astar::getXAt(int i)
78
+ {
79
+ int x,y;
80
+ intptr_t index = (intptr_t)path[i];
81
+ y = index / width;
82
+ x = index - y * width;
83
+ return x;
84
+ }
85
+
86
+ float Astar::LeastCostEstimate( void* nodeStart, void* nodeEnd )
87
+ {
88
+ int xStart, yStart, xEnd, yEnd;
89
+ NodeToXY( nodeStart, &xStart, &yStart );
90
+ NodeToXY( nodeEnd, &xEnd, &yEnd );
91
+
92
+ /* Compute the minimum path cost using distance measurement. It is possible
93
+ to compute the exact minimum path using the fact that you can move only
94
+ on a straight line or on a diagonal, and this will yield a better result.
95
+ */
96
+ int dx = xStart - xEnd;
97
+ int dy = yStart - yEnd;
98
+ return (float) sqrt( (double)(dx*dx) + (double)(dy*dy) );
99
+ }
100
+
101
+ void Astar::AdjacentCost( void* node, micropather::MPVector< StateCost > *neighbors )
102
+ {
103
+ int x, y;
104
+ const int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
105
+ const int dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
106
+ const float cost[8] = { 1.0f, 1.41f, 1.0f, 1.41f, 1.0f, 1.41f, 1.0f, 1.41f };
107
+
108
+ NodeToXY( node, &x, &y );
109
+
110
+ for( int i=0; i<8; ++i ) {
111
+ int nx = x + dx[i];
112
+ int ny = y + dy[i];
113
+
114
+ int pass = Passable( nx, ny );
115
+ if ( pass > 0 ) {
116
+ if ( pass == 1)
117
+ {
118
+ // Normal floor
119
+ StateCost nodeCost = { XYToNode( nx, ny ), cost[i] };
120
+ neighbors->push_back( nodeCost );
121
+ }
122
+ else
123
+ {
124
+ // Normal floor
125
+ StateCost nodeCost = { XYToNode( nx, ny ), FLT_MAX };
126
+ neighbors->push_back( nodeCost );
127
+ }
128
+ }
129
+ }
130
+ }
131
+
132
+ void Astar::PrintStateInfo( void* node )
133
+ {
134
+ int x, y;
135
+ NodeToXY( node, &x, &y );
136
+ printf( "(%d,%d)", x, y );
137
+ }
138
+
139
+ void Astar::Print()
140
+ {
141
+ unsigned k;
142
+ // Wildly inefficient demo code.
143
+ unsigned size = path.size();
144
+ for( k=0; k<size; ++k ) {
145
+ int x, y;
146
+ NodeToXY( path[k], &x, &y );
147
+ fprintf (stderr, "%d.%d\n", x,y);
148
+ }
149
+ }
@@ -0,0 +1,6 @@
1
+ rm -rf build
2
+ cp overrides/DetourNavMeshQuery.cpp recastnavigation/Detour/Source
3
+ cp overrides/DetourNavMeshQuery.h recastnavigation/Detour/Include
4
+ cp overrides/DetourCrowd.cpp recastnavigation/DetourCrowd/Source
5
+ premake/premake4 gmake
6
+ cd build && make
@@ -0,0 +1,16 @@
1
+
2
+ First you need to clone recastnavigation into the pathfinding folder.
3
+
4
+ There are premake4 binaries for linux and windows in the premake folder.
5
+
6
+ Run one of the premake binaries with the options for your OS. This should give you a build folder. For windows open the sln and build. For linux cd to the build folder and type 'make'.
7
+
8
+ The linux build will copy the libraries to the bin folder which is where game machine expects to find them. On windows you will need to copy them there manually.
9
+
10
+ pathfinding.cs is a wrapper around the dll for windows that can be used in Unity (or any C# game engine). Note that the query methods have coordinate mappings where we swap x and z. This is so it works correctly with our own obj export. Feel free to change that if needed. This is the C# code, native code always uses recast coordinates.
11
+
12
+ The dll provides simple functionality for loading navigation meshes and performing pathfinding queries. pathfinding.cs is a good reference for how to use it.
13
+
14
+ In ruby the Navigation module has several classes for working with navigation meshes and doing queries. It uses the ruby FFI to call the library.
15
+
16
+ Also, look at clients/unity/GameMachine/Editor/ExportScene.cs which is an obj file exporter that can export terrain + object meshes to a single obj file that recastdemo can use.
@@ -0,0 +1,194 @@
1
+ #include "crowd.h"
2
+ using namespace std;
3
+
4
+ Crowd::Crowd(Navmesh* n)
5
+
6
+ {
7
+ navmesh = n;
8
+ crowd = dtAllocCrowd();
9
+
10
+ nav = navmesh->getMesh();
11
+
12
+ crowd->init(MAX_AGENTS, 0.6f, nav);
13
+
14
+ // Make polygons with 'disabled' flag invalid.
15
+ crowd->getEditableFilter(0)->setExcludeFlags(navmesh->SAMPLE_POLYFLAGS_DISABLED);
16
+
17
+ // Setup local avoidance params to different qualities.
18
+ dtObstacleAvoidanceParams params;
19
+ // Use mostly default settings, copy from dtCrowd.
20
+ memcpy(&params, crowd->getObstacleAvoidanceParams(0), sizeof(dtObstacleAvoidanceParams));
21
+
22
+ // Low (11)
23
+ params.velBias = 0.5f;
24
+ params.adaptiveDivs = 5;
25
+ params.adaptiveRings = 2;
26
+ params.adaptiveDepth = 1;
27
+ crowd->setObstacleAvoidanceParams(0, &params);
28
+
29
+ // Medium (22)
30
+ params.velBias = 0.5f;
31
+ params.adaptiveDivs = 5;
32
+ params.adaptiveRings = 2;
33
+ params.adaptiveDepth = 2;
34
+ crowd->setObstacleAvoidanceParams(1, &params);
35
+
36
+ // Good (45)
37
+ params.velBias = 0.5f;
38
+ params.adaptiveDivs = 7;
39
+ params.adaptiveRings = 2;
40
+ params.adaptiveDepth = 3;
41
+ crowd->setObstacleAvoidanceParams(2, &params);
42
+
43
+ // High (66)
44
+ params.velBias = 0.5f;
45
+ params.adaptiveDivs = 7;
46
+ params.adaptiveRings = 3;
47
+ params.adaptiveDepth = 3;
48
+
49
+ crowd->setObstacleAvoidanceParams(3, &params);
50
+
51
+ memset(m_trails, 0, sizeof(m_trails));
52
+
53
+ m_vod = dtAllocObstacleAvoidanceDebugData();
54
+ m_vod->init(2048);
55
+
56
+ memset(&m_agentDebug, 0, sizeof(m_agentDebug));
57
+ m_agentDebug.idx = -1;
58
+ m_agentDebug.vod = m_vod;
59
+ fprintf (stderr, "Crowd initialized\n");
60
+ }
61
+
62
+ int Crowd::addAgent(const float* p, float accel, float speed, float radius, float height, int optflag, float sepWeight)
63
+ {
64
+ dtCrowdAgentParams ap;
65
+ memset(&ap, 0, sizeof(ap));
66
+ ap.radius = radius;
67
+ ap.height = height;
68
+ ap.maxAcceleration = accel;
69
+ ap.maxSpeed = speed;
70
+ ap.collisionQueryRange = ap.radius * 12.0f;
71
+ ap.pathOptimizationRange = ap.radius * 30.0f;
72
+ ap.updateFlags = 0;
73
+ ap.updateFlags |= DT_CROWD_ANTICIPATE_TURNS;
74
+ ap.updateFlags |= DT_CROWD_OPTIMIZE_VIS;
75
+ ap.updateFlags |= DT_CROWD_OPTIMIZE_TOPO;
76
+ ap.updateFlags |= DT_CROWD_OBSTACLE_AVOIDANCE;
77
+ ap.updateFlags |= DT_CROWD_SEPARATION;
78
+
79
+ ap.obstacleAvoidanceType = (unsigned char)3.0f;
80
+ ap.separationWeight = sepWeight;
81
+
82
+ int idx = crowd->addAgent(p, &ap);
83
+ if (idx != -1)
84
+ {
85
+ if (m_targetRef)
86
+ crowd->requestMoveTarget(idx, m_targetRef, m_targetPos);
87
+
88
+ // Init trail
89
+ AgentTrail* trail = &m_trails[idx];
90
+ for (int i = 0; i < AGENT_MAX_TRAIL; ++i)
91
+ dtVcopy(&trail->trail[i*3], p);
92
+ trail->htrail = 0;
93
+ }
94
+ return idx;
95
+ }
96
+
97
+ void Crowd::removeAgent(const int idx)
98
+ {
99
+ crowd->removeAgent(idx);
100
+ }
101
+
102
+ static void calcVel(float* vel, const float* pos, const float* tgt, const float speed)
103
+ {
104
+ dtVsub(vel, tgt, pos);
105
+ vel[1] = 0.0;
106
+ dtVnormalize(vel);
107
+ dtVscale(vel, vel, speed);
108
+ }
109
+
110
+ dtCrowd* Crowd::getCrowd()
111
+ {
112
+ return crowd;
113
+ }
114
+
115
+
116
+ void Crowd::setMoveTarget(const float* p, bool adjust, int agentIdx)
117
+ {
118
+
119
+ // Find nearest point on navmesh and set move request to that location.
120
+ dtNavMeshQuery* navquery = dtAllocNavMeshQuery();
121
+ navquery->init(nav, 4096);
122
+
123
+ myQueryFilter filter;
124
+ filter.setNavmesh(navmesh);
125
+ filter.setIncludeFlags(navmesh->SAMPLE_POLYFLAGS_ALL ^ navmesh->SAMPLE_POLYFLAGS_DISABLED);
126
+ filter.setExcludeFlags(0);
127
+ const float* ext = crowd->getQueryExtents();
128
+
129
+ if (adjust)
130
+ {
131
+ float vel[3];
132
+ // Request velocity
133
+ if (agentIdx != -1)
134
+ {
135
+ const dtCrowdAgent* ag = crowd->getAgent(agentIdx);
136
+ if (ag && ag->active)
137
+ {
138
+ calcVel(vel, ag->npos, p, ag->params.maxSpeed);
139
+ crowd->requestMoveVelocity(agentIdx, vel);
140
+ }
141
+ }
142
+ else
143
+ {
144
+ for (int i = 0; i < crowd->getAgentCount(); ++i)
145
+ {
146
+ const dtCrowdAgent* ag = crowd->getAgent(i);
147
+ if (!ag->active) continue;
148
+ calcVel(vel, ag->npos, p, ag->params.maxSpeed);
149
+ crowd->requestMoveVelocity(i, vel);
150
+ }
151
+ }
152
+ }
153
+ else
154
+ {
155
+ navquery->findNearestPoly(p, ext, &filter, &m_targetRef, m_targetPos);
156
+
157
+ if (agentIdx != -1)
158
+ {
159
+ const dtCrowdAgent* ag = crowd->getAgent(agentIdx);
160
+ if (ag && ag->active)
161
+ crowd->requestMoveTarget(agentIdx, m_targetRef, m_targetPos);
162
+ }
163
+ else
164
+ {
165
+ for (int i = 0; i < crowd->getAgentCount(); ++i)
166
+ {
167
+ const dtCrowdAgent* ag = crowd->getAgent(i);
168
+ if (!ag->active) continue;
169
+ crowd->requestMoveTarget(i, m_targetRef, m_targetPos);
170
+ }
171
+ }
172
+ }
173
+ }
174
+
175
+ void Crowd::updateTick(const float dt)
176
+ {
177
+ crowd->update(dt, &m_agentDebug);
178
+
179
+ // Update agent trails
180
+ for (int i = 0; i < crowd->getAgentCount(); ++i)
181
+ {
182
+ const dtCrowdAgent* ag = crowd->getAgent(i);
183
+ AgentTrail* trail = &m_trails[i];
184
+ if (!ag->active)
185
+ continue;
186
+ // Update agent movement trail.
187
+ trail->htrail = (trail->htrail + 1) % AGENT_MAX_TRAIL;
188
+ dtVcopy(&trail->trail[trail->htrail*3], ag->npos);
189
+ }
190
+ }
191
+
192
+
193
+
194
+