Cube  Diff

Differences From Artifact [51629f2c3a]:

To Artifact [7fa0099398]:


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
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







-
+

-
+



-
+




-
+

-
-
-
-
+
+

+









-
+
-



-
-
+
+
-
-
+













-
+

















-
-
+
+











-
-
+
+




















-
-
+
+







-
+





-
-
+
+
















-
-
+
+





-
+


-
+







	selectgun((a1.length > 0 ? a1.cube_intValue : -1),
	    (a2.length > 0 ? a2.cube_intValue : -1),
	    (a3.length > 0 ? a3.cube_intValue : -1));
})

// create random spread of rays for the shotgun
void
createrays(const OFVector3D *from, const OFVector3D *to)
createrays(OFVector3D from, OFVector3D to)
{
	float dist = OFDistanceOfVectors3D(*from, *to);
	float dist = OFDistanceOfVectors3D(from, to);
	float f = dist * SGSPREAD / 1000;
	for (int i = 0; i < SGRAYS; i++)
#define RNDD (rnd(101) - 50) * f
		sg[i] = OFAddVectors3D(*to, OFMakeVector3D(RNDD, RNDD, RNDD));
		sg[i] = OFAddVectors3D(to, OFMakeVector3D(RNDD, RNDD, RNDD));
}

// if lineseg hits entity bounding box
static bool
intersect(DynamicEntity *d, const OFVector3D *from, const OFVector3D *to)
intersect(DynamicEntity *d, OFVector3D from, OFVector3D to)
{
	OFVector3D v = *to, w = d.origin;
	const OFVector3D *p;
	v = OFSubtractVectors3D(v, *from);
	w = OFSubtractVectors3D(w, *from);
	OFVector3D v = OFSubtractVectors3D(to, from);
	OFVector3D w = OFSubtractVectors3D(d.origin, from);
	float c1 = OFDotProductOfVectors3D(w, v);
	OFVector3D p;

	if (c1 <= 0)
		p = from;
	else {
		float c2 = OFDotProductOfVectors3D(v, v);
		if (c2 <= c1)
			p = to;
		else {
			v = OFMultiplyVector3D(v, c1 / c2);
			v = OFAddVectors3D(v, *from);
			p = OFAddVectors3D(v, from);
			p = &v;
		}
	}

	return (p->x <= d.origin.x + d.radius &&
	    p->x >= d.origin.x - d.radius && p->y <= d.origin.y + d.radius &&
	return (p.x <= d.origin.x + d.radius && p.x >= d.origin.x - d.radius &&
	    p.y <= d.origin.y + d.radius && p.y >= d.origin.y - d.radius &&
	    p->y >= d.origin.y - d.radius && p->z <= d.origin.z + d.aboveEye &&
	    p->z >= d.origin.z - d.eyeHeight);
	    p.z <= d.origin.z + d.aboveEye && p.z >= d.origin.z - d.eyeHeight);
}

OFString *
playerincrosshair()
{
	if (demoplayback)
		return NULL;

	for (id player in players) {
		if (player == [OFNull null])
			continue;

		OFVector3D o = player1.origin;
		if (intersect(player, &o, &worldpos))
		if (intersect(player, o, worldpos))
			return [player name];
	}

	return nil;
}

#define MAXPROJ 100
static Projectile *projs[MAXPROJ];

void
projreset()
{
	for (size_t i = 0; i < MAXPROJ; i++)
		projs[i].inuse = false;
}

void
newprojectile(const OFVector3D *from, const OFVector3D *to, float speed,
    bool local, DynamicEntity *owner, int gun)
newprojectile(OFVector3D from, OFVector3D to, float speed, bool local,
    DynamicEntity *owner, int gun)
{
	for (size_t i = 0; i < MAXPROJ; i++) {
		Projectile *p = projs[i];

		if (p == nil)
			projs[i] = p = [Projectile projectile];

		if (p.inuse)
			continue;

		p.inuse = true;
		p.o = *from;
		p.to = *to;
		p.o = from;
		p.to = to;
		p.speed = speed;
		p.local = local;
		p.owner = owner;
		p.gun = gun;
		return;
	}
}

static void
hit(int target, int damage, __kindof DynamicEntity *d, DynamicEntity *at)
{
	OFVector3D o = d.origin;
	if (d == player1)
		selfdamage(damage, at == player1 ? -1 : -2, at);
	else if ([d isKindOfClass:Monster.class])
		[d incurDamage:damage fromEntity:at];
	else {
		addmsg(1, 4, SV_DAMAGE, target, damage, d.lifeSequence);
		playsound(S_PAIN1 + rnd(5), &o);
	}
	particle_splash(3, damage, 1000, &o);
	demodamage(damage, &o);
	particle_splash(3, damage, 1000, o);
	demodamage(damage, o);
}

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

static void
radialeffect(
    DynamicEntity *o, const OFVector3D *v, int cn, int qdam, DynamicEntity *at)
    DynamicEntity *o, OFVector3D v, int cn, int qdam, DynamicEntity *at)
{
	if (o.state != CS_ALIVE)
		return;

	OFVector3D origin = o.origin;
	float dist = OFDistanceOfVectors3D(*v, origin);
	OFVector3D temp = OFSubtractVectors3D(*v, origin);
	float dist = OFDistanceOfVectors3D(v, origin);
	OFVector3D temp = OFSubtractVectors3D(v, origin);
	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);

		temp =
		    OFMultiplyVector3D(temp, (RL_DAMRAD - dist) * damage / 800);
		o.velocity = OFAddVectors3D(o.velocity, temp);
	}
}

static void
splash(Projectile *p, const OFVector3D *v, const OFVector3D *vold,
    int notthisplayer, int notthismonster, int qdam)
splash(Projectile *p, OFVector3D v, OFVector3D vold, int notthisplayer,
    int notthismonster, int qdam)
{
	particle_splash(0, 50, 300, v);
	p.inuse = false;

	if (p.gun != GUN_RL) {
		playsound(S_FEXPLODE, v);
		playsound(S_FEXPLODE, &v);
		// no push?
	} else {
		playsound(S_RLHIT, v);
		playsound(S_RLHIT, &v);
		newsphere(v, RL_RADIUS, 0);
		dodynlight(vold, v, 0, 0, p.owner);

		if (!p.local)
			return;

		radialeffect(player1, v, -1, qdam, p.owner);
241
242
243
244
245
246
247
248
249


250
251
252
253
254
255
256


257
258
259
260
261
262
263
238
239
240
241
242
243
244


245
246
247
248
249
250
251


252
253
254
255
256
257
258
259
260







-
-
+
+





-
-
+
+







			if (i != notthismonster)
				radialeffect(monster, v, i, qdam, p.owner);
		}];
	}
}

static inline void
projdamage(DynamicEntity *o, Projectile *p, const OFVector3D *v, int i, int im,
    int qdam)
projdamage(
    DynamicEntity *o, Projectile *p, OFVector3D v, int i, int im, int qdam)
{
	if (o.state != CS_ALIVE)
		return;

	OFVector3D po = p.o;
	if (intersect(o, &po, v)) {
		splash(p, v, &po, i, im, qdam);
	if (intersect(o, po, v)) {
		splash(p, v, po, i, im, qdam);
		hit(i, qdam, o, p.owner);
	}
}

void
moveprojectiles(float time)
{
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
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







-
+


-
+




-
+


-
-

-
+


-
-
+
+

-
+

-
+









-
-
+










-
+







		if (time > dtime)
			dtime = time;
		v = OFMultiplyVector3D(v, time / dtime);
		v = OFAddVectors3D(v, p.o);
		if (p.local) {
			for (id player in players)
				if (player != [OFNull null])
					projdamage(player, p, &v, i, -1, qdam);
					projdamage(player, p, v, i, -1, qdam);

			if (p.owner != player1)
				projdamage(player1, p, &v, -1, -1, qdam);
				projdamage(player1, p, v, -1, -1, qdam);

			for (Monster *monster in Monster.monsters)
				if (!vreject(monster.origin, v, 10.0f) &&
				    monster != p.owner)
					projdamage(monster, p, &v, -1, i, qdam);
					projdamage(monster, p, v, -1, i, qdam);
		}
		if (p.inuse) {
			OFVector3D po = p.o;

			if (time == dtime)
				splash(p, &v, &po, -1, -1, qdam);
				splash(p, v, p.o, -1, -1, qdam);
			else {
				if (p.gun == GUN_RL) {
					dodynlight(&po, &v, 0, 255, p.owner);
					particle_splash(5, 2, 200, &v);
					dodynlight(p.o, v, 0, 255, p.owner);
					particle_splash(5, 2, 200, v);
				} else {
					particle_splash(1, 1, 200, &v);
					particle_splash(1, 1, 200, v);
					particle_splash(
					    guns[p.gun].part, 1, 1, &v);
					    guns[p.gun].part, 1, 1, v);
				}
			}
		}
		p.o = v;
	}
}

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

	case GUN_SG: {
		for (int i = 0; i < SGRAYS; i++)
			particle_splash(0, 5, 200, &sg[i]);
			particle_splash(0, 5, 200, sg[i]);
		break;
	}

	case GUN_CG:
		particle_splash(0, 100, 250, to);
		// particle_trail(1, 10, from, to);
		break;
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
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







-
+


-
-
+
+





-
-
+
+











-
+








-
+


















-
+
















-
+



-
+











-
+




-
+


-
+

		particle_trail(1, 500, from, to);
		break;
	}
}

void
hitpush(int target, int damage, DynamicEntity *d, DynamicEntity *at,
    const OFVector3D *from, const OFVector3D *to)
    OFVector3D from, OFVector3D to)
{
	hit(target, damage, d, at);
	float dist = OFDistanceOfVectors3D(*from, *to);
	OFVector3D v = OFSubtractVectors3D(*from, *to);
	float dist = OFDistanceOfVectors3D(from, to);
	OFVector3D v = OFSubtractVectors3D(from, to);
	v = OFMultiplyVector3D(v, damage / dist / 50);
	d.velocity = OFAddVectors3D(d.velocity, v);
}

void
raydamage(DynamicEntity *o, const OFVector3D *from, const OFVector3D *to,
    DynamicEntity *d, int i)
raydamage(
    DynamicEntity *o, OFVector3D from, OFVector3D to, DynamicEntity *d, int i)
{
	if (o.state != CS_ALIVE)
		return;
	int qdam = guns[d.gunSelect].damage;
	if (d.quadMillis)
		qdam *= 4;
	if ([d isKindOfClass:Monster.class])
		qdam /= MONSTERDAMAGEFACTOR;
	if (d.gunSelect == GUN_SG) {
		int damage = 0;
		for (int r = 0; r < SGRAYS; r++)
			if (intersect(o, from, &sg[r]))
			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(DynamicEntity *d, const OFVector3D *targ)
shoot(DynamicEntity *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.origin;
	OFVector3D to = *targ;
	OFVector3D to = targ;
	from.z -= 0.2f; // below eye

	float dist = OFDistanceOfVectors3D(from, to);
	OFVector3D unitv = OFSubtractVectors3D(from, to);
	unitv = OFMultiplyVector3D(unitv, 1.0f / dist);
	OFVector3D kickback =
	    OFMultiplyVector3D(unitv, guns[d.gunSelect].kickamount * -0.01f);
	d.velocity = OFAddVectors3D(d.velocity, kickback);
	if (d.pitch < 80.0f)
		d.pitch += guns[d.gunSelect].kickamount * 0.05f;

	if (d.gunSelect == GUN_FIST || d.gunSelect == GUN_BITE) {
		unitv = OFMultiplyVector3D(unitv, 3); // punch range
		to = OFAddVectors3D(from, unitv);
	}
	if (d.gunSelect == GUN_SG)
		createrays(&from, &to);
		createrays(from, to);

	if (d.quadMillis && attacktime > 200)
		playsoundc(S_ITEMPUP);
	shootv(d.gunSelect, &from, &to, d, true);
	shootv(d.gunSelect, from, to, d, true);
	if (![d isKindOfClass:Monster.class])
		addmsg(1, 8, SV_SHOT, d.gunSelect, (int)(from.x * DMF),
		    (int)(from.y * DMF), (int)(from.z * DMF), (int)(to.x * DMF),
		    (int)(to.y * DMF), (int)(to.z * DMF));
	d.gunWait = guns[d.gunSelect].attackdelay;

	if (guns[d.gunSelect].projspeed)
		return;

	[players enumerateObjectsUsingBlock:^(id player, size_t i, bool *stop) {
		if (player != [OFNull null])
			raydamage(player, &from, &to, d, i);
			raydamage(player, from, to, d, i);
	}];

	for (Monster *monster in Monster.monsters)
		if (monster != d)
			raydamage(monster, &from, &to, d, -2);
			raydamage(monster, from, to, d, -2);

	if ([d isKindOfClass:Monster.class])
		raydamage(player1, &from, &to, d, -1);
		raydamage(player1, from, to, d, -1);
}