1 /*
2    Copyright (C) 2004 by James Gregory
3    Part of the GalaxyHack project
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License.
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY.
9 
10    See the COPYING file for more details.
11 */
12 
13 
14 #ifndef GUARD_RTSUnit_Base
15 #define GUARD_RTSUnit_Base
16 
17 #include "Enums.h"
18 #include "Stuff.h"
19 #include "RTSStructs.h"
20 
21 #include <vector>
22 #include <string>
23 #include <iterator>
24 
25 using std::vector;
26 using std::string;
27 using std::istream_iterator;
28 
29 //foward declaration
30 class Group;
31 
32 class RTSUnit_Base {
33 public:
34 	RTSUnit_Base(const string& iName, int iMySide, int iMyGroup);
35 	virtual ~RTSUnit_Base();
36 
37 	void SetPos(float ix, float iy);
38 
39 	//blank version for small ships
SelectSmallTargets()40 	virtual void SelectSmallTargets() {}
41 	//blank version for everyone else
SelectSmallTargets(AICommands & theCommands)42 	virtual void SelectSmallTargets(AICommands& theCommands) {}
43 
44 	virtual void Move(float distx, float disty);
45 	void AddExtraMoveFrames();
46 	void Fire(AICommands& theCommands);
47 
48 	void BeenHit(int power);
49 
50 	virtual void Upkeep();
51 	void DeadUpkeep();
52 
53 	void DrawSelf();
DrawSelfPixels()54 	virtual void DrawSelfPixels() {}
55 
56 	void DrawUnitPic(int x, int y);
57 
58 	int GetPointsValue() const;
59 	int GetMaxPoints() const;
GetCenter()60 	CoordsInt GetCenter() const {CoordsInt tmp = {static_cast<int>(myx + width / 2), static_cast<int>(myy + height / 2)}; return tmp;}
61 
62 	//base version throws error
63 	virtual int GetCapacity() const;
64 	//base version just returns 0
GetFrCapacity()65 	virtual int GetFrCapacity() const {return 0;}
66 
67 	//standard is just width >> 1 height >> 1
68 	virtual CoordsInt GetWeakSpot() const;
69 	virtual void GetWeapCoords(vector<CoordsInt>& giveSmall, CoordsInt& giveBig) const = 0;
70 
71 	//Change:
72 	void ChangeUnitPic(const string& newPic);
73 	void ChangeSmallType(WeaponType newType, bool ignorePoints = 0);
74 	void ChangeBigType(WeaponType newType, bool ignorePoints = 0);
75 	void ChangeEngine(const string& newType, bool ignorePoints = 0);
76 	void ChangeArmour(const string& newStat, bool ignorePoints = 0);
77 	void ChangeShield(const string& newStat, bool ignorePoints = 0);
78 	virtual void ChangeCSType(CapShipType newType);
79 
80 	//Static
81 	static string UTypeToPicDir(UnitType uType);
82 
83 	string name;
84 	UnitType myType;
85 	CapShipType myCSType;
86 	string picName;
87 
88 	float speed;
89 	int armourCurrent;
90 	int armourMax;
91 	int shieldCurrent;
92 	int shieldMax;
93 	int shieldRecharge;
94 	unsigned int shieldTimer;
95 
96 	int smallNumber;
97 	WeaponType smallType;
98 	WeaponType bigType;
99 
100 	int bigAmmo;
101 
102 	string engineName;
103 	string shieldName;
104 	string armourName;
105 
106 	float myx;
107     float myy;
108 	int width;
109 	int height;
110 
111 	SDL_Rect USRect;
112 
113 	int extraMoveFrames;
114 	float extraMovex;
115 	float extraMovey;
116 	int doExtraMove;
117 
118 	int mySide;
119 	int myGroup;
120 
121 	int alive;
122 
123 protected:
124 	void LoadData();
125 	void LoadPicture();
126 	void FreePicture();
127 	virtual void DefaultTypeDepStats();
128 	void SetSpeed();
129 
130 	virtual void FireSmall(AICommands& theCommands)= 0;
131 	void FireBig(AICommands& theCommands);
132 	void FireSpecial(AICommands& theCommands);
133 
134 	virtual void InitSmall() = 0;
135 
136 	virtual void Explode() = 0;
137 
138 	virtual void SetSmallNumber() = 0;
139 
140 	WeaponStage bigStage;
141 	unsigned int bigTimer;
142 	int bigAiming;
143 	CoordsInt bigTarget;
144 	int bigTargetUnit;
145 	CoordsInt targetWeakSpot;
146 	bool weHitWithBig;
147 
148 	unsigned int explodeTimer;
149 
150 	bool bFlip;
151 	SDL_Surface* pic;
152 	SDL_Surface* picFlip;
153 };
154 
155 class AutoFireUnit: public RTSUnit_Base {
156 public:
157 	AutoFireUnit(const string& iName, int iMySide, int iMyGroup);
158 
159 	void LoadWeapCoords(istream_iterator<char>& iter, istream_iterator<char>& fileEnd);
160 	void SelectSmallTargets();
161 	virtual void Upkeep();
162 
163 	void GetWeapCoords(vector<CoordsInt>& giveSmall, CoordsInt& giveBig) const;
164 
165 protected:
166 	void InitSmall();
167 	void FindInRange(vector<CoordsInt>& inRange, int range);
168 	void SetupSmallForFiring(int nSmall, vector<CoordsInt>& inRange);
169 	void FireSmall(AICommands& theCommands);
170 
171 	CoordsInt GetWeakSpot() const;
172 
173 	vector<WeaponStage> smallStage;
174 	vector<unsigned int> smallTimer;
175 	vector<int> smallAiming;
176 	vector<CoordsInt> smallTargets;
177 
178 	vector<CoordsInt> smallPositions;
179 	CoordsInt bigPosition;
180 };
181 
182 class CapitalShip: public AutoFireUnit {
183 public:
184 	CapitalShip(int iMySide, int iMyGroup, const string& iName, CapShipType iCSType);
185 
186 	void DefaultTypeDepStats();
187 
188 	void Upkeep();
189 
GetCapacity()190 	int GetCapacity() const {return capacity;}
191 	int GetFrCapacity() const;
192 
193 	void ChangeCSType(CapShipType newType);
194 
195 	int capacity;
196 
197 protected:
198 	void Explode();
199 	void SetSmallNumber();
200 };
201 
202 class Frigate: public AutoFireUnit {
203 public:
204 	Frigate(int iMySide, int iMyGroup, const string& iName);
205 
206 	void DefaultTypeDepStats();
207 
208 	void Upkeep();
209 
210 	void DrawSelfPixels();
211 
212 protected:
213 	void Explode();
214 	void SetSmallNumber();
215 };
216 
217 class SmallShip: public RTSUnit_Base {
218 public:
219 	SmallShip(int iMySide, int iMyGroup, const string& iName);
220 
221 	void DefaultTypeDepStats();
222 
223 	void SelectSmallTargets(AICommands& theCommands);
224 
225 	void Move(float distx, float disty);
226 
227 	void Upkeep();
228 
229 	void GetWeapCoords(vector<CoordsInt>& giveSmall, CoordsInt& giveBig) const;
230 
231 protected:
232 	void InitSmall();
233 	void FireSmall(AICommands& theCommands);
234 
235 	void Explode();
236 	void SetSmallNumber();
237 
238 	WeaponStage smallStage;
239 	unsigned int smallTimer;
240 	int smallAiming;
241 	CoordsInt smallTarget;
242 };
243 
244 
245 #endif
246