File: unit_updater.h

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (190 lines) | stat: -rw-r--r-- 6,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// ==============================================================
//	This file is part of Glest (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱo Figueroa
//
//	You can redistribute this code and/or modify it under
//	the terms of the GNU General Public License as published
//	by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version
// ==============================================================

#ifndef _GLEST_GAME_UNITUPDATER_H_
#define _GLEST_GAME_UNITUPDATER_H_

#ifdef WIN32
    #include <winsock2.h>
    #include <winsock.h>
#endif

#include "gui.h"
#include "particle.h"
#include "randomgen.h"
#include "command.h"
#include "leak_dumper.h"

using Shared::Graphics::ParticleObserver;
using Shared::Util::RandomGen;

namespace Glest{ namespace Game{

class Unit;
class Map;
class ScriptManager;
class PathFinder;

// =====================================================
//	class UnitUpdater
//
///	Updates all units in the game, even the player
///	controlled units, performs basic actions only
///	such as responding to an attack
// =====================================================

class ParticleDamager;
class Cell;

class UnitRangeCellsLookupItem {
public:

	//int UnitRangeCellsLookupItemCacheTimerCountIndex;
	std::vector<Cell *> rangeCellList;

	static time_t lastDebug;
};

class AttackWarningData {
public:
	Vec2f attackPosition;
	int lastFrameCount;
};

class UnitUpdater {
private:
	friend class ParticleDamager;
	typedef vector<AttackWarningData*> AttackWarnings;

private:
	static const int maxResSearchRadius= 10;
	static const int harvestDistance= 5;
	static const int ultraResourceFactor= 3;
	static const int megaResourceFactor= 4;

private:
	const GameCamera *gameCamera;
	Gui *gui;
	Map *map;
	World *world;
	Console *console;
	ScriptManager *scriptManager;
	PathFinder *pathFinder;
	Game *game;
	//RandomGen random;
	Mutex *mutexAttackWarnings;
	float attackWarnRange;
	AttackWarnings attackWarnings;

	Mutex *mutexUnitRangeCellsLookupItemCache;
	std::map<Vec2i, std::map<int, std::map<int, UnitRangeCellsLookupItem > > > UnitRangeCellsLookupItemCache;
	//std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer;
	//int UnitRangeCellsLookupItemCacheTimerCount;

	bool findCachedCellsEnemies(Vec2i center, int range,
								int size, vector<Unit*> &enemies,
								const AttackSkillType *ast, const Unit *unit,
								const Unit *commandTarget);
	void findEnemiesForCell(const AttackSkillType *ast, Cell *cell, const Unit *unit,
							const Unit *commandTarget,vector<Unit*> &enemies);

public:
	UnitUpdater();
    void init(Game *game);
    ~UnitUpdater();

	//update skills
    bool updateUnit(Unit *unit);
	void spawnAttack(Unit *unit,string spawnUnit,int spawnUnitcount,bool spawnUnitAtTarget,Vec2i targetPos=Vec2i(-10,-10));

    //update commands
    void updateUnitCommand(Unit *unit, int frameIndex);
    void updateStop(Unit *unit, int frameIndex);
    void updateMove(Unit *unit, int frameIndex);
    void updateAttack(Unit *unit, int frameIndex);
    void updateAttackStopped(Unit *unit, int frameIndex);
    void updateBuild(Unit *unit, int frameIndex);
    void updateHarvest(Unit *unit, int frameIndex);
    void updateHarvestEmergencyReturn(Unit *unit, int frameIndex);
    void updateRepair(Unit *unit, int frameIndex);
    void updateProduce(Unit *unit, int frameIndex);
    void updateUpgrade(Unit *unit, int frameIndex);
	void updateMorph(Unit *unit, int frameIndex);
	void updateSwitchTeam(Unit *unit, int frameIndex);

	void clearUnitPrecache(Unit *unit);
	void removeUnitPrecache(Unit *unit);

	inline unsigned int getAttackWarningCount() const { return (unsigned int)attackWarnings.size(); }
	std::pair<bool,Unit *> unitBeingAttacked(const Unit *unit);
	void unitBeingAttacked(std::pair<bool,Unit *> &result, const Unit *unit, const AttackSkillType *ast,float *currentDistToUnit=NULL);
	vector<Unit*> enemyUnitsOnRange(const Unit *unit,const AttackSkillType *ast);
	void findEnemiesForCell(const Vec2i pos, int size, int sightRange, const Faction *faction, vector<Unit*> &enemies, bool attackersOnly) const;

	vector<Unit*> findUnitsInRange(const Unit *unit, int radius);

	string getUnitRangeCellsLookupItemCacheStats();

	void saveGame(XmlNode *rootNode);
	void loadGame(const XmlNode *rootNode);

	void clearCaches();

private:
    //attack
    void hit(Unit *attacker);
	void hit(Unit *attacker, const AttackSkillType* ast, const Vec2i &targetPos, Field targetField, int damagePercent);
	void damage(Unit *attacker, const AttackSkillType* ast, Unit *attacked, float distance, int damagePercent);
	void startAttackParticleSystem(Unit *unit, float lastAnimProgress, float animProgress);

	//misc
    bool searchForResource(Unit *unit, const HarvestCommandType *hct);
    bool attackerOnSight(Unit *unit, Unit **enemyPtr, bool evalMode=false);
    bool attackableOnSight(Unit *unit, Unit **enemyPtr, const AttackSkillType *ast, bool evalMode=false);
    bool attackableOnRange(Unit *unit, Unit **enemyPtr, const AttackSkillType *ast, bool evalMode=false);
	bool unitOnRange(Unit *unit, int range, Unit **enemyPtr, const AttackSkillType *ast,bool evalMode=false);
	void enemiesAtDistance(const Unit *unit, const Unit *priorityUnit, int distance, vector<Unit*> &enemies);

	Unit * findPeerUnitBuilder(Unit *unit);
	void SwapActiveCommand(Unit *unitSrc, Unit *unitDest);
	void SwapActiveCommandState(Unit *unit, CommandStateType commandStateType,
								const CommandType *commandType,
								int originalValue,int newValue);
	void findUnitsForCell(Cell *cell, vector<Unit*> &units);

};

// =====================================================
//	class ParticleDamager
// =====================================================

class ParticleDamager: public ParticleObserver {

public:
	UnitReference attackerRef;
	const AttackSkillType* ast;
	const ProjectileType* projectileType;
	UnitUpdater *unitUpdater;
	const GameCamera *gameCamera;
	Vec2i targetPos;
	Field targetField;

public:
	ParticleDamager(Unit *attacker, const ProjectileType *projectileType, UnitUpdater *unitUpdater, const GameCamera *gameCamera);
	virtual void update(ParticleSystem *particleSystem);

	virtual void saveGame(XmlNode *rootNode);
	virtual void loadGame(const XmlNode *rootNode,void *genericData);
};

}}//end namespace

#endif