summaryrefslogtreecommitdiff
path: root/src/fpsgame/entities.cpp
blob: 656a9b0cde43944d2e83056a4c73c93c08d7772b (plain) (blame)
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
#include "game.h"

int pwitemspicked[7] = { 0 };

namespace entities {
	using namespace game;
	int extraentinfosize() { return 0; }	   // size in bytes of what the 2 methods below read/write... so it can be skipped by other games

#ifndef STANDALONE
	vector<extentity *> ents;
	vector<extentity *> &getents() { return ents; }
	const char *itemname(int i) {
		int t = ents[i]->type;
		if(t<I_SHELLS || t>I_QUAD) return NULL;
		return itemstats[t-I_SHELLS].name;
	}
	int itemicon(int i) {
		int t = ents[i]->type;
		if(t<I_SHELLS || t>I_QUAD) return -1;
		return itemstats[t-I_SHELLS].icon;
	}
	const char *entmdlname(int type) {
		static const char * const entmdlnames[] = {
			NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
			"ammo/shells", "ammo/bullets", "ammo/rockets", "ammo/rrounds", "ammo/grenades", "ammo/cartridges",
			"health", "boost", "tinyhealth", "tinyarmour", "armor/green", "armor/yellow", "quad", "teleporter",
			NULL, NULL,
			"carrot",
			NULL, NULL,
			"checkpoint",
			NULL, NULL,
			NULL, NULL,
			NULL
		};
		return entmdlnames[type];
	}
	const char *entmodel(const entity &e) {
		if(e.type == TELEPORT) {
			if(e.attr2 > 0) return mapmodelname(e.attr2);
			if(e.attr2 < 0) return NULL;
		}
		return e.type < MAXENTTYPES ? entmdlname(e.type) : NULL;
	}
	void preloadentities() {
		loopi(MAXENTTYPES) {
			switch(i) {
				case I_SHELLS:
			[[fallthrough]];
				case I_BULLETS:
			[[fallthrough]];
				case I_ROCKETS:
			[[fallthrough]];
				case I_ROUNDS:
			[[fallthrough]];
				case I_GRENADES:
			[[fallthrough]];
				case I_CARTRIDGES:
					if(m_noammo) continue;
					break;
				case I_HEALTH:
			[[fallthrough]];
				case I_BOOST:
			[[fallthrough]];
				case I_TINYHEALTH:
			[[fallthrough]];
				case I_TINYARMOUR:
			[[fallthrough]];
				case I_GREENARMOUR:
			[[fallthrough]];
				case I_YELLOWARMOUR:
			[[fallthrough]];
				case I_QUAD:
					if(m_noitems) continue;
					break;
			}
			const char *mdl = entmdlname(i);
			if(!mdl) continue;
			preloadmodel(mdl);
		}
	}
	void renderentities() {
		loopv(ents) {
			extentity &e = *ents[i];
			int revs = 10;
			switch(e.type) {
				case TELEPORT:
					if(e.attr2 < 0) continue;
					break;
				default:
					if(!e.spawned() || e.type < I_SHELLS || e.type > I_QUAD) continue;
			}
			const char *mdlname = entmodel(e);
			if(mdlname) {
				vec p = e.o;
				p.z += 1+sinf(lastmillis/100.0+e.o.x+e.o.y)/20;
				rendermodel(&e.light, mdlname, ANIM_MAPMODEL|ANIM_LOOP, p, lastmillis/(float)revs, 0, MDL_SHADOW | MDL_CULL_VFC | MDL_CULL_DIST | MDL_CULL_OCCLUDED);
			}
		}
	}
	void addammo(int type, int &v, bool local) {
		itemstat &is = itemstats[type-I_SHELLS];
		v += is.add;
		if(v>is.max) v = is.max;
		if(local) msgsound(is.sound);
	}
	void repammo(fpsent *d, int type, bool local) {
		addammo(type, d->ammo[type-I_SHELLS+GUN_SG], local);
	}
	// these two functions are called when the server acknowledges that you really
	// picked up the item (in multiplayer someone may grab it before you).
	void pickupeffects(int n, fpsent *d) {
		if(!ents.inrange(n)) return;
		extentity *e = ents[n];
		int type = e->type;
		if(type<I_SHELLS || type>I_QUAD) return;
		e->clearspawned();
		e->clearnopickup();
		if(!d) return;
		itemstat &is = itemstats[type-I_SHELLS];
		fpsent *h = followingplayer(player1);
		if(d!=h || isthirdperson()) {
			//particle_text(d->abovehead(), is.name, PART_TEXT, 2000, 0xFFC864, 4.0f, -8);
			particle_icon(d->abovehead(), is.icon%4, is.icon/4, PART_HUD_ICON_GREY, 2000, 0xFFFFFF, 2.0f, -8);
		}
		playsound(itemstats[type-I_SHELLS].sound, d!=h ? &d->o : NULL, NULL, 0, 0, -1, 0, 1500);
		d->pickup(type);
		switch(type) {
			case I_TINYARMOUR:   pwitemspicked[0]++; break;
			case I_GREENARMOUR:  pwitemspicked[1]++; break;
			case I_YELLOWARMOUR: pwitemspicked[2]++; break;
			case I_TINYHEALTH:   pwitemspicked[3]++; break;
			case I_HEALTH:	   pwitemspicked[4]++; break;
			case I_BOOST:		pwitemspicked[5]++; break;
			case I_QUAD:		 pwitemspicked[6]++; break;
		}
		if(d==h) switch(type) {
			case I_BOOST:
				conoutf(CON_GAMEINFO, "\f2you got the health boost!");
				playsound(S_V_BOOST, NULL, NULL, 0, 0, -1, 0, 3000);
				break;
			case I_QUAD:
				conoutf(CON_GAMEINFO, "\f2you got the quad!");
				playsound(S_V_QUAD, NULL, NULL, 0, 0, -1, 0, 3000);
				break;
		}
	}
	// these functions are called when the client touches the item
	void teleporteffects(fpsent *d, int tp, int td, bool local) {
		if(ents.inrange(tp) && ents[tp]->type == TELEPORT) {
			extentity &e = *ents[tp];
			if(e.attr4 >= 0) {
				int snd = S_TELEPORT;
				if(e.attr4 > 0) snd = e.attr4;
				fpsent *h = followingplayer(player1);
				playsound(snd, d==h ? NULL : &e.o, NULL);
				if(d!=h && ents.inrange(td) && ents[td]->type == TELEDEST) playsound(snd, &ents[td]->o, NULL);
			}
		}
		if(local && d->clientnum >= 0) {
			sendposition(d);
			packetbuf p(32, ENET_PACKET_FLAG_RELIABLE);
			putint(p, N_TELEPORT);
			putint(p, d->clientnum);
			putint(p, tp);
			putint(p, td);
			sendclientpacket(p.finalize(), 0);
			flushclient();
		}
	}
	void jumppadeffects(fpsent *d, int jp, bool local) {
		if(ents.inrange(jp) && ents[jp]->type == JUMPPAD) {
			extentity &e = *ents[jp];
			if(e.attr4 >= 0) {
				int snd = S_JUMPPAD;
				if(e.attr4 > 0) snd = e.attr4;
				playsound(snd, d == followingplayer(player1) ? NULL : &e.o, NULL);
			}
		}
		if(local && d->clientnum >= 0) {
			sendposition(d);
			packetbuf p(16, ENET_PACKET_FLAG_RELIABLE);
			putint(p, N_JUMPPAD);
			putint(p, d->clientnum);
			putint(p, jp);
			sendclientpacket(p.finalize(), 0);
			flushclient();
		}
	}
	void teleport(int n, fpsent *d) {	 // also used by monsters {
		int e = -1, tag = ents[n]->attr1, beenhere = -1;
		for(;;) {
			e = findentity(TELEDEST, e+1);
			if(e==beenhere || e<0) { conoutf(CON_WARN, "no teleport destination for tag %d", tag); return; }
			if(beenhere<0) beenhere = e;
			if(ents[e]->attr2==tag) {
				teleporteffects(d, n, e, true);
				d->o = ents[e]->o;
				d->yaw = ents[e]->attr1;
				if(ents[e]->attr3 > 0) {
					vec dir;
					vecfromyawpitch(d->yaw, 0, 1, 0, dir);
					float speed = d->vel.magnitude2();
					d->vel.x = dir.x*speed;
					d->vel.y = dir.y*speed;
				}
				else d->vel = vec(0, 0, 0);
				entinmap(d);
				updatedynentcache(d);
				ai::inferwaypoints(d, ents[n]->o, ents[e]->o, 16.f);
				break;
			}
		}
	}
	void trypickup(int n, fpsent *d) {
		extentity *e = ents[n];
		switch(e->type) {
			default:
				if(d->canpickup(e->type)) {
					addmsg(N_ITEMPICKUP, "rci", d, n);
					e->setnopickup(); // even if someone else gets it first
				}
				break;
			case TELEPORT: {
				if(d->lastpickup==e->type && lastmillis-d->lastpickupmillis<500) break;
				if(e->attr3 > 0) {
					defformatstring(hookname, "can_teleport_%d", e->attr3);
					if(!execidentbool(hookname, true)) break;
				}
				d->lastpickup = e->type;
				d->lastpickupmillis = lastmillis;
				teleport(n, d);
				break;
			}
			case JUMPPAD: {
				if(d->lastpickup==e->type && lastmillis-d->lastpickupmillis<300) break;
				d->lastpickup = e->type;
				d->lastpickupmillis = lastmillis;
				jumppadeffects(d, n, true);
				vec v((int)(char)e->attr3*10.0f, (int)(char)e->attr2*10.0f, e->attr1*12.5f);
				if(d->ai) d->ai->becareful = true;
				d->falling = vec(0, 0, 0);
				d->physstate = PHYS_FALL;
				d->timeinair = 1;
				d->vel = v;
				break;
			}
		}
	}
	void checkitems(fpsent *d) {
		if(d->state!=CS_ALIVE) return;
		vec o = d->feetpos();
		loopv(ents) {
			extentity &e = *ents[i];
			if(e.type==NOTUSED) continue;
			if((!e.spawned() || e.nopickup()) && e.type!=TELEPORT && e.type!=JUMPPAD) continue;
			float dist = e.o.dist(o);
			if(dist<(e.type==TELEPORT ? 16 : 12)) trypickup(i, d);
		}
	}
	void checkquad(int time, fpsent *d) {
		if(d->quadmillis && (d->quadmillis -= time)<=0) {
			d->quadmillis = 0;
			fpsent *h = followingplayer(player1);
			playsound(S_PUPOUT, d==h ? NULL : &d->o);
			if(d==h) conoutf(CON_GAMEINFO, "\f2quad damage is over");
		}
	}
	void putitems(packetbuf &p) {			// puts items in network stream and also spawns them locally {
		putint(p, N_ITEMLIST);
		loopv(ents) if(ents[i]->type>=I_SHELLS && ents[i]->type<=I_QUAD && (!m_noammo || ents[i]->type<I_SHELLS || ents[i]->type>I_CARTRIDGES)) {
			putint(p, i);
			putint(p, ents[i]->type);
		}
		putint(p, -1);
	}
	void resetspawns() { loopv(ents) { extentity *e = ents[i]; e->clearspawned(); e->clearnopickup(); } }
	void spawnitems(bool force) {
		if(m_noitems) return;
		loopv(ents) {
			extentity *e = ents[i];
			if(e->type>=I_SHELLS && e->type<=I_QUAD && (!m_noammo || e->type<I_SHELLS || e->type>I_CARTRIDGES)) {
				e->setspawned(force || !server::delayspawn(e->type));
				e->clearnopickup();
			}
		}
	}
	void setspawn(int i, bool on) { if(ents.inrange(i)) { extentity *e = ents[i]; e->setspawned(on); e->clearnopickup(); } }
	extentity *newentity() { return new extentity(); }
	void deleteentity(extentity *e) { delete e; }
	void clearents() {
		while(ents.length()) deleteentity(ents.pop());
	}
	void fixentity(extentity &e) {
		if(e.type == TELEDEST) e.attr3 = e.attr2;
	}
	void entradius(extentity &e) {
		switch(e.type) {
			case TELEDEST: {
				vec dir;
				vecfromyawpitch(e.attr1, 0, 1, 0, dir);
				renderentarrow(e, dir, 4);
				break;
			}
			case TELEPORT:
				loopv(ents) if(ents[i]->type == TELEDEST && e.attr1==ents[i]->attr2) {
					renderentarrow(e, vec(ents[i]->o).sub(e.o).normalize(), e.o.dist(ents[i]->o));
					break;
				}
				break;
			case JUMPPAD:
				renderentarrow(e, vec((int)(char)e.attr3*10.0f, (int)(char)e.attr2*10.0f, e.attr1*12.5f).normalize(), 4);
				break;
			default: break;
		}
	}
	const char *entname(int i) {
		static const char * const entnames[] = {
			"none?", "light", "mapmodel", "playerstart", "none?", "particles", "sound",
			"shells", "bullets", "rockets", "riflerounds", "grenades", "cartridges",
			"health", "healthboost", "tinyhealth", "tinyarmour", "greenarmour", "yellowarmour", "quaddamage",
			"teleport", "teledest",
			"jumppad",
			"", "", "", "",
		};
		return i>=0 && size_t(i)<sizeof(entnames)/sizeof(entnames[0]) ? entnames[i] : "";
	}
	void editent(int i, bool local) {
		extentity &e = *ents[i];
		if(local) addmsg(N_EDITENT, "rii3ii5", i, (int)(e.o.x*DMF), (int)(e.o.y*DMF), (int)(e.o.z*DMF), e.type, e.attr1, e.attr2, e.attr3, e.attr4, e.attr5);
	}
#endif
}