~ubuntu-branches/ubuntu/oneiric/blobandconquer/oneiric

« back to all changes in this revision

Viewing changes to src/cplusplus/CEntityManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-15 12:04:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080615120429-5ss7cbb4z9mpywj5
Tags: 0.95-1
New upstream release. Closes: #486310

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2006 Parallel Realities
 
3
 
 
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.
 
8
 
 
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.
 
12
 
 
13
See the GNU General Public License for more details.
 
14
 
 
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.
 
18
 
 
19
*/
 
20
 
 
21
#include "../headers.h"
 
22
 
 
23
EntityManager::EntityManager()
 
24
{
 
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");
 
39
 
 
40
        uniqueId = 0;
 
41
}
 
42
 
 
43
EntityManager *EntityManager::getInstance()
 
44
{
 
45
        return &instance;
 
46
}
 
47
 
 
48
EntityManager::~EntityManager()
 
49
{
 
50
}
 
51
 
 
52
void EntityManager::destroy()
 
53
{
 
54
        clear();
 
55
}
 
56
 
 
57
Unit *EntityManager::spawnBlob(int type)
 
58
{
 
59
        uniqueId++;
 
60
        
 
61
        BlobDef *def = &blobDef[type];
 
62
 
 
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;
 
69
        
 
70
        unit->boundingBox.mins = def->bbMins;
 
71
        unit->boundingBox.maxs = def->bbMaxs;
 
72
        unit->updateBoundingBox();
 
73
        
 
74
        unit->currentWeapon = &weaponDef[def->weapon];
 
75
        
 
76
        unit->gunPosition[0] = def->gunPosition[0];
 
77
        unit->gunPosition[1] = def->gunPosition[1];
 
78
        
 
79
        blobList.add(unit);
 
80
 
 
81
        return unit;
 
82
}
 
83
 
 
84
Unit *EntityManager::spawnEnemy(int type, int enemyLevel)
 
85
{
 
86
        uniqueId++;
 
87
        
 
88
        int healthLevel = enemyLevel;
 
89
        float healthModifier = 1;
 
90
        
 
91
        Math::limit(&healthLevel, 1, 3);
 
92
        
 
93
        if (type != NME_AQUA_BLOB)
 
94
        {
 
95
                healthModifier += (0.35 * (healthLevel - 1));
 
96
        }
 
97
        
 
98
        EnemyDef *def = &enemyDef[type];
 
99
 
 
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;
 
106
        
 
107
        if (enemyLevel > 3)
 
108
        {
 
109
                int shieldChance = (enemyLevel == 4) ? 7 : 5;
 
110
                
 
111
                if ((rand() % shieldChance) == 0)
 
112
                {
 
113
                        unit->shield = unit->maxShield = Math::rrand(5, 10);
 
114
                }
 
115
        }
 
116
        
 
117
        unit->boundingBox.mins = def->bbMins;
 
118
        unit->boundingBox.maxs = def->bbMaxs;
 
119
        unit->updateBoundingBox();
 
120
        
 
121
        unit->currentWeapon = &weaponDef[def->weapon];
 
122
        
 
123
        unit->gunPosition[0] = def->gunPosition[0];
 
124
        unit->gunPosition[1] = def->gunPosition[1];
 
125
        
 
126
        enemyList.add(unit);
 
127
        
 
128
        return unit;
 
129
}
 
130
 
 
131
Item *EntityManager::spawnItem(int type)
 
132
{
 
133
        uniqueId++;
 
134
        
 
135
        ItemDef *def = &itemDef[type];
 
136
 
 
137
        Item *item = new Item(uniqueId);
 
138
        item->definition = def;
 
139
        item->itemPower = def->itemPower;
 
140
        item->health = 9999;
 
141
        
 
142
        item->boundingBox.mins = def->bbMins;
 
143
        item->boundingBox.maxs = def->bbMaxs;
 
144
        item->updateBoundingBox();
 
145
        
 
146
        item->flags |= EF_BOUNCES;
 
147
        
 
148
        itemList.add(item);
 
149
        
 
150
        return item;
 
151
}
 
152
 
 
153
Weapon *EntityManager::spawnWeapon(int type, bool fullClip)
 
154
{
 
155
        uniqueId++;
 
156
        
 
157
        WeaponDef *def = &weaponDef[type];
 
158
 
 
159
        Weapon *weapon = new Weapon(uniqueId);
 
160
        weapon->health = 9999;
 
161
        weapon->definition = def;
 
162
        
 
163
        weapon->boundingBox.mins = def->bbMins;
 
164
        weapon->boundingBox.maxs = def->bbMaxs;
 
165
        weapon->updateBoundingBox();
 
166
        
 
167
        if (fullClip)
 
168
        {
 
169
                weapon->currentAmmo = ((WeaponDef*)weapon->definition)->clipSize;
 
170
        }
 
171
        
 
172
        weaponList.add(weapon);
 
173
 
 
174
        return weapon;
 
175
}
 
176
 
 
177
Decoration *EntityManager::spawnDecoration(int type)
 
178
{
 
179
        Decoration *decoration = new Decoration();
 
180
        decorationList.add(decoration);
 
181
        
 
182
        decoration->type = type;
 
183
 
 
184
        return decoration;
 
185
}
 
186
 
 
187
Bullet *EntityManager::spawnBullet(int type)
 
188
{
 
189
        Bullet *bullet = new Bullet();
 
190
        bulletList.add(bullet);
 
191
        
 
192
        bullet->definition = &weaponDef[type];
 
193
        bullet->flags = bullet->definition->flags;
 
194
        bullet->scale = ((WeaponDef*)bullet->definition)->scale;
 
195
        
 
196
        return bullet;
 
197
}
 
198
 
 
199
Feature *EntityManager::spawnFeature(int featureType)
 
200
{
 
201
        Feature *feature = new Feature();
 
202
        featureList.add(feature);
 
203
        
 
204
        feature->definition = &featureDef[featureType];
 
205
        feature->featureType = featureType;
 
206
        feature->health = 999;
 
207
        feature->thinkTime = 0;
 
208
        
 
209
        return feature;
 
210
}
 
211
 
 
212
Door *EntityManager::spawnDoor()
 
213
{
 
214
        Door *door = new Door();
 
215
        structureList.add(door);
 
216
        
 
217
        return door;
 
218
}
 
219
 
 
220
Switch *EntityManager::spawnSwitch()
 
221
{
 
222
        uniqueId++;
 
223
 
 
224
        Switch *swt = new Switch(uniqueId);
 
225
        structureList.add(swt);
 
226
        
 
227
        return swt;
 
228
}
 
229
 
 
230
Structure *EntityManager::spawnStructure(int type)
 
231
{
 
232
        Structure *structure = new Structure();
 
233
        structure->entityType = type;
 
234
        structureList.add(structure);
 
235
        
 
236
        return structure;
 
237
}
 
238
 
 
239
Emitter *EntityManager::spawnEmitter(int type)
 
240
{
 
241
        Emitter *emitter = new Emitter();
 
242
        emitter->type = type;
 
243
        emitterList.add(emitter);
 
244
        
 
245
        return emitter;
 
246
}
 
247
 
 
248
Trap *EntityManager::spawnTrap(int type)
 
249
{
 
250
        Trap *trap = new Trap();
 
251
        trap->trapType = type;
 
252
        trapList.add(trap);
 
253
        
 
254
        return trap;
 
255
}
 
256
 
 
257
Decal *EntityManager::spawnDecal()
 
258
{
 
259
        Decal *decal = new Decal();
 
260
        decalList.add(decal);
 
261
        
 
262
        return decal;
 
263
}
 
264
 
 
265
Boss *EntityManager::spawnBoss()
 
266
{
 
267
        Boss *boss = new Boss();
 
268
        bossList.add(boss);
 
269
        
 
270
        return boss;
 
271
}
 
272
 
 
273
Q3Target *EntityManager::spawnQ3Target(const char *name)
 
274
{
 
275
        Q3Target *target = new Q3Target();
 
276
        Q3TargetTable.put(name, target);
 
277
        
 
278
        return target;
 
279
}
 
280
 
 
281
CutsceneCamera *EntityManager::spawnCutsceneCamera(const char *name)
 
282
{
 
283
        CutsceneCamera *camera = new CutsceneCamera();
 
284
        cameraList.add(camera);
 
285
        
 
286
        return camera;
 
287
}
 
288
 
 
289
void EntityManager::addEntityToDrawList(Entity *entity)
 
290
{
 
291
        Reference *ref = new Reference();
 
292
        ref->object = entity;
 
293
        
 
294
        drawList.add(ref);
 
295
}
 
296
 
 
297
Entity *EntityManager::getOwnerById(unsigned int uniqueId)
 
298
{
 
299
        for (Unit *unit = (Unit*)enemyList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
 
300
        {
 
301
                if (unit->getUniqueId() == uniqueId)
 
302
                {
 
303
                        return unit;
 
304
                }
 
305
        }
 
306
        
 
307
        for (Unit *unit = (Unit*)blobList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
 
308
        {
 
309
                if (unit->getUniqueId() == uniqueId)
 
310
                {
 
311
                        return unit;
 
312
                }
 
313
        }
 
314
        
 
315
        for (Structure *structure = (Structure*)structureList.getFirstElement() ; structure != NULL ; structure = (Structure*)structure->next)
 
316
        {
 
317
                if (structure->getUniqueId() == uniqueId)
 
318
                {
 
319
                        return structure;
 
320
                }
 
321
        }
 
322
        
 
323
        return NULL;
 
324
}
 
325
 
 
326
Entity *EntityManager::getOwnerByName(const char *name)
 
327
{
 
328
        for (Unit *unit = (Unit*)enemyList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
 
329
        {
 
330
                if (unit->name == name)
 
331
                {
 
332
                        return unit;
 
333
                }
 
334
        }
 
335
        
 
336
        for (Unit *unit = (Unit*)blobList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
 
337
        {
 
338
                if (unit->name == name)
 
339
                {
 
340
                        return unit;
 
341
                }
 
342
        }
 
343
        
 
344
        for (Structure *structure = (Structure*)structureList.getFirstElement() ; structure != NULL ; structure = (Structure*)structure->next)
 
345
        {
 
346
                if (structure->name == name)
 
347
                {
 
348
                        return structure;
 
349
                }
 
350
        }
 
351
        
 
352
        return NULL;
 
353
}
 
354
 
 
355
Item *EntityManager::getItemById(unsigned int uniqueId)
 
356
{
 
357
        for (Item *item = (Item*)itemList.getFirstElement() ; item != NULL ; item = (Item*)item->next)
 
358
        {
 
359
                if (item->getUniqueId() == uniqueId)
 
360
                {
 
361
                        return item;
 
362
                }
 
363
        }
 
364
        
 
365
        return NULL;
 
366
}
 
367
 
 
368
Weapon *EntityManager::getWeaponById(unsigned int uniqueId)
 
369
{
 
370
        for (Weapon *weapon = (Weapon*)weaponList.getFirstElement() ; weapon != NULL ; weapon = (Weapon*)weapon->next)
 
371
        {
 
372
                if (weapon->getUniqueId() == uniqueId)
 
373
                {
 
374
                        return weapon;
 
375
                }
 
376
        }
 
377
        
 
378
        return NULL;
 
379
}
 
380
 
 
381
int EntityManager::getEntityType(Properties *props)
 
382
{
 
383
        if (props->hasProperty("definitionType"))
 
384
        {
 
385
                return props->getInt("definitionType", -1);
 
386
        }
 
387
        
 
388
        if ((props->name == "ITM_CUSTOM") && (!props->hasProperty("definitionName")))
 
389
        {
 
390
                printf("ERROR - Cannot add custom item without a valid definitionName property!\n");
 
391
                exit(1);
 
392
        }
 
393
        
 
394
        // looks like a custom item... Try and find it...
 
395
        if ((props->hasProperty("definitionName")) && ((props->name == "ITM_CUSTOM") || (props->name == "Item")))
 
396
        {
 
397
                const char *name = props->getString("definitionName", "NULL");
 
398
                
 
399
                for (int i = ITM_CUSTOM_FIRST ; i < ITM_CUSTOM_LAST ; i++)
 
400
                {
 
401
                        if (itemDef[i].name == name)
 
402
                        {
 
403
                                return i;
 
404
                        }
 
405
                }
 
406
                
 
407
                printf("WARNING - Custom item specifies a custom definition of '%s' that was not found!\n", name);
 
408
                return -1;
 
409
        }
 
410
        
 
411
        return Engine::getInstance()->getValueOfDefine(props->getString("classname", "classname"));
 
412
}
 
413
 
 
414
Entity *EntityManager::getAnyEntityByName(const char *name)
 
415
{
 
416
        Entity *ent = getOwnerByName(name);
 
417
                        
 
418
        if (ent != NULL)
 
419
        {
 
420
                return ent;
 
421
        }
 
422
        
 
423
        for (ent = (Entity*)bossList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
424
        {
 
425
                if (ent->name == name)
 
426
                {
 
427
                        return ent;
 
428
                }
 
429
        }
 
430
        
 
431
        for (ent = (Entity*)itemList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
432
        {
 
433
                if (ent->name == name)
 
434
                {
 
435
                        return ent;
 
436
                }
 
437
        }
 
438
        
 
439
        for (ent = (Entity*)weaponList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
440
        {
 
441
                if (ent->name == name)
 
442
                {
 
443
                        return ent;
 
444
                }
 
445
        }
 
446
        
 
447
        for (ent = (Entity*)cameraList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
448
        {
 
449
                if (ent->name == name)
 
450
                {
 
451
                        return ent;
 
452
                }
 
453
        }
 
454
        
 
455
        return NULL;
 
456
}
 
457
 
 
458
Vector EntityManager::getEntityPosition(const char *name)
 
459
{
 
460
        Q3Target *target = (Q3Target*)Q3TargetTable.get(name);
 
461
        
 
462
        if (target != NULL)
 
463
        {
 
464
                return target->origin;
 
465
        }
 
466
        
 
467
        return getAnyEntityByName(name)->position;
 
468
}
 
469
 
 
470
float EntityManager::getEntityStrength(Entity *entity)
 
471
{
 
472
        switch (entity->entityType)
 
473
        {
 
474
                case ET_BLOB:
 
475
                        return (entity->definition->type == BLOB_BOB) ? 0.35 : 0;
 
476
                        break;
 
477
                        
 
478
                case ET_BIOMECH:
 
479
                        return 0.1;
 
480
                        break;
 
481
                        
 
482
                default:
 
483
                        return 1.0;
 
484
                        break;
 
485
        }
 
486
        
 
487
        // shouldn't get here...
 
488
        return 0.0;
 
489
}
 
490
 
 
491
void EntityManager::clear()
 
492
{
 
493
        blobList.clear();
 
494
        enemyList.clear();
 
495
        itemList.clear();
 
496
        weaponList.clear();
 
497
        decorationList.clear();
 
498
        featureList.clear();
 
499
        bulletList.clear();
 
500
        structureList.clear();
 
501
        cameraList.clear();
 
502
        Q3TargetTable.clear();
 
503
        bossList.clear();
 
504
        
 
505
        uniqueId = 0;
 
506
}
 
507
 
 
508
void EntityManager::load(Properties *props)
 
509
{
 
510
        uniqueId = props->getInt("uniqueId", 0);
 
511
}
 
512
 
 
513
void EntityManager::save(FILE *fp)
 
514
{
 
515
        debug(("EntityManager::save()\n"));
 
516
        
 
517
        Unit *unit;
 
518
        Entity *ent;
 
519
        Boss *boss;
 
520
        
 
521
        debug(("EntityManager::save() - Blobs\n"));
 
522
        for (ent = (Entity*)blobList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
523
        {
 
524
                ent->save(fp);
 
525
        }
 
526
        
 
527
        debug(("EntityManager::save() - Enemies\n"));
 
528
        for (unit = (Unit*)enemyList.getFirstElement() ; unit != NULL ; unit = (Unit*)unit->next)
 
529
        {
 
530
                unit->save(fp);
 
531
        }
 
532
        
 
533
        debug(("EntityManager::save() - Bosses\n"));
 
534
        for (boss = (Boss*)bossList.getFirstElement() ; boss != NULL ; boss = (Boss*)boss->next)
 
535
        {
 
536
                boss->save(fp);
 
537
        }
 
538
        
 
539
        debug(("EntityManager::save() - Items\n"));
 
540
        for (ent = (Entity*)itemList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
541
        {
 
542
                ent->save(fp);
 
543
        }
 
544
        
 
545
        debug(("EntityManager::save() - Weapons\n"));
 
546
        for (ent = (Entity*)weaponList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
547
        {
 
548
                ent->save(fp);
 
549
        }
 
550
        
 
551
        debug(("EntityManager::save() - Bullets\n"));
 
552
        for (ent = (Entity*)bulletList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
553
        {
 
554
                ent->save(fp);
 
555
        }
 
556
        
 
557
        debug(("EntityManager::save() - Features\n"));
 
558
        for (ent = (Entity*)featureList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
559
        {
 
560
                ent->save(fp);
 
561
        }
 
562
        
 
563
        debug(("EntityManager::save() - Decoration\n"));
 
564
        for (ent = (Entity*)decorationList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
565
        {
 
566
                ent->save(fp);
 
567
        }
 
568
        
 
569
        debug(("EntityManager::save() - Structures\n"));
 
570
        for (ent = (Entity*)structureList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
571
        {
 
572
                ent->save(fp);
 
573
        }
 
574
        
 
575
        debug(("EntityManager::save() - Cameras\n"));
 
576
        for (ent = (Entity*)cameraList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
577
        {
 
578
                ent->save(fp);
 
579
        }
 
580
        
 
581
        debug(("EntityManager::save() - Emitters\n"));
 
582
        for (ent = (Entity*)emitterList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
583
        {
 
584
                ent->save(fp);
 
585
        }
 
586
        
 
587
        debug(("EntityManager::save() - Traps\n"));
 
588
        for (ent = (Entity*)trapList.getFirstElement() ; ent != NULL ; ent = (Entity*)ent->next)
 
589
        {
 
590
                ent->save(fp);
 
591
        }
 
592
        
 
593
        debug(("EntityManager::save() - Decals\n"));
 
594
        for (Decal *decal = (Decal*)decalList.getFirstElement() ; decal != NULL ; decal = (Decal*)decal->next)
 
595
        {
 
596
                decal->save(fp);
 
597
        }
 
598
        
 
599
        debug(("EntityManager::save() - Q3TargetTable\n"));
 
600
        List *q3TargetList = Q3TargetTable.toList();
 
601
        Q3Target *target;
 
602
        for (Reference *ref = (Reference*)q3TargetList->getFirstElement() ; ref != NULL ; ref = (Reference*)ref->next)
 
603
        {
 
604
                target = (Q3Target*)ref->object;
 
605
                target->save(fp);
 
606
        }
 
607
        
 
608
        // Do this last to keep the Entity Manager in sync upon a reload!
 
609
        Properties props;
 
610
        props.setName("EntityManager");
 
611
        props.setProperty("uniqueId", uniqueId);
 
612
        props.save(fp);
 
613
        
 
614
        debug(("EntityManager::save() - Done\n"));
 
615
}
 
616
 
 
617
void EntityManager::saveCustomItems(FILE *fp)
 
618
{
 
619
        debug(("EntityManager::saveCustomItems()\n"));
 
620
        
 
621
        Properties props;
 
622
        props.setName("CustomItem");
 
623
        
 
624
        for (int i = ITM_CUSTOM_FIRST ; i < ITM_CUSTOM_LAST ; i++)
 
625
        {
 
626
                if (itemDef[i].name == "")
 
627
                {
 
628
                        continue;
 
629
                }
 
630
                
 
631
                props.clear();
 
632
                
 
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);
 
642
                
 
643
                props.save(fp);
 
644
        }
 
645
}
 
646
 
 
647
EntityManager EntityManager::instance;