2
Copyright (C) 2006 Parallel Realities
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
See the GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include "../headers.h"
23
EntityManager::EntityManager()
25
blobList.setName("BlobList");
26
enemyList.setName("EnemyList");
27
itemList.setName("ItemList");
28
weaponList.setName("WeaponList");
29
decorationList.setName("DecorationList");
30
bulletList.setName("BulletList");
31
featureList.setName("FeatureList");
32
structureList.setName("StructureList");
33
cameraList.setName("CameraTable");
34
Q3TargetTable.name = "TargetTable";
35
emitterList.setName("EmitterList");
36
trapList.setName("TrapList");
37
decalList.setName("DecalList");
38
bossList.setName("BossList");
43
EntityManager *EntityManager::getInstance()
48
EntityManager::~EntityManager()
52
void EntityManager::destroy()
57
Unit *EntityManager::spawnBlob(int type)
61
BlobDef *def = &blobDef[type];
63
Unit *unit = new Unit(uniqueId);
64
unit->entityType = ET_BLOB;
65
unit->definition = def;
66
unit->health = unit->maxHealth = def->hp;
67
unit->shield = unit->maxShield = def->shield;
68
unit->flags = def->flags;
70
unit->boundingBox.mins = def->bbMins;
71
unit->boundingBox.maxs = def->bbMaxs;
72
unit->updateBoundingBox();
74
unit->currentWeapon = &weaponDef[def->weapon];
76
unit->gunPosition[0] = def->gunPosition[0];
77
unit->gunPosition[1] = def->gunPosition[1];
84
Unit *EntityManager::spawnEnemy(int type, int enemyLevel)
88
int healthLevel = enemyLevel;
89
float healthModifier = 1;
91
Math::limit(&healthLevel, 1, 3);
93
if (type != NME_AQUA_BLOB)
95
healthModifier += (0.35 * (healthLevel - 1));
98
EnemyDef *def = &enemyDef[type];
100
Unit *unit = new Unit(uniqueId);
101
unit->entityType = ET_BIOMECH;
102
unit->definition = def;
103
unit->health = unit->maxHealth = Math::rrand((int)(def->minHP * healthModifier), (int)(def->maxHP * healthModifier));
104
unit->shield = unit->maxShield = def->shield;
105
unit->flags = def->flags;
109
int shieldChance = (enemyLevel == 4) ? 7 : 5;
111
if ((rand() % shieldChance) == 0)
113
unit->shield = unit->maxShield = Math::rrand(5, 10);
117
unit->boundingBox.mins = def->bbMins;
118
unit->boundingBox.maxs = def->bbMaxs;
119
unit->updateBoundingBox();
121
unit->currentWeapon = &weaponDef[def->weapon];
123
unit->gunPosition[0] = def->gunPosition[0];
124
unit->gunPosition[1] = def->gunPosition[1];
131
Item *EntityManager::spawnItem(int type)
135
ItemDef *def = &itemDef[type];
137
Item *item = new Item(uniqueId);
138
item->definition = def;
139
item->itemPower = def->itemPower;
142
item->boundingBox.mins = def->bbMins;
143
item->boundingBox.maxs = def->bbMaxs;
144
item->updateBoundingBox();
146
item->flags |= EF_BOUNCES;
153
Weapon *EntityManager::spawnWeapon(int type, bool fullClip)
157
WeaponDef *def = &weaponDef[type];
159
Weapon *weapon = new Weapon(uniqueId);
160
weapon->health = 9999;
161
weapon->definition = def;
163
weapon->boundingBox.mins = def->bbMins;
164
weapon->boundingBox.maxs = def->bbMaxs;
165
weapon->updateBoundingBox();
169
weapon->currentAmmo = ((WeaponDef*)weapon->definition)->clipSize;
172
weaponList.add(weapon);
177
Decoration *EntityManager::spawnDecoration(int type)
179
Decoration *decoration = new Decoration();
180
decorationList.add(decoration);
182
decoration->type = type;
187
Bullet *EntityManager::spawnBullet(int type)
189
Bullet *bullet = new Bullet();
190
bulletList.add(bullet);
192
bullet->definition = &weaponDef[type];
193
bullet->flags = bullet->definition->flags;
194
bullet->scale = ((WeaponDef*)bullet->definition)->scale;
199
Feature *EntityManager::spawnFeature(int featureType)
201
Feature *feature = new Feature();
202
featureList.add(feature);
204
feature->definition = &featureDef[featureType];
205
feature->featureType = featureType;
206
feature->health = 999;
207
feature->thinkTime = 0;
212
Door *EntityManager::spawnDoor()
214
Door *door = new Door();
215
structureList.add(door);
220
Switch *EntityManager::spawnSwitch()
224
Switch *swt = new Switch(uniqueId);
225
structureList.add(swt);
230
Structure *EntityManager::spawnStructure(int type)
232
Structure *structure = new Structure();
233
structure->entityType = type;
234
structureList.add(structure);
239
Emitter *EntityManager::spawnEmitter(int type)
241
Emitter *emitter = new Emitter();
242
emitter->type = type;
243
emitterList.add(emitter);
248
Trap *EntityManager::spawnTrap(int type)
250
Trap *trap = new Trap();
251
trap->trapType = type;
257
Decal *EntityManager::spawnDecal()
259
Decal *decal = new Decal();
260
decalList.add(decal);
265
Boss *EntityManager::spawnBoss()
267
Boss *boss = new Boss();
273
Q3Target *EntityManager::spawnQ3Target(const char *name)
275
Q3Target *target = new Q3Target();
276
Q3TargetTable.put(name, target);
281
CutsceneCamera *EntityManager::spawnCutsceneCamera(const char *name)
283
CutsceneCamera *camera = new CutsceneCamera();
284
cameraList.add(camera);
289
void EntityManager::addEntityToDrawList(Entity *entity)
291
Reference *ref = new Reference();
292
ref->object = entity;
297
Entity *EntityManager::getOwnerById(unsigned int uniqueId)
299
for (Unit *unit = (Unit*)enemyList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
301
if (unit->getUniqueId() == uniqueId)
307
for (Unit *unit = (Unit*)blobList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
309
if (unit->getUniqueId() == uniqueId)
315
for (Structure *structure = (Structure*)structureList.getFirstElement() ; structure != NULL ; structure = (Structure*)structure->next)
317
if (structure->getUniqueId() == uniqueId)
326
Entity *EntityManager::getOwnerByName(const char *name)
328
for (Unit *unit = (Unit*)enemyList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
330
if (unit->name == name)
336
for (Unit *unit = (Unit*)blobList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
338
if (unit->name == name)
344
for (Structure *structure = (Structure*)structureList.getFirstElement() ; structure != NULL ; structure = (Structure*)structure->next)
346
if (structure->name == name)
355
Item *EntityManager::getItemById(unsigned int uniqueId)
357
for (Item *item = (Item*)itemList.getFirstElement() ; item != NULL ; item = (Item*)item->next)
359
if (item->getUniqueId() == uniqueId)
368
Weapon *EntityManager::getWeaponById(unsigned int uniqueId)
370
for (Weapon *weapon = (Weapon*)weaponList.getFirstElement() ; weapon != NULL ; weapon = (Weapon*)weapon->next)
372
if (weapon->getUniqueId() == uniqueId)
381
int EntityManager::getEntityType(Properties *props)
383
if (props->hasProperty("definitionType"))
385
return props->getInt("definitionType", -1);
388
if ((props->name == "ITM_CUSTOM") && (!props->hasProperty("definitionName")))
390
printf("ERROR - Cannot add custom item without a valid definitionName property!\n");
394
// looks like a custom item... Try and find it...
395
if ((props->hasProperty("definitionName")) && ((props->name == "ITM_CUSTOM") || (props->name == "Item")))
397
const char *name = props->getString("definitionName", "NULL");
399
for (int i = ITM_CUSTOM_FIRST ; i < ITM_CUSTOM_LAST ; i++)
401
if (itemDef[i].name == name)
407
printf("WARNING - Custom item specifies a custom definition of '%s' that was not found!\n", name);
411
return Engine::getInstance()->getValueOfDefine(props->getString("classname", "classname"));
414
Entity *EntityManager::getAnyEntityByName(const char *name)
416
Entity *ent = getOwnerByName(name);
423
for (ent = (Entity*)bossList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
425
if (ent->name == name)
431
for (ent = (Entity*)itemList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
433
if (ent->name == name)
439
for (ent = (Entity*)weaponList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
441
if (ent->name == name)
447
for (ent = (Entity*)cameraList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
449
if (ent->name == name)
458
Vector EntityManager::getEntityPosition(const char *name)
460
Q3Target *target = (Q3Target*)Q3TargetTable.get(name);
464
return target->origin;
467
return getAnyEntityByName(name)->position;
470
float EntityManager::getEntityStrength(Entity *entity)
472
switch (entity->entityType)
475
return (entity->definition->type == BLOB_BOB) ? 0.35 : 0;
487
// shouldn't get here...
491
void EntityManager::clear()
497
decorationList.clear();
500
structureList.clear();
502
Q3TargetTable.clear();
508
void EntityManager::load(Properties *props)
510
uniqueId = props->getInt("uniqueId", 0);
513
void EntityManager::save(FILE *fp)
515
debug(("EntityManager::save()\n"));
521
debug(("EntityManager::save() - Blobs\n"));
522
for (ent = (Entity*)blobList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
527
debug(("EntityManager::save() - Enemies\n"));
528
for (unit = (Unit*)enemyList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
533
debug(("EntityManager::save() - Bosses\n"));
534
for (boss = (Boss*)bossList.getFirstElement() ; boss != NULL ; boss = (Boss*)boss->next)
539
debug(("EntityManager::save() - Items\n"));
540
for (ent = (Entity*)itemList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
545
debug(("EntityManager::save() - Weapons\n"));
546
for (ent = (Entity*)weaponList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
551
debug(("EntityManager::save() - Bullets\n"));
552
for (ent = (Entity*)bulletList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
557
debug(("EntityManager::save() - Features\n"));
558
for (ent = (Entity*)featureList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
563
debug(("EntityManager::save() - Decoration\n"));
564
for (ent = (Entity*)decorationList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
569
debug(("EntityManager::save() - Structures\n"));
570
for (ent = (Entity*)structureList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
575
debug(("EntityManager::save() - Cameras\n"));
576
for (ent = (Entity*)cameraList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
581
debug(("EntityManager::save() - Emitters\n"));
582
for (ent = (Entity*)emitterList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
587
debug(("EntityManager::save() - Traps\n"));
588
for (ent = (Entity*)trapList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
593
debug(("EntityManager::save() - Decals\n"));
594
for (Decal *decal = (Decal*)decalList.getFirstElement() ; decal != NULL ; decal = (Decal*)decal->next)
599
debug(("EntityManager::save() - Q3TargetTable\n"));
600
List *q3TargetList = Q3TargetTable.toList();
602
for (Reference *ref = (Reference*)q3TargetList->getFirstElement() ; ref != NULL ; ref = (Reference*)ref->next)
604
target = (Q3Target*)ref->object;
608
// Do this last to keep the Entity Manager in sync upon a reload!
610
props.setName("EntityManager");
611
props.setProperty("uniqueId", uniqueId);
614
debug(("EntityManager::save() - Done\n"));
617
void EntityManager::saveCustomItems(FILE *fp)
619
debug(("EntityManager::saveCustomItems()\n"));
622
props.setName("CustomItem");
624
for (int i = ITM_CUSTOM_FIRST ; i < ITM_CUSTOM_LAST ; i++)
626
if (itemDef[i].name == "")
633
props.setProperty("name", itemDef[i].name.getText());
634
props.setProperty("model1", itemDef[i].modelName[0].getText());
635
props.setProperty("model2", itemDef[i].modelName[1].getText());
636
props.setProperty("texture1", itemDef[i].textureName[0].getText());
637
props.setProperty("texture2", itemDef[i].textureName[1].getText());
638
props.setProperty("bbMins", itemDef[i].bbMins);
639
props.setProperty("bbMaxs", itemDef[i].bbMaxs);
640
props.setProperty("customCarriable", itemDef[i].customCarriable);
641
props.setProperty("collatable", itemDef[i].collatable);
647
EntityManager EntityManager::instance;