1
// entities.cpp: map entity related functions (pickup etc.)
8
const char *entnames[] =
10
"none?", "light", "playerstart",
11
"pistol", "ammobox","grenades",
12
"health", "armour", "akimbo",
13
"mapmodel", "trigger",
15
"sound", "clip", "", ""
18
const char *entmdlnames[] =
20
"pickups/pistolclips", "pickups/ammobox", "pickups/nades", "pickups/health", "pickups/kevlar", "pickups/akimbo",
23
void renderent(entity &e)
25
const char *mdlname = entmdlnames[e.type-I_CLIPS];
26
float z = (float)(1+sinf(lastmillis/100.0f+e.x+e.y)/20),
27
yaw = lastmillis/10.0f;
28
rendermodel(mdlname, ANIM_MAPMODEL|ANIM_LOOP|ANIM_DYNALLOC, 0, 0, vec(e.x, e.y, z+S(e.x, e.y)->floor+e.attr1), yaw, 0);
31
void renderclip(entity &e, bool ismm = false)
33
float xradius = max(float(e.attr2), 0.1f), yradius = max(float(e.attr3), 0.1f);
34
vec bbmin(e.x - xradius, e.y - yradius, float(S(e.x, e.y)->floor+e.attr1)),
35
bbmax(e.x + xradius, e.y + yradius, bbmin.z + max(float(e.attr4), 0.1f));
37
glDisable(GL_TEXTURE_2D);
38
if(ismm) linestyle(1, 0, 0xFF, 0);
39
else linestyle(1, 0xFF, 0xFF, 0);
42
glVertex3f(bbmin.x, bbmin.y, bbmin.z);
43
loopi(2) glVertex3f(bbmax.x, bbmin.y, bbmin.z);
44
loopi(2) glVertex3f(bbmax.x, bbmax.y, bbmin.z);
45
loopi(2) glVertex3f(bbmin.x, bbmax.y, bbmin.z);
46
glVertex3f(bbmin.x, bbmin.y, bbmin.z);
48
glVertex3f(bbmin.x, bbmin.y, bbmax.z);
49
loopi(2) glVertex3f(bbmax.x, bbmin.y, bbmax.z);
50
loopi(2) glVertex3f(bbmax.x, bbmax.y, bbmax.z);
51
loopi(2) glVertex3f(bbmin.x, bbmax.y, bbmax.z);
52
glVertex3f(bbmin.x, bbmin.y, bbmax.z);
54
loopi(8) glVertex3f(i&2 ? bbmax.x : bbmin.x, i&4 ? bbmax.y : bbmin.y, i&1 ? bbmax.z : bbmin.z);
57
glEnable(GL_TEXTURE_2D);
60
void rendermapmodels()
67
mapmodelinfo &mmi = getmminfo(e.attr2);
69
rendermodel(mmi.name, ANIM_MAPMODEL|ANIM_LOOP, e.attr4, 0, vec(e.x, e.y, (float)S(e.x, e.y)->floor+mmi.zoff+e.attr3), (float)((e.attr1+7)-(e.attr1+7)%15), 0, 10.0f);
74
VAR(showmodelclipping, 0, 0, 1);
78
if(editmode && !reflecting && !refracting && !stenciling)
80
static int lastsparkle = 0;
81
if(lastmillis - lastsparkle >= 20)
83
lastsparkle = lastmillis - (lastmillis%20);
84
int closest = closestent();
88
if(e.type==NOTUSED) continue;
90
if(vec(v).sub(camera1->o).dot(camdir) < 0) continue;
91
particle_splash(i == closest ? 12 : 2, 2, 40, v);
100
if((!OUTBORD(e.x, e.y) && e.spawned) || editmode)
109
s_sprintfd(path)("pickups/flags/%s", team_string(e.attr2));
110
rendermodel(path, ANIM_FLAG|ANIM_LOOP, 0, 0, vec(e.x, e.y, (float)S(e.x, e.y)->floor), (float)((e.attr1+7)-(e.attr1+7)%15), 0, 120.0f);
112
else if(e.type==CLIP && !stenciling) renderclip(e);
113
else if(showmodelclipping && e.type == MAPMODEL && !stenciling)
115
mapmodelinfo &mmi = getmminfo(e.attr2);
120
ce.attr1 = mmi.zoff+e.attr3;
121
ce.attr2 = ce.attr3 = mmi.rad;
123
renderclip(ce, true);
130
flaginfo &f = flaginfos[i];
134
if(f.actor && f.actor != player1)
136
if(OUTBORD(f.actor->o.x, f.actor->o.y)) break;
137
s_sprintfd(path)("pickups/flags/small_%s%s", m_ktf ? "" : team_string(i), m_htf ? "_htf" : m_ktf ? "ktf" : "");
138
rendermodel(path, ANIM_FLAG|ANIM_START|ANIM_DYNALLOC, 0, 0, vec(f.actor->o).add(vec(0, 0, 0.3f+(sinf(lastmillis/100.0f)+1)/10)), lastmillis/2.5f, 0, 120.0f);
142
if(!numflagspawn[i]) break;
145
if(OUTBORD(f.pos.x, f.pos.y)) break;
146
entity &e = *f.flagent;
147
s_sprintfd(path)("pickups/flags/%s%s", m_ktf ? "" : team_string(i), m_htf ? "_htf" : m_ktf ? "ktf" : "");
148
rendermodel(path, ANIM_FLAG|ANIM_LOOP, 0, 0, vec(f.pos.x, f.pos.y, f.state==CTFF_INBASE ? (float)S(int(f.pos.x), int(f.pos.y))->floor : f.pos.z), (float)((e.attr1+7)-(e.attr1+7)%15), 0, 120.0f);
157
// these two functions are called when the server acknowledges that you really
158
// picked up the item (in multiplayer someone may grab it before you).
160
void pickupeffects(int n, playerent *d)
162
if(!ents.inrange(n)) return;
167
itemstat &is = d->itemstats(e.type);
168
if(d!=player1 && d->type!=ENT_BOT) return;
171
if(d==player1) playsoundc(is.sound);
172
else playsound(is.sound, d);
178
case I_AKIMBO: w = d->weapons[GUN_AKIMBO]; break;
179
case I_CLIPS: w = d->weapons[GUN_PISTOL]; break;
180
case I_AMMO: w = d->primweap; break;
181
case I_GRENADE: w = d->weapons[GUN_GRENADE]; break;
183
if(w) w->onammopicked();
186
// these functions are called when the client touches the item
188
void trypickup(int n, playerent *d)
194
if(d->canpickup(e.type))
196
if(d->type==ENT_PLAYER) addmsg(SV_ITEMPICKUP, "ri", n);
197
else if(d->type==ENT_BOT && serverpickup(n, -1)) pickupeffects(n, d);
203
if(!d->crouching) d->onladder = true;
208
void trypickupflag(int flag, playerent *d)
212
flaginfo &f = flaginfos[flag];
213
flaginfo &of = flaginfos[team_opposite(flag)];
214
if(f.state == CTFF_STOLEN) return;
215
bool own = flag == team_int(d->team);
219
if(own) // it's the own flag
221
if(f.state == CTFF_DROPPED) flagreturn(flag);
222
else if(f.state == CTFF_INBASE && of.state == CTFF_STOLEN && of.actor == d && of.ack) flagscore(of.team);
224
else flagpickup(flag);
234
if(f.state == CTFF_DROPPED) flagscore(f.team); // may not count!
239
if(f.state != CTFF_INBASE) return;
245
void checkitems(playerent *d)
247
if(editmode || d->state!=CS_ALIVE) return;
249
float eyeheight = d->eyeheight;
253
if(e.type==NOTUSED) continue;
256
if(OUTBORD(e.x, e.y)) continue;
257
vec v(e.x, e.y, d->o.z);
258
float dist1 = d->o.dist(v);
259
float dist2 = d->o.z - (S(e.x, e.y)->floor+eyeheight);
260
if(dist1<1.5f && dist2<e.attr1) trypickup(i, d);
264
if(!e.spawned) continue;
265
if(OUTBORD(e.x, e.y)) continue;
267
if(e.type==CTF_FLAG) continue;
268
// simple 2d collision
269
vec v(e.x, e.y, S(e.x, e.y)->floor+eyeheight);
270
if(isitem(e.type)) v.z += e.attr1;
271
if(d->o.dist(v)<2.5f) trypickup(i, d);
275
flaginfo &f = flaginfos[i];
276
entity &e = *f.flagent;
277
if(!e.spawned || !f.ack || (f.state == CTFF_INBASE && !numflagspawn[i])) continue;
278
if(OUTBORD(f.pos.x, f.pos.y)) continue;
279
if(f.state==CTFF_DROPPED) // 3d collision for dropped ctf flags
281
if(objcollide(d, f.pos, 2.5f, 4.0f)) trypickupflag(i, d);
283
else // simple 2d collision
286
v.z = S(int(v.x), int(v.y))->floor + eyeheight;
287
if(d->o.dist(v)<2.5f) trypickupflag(i, d);
292
void putitems(ucharbuf &p) // puts items in network stream and also spawns them locally
294
loopv(ents) if(ents[i].fitsmode(gamemode) || (multiplayer(false) && gamespeed!=100 && (i=-1)))
297
putint(p, ents[i].type);
298
ents[i].spawned = true;
304
loopv(ents) ents[i].spawned = false;
305
if(m_noitemsnade || m_pistol)
307
loopv(ents) ents[i].transformtype(gamemode);
310
void setspawn(int i, bool on) { if(ents.inrange(i)) ents[i].spawned = on; }
312
bool selectnextprimary(int num)
320
player1->setnextprimary(num);
321
addmsg(SV_PRIMARYWEAP, "ri", player1->nextprimweap->type);
325
conoutf("this is not a valid primary weapon");
330
VARFP(nextprimary, 0, GUN_ASSAULT, NUMGUNS,
332
if(!selectnextprimary(nextprimary)) selectnextprimary((nextprimary = GUN_ASSAULT));
335
// flag ent actions done by the local player
337
int flagdropmillis = 0;
339
void flagpickup(int fln)
341
if(flagdropmillis && flagdropmillis>lastmillis) return;
342
flaginfo &f = flaginfos[fln];
343
f.flagent->spawned = false;
344
f.state = CTFF_STOLEN;
345
f.actor = player1; // do this although we don't know if we picked the flag to avoid getting it after a possible respawn
346
f.actor_cn = getclientnum();
348
addmsg(SV_FLAGACTION, "rii", FA_PICKUP, f.team);
351
void tryflagdrop(bool manual)
355
flaginfo &f = flaginfos[i];
356
if(f.state==CTFF_STOLEN && f.actor==player1)
358
f.flagent->spawned = false;
359
f.state = CTFF_DROPPED;
361
flagdropmillis = lastmillis+3000;
362
addmsg(SV_FLAGACTION, "rii", manual ? FA_DROP : FA_LOST, f.team);
367
void flagreturn(int fln)
369
flaginfo &f = flaginfos[fln];
370
f.flagent->spawned = false;
372
addmsg(SV_FLAGACTION, "rii", FA_RETURN, f.team);
375
void flagscore(int fln)
377
flaginfo &f = flaginfos[fln];
379
addmsg(SV_FLAGACTION, "rii", FA_SCORE, f.team);
382
// flag ent actions from the net
384
void flagstolen(int flag, int act)
386
playerent *actor = act == getclientnum() ? player1 : getclient(act);
387
flaginfo &f = flaginfos[flag];
388
f.actor = actor; // could be NULL if we just connected
390
f.flagent->spawned = false;
394
void flagdropped(int flag, float x, float y, float z)
396
flaginfo &f = flaginfos[flag];
397
if(OUTBORD(x, y)) return; // valid pos
404
p.aboveeye = p.eyeheight = p.maxeyeheight = 0.4f;
407
bool oldcancollide = false;
410
oldcancollide = f.actor->cancollide;
411
f.actor->cancollide = false; // avoid collision with owner
413
loopi(100) // perform physics steps
415
moveplayer(&p, 10, true, 50);
418
if(f.actor) f.actor->cancollide = oldcancollide; // restore settings
420
f.pos.x = round(p.o.x);
421
f.pos.y = round(p.o.y);
422
f.pos.z = round(p.o.z);
423
if(f.pos.z < hdr.waterlevel) f.pos.z = (short) hdr.waterlevel;
424
f.flagent->spawned = true;
428
void flaginbase(int flag)
430
flaginfo &f = flaginfos[flag];
431
f.actor = NULL; f.actor_cn = -1;
432
f.pos = vec(f.flagent->x, f.flagent->y, f.flagent->z);
433
f.flagent->spawned = true;
437
void flagidle(int flag)
440
flaginfos[flag].flagent->spawned = false;
445
int entcnt[MAXENTTYPES] = {0}, clipents = 0, spawncnt[5] = {0};
449
if(e.type >= MAXENTTYPES) continue;
455
mapmodelinfo &mmi = getmminfo(e.attr2);
456
if(&mmi && mmi.h) clipents++;
460
if(e.attr2 < 2) spawncnt[e.attr2]++;
461
if(e.attr2 == 100) spawncnt[2]++;
464
if(e.attr2 < 2) spawncnt[e.attr2 + 3]++;
470
if(entcnt[i]) switch(i)
472
case MAPMODEL: conoutf(" %d %s, %d clipped", entcnt[i], entnames[i], clipents); break;
473
case PLAYERSTART: conoutf(" %d %s, %d CLA, %d RVSF, %d FFA", entcnt[i], entnames[i], spawncnt[0], spawncnt[1], spawncnt[2]); break;
474
case CTF_FLAG: conoutf(" %d %s, %d CLA, %d RVSF", entcnt[i], entnames[i], spawncnt[3], spawncnt[4]); break;
475
default: conoutf(" %d %s", entcnt[i], entnames[i]); break;
478
conoutf("total entities: %d", ents.length());
481
COMMAND(entstats, ARG_NONE);