Debian Codegraph @

engine/include/player.h

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name player.h - The player headerfile. */
11 //
12 // (c) Copyright 1998-2007 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27
28 #ifndef __PLAYER_H__
29 #define __PLAYER_H__
30
31 //@{
32
33 /*----------------------------------------------------------------------------
34 -- Documentation
35 ----------------------------------------------------------------------------*/
36
37 /**
38 ** @class CPlayer player.h
39 **
40 ** \#include "player.h"
41 **
42 ** This structure contains all informations about a player in game.
43 **
44 ** The player structure members:
45 **
46 ** CPlayer::Index
47 **
48 ** This is the unique slot number. It is not possible that two
49 ** players have the same slot number at the same time. The slot
50 ** numbers are reused in the future. This means if a player is
51 ** defeated, a new player can join using this slot. Currently
52 ** #PlayerMax players are supported. This member is used to
53 ** access bit fields.
54 ** Slot #PlayerNumNeutral is reserved for the neutral units
55 ** like gold-mines or critters.
56 **
57 ** @note Should call this member Slot?
58 **
59 ** CPlayer::Name
60 **
61 ** Name of the player, encoded in UTF-8.
62 ** In local games, the name is used for displays, and there is no
63 ** strict restriction on the length, although there is limited
64 ** space on the screen.
65 ** In network games, the name is also used for chatting,
66 ** and it is restricted to NetPlayerNameSize bytes, including
67 ** the final zero.
68 **
69 ** CPlayer::Type
70 **
71 ** Type of the player. This field is setup from the level (map).
72 ** We support currently #PlayerNeutral,
73 ** #PlayerNobody, #PlayerComputer, #PlayerPerson,
74 ** #PlayerRescuePassive and #PlayerRescueActive.
75 ** @see #PlayerTypes.
76 **
77 ** CPlayer::AiName
78 **
79 ** AI name for computer. This field is setup
80 ** from the map. Used to select the AI for the computer
81 ** player. Might differ from CAiType::Name of the AI
82 ** that was actually selected.
83 **
84 ** CPlayer::Team
85 **
86 ** Team of player. Selected during network game setup. All players
87 ** of the same team are allied and enemy to all other teams.
88 ** @note It is planned to show the team on the map.
89 **
90 ** CPlayer::Enemy
91 **
92 ** A bit field which contains the enemies of this player.
93 ** If CPlayer::Enemy & (1<<CPlayer::Index) != 0 its an enemy.
94 ** Setup during startup using the CPlayer::Team, can later be
95 ** changed with diplomacy. CPlayer::Enemy and CPlayer::Allied
96 ** are combined, if none bit is set, the player is neutral.
97 ** @note You can be allied to a player, which sees you as enemy.
98 **
99 ** CPlayer::Allied
100 **
101 ** A bit field which contains the allies of this player.
102 ** If CPlayer::Allied & (1<<CPlayer::Index) != 0 its an allied.
103 ** Setup during startup using the Player:Team, can later be
104 ** changed with diplomacy. CPlayer::Enemy and CPlayer::Allied
105 ** are combined, if none bit is set, the player is neutral.
106 ** @note You can be allied to a player, which sees you as enemy.
107 **
108 ** CPlayer::SharedVision
109 **
110 ** A bit field which contains shared vision for this player.
111 ** Shared vision only works when it's activated both ways. Really.
112 **
113 ** CPlayer::StartX CPlayer::StartY
114 **
115 ** The tile map coordinates of the player start position. 0,0 is
116 ** the upper left on the map. This members are setup from the
117 ** map and only important for the game start.
118 ** They control which part of the map is initially scrolled to the view,
119 ** and in some game types (e.g. ::SettingsGameTypeTopVsBottom),
120 ** they also control which players are initially allied.
121 **
122 ** CPlayer::UnitTypesCount[::UnitTypeMax]
123 **
124 ** Total count for each different unit type. Used by the AI and
125 ** for dependencies checks. The addition of all counts should
126 ** be CPlayer::TotalNumUnits.
127 ** @note Should not use the maximum number of unit-types here,
128 ** only the real number of unit-types used.
129 **
130 ** CPlayer::AiEnabled
131 **
132 ** If the player is controlled by the computer and this flag is
133 ** true, than the player is handled by the AI on this local
134 ** computer.
135 **
136 ** @note Currently the AI is calculated parallel on all computers
137 ** in a network play. It is planned to change this.
138 **
139 ** CPlayer::Ai
140 **
141 ** AI structure pointer. Please look at #PlayerAi for more
142 ** informations.
143 **
144 ** CPlayer::Units
145 **
146 ** A table of all (CPlayer::TotalNumUnits) units of the player.
147 **
148 ** CPlayer::TotalNumUnits
149 **
150 ** Total number of units (incl. buildings) in the CPlayer::Units
151 ** table.
152 **
153 ** CPlayer::NumBuildings
154 **
155 ** Total number of buildings in the CPlayer::Units table.
156 **
157 ** CPlayer::UnitLimit
158 **
159 ** Number of non-building units allowed.
160 ** When CPlayer::TotalNumUnit - CPlayer::NumBuildings >= CPlayer::UnitLimit,
161 ** the player cannot train any more non-building units;
162 ** but may still be able to build buildings.
163 ** @note that all limits are always checked.
164 **
165 ** CPlayer::BuildingLimit
166 **
167 ** Number of buildings allowed.
168 ** When CPlayer::NumBuildings >= CPlayer::BuildingLimit,
169 ** the player cannot build any more buildings;
170 ** but may still be able to train other units.
171 ** @note that all limits are always checked.
172 **
173 ** CPlayer::TotalUnitLimit
174 **
175 ** Number of total units allowed.
176 ** When CPlayer::TotalNumUnits >= CPlayer::TotalUnitLimit,
177 ** the player cannot train or build any more units of any kind.
178 ** @note that all limits are always checked.
179 **
180 ** CPlayer::Score
181 **
182 ** Total number of points. You can get points for killing units,
183 ** destroying buildings ...
184 **
185 ** CPlayer::TotalUnits
186 **
187 ** Total number of non-building units made or captured
188 ** by the player, including any that have since been killed.
189 ** This counter never decreases during a game.
190 **
191 ** CPlayer::TotalBuildings
192 **
193 ** Total number of buildings made or captured by the player,
194 ** including any that have since been razed.
195 ** This counter never decreases during a game.
196 **
197 ** CPlayer::TotalResources[::MaxCosts]
198 **
199 ** Total number of resources collected.
200 ** This counter never decreases during a game, unless some
201 ** unit type has been strangely configured with a negative
202 ** CUnitType::ProductionRate.
203 **
204 ** CPlayer::TotalRazings
205 **
206 ** Total number of buildings destroyed.
207 **
208 ** CPlayer::TotalKills
209 **
210 ** Total number of kills.
211 **
212 ** CPlayer::Color
213 **
214 ** Color of units of this player on the minimap. Index number
215 ** into the global palette.
216 **
217 ** CPlayer::UnitColors
218 **
219 ** Unit colors of this player. Contains the hardware dependent
220 ** pixel values for the player colors (palette index #208-#211).
221 ** Setup from the global palette.
222 ** @note Index #208-#211 are various SHADES of the team color
223 ** (#208 is brightest shade, #211 is darkest shade) .... these
224 ** numbers are NOT red=#208, blue=#209, etc
225 **
226 ** CPlayer::Allow
227 **
228 ** Contains which unit-types are allowed for the
229 ** player. Possible values are:
230 ** @li `A' -- allowed,
231 ** @li `F' -- forbidden,
232 ** @li `R' -- acquired, perhaps other values
233 ** @li `Q' -- acquired but forbidden (does it make sense?:))
234 ** @li `E' -- enabled, allowed by level but currently forbidden
235 ** @see CAllow
236 */
237
238 /*----------------------------------------------------------------------------
239 -- Includes
240 ----------------------------------------------------------------------------*/
241
242 #include <map>
243 #include <string>
244 #include "upgrade_structs.h"
245 #include "video.h"
246
247 /*----------------------------------------------------------------------------
248 -- Declarations
249 ----------------------------------------------------------------------------*/
250
251 typedef int lua_Object; // from tolua++.h
252 struct lua_State;
253
254 class CUnit;
255 class CUnitType;
256 class PlayerAi;
257 class CFile;
258
259 /**
260 ** Types for the player
261 **
262 ** #PlayerNeutral
263 **
264 ** This player is controlled by the computer doing nothing.
265 **
266 ** #PlayerNobody
267 **
268 ** This player is unused. Nobody controlls this player.
269 **
270 ** #PlayerComputer
271 **
272 ** This player is controlled by the computer. CPlayer::AiNum
273 ** selects the AI strategy.
274 **
275 ** #PlayerPerson
276 **
277 ** This player is contolled by a person. This can be the player
278 ** sitting on the local computer or player playing over the
279 ** network.
280 **
281 ** #PlayerRescuePassive
282 **
283 ** This player does nothing, the game pieces just sit in the game
284 ** (being passive)... when a person player moves next to a
285 ** PassiveRescue unit/building, then it is "rescued" and becomes
286 ** part of that persons team. If the city center is rescued, than
287 ** all units of this player are rescued.
288 **
289 ** #PlayerRescueActive
290 **
291 ** This player is controlled by the computer. CPlayer::AiNum
292 ** selects the AI strategy. Until it is rescued it plays like
293 ** an ally. The first person which reaches units of this player,
294 ** can rescue them. If the city center is rescued, than all units
295 ** of this player are rescued.
296 */
297 enum PlayerTypes {
298 PlayerNeutral = 2, /// neutral
299 PlayerNobody = 3, /// unused slot
300 PlayerComputer = 4, /// computer player
301 PlayerPerson = 5, /// human player
302 PlayerRescuePassive = 6, /// rescued passive
303 PlayerRescueActive = 7, /// rescued active
304 };
305
306 #define PlayerNumNeutral (PlayerMax - 1) /// this is the neutral player slot
307
308 /**
309 ** Notify types. Noties are send to the player.
310 */
311 enum NotifyType {
312 NotifyRed, /// Red alram
313 NotifyYellow, /// Yellow alarm
314 NotifyGreen, /// Green alarm
315 };
316
317 /*----------------------------------------------------------------------------
318 -- Player type
319 ----------------------------------------------------------------------------*/
320
321 /// Player structure
322 class CPlayer
323 {
324 public:
325 int Index; /// player as number
326 std::string Name; /// name of non computer
327
328 PlayerTypes Type; /// type of player (human,computer,...)
329 std::string AiName; /// AI for computer
330
331 // friend enemy detection
332 int Team; /// team of player
333 unsigned Enemy; /// enemy bit field for this player
334 unsigned Allied; /// allied bit field for this player
335 unsigned SharedVision; /// shared vision bit field
336
337 int StartX; /// map tile start X position
338 int StartY; /// map tile start Y position
339 inline void SetStartView(int x, int y) { StartX = x; StartY = y; }
340
341 std::map<CUnit *, int *> UnitsConsumingResourcesActual;
342 std::map<CUnit *, int *> UnitsConsumingResourcesRequested;
343 int ProductionRate[MaxCosts]; /// Rate that resources are produced
344 int ActualUtilizationRate[MaxCosts]; /// Rate that resources are used
345 int RequestedUtilizationRate[MaxCosts]; /// Rate that resources are used
346 int StoredResources[MaxCosts]; /// Amount of resources in storage
347 int StorageCapacity[MaxCosts]; /// Storage capacity of resources
348
349 inline void SetEnergyProductionRate(int v) { ProductionRate[EnergyCost] = v; }
350 inline int GetEnergyProductionRate() { return ProductionRate[EnergyCost]; }
351 inline void SetMagmaProductionRate(int v) { ProductionRate[MagmaCost] = v; }
352 inline int GetMagmaProductionRate() { return ProductionRate[MagmaCost]; }
353
354 inline void SetEnergyStored(int v) { StoredResources[EnergyCost] = CYCLES_PER_SECOND * v; }
355 inline int GetEnergyStored() { return StoredResources[EnergyCost] / CYCLES_PER_SECOND; }
356 inline void SetMagmaStored(int v) { StoredResources[MagmaCost] = CYCLES_PER_SECOND * v; }
357 inline int GetMagmaStored() { return StoredResources[MagmaCost] / CYCLES_PER_SECOND; }
358
359 inline void SetEnergyStorageCapacity(int v) { StorageCapacity[EnergyCost] = CYCLES_PER_SECOND * v; }
360 inline int GetEnergyStorageCapacity() { return StorageCapacity[EnergyCost] / CYCLES_PER_SECOND; }
361 inline void SetMagmaStorageCapacity(int v) { StorageCapacity[MagmaCost] = CYCLES_PER_SECOND * v; }
362 inline int GetMagmaStorageCapacity() { return StorageCapacity[MagmaCost] / CYCLES_PER_SECOND; }
363
364 void AddToUnitsConsumingResources(CUnit *unit, int costs[MaxCosts]);
365 void RemoveFromUnitsConsumingResources(CUnit *unit);
366 void UpdateUnitsConsumingResources(CUnit *unit, int costs[MaxCosts]);
367 void RebuildUnitsConsumingResourcesList();
368 void ClearResourceVariables();
369
370 // FIXME: shouldn't use the constant
371 int UnitTypesCount[UnitTypeMax]; /// total units of unit-type
372
373 int AiEnabled; /// handle AI on local computer
374 PlayerAi *Ai; /// Ai structure pointer
375
376 CUnit *Units[UnitMax]; /// units of this player
377 int TotalNumUnits; /// total # units for units' list
378 int NumBuildings; /// # buildings
379
380 int UnitLimit; /// # food units allowed
381 int BuildingLimit; /// # buildings allowed
382 int TotalUnitLimit; /// # total unit number allowed
383
384 int Score; /// Points for killing ...
385 int TotalUnits;
386 int TotalBuildings;
387 int TotalResources[MaxCosts];
388 int TotalRazings;
389 int TotalKills; /// How many unit killed
390
391 inline void SetTotalEnergy(int v) { TotalResources[EnergyCost] = CYCLES_PER_SECOND * v; }
392 inline int GetTotalEnergy() { return TotalResources[EnergyCost] / CYCLES_PER_SECOND; }
393 inline void SetTotalMagma(int v) { TotalResources[MagmaCost] = CYCLES_PER_SECOND * v; }
394 inline int GetTotalMagma() { return TotalResources[MagmaCost] / CYCLES_PER_SECOND; }
395
396 Uint32 Color; /// color of units on minimap
397
398 CUnitColors UnitColors; /// Unit colors for new units
399
400 // Allows:
401 CAllow Allow; /// Allowed for player
402
403
404 /// Change player name
405 void SetName(const std::string &name);
406
407 /// Clear turn related player data
408 void Clear();
409
410 /// Set a resource of the player
411 void SetResource(int resource, int value);
412
413 /// Check if the unit-type didn't break any unit limits and supply/demand
414 int CheckLimits(const CUnitType *type) const;
415
416 /// Does the player have units of that type
417 int HaveUnitTypeByType(const CUnitType *type) const;
418 /// Does the player have units of that type
419 int HaveUnitTypeByIdent(const std::string &ident) const;
420
421 /// Notify player about a problem
422 void Notify(int type, int x, int y, const char *fmt, ...) const GCC_ATTR((format(printf, 5, 6)));
423
424 bool IsEnemy(const CPlayer *x) const;
425 bool IsEnemy(const CUnit *x) const;
426 bool IsAllied(const CPlayer *x) const;
427 bool IsAllied(const CUnit *x) const;
428 bool IsSharedVision(const CPlayer *x) const;
429 bool IsSharedVision(const CUnit *x) const;
430 bool IsBothSharedVision(const CPlayer *x) const;
431 bool IsBothSharedVision(const CUnit *x) const;
432 bool IsTeamed(const CPlayer *x) const;
433 bool IsTeamed(const CUnit *x) const;
434 };
435
436 /*----------------------------------------------------------------------------
437 -- Variables
438 ----------------------------------------------------------------------------*/
439
440 extern int NumPlayers; /// How many player slots used
441 extern CPlayer Players[PlayerMax]; /// All players
442 extern CPlayer *ThisPlayer; /// Player on local computer
443 extern int NoRescueCheck; /// Disable rescue check
444 extern SDL_Color *PlayerColorsRGB[PlayerMax]; /// Player colors
445 extern Uint32 *PlayerColors[PlayerMax]; /// Player colors
446 extern std::string PlayerColorNames[PlayerMax]; /// Player color names
447
448 /**
449 ** Which indexes to replace with player color
450 */
451 extern int PlayerColorIndexStart;
452 extern int PlayerColorIndexCount;
453
454 /*----------------------------------------------------------------------------
455 -- Functions
456 ----------------------------------------------------------------------------*/
457
458 /// Init players
459 extern void InitPlayers(void);
460 /// Clean up players
461 extern void CleanPlayers(void);
462 #ifdef DEBUG
463 extern void FreePlayerColors();
464 #endif
465 /// Save players
466 extern void SavePlayers(CFile *file);
467
468 /// Create a new player
469 extern void CreatePlayer(PlayerTypes type);
470
471
472 /// Initialize the computer opponent AI
473 extern void PlayersInitAi(void);
474 /// Called each game cycle for player handlers (AI)
475 extern void PlayersEachCycle(void);
476 /// Called each second for a given player handler (AI)
477 extern void PlayersEachSecond(int player);
478
479 /// Change current color set to new player of the sprite
480 extern void GraphicPlayerPixels(CPlayer *player, const CGraphic *sprite);
481
482 /// Calculate how many resources the unit needs to request
483 void CalculateRequestedAmount(CUnitType *utype, int bcosts[MaxCosts], int costs[MaxCosts]);
484
485 /// Output debug informations for players
486 extern void DebugPlayers(void);
487
488 /// register ccl features
489 extern void PlayerCclRegister(void);
490
491 extern void CclChangeUnitsOwner(
492 const int topLeft[2],
493 const int bottomRight[2],
494 int oldPlayer,
495 int newPlayer,
496 lua_Object unitTypeLua,
497 lua_State *l);
498
499 /// Allowed to select multiple units, maybe not mine
500 #define CanSelectMultipleUnits(player) \
501 ((player) == ThisPlayer || ThisPlayer->IsTeamed((player)))
502
503 //@}
504
505 #endif // !__PLAYER_H__