1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef MOVEDEF_HANDLER_H
4 #define MOVEDEF_HANDLER_H
5 
6 #include <vector>
7 #include <map>
8 #include <string>
9 
10 #include "System/float3.h"
11 #include "System/creg/creg_cond.h"
12 
13 class MoveDefHandler;
14 class CSolidObject;
15 class LuaTable;
16 
17 #pragma pack(push, 1)
18 struct MoveDef {
19 	CR_DECLARE_STRUCT(MoveDef)
20 
21 	MoveDef();
22 	MoveDef(const LuaTable& moveDefTable, int moveDefID);
23 
24 	bool TestMoveSquare(
25 		const CSolidObject* collider,
26 		const int xTestMoveSqr,
27 		const int zTestMoveSqr,
28 		const float3& testMoveDir = ZeroVector,
29 		bool testTerrain = true,
30 		bool testObjects = true,
31 		bool centerOnly = false,
32 		float* minSpeedMod = NULL,
33 		int* maxBlockBit = NULL
34 	) const;
35 	bool TestMoveSquare(
36 		const CSolidObject* collider,
37 		const float3& testMovePos,
38 		const float3& testMoveDir = ZeroVector,
39 		bool testTerrain = true,
40 		bool testObjects = true,
41 		bool centerOnly = false,
42 		float* minSpeedMod = NULL,
43 		int* maxBlockBit = NULL
44 	) const;
45 	float GetDepthMod(const float height) const;
46 	unsigned int GetCheckSum() const;
47 
GetDefaultMinWaterDepthMoveDef48 	static float GetDefaultMinWaterDepth() { return -1e6f; }
GetDefaultMaxWaterDepthMoveDef49 	static float GetDefaultMaxWaterDepth() { return +1e6f; }
50 
51 	/// determines which of the {tank,kbot,hover,ship}Speed
52 	/// modifiers this MoveDef receives from a terrain-type
53 	enum SpeedModClass {
54 		Tank  = 0,
55 		KBot  = 1,
56 		Hover = 2,
57 		Ship  = 3
58 	};
59 	enum TerrainClass {
60 		Land  = 0, /// we are restricted to "land" (terrain with height >= 0)
61 		Water = 1, /// we are restricted to "water" (terrain with height < 0)
62 		Mixed = 2, /// we can exist at heights both greater and smaller than 0
63 	};
64 	enum DepthModParams {
65 		DEPTHMOD_MIN_HEIGHT = 0,
66 		DEPTHMOD_MAX_HEIGHT = 1,
67 		DEPTHMOD_MAX_SCALE  = 2,
68 		DEPTHMOD_QUA_COEFF  = 3,
69 		DEPTHMOD_LIN_COEFF  = 4,
70 		DEPTHMOD_CON_COEFF  = 5,
71 		DEPTHMOD_NUM_PARAMS = 6,
72 	};
73 	enum SpeedModMults {
74 		SPEEDMOD_MOBILE_IDLE_MULT = 0,
75 		SPEEDMOD_MOBILE_BUSY_MULT = 1,
76 		SPEEDMOD_MOBILE_MOVE_MULT = 2,
77 		SPEEDMOD_MOBILE_NUM_MULTS = 3,
78 	};
79 
80 	std::string name;
81 
82 	SpeedModClass speedModClass;
83 	TerrainClass terrainClass;
84 
85 	/// of the footprint
86 	int xsize, xsizeh;
87 	int zsize, zsizeh;
88 
89 	/// minWaterDepth for ships, maxWaterDepth otherwise
90 	/// controls movement and (un-)loading constraints
91 	float depth;
92 	float depthModParams[DEPTHMOD_NUM_PARAMS];
93 	float maxSlope;
94 	float slopeMod;
95 	float crushStrength;
96 
97 	// PF speedmod-multipliers for squares blocked by mobile units
98 	// (which can respectively be "idle" == non-moving and have no
99 	// orders, "busy" == non-moving but have orders, or "moving")
100 	// NOTE:
101 	//     includes one extra padding element to make the moveMath
102 	//     member start on an 8-byte boundary for 64-bit platforms
103 	float speedModMults[SPEEDMOD_MOBILE_NUM_MULTS + 1];
104 
105 	unsigned int pathType;
106 	/// number of UnitDef types that refer to this MoveDef class
107 	unsigned int udRefCount;
108 
109 	/// heatmap path-cost modifier
110 	float heatMod;
111 	float flowMod;
112 
113 	/// heat produced by a path
114 	int heatProduced;
115 
116 	/// do we stick to the ground when in water?
117 	bool followGround;
118 	/// are we supposed to be a purely sub-surface ship?
119 	bool subMarine;
120 
121 	/// do we try to pathfind around squares blocked by mobile units?
122 	///
123 	/// this also serves as a padding byte for alignment so compiler
124 	/// does not insert it (GetCheckSum would need to skip such bytes
125 	/// otherwise, since they are never initialized)
126 	bool avoidMobilesOnPath;
127 	bool allowTerrainCollisions;
128 
129 	/// do we leave heat and avoid any left by others?
130 	bool heatMapping;
131 	bool flowMapping;
132 };
133 #pragma pack(pop)
134 
135 
136 class LuaParser;
137 class MoveDefHandler
138 {
139 	CR_DECLARE_STRUCT(MoveDefHandler)
140 public:
141 	MoveDefHandler(LuaParser* defsParser);
142 
GetMoveDefByPathType(unsigned int pathType)143 	MoveDef* GetMoveDefByPathType(unsigned int pathType) { return &moveDefs[pathType]; }
144 	MoveDef* GetMoveDefByName(const std::string& name);
145 
GetNumMoveDefs()146 	unsigned int GetNumMoveDefs() const { return moveDefs.size(); }
GetCheckSum()147 	unsigned int GetCheckSum() const { return checksum; }
148 
149 private:
150 	std::vector<MoveDef> moveDefs;
151 	std::map<std::string, int> moveDefNames;
152 
153 	unsigned int checksum;
154 };
155 
156 extern MoveDefHandler* moveDefHandler;
157 
158 #endif // MOVEDEF_HANDLER_H
159 
160