1 // _____________________________________________________
2 //
3 // RAI - Skirmish AI for Spring
4 // Author: Reth / Michael Vadovszki
5 // _____________________________________________________
6 
7 #ifndef RAI_UNITDEF_H
8 #define RAI_UNITDEF_H
9 
10 struct sRAIUnitDefBL;
11 struct sRAIUnitDef;
12 struct sRAIBuildList;
13 class cRAIUnitDefHandler;
14 
15 #include "GResourceMap.h"
16 #include "GTerrainMap.h"
17 using std::map;
18 using std::set;
19 
20 const int TASK_NONE = 1; // uses default combat behaviors
21 const int TASK_CONSTRUCT = 2;
22 const int TASK_ASSAULT = 3;
23 const int TASK_SCOUT = 4;
24 const int TASK_SUICIDE = 5;
25 const int TASK_SUPPORT = 6;
26 const int TASK_TRANSPORT = 7;
27 
28 struct sWeaponEfficiency
29 {
30 	float BestRange;	// Best range for fighting this enemy
31 	float rate;			// unused
32 };
33 
34 struct sRAIPrerequisite
35 {
sRAIPrerequisitesRAIPrerequisite36 	sRAIPrerequisite(sRAIUnitDef* RAIud)
37 	{
38 		udr=RAIud;
39 		buildLine=0;
40 	};
41 	sRAIUnitDef *udr;
42 	int buildLine;		// This refers to how many other builders must be build to reach this option
43 };
44 
45 struct sRAIUnitDefBL
46 {
47 	sRAIUnitDefBL(sRAIUnitDef* RAIud, sRAIBuildList* BuildList, float Efficiency=-1.0, int Task=-1);
48 	~sRAIUnitDefBL();
49 
50 	sRAIUnitDef* RUD;
51 	sRAIBuildList* RBL;
52 
53 	int udIndex;
54 //	int listIndex;
55 	float efficiency;
56 	int task;
57 };
58 
59 struct sRAIUnitDef
60 {
61 	sRAIUnitDef(const UnitDef *unitdef, IAICallback* callback, GlobalResourceMap* RM, GlobalTerrainMap* TM, float EnergyToMetalRatio, cLogFile *l, float MaxFiringRange=3000.0);
62 	int GetPrerequisite(); // returns UnitDef ID
63 	int GetPrerequisiteNewBuilder();
64 	void SetUnitLimit(int num);
65 	void SetULConstructs(int num); // sets the number of units that can be built excluding 'UnitsActive' && 'UnitConstructsActive'
66 	void CheckUnitLimit();		// called after 'UnitsActive' or 'UnitConstructs' is modified, updates 'RBUnitLimit'
67 	void CheckBuildOptions();	// called after 'RBUnitLimit','RBCost','RBPrereq','Disabled' or 'UnitsActive' is modified, updates 'CanBuild','CanBeBuilt','RBPrereq', and 'HasPrerequisite' for all 'BuildOptions'
68 	bool IsNano();
69 	bool IsCategory(string category); // only used during initialization
70 	sRAIUnitDefBL* GetBuildList(string Name);
71 
72 	sRAIUnitDefBL *List[35];	// possible lists a unit may be assigned to
73 	int ListSize;				// 'List' array size in use
74 	map<int,sRAIUnitDef*> BuildOptions;					// key value = UnitDef ID, You could think of this as an improved version of UnitDef->buildOptions
75 	map<int,sRAIUnitDef*> PrerequisiteOptions;			// key value = UnitDef ID, a list of the types of units can build this unit
76 	map<int,sRAIPrerequisite> AllPrerequisiteOptions;	// key value = UnitDef ID, a list of anything that can build this unit, or somthing that can build a unit that can build this unit, or so on..
77 //	map<int,sWeaponEfficiency> WeaponEff;				// UNFINISHED: key value = Enemy UnitDef ID
78 	TerrainMapMobileType *mobileType;		// which Terrain-Map to use for building placement, immobile factories inherent this from it's build options
79 	TerrainMapImmobileType *immobileType;	// which Terrain-Map to use for building placement
80 	const UnitDef* ud;			// Always valid
81 	bool HighEnergyDemand;		// This unit requires an unbalanced amount of energy to remain active, EX: metal makers, moho-mines in XTA
82 	float MetalDifference;		// How much metal will this produce/cost
83 	float EnergyDifference;		// How much energy will this produce/cost
84 	float OnOffMetalDifference;	// How much metal will this produce/cost when turned on
85 	float OnOffEnergyDifference;// How much energy will this produce/cost when turned on, does not consider cloaks
86 	float CloakMaxEnergyDifference;	// - value, = to moving cloaking cost for units that can move or = to normal cost for units that can not
87 	float MetalPCost;			// How much metal production is needed to consider building this
88 	float EnergyPCost;			// How much energy production is needed to consider building this
89 	bool IsBomber;
90 	sWeaponEfficiency WeaponLandEff;	// used if enemy unitdef is unknown
91 	sWeaponEfficiency WeaponAirEff;		// used if enemy unitdef is unknown
92 	sWeaponEfficiency WeaponSeaEff;		// used if enemy unitdef is unknown
93 	const WeaponDef *DGun;		// valid if ud->canDGun=true && a dgun was found, otherwise 0
94 	const WeaponDef *SWeapon;	// valid if a weapon is iW->def->stockpile && iW->def->manualfire, otherwise 0
95 	int WeaponEnergyDifference;	// - value, energy drain per second if firing all weapons continuously
96 	float WeaponMaxEnergyCost;	// the highest amount of energy storage needed to fire any of it's indiviual weapons
97 	float WeaponGuardRange;		// Base Defences only, distance for nearby structures to be considered 'guarded'
98 //	set<int> WeaponDamageType;	// UNFINISHED: types of armors that this unit can damage
99 
100 	// This variables will change during the course of a game
101 	set<int> UnitsActive;			// existing units of this type, used to determine active build options
102 	set<int> UnitConstructsActive;	// existing constructions of this type
103 	int UnitLimit[2];		// [0]: virtual unit limit, max units RAI is willing to build, ud->maxThisUnit is considered  [1]: remaining geo-sites/metal-sites, basicly a construction unit limit
104 	int UnitConstructs;		// units of this type being built, used to help determine active build options
105 	bool CanBuild;			// 'UnitsActive' is > 0
106 	bool CanBeBuilt;		// reverse result of (Disabled, RBUnitLimit, RBCost, RBPrereq) - in other words, RAI is willing to build this
107 	bool HasPrerequisite;	// A unit in 'PrerequisiteOptions' has 'CanBuild' enabled
108 	bool Disabled;			// RAI will not build this unit, however it may still use the unit if gained through given,captured,resurrected
109 	bool RBUnitLimit;		// (R)estrict (B)uilding: the Max of this Unit has been built
110 	bool RBCost;			// (R)estrict (B)uilding: the Cost is too high to build right now
111 	bool RBPrereq;			// (R)estrict (B)uilding: No buildline of units in "AllPrerequisiteOptions" has 'CanBuild or CanBeBuilt' enabled
112 private:
113 	// only used during initialization
114 	bool CheckWeaponType(vector<UnitDef::UnitDefWeapon>::const_iterator udw, int type); // type = 1-3 (land,air,sea)
115 	void SetBestWeaponEff(sWeaponEfficiency *we, int type, float MaxFiringRange);
116 };
117 
118 struct sRAIBuildList
119 {
120 	sRAIBuildList(int MaxDefSize, cRAIUnitDefHandler *UDRHandler);
121 	~sRAIBuildList();
122 	void Disable(int udIndex, bool value=true);
123 	void UDefSwitch(int index1, int index2);
124 
125 	string Name;			// for debugging
126 	cRAIUnitDefHandler *UDR;
127 	sRAIUnitDefBL **UDef;	// Possible Units to Build on this list, index is valid if < UDefSize
128 	int UDefActive;			// 'UDef' indexes below this value have 'CanBeBuilt=true', for later entries 'CanBeBuilt=false'
129 	int UDefActiveTemp;		// Same as UDefActive, but may be moddifed to temperaryly disable some units within 'UDefActive'
130 	int UDefSize;
131 	int priority;			// Amount to build compared to other build lists, if this = -1 then these units are only built based on the needs of a different system
132 	int minUnits;			// (Old) build at least this many before building anything else, however build demands will take presidence.  This is also used to rate the importantance of the buildlist
133 	int unitsActive;		// The amount of units build from this list, used to keep track of the ratio of build options
134 	int index;
135 
136 	// only used during initialization
137 	float minEfficiency;	// The minimal efficiency a unit needs to be accepted for use in this build list.  Note: 1.0 = average value (default)
138 							// Remember that it is not uncommon for there to be a unit this is bad at everything, such units are assigned to the task they are least bad at in respect to MinEfficiency
139 };
140 
141 class cRAIUnitDefHandler
142 {
143 public:
144 	cRAIUnitDefHandler(IAICallback* callback, GlobalResourceMap *RM, GlobalTerrainMap* TM, cLogFile *log);
145 	~cRAIUnitDefHandler();
146 
147 	map<int,sRAIUnitDef> UDR;	// complete record of all unit definitions found, key value = Unit Def ID
148 	sRAIBuildList *BL[35];
149 	int BLSize;
150 	int BLActive;
151 	void BLSwitch(int index1, int index2);
152 
153 	// special buildlists
154 	sRAIBuildList *BLBuilder;
155 	sRAIBuildList *BLEnergy;
156 	sRAIBuildList *BLEnergyL;
157 	sRAIBuildList *BLMetal;
158 	sRAIBuildList *BLMetalL;
159 	sRAIBuildList *BLEnergyStorage;
160 	sRAIBuildList *BLMetalStorage;
161 	sRAIBuildList *BLMobileRadar;
162 	sRAIBuildList *BLAirBase;
163 
164 	set<TerrainMapMobileType*> RBMobile;	// Restricts the building of certain units until a useable area has been reached
165 	set<TerrainMapImmobileType*> RBImmobile;// Restricts the building of certain units until a useable area has been reached
166 	float EnergyToMetalRatio;
167 	float AverageConstructSpeed;
168 private:
169 	struct sBuildLine
170 	{
sBuildLinesBuildLine171 		sBuildLine(int id, int bl)
172 		{
173 			ID=id;
174 			BL=bl;
175 		};
176 		int ID;	// UnitDef ID
177 		int BL;	// BuildLine, the amount of prerequisites that need to be build to reach this option
178 	};
179 	cLogFile *l;
180 };
181 
182 #endif
183