File: spawners.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (449 lines) | stat: -rw-r--r-- 12,304 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
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.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// spawners.cpp: Various spawning entities

/*****************************************************************************/
/*QUAKED func_spawn(0 0.25 0.5) (-8 -8 -8) (8 8 8)
"modelname" The name of the TIKI file you wish to spawn. (Required)
"spawntargetname" This will be the targetname of the spawned model. (default is null)
"spawntarget" This will be the target of the spawned model. (default is null)
"pickup_thread" passed on to the spawned model
"key"       The item needed to activate this. (default nothing)
"attackmode" Attacking mode of the spawned actor (default 0)
******************************************************************************/

#include "spawners.h"
#include "game.h"
#include "g_phys.h"
#include "g_spawn.h"

Event EV_Spawn_ModelName
( 
    "modelname",
    EV_DEFAULT,
    "s",
    "model_name",
    "Sets the model name for this spawn entity.",
    EV_NORMAL
);
Event EV_Spawn_SpawnTargetName
( 
    "spawntargetname",
    EV_DEFAULT,
    "s",
    "spawntargetname",
    "Sets spawn target name for this spawn entity.",
    EV_NORMAL
);
Event EV_Spawn_SpawnTarget
( 
    "spawntarget",
    EV_DEFAULT,
    "s",
    "spawntarget",
    "Sets spawn target for this spawn entity.",
    EV_NORMAL
);
Event EV_Spawn_AttackMode
( 
    "attackmode",
    EV_DEFAULT,
    "i",
    "attackmode",
    "Sets the attackmode for this spawn entity.",
    EV_NORMAL
);

Event EV_Spawn_PickupThread
( 
    "pickup_thread",
    EV_DEFAULT,
    "s",
    "threadName",
    "Sets the pickup thread for the spawned entity.",
    EV_NORMAL
);

Event EV_Spawn_AddSpawnItem
( 
    "spawnitem",
    EV_DEFAULT,
    "s",
    "spawn_item_name",
    "Adds this named item to what will be spawned when this spawned entity is killed, if it is an actor.",
    EV_NORMAL
);
Event EV_Spawn_SetSpawnChance
( 
    "spawnchance",
    EV_DEFAULT,
    "f",
    "spawn_chance",
    "Sets the chance that this spawned entity will spawn something when killed, if it is an actor.",
    EV_NORMAL
);

CLASS_DECLARATION(ScriptSlave, Spawn, "func_spawn") {
    {&EV_Activate,              &Spawn::DoSpawn        },
    {&EV_Spawn_ModelName,       &Spawn::ModelName      },
    {&EV_Spawn_SpawnTargetName, &Spawn::SpawnTargetName},
    {&EV_Spawn_AttackMode,      &Spawn::AttackMode     },
    {&EV_Spawn_SpawnTarget,     &Spawn::SpawnTarget    },
    {&EV_Spawn_PickupThread,    &Spawn::SetPickupThread},
    {&EV_SetAngle,              &Spawn::SetAngleEvent  },
    {&EV_Spawn_AddSpawnItem,    &Spawn::SetSpawnItem   },
    {&EV_Spawn_SetSpawnChance,  &Spawn::SetSpawnChance },
    {NULL,                      NULL                   }
};

void Spawn::SetAngleEvent(Event *ev)
{
    SimpleEntity::SetAngleEvent(ev);
}

void Spawn::SetPickupThread(Event *ev)
{
    pickup_thread = ev->GetString(1);
}

void Spawn::ModelName(Event *ev)
{
    modelname = ev->GetString(1);
    CacheResource(modelname);
}

void Spawn::SpawnTargetName(Event *ev)
{
    spawntargetname = ev->GetString(1);
}

void Spawn::SpawnTarget(Event *ev)
{
    spawntarget = ev->GetString(1);
}

void Spawn::AttackMode(Event *ev)
{
    attackmode = ev->GetInteger(1);
}

void Spawn::SetSpawnItem(Event *ev)
{
    spawnitem = ev->GetString(1);
}

void Spawn::SetSpawnChance(Event *ev)
{
    spawnchance = ev->GetFloat(1);
}

Spawn::Spawn()
{
    setSolidType(SOLID_NOT);
    setMoveType(MOVETYPE_NONE);
    hideModel();

    spawnchance = 0;
    attackmode  = 0;
}

void Spawn::SetArgs(SpawnArgs& args)
{
    args.setArg("origin", va("%f %f %f", origin[0], origin[1], origin[2]));
    args.setArg("angle", va("%f", angles[1]));
    args.setArg("angles", va("%f %f %f", angles[0], angles[1], angles[2]));
    args.setArg("model", modelname.c_str());
    args.setArg("attackmode", va("%i", attackmode));
    args.setArg("scale", va("%f", edict->s.scale));
    if (spawntargetname.length()) {
        args.setArg("targetname", spawntargetname.c_str());
    }
    if (spawntarget.length()) {
        args.setArg("target", spawntarget.c_str());
    }
    if (pickup_thread.length()) {
        args.setArg("pickup_thread", pickup_thread.c_str());
    }
    if (spawnitem.length()) {
        args.setArg("spawnitem", spawnitem.c_str());
        args.setArg("spawnchance", va("%f", spawnchance));
    }
}

void Spawn::DoSpawn(Event *ev)
{
    Entity   *spawn;
    SpawnArgs args;

    if (!modelname.length()) {
        warning("Spawn", "modelname not set");
    }

    SetArgs(args);

    spawn = (Entity *)args.Spawn();

    if (spawn) {
        // make sure spawned entity starts falling if necessary
        spawn->velocity = "0 0 -1";

        Event *e = new Event(EV_SetAnim);
        e->AddString("idle");
        spawn->PostEvent(e, EV_SPAWNARG);
    }
}

/*****************************************************************************/
/*QUAKED func_randomspawn(0 0.25 0.5) (-8 -8 -8) (8 8 8) START_OFF
Randomly spawns an entity.  The time between spawns is determined by min_time and max_time
The entity can be turned off and on by triggering it
"modelname"   The name of the TIKI file you wish to spawn. (Required)
"key"         The item needed to activate this. (default nothing)
"min_time" The minimum time between spawns (default 0.2 seconds)
"max_time" The maximum time between spawns (default 1 seconds)
START_OFF - spawn is off by default
******************************************************************************/

Event EV_RandomSpawn_MinTime
(
    "min_time",
    EV_DEFAULT,
    "f",
    "minTime",
    "Minimum time between random spawns.", 
    EV_NORMAL
);

Event EV_RandomSpawn_MaxTime
(
    "max_time",
    EV_DEFAULT,
    "f",
    "maxTime",
    "Maximum time between random spawns.",
    EV_NORMAL
);

Event EV_RandomSpawn_Think
(
    "_randomspawn_think",
    EV_DEFAULT,
    NULL,
    NULL,
    "The function that actually spawns things in.",
    EV_NORMAL
);

CLASS_DECLARATION(Spawn, RandomSpawn, "func_randomspawn") {
    {&EV_Activate,            &RandomSpawn::ToggleSpawn},
    {&EV_RandomSpawn_MinTime, &RandomSpawn::MinTime    },
    {&EV_RandomSpawn_MaxTime, &RandomSpawn::MaxTime    },
    {&EV_RandomSpawn_Think,   &RandomSpawn::Think      },
    {NULL,                    NULL                     }
};

RandomSpawn::RandomSpawn()
{
    min_time = 0.2f;
    max_time = 1.0f;
    if (!LoadingSavegame && !(spawnflags & 1)) {
        PostEvent(EV_RandomSpawn_Think, min_time + (G_Random(max_time - min_time)));
    }
}

void RandomSpawn::MinTime(Event *ev)
{
    min_time = ev->GetFloat(1);
}

void RandomSpawn::MaxTime(Event *ev)
{
    max_time = ev->GetFloat(1);
}

void RandomSpawn::ToggleSpawn(Event *ev)
{
    if (EventPending(EV_RandomSpawn_Think)) {
        // if currently on, turn it off
        CancelEventsOfType(EV_RandomSpawn_Think);
    } else {
        Think(NULL);
    }
}

void RandomSpawn::Think(Event *ev)
{
    CancelEventsOfType(EV_RandomSpawn_Think);

    //
    // spawn our entity
    //
    DoSpawn(NULL);

    //
    // post the next time
    //
    PostEvent(EV_RandomSpawn_Think, min_time + (G_Random(max_time - min_time)));
}

/*****************************************************************************/
/*QUAKED func_respawn(0 0.25 0.5) (-8 -8 -8) (8 8 8)
When the thing that is spawned is killed, this func_respawn will get
triggered.
"modelname"   The name of the TIKI file you wish to spawn. (Required)
"key"         The item needed to activate this. (default nothing)
******************************************************************************/

CLASS_DECLARATION(Spawn, ReSpawn, "func_respawn") {
    {NULL, NULL}
};

void ReSpawn::DoSpawn(Event *ev)
{
    Entity   *spawn;
    SpawnArgs args;

    SetArgs(args);

    // This will trigger the func_respawn when the thing dies
    args.setArg("targetname", TargetName());
    args.setArg("target", TargetName());

    spawn = (Entity *)args.Spawn();
    if (spawn) {
        // make sure spawned entity starts falling if necessary
        spawn->velocity = "0 0 -1";
    }
}

/*****************************************************************************/
/*QUAKED func_spawnoutofsight(0 0.25 0.5) (-8 -8 -8) (8 8 8)
Will only spawn something out of sight of its targets.
"modelname"   The name of the TIKI file you wish to spawn. (Required)
"spawntargetname" This will be the targetname of the spawned model. (default is null)
"spawntarget" This will be the target of the spawned model. (default is null)
"key"         The item needed to activate this. (default nothing)
******************************************************************************/

CLASS_DECLARATION(Spawn, SpawnOutOfSight, "func_spawnoutofsight") {
    {NULL, NULL}
};

void SpawnOutOfSight::DoSpawn(Event *ev)
{
    int        i;
    Entity    *ent;
    gentity_t *ed;
    trace_t    trace;
    qboolean   seen = false;

    // Check to see if I can see any players before spawning
    for (i = 0; i < game.maxclients; i++) {
        ed = &g_entities[i];
        if (!ed->inuse || !ed->entity) {
            continue;
        }

        ent = ed->entity;
        if ((ent->health < 0) || (ent->flags & FL_NOTARGET)) {
            continue;
        }

        trace =
            G_Trace(origin, vec_zero, vec_zero, ent->centroid, this, MASK_OPAQUE, false, "SpawnOutOfSight::DoSpawn");
        if (trace.fraction == 1.0) {
            seen = true;
            break;
        }
    }

    if (seen) {
        return;
    }

    Spawn::DoSpawn(ev);
}

/*****************************************************************************/
/*QUAKED func_spawnchain(0 0.25 0.5) (-8 -8 -8) (8 8 8)
Tries to spawn something out of the sight of players.  If it fails, it will
trigger its targets.
"modelname"   The name of the TIKI file you wish to spawn. (Required)
"spawntargetname" This will be the targetname of the spawned model. (default is null)
"spawntarget" This will be the target of the spawned model. (default is null)
"key"         The item needed to activate this. (default nothing)
******************************************************************************/

CLASS_DECLARATION(Spawn, SpawnChain, "func_spawnchain") {
    {NULL, NULL}
};

void SpawnChain::DoSpawn(Event *ev)
{
    int         i;
    Entity     *ent;
    gentity_t  *ed;
    trace_t     trace;
    qboolean    seen = false;
    const char *name;
    Event      *event;

    // Check to see if this can see any players before spawning
    for (i = 0; i < game.maxclients; i++) {
        ed = &g_entities[i];
        if (!ed->inuse || !ed->entity) {
            continue;
        }

        ent = ed->entity;
        if ((ent->health < 0) || (ent->flags & FL_NOTARGET)) {
            continue;
        }

        trace = G_Trace(origin, vec_zero, vec_zero, ent->centroid, this, MASK_OPAQUE, false, "SpawnChain::DoSpawn");
        if (trace.fraction == 1.0) {
            seen = true;
            break;
        }
    }

    // Couldn't spawn anything, so activate targets
    if (seen) {
        name = Target();
        if (name && strcmp(name, "")) {
            ent = NULL;
            do {
                ent = (Entity *)G_FindTarget(ent, name);
                if (!ent) {
                    break;
                }
                event = new Event(EV_Activate);
                event->AddEntity(world);
                ent->PostEvent(event, 0);
            } while (1);
        }
        return;
    }

    Spawn::DoSpawn(ev);
}