File: ExplosionGenerator.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (239 lines) | stat: -rwxr-xr-x 6,750 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef EXPLOSION_GENERATOR_H
#define EXPLOSION_GENERATOR_H

#include <map>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>

#include "Sim/Objects/WorldObject.h"

class LuaParser;
class LuaTable;
class float3;
class CUnit;
class IExplosionGenerator;


class CExpGenSpawnable: public CWorldObject
{
	CR_DECLARE(CExpGenSpawnable);
public:
	CExpGenSpawnable();
	CExpGenSpawnable(const float3& pos);
	virtual ~CExpGenSpawnable() {}

	virtual void Init(const float3& pos, CUnit* owner) = 0;
};



//! Finds C++ classes with class aliases
class ClassAliasList
{
public:
	void Load(const LuaTable&);
	void Clear() { aliases.clear(); }

	creg::Class* GetClass(const std::string& name);
	std::string FindAlias(const std::string& className);

private:
	std::map<std::string, std::string> aliases;
};



//! loads and stores a list of explosion generators
class CExplosionGeneratorHandler
{
public:
	CExplosionGeneratorHandler();
	~CExplosionGeneratorHandler();

	void ParseExplosionTables();
	IExplosionGenerator* LoadGenerator(const std::string& tag);
	void UnloadGenerator(IExplosionGenerator* explGen);
	const LuaTable* GetExplosionTableRoot() const { return explTblRoot; }
	void ReloadGenerators(const std::string&);

	ClassAliasList projectileClasses;
	ClassAliasList generatorClasses;

protected:
	LuaParser* exploParser;
	LuaParser* aliasParser;
	LuaTable*  explTblRoot;

	// number of times a CEG instance was requested via LoadGenerator()
	// used to assign a unique ID to each CEG (zero is reserved for gCEG)
	unsigned int numLoadedGenerators;

	std::map<unsigned int, IExplosionGenerator*> explosionGenerators;
};




//! Base explosion generator class
class IExplosionGenerator
{
	CR_DECLARE(IExplosionGenerator);

public:
	IExplosionGenerator(): generatorID(0) {}
	virtual ~IExplosionGenerator() {}

	virtual unsigned int Load(CExplosionGeneratorHandler* handler, const std::string& tag) = 0;
	virtual void Reload(CExplosionGeneratorHandler* handler, const std::string& tag) {}
	virtual void Unload(CExplosionGeneratorHandler* handler) {}
	virtual bool Explosion(unsigned int explosionID, const float3& pos, float damage, float radius, CUnit* owner, float gfxMod, CUnit* hit, const float3& dir) = 0;

	unsigned int GetGeneratorID() const { return generatorID; }
	void SetGeneratorID(unsigned int id) { generatorID = id; }

protected:
	unsigned int generatorID;
};


//! everything is calculated from damage and radius
class CStdExplosionGenerator: public IExplosionGenerator
{
	CR_DECLARE(CStdExplosionGenerator);

public:
	CStdExplosionGenerator(): IExplosionGenerator() {}
	virtual ~CStdExplosionGenerator() {}

	unsigned int Load(CExplosionGeneratorHandler* handler, const std::string& tag) { return -1U; }
	bool Explosion(unsigned int explosionID, const float3& pos, float damage, float radius, CUnit* owner, float gfxMod, CUnit* hit, const float3& dir);
};


//! Uses explosion info from a script file; defines the
//! result of an explosion as a series of new projectiles
class CCustomExplosionGenerator: public CStdExplosionGenerator
{
	CR_DECLARE(CCustomExplosionGenerator);

protected:
	struct ProjectileSpawnInfo {
		ProjectileSpawnInfo()
			: projectileClass(NULL)
			, count(0)
			, flags(0)
		{}
		ProjectileSpawnInfo(const ProjectileSpawnInfo& psi)
			: projectileClass(psi.projectileClass)
			, code(psi.code)
			, count(psi.count)
			, flags(psi.flags)
		{}

		creg::Class* projectileClass;

		/// parsed explosion script code
		std::vector<char> code;

		/// number of projectiles spawned of this type
		int count;
		unsigned int flags;
	};

	// TODO: Handle ground flashes with more flexibility like the projectiles
	struct GroundFlashInfo {
		GroundFlashInfo()
			: flashSize(0.0f)
			, flashAlpha(0.0f)
			, circleGrowth(0.0f)
			, circleAlpha(0.0f)
			, ttl(0)
			, color(0.0f, 0.0f, 0.0f)
			, flags(0)
		{}

		float flashSize;
		float flashAlpha;
		float circleGrowth;
		float circleAlpha;
		int ttl;
		float3 color;
		unsigned int flags;
	};

	struct CEGData {
		std::vector<ProjectileSpawnInfo> projectileSpawn;
		GroundFlashInfo groundFlash;
		bool useDefaultExplosions;
	};

	//! maps cegTags to explosion handles
	std::map<std::string, unsigned int> explosionIDs;
	//! indexed by explosion handles
	std::vector<CEGData> explosionData;

	/**
	 * Explosion generators used by explosionData.projectileSpawn.
	 * We only need this for unloading them later.
	 * @see #ClearCache
	 */
	std::vector<IExplosionGenerator*> spawnExplGens;

	void ParseExplosionCode(ProjectileSpawnInfo* psi, const int offset, const boost::shared_ptr<creg::IType> type, const std::string& script, std::string& code);
	void ExecuteExplosionCode(const char* code, float damage, char* instance, int spawnIndex, const float3& dir, bool synced);

public:
	CCustomExplosionGenerator(): CStdExplosionGenerator() {}
	~CCustomExplosionGenerator() { ClearCache(); }

	static void OutputProjectileClassInfo();

	/// @throws content_error/runtime_error on errors
	unsigned int Load(CExplosionGeneratorHandler* handler, const std::string& tag);
	void Reload(CExplosionGeneratorHandler* handler, const std::string& tag);
	void Unload(CExplosionGeneratorHandler* handler);
	bool Explosion(unsigned int explosionID, const float3& pos, float damage, float radius, CUnit* owner, float gfxMod, CUnit* hit, const float3& dir);

	void ClearCache();

	enum {
		SPW_WATER      =  1,
		SPW_GROUND     =  2,
		SPW_AIR        =  4,
		SPW_UNDERWATER =  8,
		SPW_UNIT       = 16,  // only execute when the explosion hits a unit
		SPW_NO_UNIT    = 32,  // only execute when the explosion doesn't hit a unit (environment)
		SPW_SYNCED     = 64,  // spawn this projectile even if particleSaturation > 1
	};

	enum {
		OP_END      =  0,
		OP_STOREI   =  1, // int
		OP_STOREF   =  2, // float
		OP_STOREC   =  3, // char
		OP_ADD      =  4,
		OP_RAND     =  5,
		OP_DAMAGE   =  6,
		OP_INDEX    =  7,
		OP_LOADP    =  8, // load a void* into the pointer register
		OP_STOREP   =  9, // store the pointer register into a void*
		OP_DIR      = 10, // store the float3 direction
		OP_SAWTOOTH = 11, // Performs a modulo to create a sawtooth wave
		OP_DISCRETE = 12, // Floors the value to a multiple of its parameter
		OP_SINE     = 13, // Uses val as the phase of a sine wave
		OP_YANK     = 14, // Moves the input value into a buffer, returns zero
		OP_MULTIPLY = 15, // Multiplies with buffer value
		OP_ADDBUFF  = 16, // Adds buffer value
		OP_POW      = 17, // Power with code as exponent
		OP_POWBUFF  = 18, // Power with buffer as exponent
	};
};


extern CExplosionGeneratorHandler* explGenHandler;
extern CCustomExplosionGenerator* gCEG;

#endif // EXPLOSION_GENERATOR_H