-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathg_monster_spawn.cpp
363 lines (288 loc) · 10.2 KB
/
g_monster_spawn.cpp
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
// Copyright (c) ZeniMax Media Inc.
// Licensed under the GNU General Public License 2.0.
#include "g_local.h"
//
// Monster spawning code
//
// Used by the carrier, the medic_commander, and the black widow
//
// The sequence to create a flying monster is:
//
// FindSpawnPoint - tries to find suitable spot to spawn the monster in
// CreateFlyMonster - this verifies the point as good and creates the monster
// To create a ground walking monster:
//
// FindSpawnPoint - same thing
// CreateGroundMonster - this checks the volume and makes sure the floor under the volume is suitable
//
// FIXME - for the black widow, if we want the stalkers coming in on the roof, we'll have to tweak some things
//
// CreateMonster
//
gentity_t *CreateMonster(const vec3_t &origin, const vec3_t &angles, const char *classname)
{
gentity_t *newEnt;
newEnt = G_Spawn();
newEnt->s.origin = origin;
newEnt->s.angles = angles;
newEnt->classname = classname;
newEnt->monsterinfo.aiflags |= AI_DO_NOT_COUNT;
newEnt->gravityVector = { 0, 0, -1 };
ED_CallSpawn(newEnt);
newEnt->s.renderfx |= RF_IR_VISIBLE;
return newEnt;
}
gentity_t *CreateFlyMonster(const vec3_t &origin, const vec3_t &angles, const vec3_t &mins, const vec3_t &maxs, const char *classname)
{
if (!CheckSpawnPoint(origin, mins, maxs))
return nullptr;
return (CreateMonster(origin, angles, classname));
}
// This is just a wrapper for CreateMonster that looks down height # of CMUs and sees if there
// are bad things down there or not
gentity_t *CreateGroundMonster(const vec3_t &origin, const vec3_t &angles, const vec3_t &entMins, const vec3_t &entMaxs, const char *classname, float height)
{
gentity_t *newEnt;
// check the ground to make sure it's there, it's relatively flat, and it's not toxic
if (!CheckGroundSpawnPoint(origin, entMins, entMaxs, height, -1.f))
return nullptr;
newEnt = CreateMonster(origin, angles, classname);
if (!newEnt)
return nullptr;
return newEnt;
}
// FindSpawnPoint
// PMM - this is used by the medic commander (possibly by the carrier) to find a good spawn point
// if the startpoint is bad, try above the startpoint for a bit
bool FindSpawnPoint(const vec3_t &startpoint, const vec3_t &mins, const vec3_t &maxs, vec3_t &spawnpoint, float maxMoveUp, bool drop)
{
spawnpoint = startpoint;
// drop first
if (!drop || !M_droptofloor_generic(spawnpoint, mins, maxs, false, nullptr, MASK_MONSTERSOLID, false))
{
spawnpoint = startpoint;
// fix stuck if we couldn't drop initially
if (G_FixStuckObject_Generic(spawnpoint, mins, maxs, [] (const vec3_t &start, const vec3_t &mins, const vec3_t &maxs, const vec3_t &end) {
return gi.trace(start, mins, maxs, end, nullptr, MASK_MONSTERSOLID);
}) == stuck_result_t::NO_GOOD_POSITION)
return false;
// fixed, so drop again
if (drop && !M_droptofloor_generic(spawnpoint, mins, maxs, false, nullptr, MASK_MONSTERSOLID, false))
return false; // ???
}
return true;
}
// FIXME - all of this needs to be tweaked to handle the new gravity rules
// if we ever want to spawn stuff on the roof
//
// CheckSpawnPoint
//
// PMM - checks volume to make sure we can spawn a monster there (is it solid?)
//
// This is all fliers should need
bool CheckSpawnPoint(const vec3_t &origin, const vec3_t &mins, const vec3_t &maxs)
{
trace_t tr;
if (!mins || !maxs)
return false;
tr = gi.trace(origin, mins, maxs, origin, nullptr, MASK_MONSTERSOLID);
if (tr.startsolid || tr.allsolid)
return false;
if (tr.ent != world)
return false;
return true;
}
//
// CheckGroundSpawnPoint
//
// PMM - used for walking monsters
// checks:
// 1) is there a ground within the specified height of the origin?
// 2) is the ground non-water?
// 3) is the ground flat enough to walk on?
//
bool CheckGroundSpawnPoint(const vec3_t &origin, const vec3_t &entMins, const vec3_t &entMaxs, float height, float gravity)
{
if (!CheckSpawnPoint(origin, entMins, entMaxs))
return false;
if (M_CheckBottom_Fast_Generic(origin + entMins, origin + entMaxs, false))
return true;
if (M_CheckBottom_Slow_Generic(origin, entMins, entMaxs, nullptr, MASK_MONSTERSOLID, false, false))
return true;
return false;
}
// ****************************
// SPAWNGROW stuff
// ****************************
constexpr gtime_t SPAWNGROW_LIFESPAN = 1000_ms;
static THINK(spawngrow_think) (gentity_t *self) -> void
{
if (level.time >= self->timestamp)
{
G_FreeEntity(self->target_ent);
G_FreeEntity(self);
return;
}
self->s.angles += self->avelocity * gi.frame_time_s;
float t = 1.f - ((level.time - self->teleport_time).seconds() / self->wait);
self->s.scale = clamp(lerp(self->decel, self->accel, t) / 16.f, 0.001f, 16.f);
self->s.alpha = t * t;
self->nextthink += FRAME_TIME_MS;
}
static vec3_t SpawnGro_laser_pos(gentity_t *ent)
{
// pick random direction
float theta = frandom(2 * PIf);
float phi = acos(crandom());
vec3_t d {
sin(phi) * cos(theta),
sin(phi) * sin(theta),
cos(phi)
};
return ent->s.origin + (d * ent->owner->s.scale * 9.f);
}
static THINK(SpawnGro_laser_think) (gentity_t *self) -> void
{
self->s.old_origin = SpawnGro_laser_pos(self);
gi.linkentity(self);
self->nextthink = level.time + 1_ms;
}
void SpawnGrow_Spawn(const vec3_t &startpos, float start_size, float end_size)
{
gentity_t *ent;
ent = G_Spawn();
ent->s.origin = startpos;
ent->s.angles[PITCH] = (float) irandom(360);
ent->s.angles[YAW] = (float) irandom(360);
ent->s.angles[ROLL] = (float) irandom(360);
ent->avelocity[0] = frandom(280.f, 360.f) * 2.f;
ent->avelocity[1] = frandom(280.f, 360.f) * 2.f;
ent->avelocity[2] = frandom(280.f, 360.f) * 2.f;
ent->solid = SOLID_NOT;
ent->s.renderfx |= RF_IR_VISIBLE;
ent->movetype = MOVETYPE_NONE;
ent->classname = "spawngro";
ent->s.modelindex = gi.modelindex("models/items/spawngro3/tris.md2");
ent->s.skinnum = 1;
ent->accel = start_size;
ent->decel = end_size;
ent->think = spawngrow_think;
ent->s.scale = clamp(start_size / 16.f, 0.001f, 8.f);
ent->teleport_time = level.time;
ent->wait = SPAWNGROW_LIFESPAN.seconds();
ent->timestamp = level.time + SPAWNGROW_LIFESPAN;
ent->nextthink = level.time + FRAME_TIME_MS;
gi.linkentity(ent);
// [Paril-KEX]
gentity_t *beam = ent->target_ent = G_Spawn();
beam->s.modelindex = MODELINDEX_WORLD;
beam->s.renderfx = RF_BEAM_LIGHTNING | RF_NO_ORIGIN_LERP;
beam->s.frame = 1;
beam->s.skinnum = 0x30303030;
beam->classname = "spawngro_beam";
beam->angle = end_size;
beam->owner = ent;
beam->s.origin = ent->s.origin;
beam->think = SpawnGro_laser_think;
beam->nextthink = level.time + 1_ms;
beam->s.old_origin = SpawnGro_laser_pos(beam);
gi.linkentity(beam);
}
// ****************************
// WidowLeg stuff
// ****************************
constexpr int32_t MAX_LEGSFRAME = 23;
constexpr gtime_t LEG_WAIT_TIME = 1_sec;
void ThrowMoreStuff(gentity_t *self, const vec3_t &point);
void ThrowSmallStuff(gentity_t *self, const vec3_t &point);
void ThrowWidowGibLoc(gentity_t *self, const char *gibname, int damage, gib_type_t type, const vec3_t *startpos, bool fade);
void ThrowWidowGibSized(gentity_t *self, const char *gibname, int damage, gib_type_t type, const vec3_t *startpos, int hitsound, bool fade);
static THINK(widowlegs_think) (gentity_t *self) -> void
{
vec3_t offset;
vec3_t point;
vec3_t f, r, u;
if (self->s.frame == 17)
{
offset = { 11.77f, -7.24f, 23.31f };
AngleVectors(self->s.angles, f, r, u);
point = G_ProjectSource2(self->s.origin, offset, f, r, u);
gi.WriteByte(svc_temp_entity);
gi.WriteByte(TE_EXPLOSION1);
gi.WritePosition(point);
gi.multicast(point, MULTICAST_ALL, false);
ThrowSmallStuff(self, point);
}
if (self->s.frame < MAX_LEGSFRAME)
{
self->s.frame++;
self->nextthink = level.time + 10_hz;
return;
}
else if (self->wait == 0)
{
self->wait = (level.time + LEG_WAIT_TIME).seconds();
}
if (level.time > gtime_t::from_sec(self->wait))
{
AngleVectors(self->s.angles, f, r, u);
offset = { -65.6f, -8.44f, 28.59f };
point = G_ProjectSource2(self->s.origin, offset, f, r, u);
gi.WriteByte(svc_temp_entity);
gi.WriteByte(TE_EXPLOSION1);
gi.WritePosition(point);
gi.multicast(point, MULTICAST_ALL, false);
ThrowSmallStuff(self, point);
ThrowWidowGibSized(self, "models/monsters/blackwidow/gib1/tris.md2", 80 + (int) frandom(20.0f), GIB_METALLIC, &point, 0, true);
ThrowWidowGibSized(self, "models/monsters/blackwidow/gib2/tris.md2", 80 + (int) frandom(20.0f), GIB_METALLIC, &point, 0, true);
offset = { -1.04f, -51.18f, 7.04f };
point = G_ProjectSource2(self->s.origin, offset, f, r, u);
gi.WriteByte(svc_temp_entity);
gi.WriteByte(TE_EXPLOSION1);
gi.WritePosition(point);
gi.multicast(point, MULTICAST_ALL, false);
ThrowSmallStuff(self, point);
ThrowWidowGibSized(self, "models/monsters/blackwidow/gib1/tris.md2", 80 + (int) frandom(20.0f), GIB_METALLIC, &point, 0, true);
ThrowWidowGibSized(self, "models/monsters/blackwidow/gib2/tris.md2", 80 + (int) frandom(20.0f), GIB_METALLIC, &point, 0, true);
ThrowWidowGibSized(self, "models/monsters/blackwidow/gib3/tris.md2", 80 + (int) frandom(20.0f), GIB_METALLIC, &point, 0, true);
G_FreeEntity(self);
return;
}
if ((level.time > gtime_t::from_sec(self->wait - 0.5f)) && (self->count == 0))
{
self->count = 1;
AngleVectors(self->s.angles, f, r, u);
offset = { 31, -88.7f, 10.96f };
point = G_ProjectSource2(self->s.origin, offset, f, r, u);
gi.WriteByte(svc_temp_entity);
gi.WriteByte(TE_EXPLOSION1);
gi.WritePosition(point);
gi.multicast(point, MULTICAST_ALL, false);
// ThrowSmallStuff (self, point);
offset = { -12.67f, -4.39f, 15.68f };
point = G_ProjectSource2(self->s.origin, offset, f, r, u);
gi.WriteByte(svc_temp_entity);
gi.WriteByte(TE_EXPLOSION1);
gi.WritePosition(point);
gi.multicast(point, MULTICAST_ALL, false);
// ThrowSmallStuff (self, point);
self->nextthink = level.time + 10_hz;
return;
}
self->nextthink = level.time + 10_hz;
}
void Widowlegs_Spawn(const vec3_t &startpos, const vec3_t &angles)
{
gentity_t *ent;
ent = G_Spawn();
ent->s.origin = startpos;
ent->s.angles = angles;
ent->solid = SOLID_NOT;
ent->s.renderfx = RF_IR_VISIBLE;
ent->movetype = MOVETYPE_NONE;
ent->classname = "widowlegs";
ent->s.modelindex = gi.modelindex("models/monsters/legs/tris.md2");
ent->think = widowlegs_think;
ent->nextthink = level.time + 10_hz;
gi.linkentity(ent);
}