1 #ifndef CUNIT_TABLE_H
2 #define CUNIT_TABLE_H
3 
4 #include <map>
5 #include <vector>
6 #include <stack>
7 #include <string>
8 
9 #include "ARegistrar.h"
10 #include "headers/Defines.h"
11 #include "headers/HEngine.h"
12 #include "CWishList.h"
13 
14 class CUnit;
15 class AIClasses;
16 
17 struct UnitCategoryCompare {
operatorUnitCategoryCompare18 	bool operator() (const unitCategory& a, const unitCategory& b) const {
19 		if (a.none() && b.any())
20 			return true;
21 		if (a.any() && b.none())
22 			return false;
23 		for (int bit = 0; bit < MAX_CATEGORIES; bit++) {
24 			if (a[bit]) {
25 				if (!b[bit])
26 					return true;
27 			}
28 			else if(b[bit])
29 				return false;
30 		}
31 		return false;
32 	}
33 };
34 
35 /* Unit wrapper struct */
36 struct UnitType {
37 	const UnitDef* def; /* As defined by spring */
38 	int techLevel;      /* By looking at the factory cost in which it can be build */
39 	float dps;          /* A `briljant' measurement for the power of a unit :P */
40 	float cost;         /* Cost defined in energy units */
41 	float costMetal;
42 	float energyMake;   /* Netto energy this unit provides */
43 	float metalMake;    /* Netto metal this unit provides */
44 	unitCategory cats;  /* Custom categories this unit belongs */
45 	std::map<int, UnitType*> buildBy;
46 	std::map<int, UnitType*> canBuild;
47 
getIDUnitType48 	inline int getID() const { return def->id; }
49 };
50 
51 class CUnitTable: public ARegistrar {
52 
53 public:
54 	typedef std::map<unitCategory, std::string, UnitCategoryCompare> UnitCategory2StrMap;
55 
56 	CUnitTable(AIClasses* ai);
57 	~CUnitTable();
58 
59 	/* unitCategories in string format, see Defines.h */
60 	static std::map<unitCategory, std::string, UnitCategoryCompare> cat2str;
61 	static std::map<std::string, unitCategory> str2cat;
62 	/* Unit categories in a vector */
63 	static std::vector<unitCategory> cats;
64 	/* Total number of unit types */
65 	int numUnits;
66 	/* Max unit power */
67 	float maxUnitPower;
68 	/* All units flattened in a map <udef_id, unit_type> */
69 	std::map<int, UnitType> units;
70 	/* movetypes, used by pathfinder <move_type_id, move_data> */
71 	std::map<int, MoveData*> moveTypes;
72 	/* Ingame units, set in eco module */
73 	std::map<int, bool>         idle;
74 	std::map<int, bool>         builders; // <unit_id, job_is_finished>
75 	std::map<int, CUnit*>       metalMakers;
76 	std::map<int, CUnit*>       activeUnits;
77 	std::map<int, CUnit*>       factories;
78 	std::map<int, CUnit*>       defenses;
79 	std::map<int, CUnit*>       staticUnits;
80 	std::map<int, CUnit*>       staticWaterUnits;
81 	std::map<int, CUnit*>       staticEconomyUnits;
82 	std::map<int, CUnit*>       energyStorages;
83 	std::map<int, CUnit*>       unitsUnderPlayerControl;
84 	std::map<int, unitCategory> unitsUnderConstruction; // <unit_id, cats_from_wishlist>
85 	std::map<int, Wish>         unitsBuilding; // <builder_id, wish>
86 
87 
88 	static CUnit* getUnitByDef(std::map<int, CUnit*>& dic, const UnitDef* udef);
89 
90 	static CUnit* getUnitByDef(std::map<int, CUnit*>& dic, int did);
91 	/* Returns a fresh CUnit instance */
92 	CUnit* requestUnit(int uid, int bid);
93 	/* Return unit by ingame id */
94 	CUnit* getUnit(int id);
95 	/* Called on each frame%MULTIPLEXER */
96 	void update();
97 	/* Overload */
98 	void remove(ARegistrar &unit);
99 	/* Returns a unittype with categories that ut can build */
100 	UnitType* canBuild(UnitType *ut, unitCategory categories);
101 
102 	void getBuildables(UnitType *ut, unitCategory include, unitCategory exclude, std::multimap<float, UnitType*>& candidates);
103 
104 	int factoryCount(unitCategory c);
105 
106 	bool gotFactory(unitCategory c);
107 
108 	int unitCount(unitCategory c);
109 	/* Select first unit type which matches requested cats */
110 	UnitType* getUnitTypeByCats(unitCategory c);
111 
112 	int setOnOff(std::map<int, CUnit*>& list, bool value);
113 	/* Debugging functions */
114 	std::string debugCategories(UnitType* ut);
115 
116 	std::string debugCategories(const unitCategory& categories);
117 
118 	void debugUnitDefs(UnitType* ut);
119 
120 	void debugWeapons(UnitType* ut);
121 
122 protected:
123 	AIClasses* ai;
124 
125 private:
126 	char buf[255];
127 
128 	/* Build the lists buildby and canbuild per unit */
129 	void buildTechTree();
130 	/* Generate the categorizations config file */
131 	void generateCategorizationFile(std::string& fileName);
132 	/* Categorize the units, see defines.h for categories */
133 	unitCategory categorizeUnit(UnitType *ut);
134 	/* Calculate the unit damage per second */
135 	float calcUnitDps(UnitType *ut);
136 	/* Create a UnitType of ud and insert into units */
137 	UnitType* insertUnit(const UnitDef *ud);
138 };
139 
140 #endif
141