Cube  Check-in [3d4489fdbb]

Overview
Comment:weapon.mm: Some cleanups
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3d4489fdbbc0cbf6515be7986569ca914232cc99a22550ed935c99b766574312
User & Date: js on 2025-03-04 01:00:29
Other Links: manifest | tags
Context
2025-03-04
01:01
Remove hashtable check-in: 67c1421660 user: js tags: trunk
01:00
weapon.mm: Some cleanups check-in: 3d4489fdbb user: js tags: trunk
00:51
Convert md2 and mapmodelinfo to ObjC check-in: cfbd2ec1e6 user: js tags: trunk
Changes

Modified src/weapon.mm from [88f240c5f9] to [53bbf8febc].

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
// weapon.cpp: all shooting and effects code

#include "cube.h"

struct guninfo {
	short sound, attackdelay, damage, projspeed, part, kickamount;
	char *name;
};

const int MONSTERDAMAGEFACTOR = 4;
const int SGRAYS = 20;
const float SGSPREAD = 2;
OFVector3D sg[SGRAYS];

guninfo guns[NUMGUNS] = {
    {S_PUNCH1, 250, 50, 0, 0, 1, "fist"},
    {S_SG, 1400, 10, 0, 0, 20, "shotgun"}, // *SGRAYS
    {S_CG, 100, 30, 0, 0, 7, "chaingun"},
    {S_RLFIRE, 800, 120, 80, 0, 10, "rocketlauncher"},
    {S_RIFLE, 1500, 100, 0, 0, 30, "rifle"},
    {S_FLAUNCH, 200, 20, 50, 4, 1, "fireball"},
    {S_ICEBALL, 200, 40, 30, 6, 1, "iceball"},
    {S_SLIMEBALL, 200, 30, 160, 7, 1, "slimeball"},
    {S_PIGR1, 250, 50, 0, 0, 1, "bite"},
};

void
selectgun(int a, int b, int c)
{
	if (a < -1 || b < -1 || c < -1 || a >= NUMGUNS || b >= NUMGUNS ||
	    c >= NUMGUNS)






|








|
|
|
|
|
|
|
|
|







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
// weapon.cpp: all shooting and effects code

#include "cube.h"

struct guninfo {
	short sound, attackdelay, damage, projspeed, part, kickamount;
	OFString *name;
};

const int MONSTERDAMAGEFACTOR = 4;
const int SGRAYS = 20;
const float SGSPREAD = 2;
OFVector3D sg[SGRAYS];

guninfo guns[NUMGUNS] = {
    {S_PUNCH1, 250, 50, 0, 0, 1, @"fist"},
    {S_SG, 1400, 10, 0, 0, 20, @"shotgun"}, // *SGRAYS
    {S_CG, 100, 30, 0, 0, 7, @"chaingun"},
    {S_RLFIRE, 800, 120, 80, 0, 10, @"rocketlauncher"},
    {S_RIFLE, 1500, 100, 0, 0, 30, @"rifle"},
    {S_FLAUNCH, 200, 20, 50, 4, 1, @"fireball"},
    {S_ICEBALL, 200, 40, 30, 6, 1, @"iceball"},
    {S_SLIMEBALL, 200, 30, 160, 7, 1, @"slimeball"},
    {S_PIGR1, 250, 50, 0, 0, 1, @"bite"},
};

void
selectgun(int a, int b, int c)
{
	if (a < -1 || b < -1 || c < -1 || a >= NUMGUNS || b >= NUMGUNS ||
	    c >= NUMGUNS)
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
	else if (s != GUN_RIFLE && player1->ammo[GUN_RIFLE])
		s = GUN_RIFLE;
	else
		s = GUN_FIST;
	if (s != player1->gunselect)
		playsoundc(S_WEAPLOAD);
	player1->gunselect = s;
	// conoutf(@"%s selected", (int)guns[s].name);
};


int
reloadtime(int gun)
{
	return guns[gun].attackdelay;
};


void
weapon(OFString *a1, OFString *a2, OFString *a3)
{
	@autoreleasepool {
		selectgun(a1.UTF8String[0] ? atoi(a1.UTF8String) : -1,
		    a2.UTF8String[0] ? atoi(a2.UTF8String) : -1,
		    a3.UTF8String[0] ? atoi(a3.UTF8String) : -1);
	}

}
COMMAND(weapon, ARG_3STR)

void
createrays(OFVector3D &from,
    OFVector3D &to) // create random spread of rays for the shotgun
{
	vdist(dist, dvec, from, to);
	float f = dist * SGSPREAD / 1000;
	loopi(SGRAYS)
	{
#define RNDD (rnd(101) - 50) * f
		OFVector3D r = OFMakeVector3D(RNDD, RNDD, RNDD);
		sg[i] = to;
		vadd(sg[i], r);
	};
};



bool
intersect(dynent *d, OFVector3D &from,
    OFVector3D &to) // if lineseg hits entity bounding box
{
	OFVector3D v = to, w = d->o, *p;
	vsub(v, from);







|
<
>





<
>




<
|
<
|
<
>















<
<
>
>







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
	else if (s != GUN_RIFLE && player1->ammo[GUN_RIFLE])
		s = GUN_RIFLE;
	else
		s = GUN_FIST;
	if (s != player1->gunselect)
		playsoundc(S_WEAPLOAD);
	player1->gunselect = s;
	// conoutf(@"%s selected", (int)guns[s].name.UTF8String);

}

int
reloadtime(int gun)
{
	return guns[gun].attackdelay;

}

void
weapon(OFString *a1, OFString *a2, OFString *a3)
{

	selectgun((a1.length > 0 ? (int)a1.longLongValue : -1),

	    (a2.length > 0 ? (int)a2.longLongValue : -1),

	    (a3.length > 0 ? (int)a3.longLongValue : -1));
}
COMMAND(weapon, ARG_3STR)

void
createrays(OFVector3D &from,
    OFVector3D &to) // create random spread of rays for the shotgun
{
	vdist(dist, dvec, from, to);
	float f = dist * SGSPREAD / 1000;
	loopi(SGRAYS)
	{
#define RNDD (rnd(101) - 50) * f
		OFVector3D r = OFMakeVector3D(RNDD, RNDD, RNDD);
		sg[i] = to;
		vadd(sg[i], r);


	}
}

bool
intersect(dynent *d, OFVector3D &from,
    OFVector3D &to) // if lineseg hits entity bounding box
{
	OFVector3D v = to, w = d->o, *p;
	vsub(v, from);
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
		if (c2 <= c1)
			p = &to;
		else {
			float f = c1 / c2;
			vmul(v, f);
			vadd(v, from);
			p = &v;
		};
	};



	return p->x <= d->o.x + d->radius && p->x >= d->o.x - d->radius &&
	       p->y <= d->o.y + d->radius && p->y >= d->o.y - d->radius &&
	       p->z <= d->o.z + d->aboveeye && p->z >= d->o.z - d->eyeheight;
};


char *
playerincrosshair()
{
	if (demoplayback)
		return NULL;
	loopv(players)
	{
		dynent *o = players[i];
		if (!o)
			continue;
		if (intersect(o, player1->o, worldpos))
			return o->name;
	};

	return NULL;
};


const int MAXPROJ = 100;
struct projectile {
	OFVector3D o, to;
	float speed;
	dynent *owner;
	int gun;
	bool inuse, local;
};
projectile projs[MAXPROJ];

void
projreset()
{
	loopi(MAXPROJ) projs[i].inuse = false;
};


void
newprojectile(OFVector3D &from, OFVector3D &to, float speed, bool local,
    dynent *owner, int gun)
{
	loopi(MAXPROJ)
	{
		projectile *p = &projs[i];
		if (p->inuse)
			continue;
		p->inuse = true;
		p->o = from;
		p->to = to;
		p->speed = speed;
		p->local = local;
		p->owner = owner;
		p->gun = gun;
		return;
	};
};



void
hit(int target, int damage, dynent *d, dynent *at)
{
	if (d == player1)
		selfdamage(damage, at == player1 ? -1 : -2, at);
	else if (d->monsterstate)
		monsterpain(d, damage, at);
	else {
		addmsg(1, 4, SV_DAMAGE, target, damage, d->lifesequence);
		playsound(S_PAIN1 + rnd(5), &d->o);
	};
	particle_splash(3, damage, 1000, d->o);
	demodamage(damage, d->o);
};


const float RL_RADIUS = 5;
const float RL_DAMRAD = 7; // hack

void
radialeffect(dynent *o, OFVector3D &v, int cn, int qdam, dynent *at)
{
	if (o->state != CS_ALIVE)
		return;
	vdist(dist, temp, v, o->o);
	dist -= 2; // account for eye distance imprecision
	if (dist < RL_DAMRAD) {
		if (dist < 0)
			dist = 0;
		int damage = (int)(qdam * (1 - (dist / RL_DAMRAD)));
		hit(cn, damage, o, at);
		vmul(temp, (RL_DAMRAD - dist) * damage / 800);
		vadd(o->vel, temp);
	};
};



void
splash(projectile *p, OFVector3D &v, OFVector3D &vold, int notthisplayer,
    int notthismonster, int qdam)
{
	particle_splash(0, 50, 300, v);
	p->inuse = false;







<
<
|
>
>
|
|
|
<
>













<
>

<
>








<
|





<
>


















<
<
>
>














<
>


















<
<
>
>







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
		if (c2 <= c1)
			p = &to;
		else {
			float f = c1 / c2;
			vmul(v, f);
			vadd(v, from);
			p = &v;


		}
	}

	return (p->x <= d->o.x + d->radius && p->x >= d->o.x - d->radius &&
	        p->y <= d->o.y + d->radius && p->y >= d->o.y - d->radius &&
	        p->z <= d->o.z + d->aboveeye && p->z >= d->o.z - d->eyeheight);

}

char *
playerincrosshair()
{
	if (demoplayback)
		return NULL;
	loopv(players)
	{
		dynent *o = players[i];
		if (!o)
			continue;
		if (intersect(o, player1->o, worldpos))
			return o->name;

	}
	return NULL;

}

const int MAXPROJ = 100;
struct projectile {
	OFVector3D o, to;
	float speed;
	dynent *owner;
	int gun;
	bool inuse, local;

} projs[MAXPROJ];

void
projreset()
{
	loopi(MAXPROJ) projs[i].inuse = false;

}

void
newprojectile(OFVector3D &from, OFVector3D &to, float speed, bool local,
    dynent *owner, int gun)
{
	loopi(MAXPROJ)
	{
		projectile *p = &projs[i];
		if (p->inuse)
			continue;
		p->inuse = true;
		p->o = from;
		p->to = to;
		p->speed = speed;
		p->local = local;
		p->owner = owner;
		p->gun = gun;
		return;


	}
}

void
hit(int target, int damage, dynent *d, dynent *at)
{
	if (d == player1)
		selfdamage(damage, at == player1 ? -1 : -2, at);
	else if (d->monsterstate)
		monsterpain(d, damage, at);
	else {
		addmsg(1, 4, SV_DAMAGE, target, damage, d->lifesequence);
		playsound(S_PAIN1 + rnd(5), &d->o);
	};
	particle_splash(3, damage, 1000, d->o);
	demodamage(damage, d->o);

}

const float RL_RADIUS = 5;
const float RL_DAMRAD = 7; // hack

void
radialeffect(dynent *o, OFVector3D &v, int cn, int qdam, dynent *at)
{
	if (o->state != CS_ALIVE)
		return;
	vdist(dist, temp, v, o->o);
	dist -= 2; // account for eye distance imprecision
	if (dist < RL_DAMRAD) {
		if (dist < 0)
			dist = 0;
		int damage = (int)(qdam * (1 - (dist / RL_DAMRAD)));
		hit(cn, damage, o, at);
		vmul(temp, (RL_DAMRAD - dist) * damage / 800);
		vadd(o->vel, temp);


	}
}

void
splash(projectile *p, OFVector3D &v, OFVector3D &vold, int notthisplayer,
    int notthismonster, int qdam)
{
	particle_splash(0, 50, 300, v);
	p->inuse = false;
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
		{
			if (i == notthisplayer)
				continue;
			dynent *o = players[i];
			if (!o)
				continue;
			radialeffect(o, v, i, qdam, p->owner);
		};

		dvector &mv = getmonsters();
		loopv(mv) if (i != notthismonster)
		    radialeffect(mv[i], v, i, qdam, p->owner);
	};
};



inline void
projdamage(dynent *o, projectile *p, OFVector3D &v, int i, int im, int qdam)
{
	if (o->state != CS_ALIVE)
		return;
	if (intersect(o, p->o, v)) {
		splash(p, v, p->o, i, im, qdam);
		hit(i, qdam, o, p->owner);
	};
};



void
moveprojectiles(float time)
{
	loopi(MAXPROJ)
	{
		projectile *p = &projs[i];







<
>



<
<
>
>









<
<
>
>







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
		{
			if (i == notthisplayer)
				continue;
			dynent *o = players[i];
			if (!o)
				continue;
			radialeffect(o, v, i, qdam, p->owner);

		}
		dvector &mv = getmonsters();
		loopv(mv) if (i != notthismonster)
		    radialeffect(mv[i], v, i, qdam, p->owner);


	}
}

inline void
projdamage(dynent *o, projectile *p, OFVector3D &v, int i, int im, int qdam)
{
	if (o->state != CS_ALIVE)
		return;
	if (intersect(o, p->o, v)) {
		splash(p, v, p->o, i, im, qdam);
		hit(i, qdam, o, p->owner);


	}
}

void
moveprojectiles(float time)
{
	loopi(MAXPROJ)
	{
		projectile *p = &projs[i];
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
					particle_splash(1, 1, 200, v);
					particle_splash(
					    guns[p->gun].part, 1, 1, v);
				}
			}
		}
		p->o = v;
	};
};



void
shootv(int gun, OFVector3D &from, OFVector3D &to, dynent *d,
    bool local) // create visual effect from a shot
{
	playsound(guns[gun].sound, d == player1 ? NULL : &d->o);
	int pspeed = 25;
	switch (gun) {
	case GUN_FIST:
		break;

	case GUN_SG: {
		loopi(SGRAYS) particle_splash(0, 5, 200, sg[i]);
		break;
	};


	case GUN_CG:
		particle_splash(0, 100, 250, to);
		// particle_trail(1, 10, from, to);
		break;

	case GUN_RL:
	case GUN_FIREBALL:
	case GUN_ICEBALL:
	case GUN_SLIMEBALL:
		pspeed = guns[gun].projspeed;
		if (d->monsterstate)
			pspeed /= 2;
		newprojectile(from, to, (float)pspeed, local, d, gun);
		break;

	case GUN_RIFLE:
		particle_splash(0, 50, 200, to);
		particle_trail(1, 500, from, to);
		break;
	};
};



void
hitpush(int target, int damage, dynent *d, dynent *at, OFVector3D &from,
    OFVector3D &to)
{
	hit(target, damage, d, at);
	vdist(dist, v, from, to);







<
<
>
>














<
>




















<
<
>
>







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
					particle_splash(1, 1, 200, v);
					particle_splash(
					    guns[p->gun].part, 1, 1, v);
				}
			}
		}
		p->o = v;


	}
}

void
shootv(int gun, OFVector3D &from, OFVector3D &to, dynent *d,
    bool local) // create visual effect from a shot
{
	playsound(guns[gun].sound, d == player1 ? NULL : &d->o);
	int pspeed = 25;
	switch (gun) {
	case GUN_FIST:
		break;

	case GUN_SG: {
		loopi(SGRAYS) particle_splash(0, 5, 200, sg[i]);
		break;

	}

	case GUN_CG:
		particle_splash(0, 100, 250, to);
		// particle_trail(1, 10, from, to);
		break;

	case GUN_RL:
	case GUN_FIREBALL:
	case GUN_ICEBALL:
	case GUN_SLIMEBALL:
		pspeed = guns[gun].projspeed;
		if (d->monsterstate)
			pspeed /= 2;
		newprojectile(from, to, (float)pspeed, local, d, gun);
		break;

	case GUN_RIFLE:
		particle_splash(0, 50, 200, to);
		particle_trail(1, 500, from, to);
		break;


	}
}

void
hitpush(int target, int damage, dynent *d, dynent *at, OFVector3D &from,
    OFVector3D &to)
{
	hit(target, damage, d, at);
	vdist(dist, v, from, to);
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
	if (d->gunselect == GUN_SG) {
		int damage = 0;
		loop(r, SGRAYS) if (intersect(o, from, sg[r])) damage += qdam;
		if (damage)
			hitpush(i, damage, o, d, from, to);
	} else if (intersect(o, from, to))
		hitpush(i, qdam, o, d, from, to);
};


void
shoot(dynent *d, OFVector3D &targ)
{
	int attacktime = lastmillis - d->lastaction;
	if (attacktime < d->gunwait)
		return;
	d->gunwait = 0;
	if (!d->attacking)
		return;
	d->lastaction = lastmillis;
	d->lastattackgun = d->gunselect;
	if (!d->ammo[d->gunselect]) {
		playsoundc(S_NOAMMO);
		d->gunwait = 250;
		d->lastattackgun = -1;
		return;
	};

	if (d->gunselect)
		d->ammo[d->gunselect]--;
	OFVector3D from = d->o;
	OFVector3D to = targ;
	from.z -= 0.2f; // below eye

	vdist(dist, unitv, from, to);
	vdiv(unitv, dist);
	OFVector3D kickback = unitv;
	vmul(kickback, guns[d->gunselect].kickamount * -0.01f);
	vadd(d->vel, kickback);
	if (d->pitch < 80.0f)
		d->pitch += guns[d->gunselect].kickamount * 0.05f;

	if (d->gunselect == GUN_FIST || d->gunselect == GUN_BITE) {
		vmul(unitv, 3); // punch range
		to = from;
		vadd(to, unitv);
	};

	if (d->gunselect == GUN_SG)
		createrays(from, to);

	if (d->quadmillis && attacktime > 200)
		playsoundc(S_ITEMPUP);
	shootv(d->gunselect, from, to, d, true);
	if (!d->monsterstate)







<
>

















<
>


















<
>







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
	if (d->gunselect == GUN_SG) {
		int damage = 0;
		loop(r, SGRAYS) if (intersect(o, from, sg[r])) damage += qdam;
		if (damage)
			hitpush(i, damage, o, d, from, to);
	} else if (intersect(o, from, to))
		hitpush(i, qdam, o, d, from, to);

}

void
shoot(dynent *d, OFVector3D &targ)
{
	int attacktime = lastmillis - d->lastaction;
	if (attacktime < d->gunwait)
		return;
	d->gunwait = 0;
	if (!d->attacking)
		return;
	d->lastaction = lastmillis;
	d->lastattackgun = d->gunselect;
	if (!d->ammo[d->gunselect]) {
		playsoundc(S_NOAMMO);
		d->gunwait = 250;
		d->lastattackgun = -1;
		return;

	}
	if (d->gunselect)
		d->ammo[d->gunselect]--;
	OFVector3D from = d->o;
	OFVector3D to = targ;
	from.z -= 0.2f; // below eye

	vdist(dist, unitv, from, to);
	vdiv(unitv, dist);
	OFVector3D kickback = unitv;
	vmul(kickback, guns[d->gunselect].kickamount * -0.01f);
	vadd(d->vel, kickback);
	if (d->pitch < 80.0f)
		d->pitch += guns[d->gunselect].kickamount * 0.05f;

	if (d->gunselect == GUN_FIST || d->gunselect == GUN_BITE) {
		vmul(unitv, 3); // punch range
		to = from;
		vadd(to, unitv);

	}
	if (d->gunselect == GUN_SG)
		createrays(from, to);

	if (d->quadmillis && attacktime > 200)
		playsoundc(S_ITEMPUP);
	shootv(d->gunselect, from, to, d, true);
	if (!d->monsterstate)
411
412
413
414
415
416
417
418

419
420
421
422
423
424
425


	loopv(players)
	{
		dynent *o = players[i];
		if (!o)
			continue;
		raydamage(o, from, to, d, i);
	};


	dvector &v = getmonsters();
	loopv(v) if (v[i] != d) raydamage(v[i], from, to, d, -2);

	if (d->monsterstate)
		raydamage(player1, from, to, d, -1);
};








<
>






<
>
408
409
410
411
412
413
414

415
416
417
418
419
420
421

422

	loopv(players)
	{
		dynent *o = players[i];
		if (!o)
			continue;
		raydamage(o, from, to, d, i);

	}

	dvector &v = getmonsters();
	loopv(v) if (v[i] != d) raydamage(v[i], from, to, d, -2);

	if (d->monsterstate)
		raydamage(player1, from, to, d, -1);

}