game_machine 0.0.11 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,49 @@
1
+
2
+ #include <ctype.h>
3
+ #include <stdio.h>
4
+ #include <memory.h>
5
+ #include <math.h>
6
+
7
+ #include <vector>
8
+ #include <iostream>
9
+
10
+
11
+ #include "common.h"
12
+ #include "micropather.h"
13
+ using namespace micropather;
14
+ using namespace std;
15
+
16
+
17
+ class Astar : public Graph {
18
+ int width, height;
19
+ vector<char> gmap;
20
+ private:
21
+ Astar( const Astar& );
22
+ void operator=( const Astar& );
23
+ MPVector<void*> path;
24
+ MicroPather* pather;
25
+
26
+
27
+ public:
28
+ Astar(int a, int b);
29
+ virtual ~Astar() {
30
+ delete pather;
31
+ }
32
+ void setPassable(int x, int y);
33
+ void setBlocked(int x, int y);
34
+ int Passable( int nx, int ny );
35
+ void ClearPath();
36
+ int findPath(int startx, int starty, int endx, int endy);
37
+ void NodeToXY( void* node, int* x, int* y );
38
+ void* XYToNode( int x, int y );
39
+ int getYAt(int i);
40
+ int getXAt(int i);
41
+ virtual float LeastCostEstimate( void* nodeStart, void* nodeEnd );
42
+ virtual void AdjacentCost( void* node, micropather::MPVector< StateCost > *neighbors );
43
+ virtual void PrintStateInfo( void* node );
44
+ void Print();
45
+ };
46
+
47
+
48
+
49
+
@@ -0,0 +1,5 @@
1
+ #if _MSC_VER // TRUE for Microsoft compiler.
2
+ #define EXPORT_API __declspec(dllexport) // Required for VC++
3
+ #else
4
+ #define EXPORT_API // Otherwise don't define.
5
+ #endif
@@ -0,0 +1,43 @@
1
+ #define _USE_MATH_DEFINES
2
+ #include <ctype.h>
3
+ #include <stdio.h>
4
+ #include <memory.h>
5
+ #include <math.h>
6
+ #include <vector>
7
+ #include <iostream>
8
+
9
+ #include "Recast.h"
10
+ #include "DetourNavMesh.h"
11
+ #include "DetourNavMeshQuery.h"
12
+ #include "DetourCrowd.h"
13
+
14
+ #include "common.h"
15
+ #include "navmesh.h"
16
+
17
+
18
+ class Crowd {
19
+ Navmesh* navmesh;
20
+ dtNavMesh* nav;
21
+ dtCrowd* crowd;
22
+ dtCrowdAgentDebugInfo m_agentDebug;
23
+ dtObstacleAvoidanceDebugData* m_vod;
24
+ float m_targetPos[3];
25
+ dtPolyRef m_targetRef;
26
+
27
+ static const int AGENT_MAX_TRAIL = 64;
28
+ static const int MAX_AGENTS = 128;
29
+ struct AgentTrail
30
+ {
31
+ float trail[AGENT_MAX_TRAIL*3];
32
+ int htrail;
33
+ };
34
+ AgentTrail m_trails[MAX_AGENTS];
35
+
36
+ public:
37
+ Crowd(Navmesh* n);
38
+ int addAgent(const float* p, float accel, float speed, float radius, float height, int optflag, float sepWeight);
39
+ void removeAgent(const int idx);
40
+ void setMoveTarget(const float* p, bool adjust, int agentIdx);
41
+ void updateTick(const float dt);
42
+ dtCrowd* getCrowd();
43
+ };
@@ -0,0 +1,511 @@
1
+ /*
2
+ Copyright (c) 2000-2013 Lee Thomason (www.grinninglizard.com)
3
+ Micropather
4
+
5
+ This software is provided 'as-is', without any express or implied
6
+ warranty. In no event will the authors be held liable for any
7
+ damages arising from the use of this software.
8
+
9
+ Permission is granted to anyone to use this software for any
10
+ purpose, including commercial applications, and to alter it and
11
+ redistribute it freely, subject to the following restrictions:
12
+
13
+ 1. The origin of this software must not be misrepresented; you must
14
+ not claim that you wrote the original software. If you use this
15
+ software in a product, an acknowledgment in the product documentation
16
+ would be appreciated but is not required.
17
+
18
+ 2. Altered source versions must be plainly marked as such, and
19
+ must not be misrepresented as being the original software.
20
+
21
+ 3. This notice may not be removed or altered from any source
22
+ distribution.
23
+ */
24
+ #ifndef GRINNINGLIZARD_MICROPATHER_INCLUDED
25
+ #define GRINNINGLIZARD_MICROPATHER_INCLUDED
26
+
27
+
28
+ /** @mainpage MicroPather
29
+
30
+ MicroPather is a path finder and A* solver (astar or a-star) written in platform independent
31
+ C++ that can be easily integrated into existing code. MicroPather focuses on being a path
32
+ finding engine for video games but is a generic A* solver. MicroPather is open source, with
33
+ a license suitable for open source or commercial use.
34
+ */
35
+
36
+ // This probably works to remove, but isn't currently tested in STL mode.
37
+ #define GRINLIZ_NO_STL
38
+
39
+ #ifdef GRINLIZ_NO_STL
40
+ # define MP_VECTOR micropather::MPVector
41
+ #else
42
+ # include <vector>
43
+ # define MP_VECTOR std::vector
44
+ #endif
45
+ #include <float.h>
46
+
47
+ #ifdef _DEBUG
48
+ #ifndef DEBUG
49
+ #define DEBUG
50
+ #endif
51
+ #endif
52
+
53
+
54
+ #if defined( _DEBUG )
55
+ # if defined( _MSC_VER )
56
+ # define MPASSERT( x ) if ( !(x)) { _asm { int 3 } }
57
+ # else
58
+ # include <assert.h>
59
+ # define MPASSERT assert
60
+ # endif
61
+ #else
62
+ # define MPASSERT( x ) {}
63
+ #endif
64
+
65
+
66
+ #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
67
+ #include <stdlib.h>
68
+ typedef uintptr_t MP_UPTR;
69
+ #elif defined (__GNUC__) && (__GNUC__ >= 3 )
70
+ #include <stdint.h>
71
+ #include <stdlib.h>
72
+ typedef uintptr_t MP_UPTR;
73
+ #else
74
+ // Assume not 64 bit pointers. Get a new compiler.
75
+ typedef unsigned MP_UPTR;
76
+ #endif
77
+
78
+ namespace micropather
79
+ {
80
+ #ifdef GRINLIZ_NO_STL
81
+
82
+ /* WARNING: vector partial replacement. Does everything needed to replace std::vector
83
+ for micropather, but only works on Plain Old Data types. Doesn't call copy/construct/destruct
84
+ correctly for general use.
85
+ */
86
+ template <typename T>
87
+ class MPVector {
88
+ public:
89
+ MPVector() : m_allocated( 0 ), m_size( 0 ), m_buf ( 0 ) {}
90
+ ~MPVector() { delete [] m_buf; }
91
+
92
+ void clear() { m_size = 0; } // see warning above
93
+ void resize( unsigned s ) { capacity( s );
94
+ m_size = s;
95
+ }
96
+ T& operator[](unsigned i) { MPASSERT( i>=0 && i<m_size );
97
+ return m_buf[i];
98
+ }
99
+ const T& operator[](unsigned i) const { MPASSERT( i>=0 && i<m_size );
100
+ return m_buf[i];
101
+ }
102
+ void push_back( const T& t ) { capacity( m_size+1 );
103
+ m_buf[m_size++] = t;
104
+ }
105
+ unsigned size() const { return m_size; }
106
+
107
+ private:
108
+ void capacity( unsigned cap ) {
109
+ if ( m_allocated < cap ) {
110
+ unsigned newAllocated = cap * 3/2 + 16;
111
+ T* newBuf = new T[newAllocated];
112
+ MPASSERT( m_size <= m_allocated );
113
+ MPASSERT( m_size < newAllocated );
114
+ memcpy( newBuf, m_buf, sizeof(T)*m_size );
115
+ delete [] m_buf;
116
+ m_buf = newBuf;
117
+ m_allocated = newAllocated;
118
+ }
119
+ }
120
+ unsigned m_allocated;
121
+ unsigned m_size;
122
+ T* m_buf;
123
+ };
124
+ #endif
125
+
126
+ /**
127
+ Used to pass the cost of states from the cliet application to MicroPather. This
128
+ structure is copied in a vector.
129
+
130
+ @sa AdjacentCost
131
+ */
132
+ struct StateCost
133
+ {
134
+ void* state; ///< The state as a void*
135
+ float cost; ///< The cost to the state. Use FLT_MAX for infinite cost.
136
+ };
137
+
138
+
139
+ /**
140
+ A pure abstract class used to define a set of callbacks.
141
+ The client application inherits from
142
+ this class, and the methods will be called when MicroPather::Solve() is invoked.
143
+
144
+ The notion of a "state" is very important. It must have the following properties:
145
+ - Unique
146
+ - Unchanging (unless MicroPather::Reset() is called)
147
+
148
+ If the client application represents states as objects, then the state is usually
149
+ just the object cast to a void*. If the client application sees states as numerical
150
+ values, (x,y) for example, then state is an encoding of these values. MicroPather
151
+ never interprets or modifies the value of state.
152
+ */
153
+ class Graph
154
+ {
155
+ public:
156
+ virtual ~Graph() {}
157
+
158
+ /**
159
+ Return the least possible cost between 2 states. For example, if your pathfinding
160
+ is based on distance, this is simply the straight distance between 2 points on the
161
+ map. If you pathfinding is based on minimum time, it is the minimal travel time
162
+ between 2 points given the best possible terrain.
163
+ */
164
+ virtual float LeastCostEstimate( void* stateStart, void* stateEnd ) = 0;
165
+
166
+ /**
167
+ Return the exact cost from the given state to all its neighboring states. This
168
+ may be called multiple times, or cached by the solver. It *must* return the same
169
+ exact values for every call to MicroPather::Solve(). It should generally be a simple,
170
+ fast function with no callbacks into the pather.
171
+ */
172
+ virtual void AdjacentCost( void* state, MP_VECTOR< micropather::StateCost > *adjacent ) = 0;
173
+
174
+ /**
175
+ This function is only used in DEBUG mode - it dumps output to stdout. Since void*
176
+ aren't really human readable, normally you print out some concise info (like "(1,2)")
177
+ without an ending newline.
178
+ */
179
+ virtual void PrintStateInfo( void* state ) = 0;
180
+ };
181
+
182
+
183
+ class PathNode;
184
+
185
+ struct NodeCost
186
+ {
187
+ PathNode* node;
188
+ float cost;
189
+ };
190
+
191
+
192
+ /*
193
+ Every state (void*) is represented by a PathNode in MicroPather. There
194
+ can only be one PathNode for a given state.
195
+ */
196
+ class PathNode
197
+ {
198
+ public:
199
+ void Init( unsigned _frame,
200
+ void* _state,
201
+ float _costFromStart,
202
+ float _estToGoal,
203
+ PathNode* _parent );
204
+
205
+ void Clear() {
206
+ memset( this, 0, sizeof( PathNode ) );
207
+ numAdjacent = -1;
208
+ cacheIndex = -1;
209
+ }
210
+ void InitSentinel() {
211
+ Clear();
212
+ Init( 0, 0, FLT_MAX, FLT_MAX, 0 );
213
+ prev = next = this;
214
+ }
215
+
216
+ void *state; // the client state
217
+ float costFromStart; // exact
218
+ float estToGoal; // estimated
219
+ float totalCost; // could be a function, but save some math.
220
+ PathNode* parent; // the parent is used to reconstruct the path
221
+ unsigned frame; // unique id for this path, so the solver can distinguish
222
+ // correct from stale values
223
+
224
+ int numAdjacent; // -1 is unknown & needs to be queried
225
+ int cacheIndex; // position in cache
226
+
227
+ PathNode *child[2]; // Binary search in the hash table. [left, right]
228
+ PathNode *next, *prev; // used by open queue
229
+
230
+ bool inOpen;
231
+ bool inClosed;
232
+
233
+ void Unlink() {
234
+ next->prev = prev;
235
+ prev->next = next;
236
+ next = prev = 0;
237
+ }
238
+ void AddBefore( PathNode* addThis ) {
239
+ addThis->next = this;
240
+ addThis->prev = prev;
241
+ prev->next = addThis;
242
+ prev = addThis;
243
+ }
244
+ #ifdef DEBUG
245
+ void CheckList()
246
+ {
247
+ MPASSERT( totalCost == FLT_MAX );
248
+ for( PathNode* it = next; it != this; it=it->next ) {
249
+ MPASSERT( it->prev == this || it->totalCost >= it->prev->totalCost );
250
+ MPASSERT( it->totalCost <= it->next->totalCost );
251
+ }
252
+ }
253
+ #endif
254
+
255
+ void CalcTotalCost() {
256
+ if ( costFromStart < FLT_MAX && estToGoal < FLT_MAX )
257
+ totalCost = costFromStart + estToGoal;
258
+ else
259
+ totalCost = FLT_MAX;
260
+ }
261
+
262
+ private:
263
+
264
+ void operator=( const PathNode& );
265
+ };
266
+
267
+
268
+ /* Memory manager for the PathNodes. */
269
+ class PathNodePool
270
+ {
271
+ public:
272
+ PathNodePool( unsigned allocate, unsigned typicalAdjacent );
273
+ ~PathNodePool();
274
+
275
+ // Free all the memory except the first block. Resets all memory.
276
+ void Clear();
277
+
278
+ // Essentially:
279
+ // pNode = Find();
280
+ // if ( !pNode )
281
+ // pNode = New();
282
+ //
283
+ // Get the PathNode associated with this state. If the PathNode already
284
+ // exists (allocated and is on the current frame), it will be returned.
285
+ // Else a new PathNode is allocated and returned. The returned object
286
+ // is always fully initialized.
287
+ //
288
+ // NOTE: if the pathNode exists (and is current) all the initialization
289
+ // parameters are ignored.
290
+ PathNode* GetPathNode( unsigned frame,
291
+ void* _state,
292
+ float _costFromStart,
293
+ float _estToGoal,
294
+ PathNode* _parent );
295
+
296
+ // Get a pathnode that is already in the pool.
297
+ PathNode* FetchPathNode( void* state );
298
+
299
+ // Store stuff in cache
300
+ bool PushCache( const NodeCost* nodes, int nNodes, int* start );
301
+
302
+ // Get neighbors from the cache
303
+ // Note - always access this with an offset. Can get re-allocated.
304
+ void GetCache( int start, int nNodes, NodeCost* nodes ) {
305
+ MPASSERT( start >= 0 && start < cacheCap );
306
+ MPASSERT( nNodes > 0 );
307
+ MPASSERT( start + nNodes <= cacheCap );
308
+ memcpy( nodes, &cache[start], sizeof(NodeCost)*nNodes );
309
+ }
310
+
311
+ // Return all the allocated states. Useful for visuallizing what
312
+ // the pather is doing.
313
+ void AllStates( unsigned frame, MP_VECTOR< void* >* stateVec );
314
+
315
+ private:
316
+ struct Block
317
+ {
318
+ Block* nextBlock;
319
+ PathNode pathNode[1];
320
+ };
321
+
322
+ unsigned Hash( void* voidval );
323
+ unsigned HashSize() const { return 1<<hashShift; }
324
+ unsigned HashMask() const { return ((1<<hashShift)-1); }
325
+ void AddPathNode( unsigned key, PathNode* p );
326
+ Block* NewBlock();
327
+ PathNode* Alloc();
328
+
329
+ PathNode** hashTable;
330
+ Block* firstBlock;
331
+ Block* blocks;
332
+
333
+ NodeCost* cache;
334
+ int cacheCap;
335
+ int cacheSize;
336
+
337
+ PathNode freeMemSentinel;
338
+ unsigned allocate; // how big a block of pathnodes to allocate at once
339
+ unsigned nAllocated; // number of pathnodes allocated (from Alloc())
340
+ unsigned nAvailable; // number available for allocation
341
+
342
+ unsigned hashShift;
343
+ unsigned totalCollide;
344
+ };
345
+
346
+
347
+ /* Used to cache results of paths. Much, much faster
348
+ to return an existing solution than to calculate
349
+ a new one. A post on this is here: http://grinninglizard.com/altera/programming/a-path-caching-2/
350
+ */
351
+ class PathCache
352
+ {
353
+ public:
354
+ struct Item {
355
+ // The key:
356
+ void* start;
357
+ void* end;
358
+
359
+ bool KeyEqual( const Item& item ) const { return start == item.start && end == item.end; }
360
+ bool Empty() const { return start == 0 && end == 0; }
361
+
362
+ // Data:
363
+ void* next;
364
+ float cost; // from 'start' to 'next'. FLT_MAX if unsolveable.
365
+
366
+ unsigned Hash() const {
367
+ const unsigned char *p = (const unsigned char *)(&start);
368
+ unsigned int h = 2166136261U;
369
+
370
+ for( unsigned i=0; i<sizeof(void*)*2; ++i, ++p ) {
371
+ h ^= *p;
372
+ h *= 16777619;
373
+ }
374
+ return h;
375
+ }
376
+ };
377
+
378
+ PathCache( int itemsToAllocate );
379
+ ~PathCache();
380
+
381
+ void Reset();
382
+ void Add( const MP_VECTOR< void* >& path, const MP_VECTOR< float >& cost );
383
+ void AddNoSolution( void* end, void* states[], int count );
384
+ int Solve( void* startState, void* endState, MP_VECTOR< void* >* path, float* totalCost );
385
+
386
+ int AllocatedBytes() const { return allocated * sizeof(Item); }
387
+ int UsedBytes() const { return nItems * sizeof(Item); }
388
+
389
+ int hit;
390
+ int miss;
391
+
392
+ private:
393
+ void AddItem( const Item& item );
394
+ const Item* Find( void* start, void* end );
395
+
396
+ Item* mem;
397
+ int allocated;
398
+ int nItems;
399
+ };
400
+
401
+ struct CacheData {
402
+ CacheData() : nBytesAllocated(0), nBytesUsed(0), memoryFraction(0), hit(0), miss(0), hitFraction(0) {}
403
+ int nBytesAllocated;
404
+ int nBytesUsed;
405
+ float memoryFraction;
406
+
407
+ int hit;
408
+ int miss;
409
+ float hitFraction;
410
+ };
411
+
412
+ /**
413
+ Create a MicroPather object to solve for a best path. Detailed usage notes are
414
+ on the main page.
415
+ */
416
+ class MicroPather
417
+ {
418
+ friend class micropather::PathNode;
419
+
420
+ public:
421
+ enum
422
+ {
423
+ SOLVED,
424
+ NO_SOLUTION,
425
+ START_END_SAME,
426
+
427
+ // internal
428
+ NOT_CACHED
429
+ };
430
+
431
+ /**
432
+ Construct the pather, passing a pointer to the object that implements
433
+ the Graph callbacks.
434
+
435
+ @param graph The "map" that implements the Graph callbacks.
436
+ @param allocate How many states should be internally allocated at a time. This
437
+ can be hard to get correct. The higher the value, the more memory
438
+ MicroPather will use.
439
+ - If you have a small map (a few thousand states?) it may make sense
440
+ to pass in the maximum value. This will cache everything, and MicroPather
441
+ will only need one main memory allocation. For a chess board, allocate
442
+ would be set to 8x8 (64)
443
+ - If your map is large, something like 1/4 the number of possible
444
+ states is good.
445
+ - If your state space is huge, use a multiple (5-10x) of the normal
446
+ path. "Occasionally" call Reset() to free unused memory.
447
+ @param typicalAdjacent Used to determine cache size. The typical number of adjacent states
448
+ to a given state. (On a chessboard, 8.) Higher values use a little
449
+ more memory.
450
+ @param cache Turn on path caching. Uses more memory (yet again) but at a huge speed
451
+ advantage if you may call the pather with the same path or sub-path, which
452
+ is common for pathing over maps in games.
453
+ */
454
+ MicroPather( Graph* graph, unsigned allocate = 250, unsigned typicalAdjacent=6, bool cache=true );
455
+ ~MicroPather();
456
+
457
+ /**
458
+ Solve for the path from start to end.
459
+
460
+ @param startState Input, the starting state for the path.
461
+ @param endState Input, the ending state for the path.
462
+ @param path Output, a vector of states that define the path. Empty if not found.
463
+ @param totalCost Output, the cost of the path, if found.
464
+ @return Success or failure, expressed as SOLVED, NO_SOLUTION, or START_END_SAME.
465
+ */
466
+ int Solve( void* startState, void* endState, MP_VECTOR< void* >* path, float* totalCost );
467
+
468
+ /**
469
+ Find all the states within a given cost from startState.
470
+
471
+ @param startState Input, the starting state for the path.
472
+ @param near All the states within 'maxCost' of 'startState', and cost to that state.
473
+ @param maxCost Input, the maximum cost that will be returned. (Higher values return
474
+ larger 'near' sets and take more time to compute.)
475
+ @return Success or failure, expressed as SOLVED or NO_SOLUTION.
476
+ */
477
+ int SolveForNearStates( void* startState, MP_VECTOR< StateCost >* near, float maxCost );
478
+
479
+ /** Should be called whenever the cost between states or the connection between states changes.
480
+ Also frees overhead memory used by MicroPather, and calling will free excess memory.
481
+ */
482
+ void Reset();
483
+
484
+ // Debugging function to return all states that were used by the last "solve"
485
+ void StatesInPool( MP_VECTOR< void* >* stateVec );
486
+ void GetCacheData( CacheData* data );
487
+
488
+ private:
489
+ MicroPather( const MicroPather& ); // undefined and unsupported
490
+ void operator=( const MicroPather ); // undefined and unsupported
491
+
492
+ void GoalReached( PathNode* node, void* start, void* end, MP_VECTOR< void* > *path );
493
+
494
+ void GetNodeNeighbors( PathNode* node, MP_VECTOR< NodeCost >* neighborNode );
495
+
496
+ #ifdef DEBUG
497
+ //void DumpStats();
498
+ #endif
499
+
500
+ PathNodePool pathNodePool;
501
+ MP_VECTOR< StateCost > stateCostVec; // local to Solve, but put here to reduce memory allocation
502
+ MP_VECTOR< NodeCost > nodeCostVec; // local to Solve, but put here to reduce memory allocation
503
+ MP_VECTOR< float > costVec;
504
+
505
+ Graph* graph;
506
+ unsigned frame; // incremented with every solve, used to determine if cached data needs to be refreshed
507
+ PathCache* pathCache;
508
+ };
509
+ }; // namespace grinliz
510
+
511
+ #endif