Cube  Check-in [e8f80b0482]

Overview
Comment:Convert monster into a class
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e8f80b0482846dfbd5bf97ed375b13687d325e9ef7e2a79176dac49722180124
User & Date: js on 2025-03-23 02:03:32
Other Links: manifest | tags
Context
2025-03-23
02:47
Remove loop[ijkl] check-in: 6b85eefc85 user: js tags: trunk
02:03
Convert monster into a class check-in: e8f80b0482 user: js tags: trunk
2025-03-22
23:10
Clean up DynamicEntity check-in: b787ad5a04 user: js tags: trunk
Changes

Modified src/DynamicEntity.h from [52cd3f4f7c] to [39b63673d9].

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
@property (nonatomic) int lifeSequence;
// one of CS_* below
@property (nonatomic) int state;
@property (nonatomic) int frags;
@property (nonatomic) int health, armour, armourType, quadMillis;
@property (nonatomic) int gunSelect, gunWait;
@property (nonatomic) int lastAction, lastAttackGun, lastMove;
@property (nonatomic) bool attacking;
@property (readonly, nonatomic) int *ammo;
// one of M_* below, M_NONE means human
@property (nonatomic) int monsterState;
// see monster.m
@property (nonatomic) int monsterType;
// monster wants to kill this entity
@property (nonatomic) DynamicEntity *enemy;
// monster wants to look in this direction
@property (nonatomic) float targetYaw;
// used by physics to signal ai
@property (nonatomic) bool blocked, moving;
// millis at which transition to another monsterstate takes place
@property (nonatomic) int trigger;
// delayed attacks
@property (nonatomic) OFVector3D attackTarget;
// how many times already hit by fellow monster
@property (nonatomic) int anger;
@property (copy, nonatomic) OFString *name, *team;

+ (instancetype)entity;
- (OFData *)dataBySerializing;
- (void)setFromSerializedData:(OFData *)data;
- (void)resetMovement;
// reset player state not persistent accross spawns







<

<
<
<
|
<
<
<
<


<
<
<
<
<
<







25
26
27
28
29
30
31

32



33




34
35






36
37
38
39
40
41
42
@property (nonatomic) int lifeSequence;
// one of CS_* below
@property (nonatomic) int state;
@property (nonatomic) int frags;
@property (nonatomic) int health, armour, armourType, quadMillis;
@property (nonatomic) int gunSelect, gunWait;
@property (nonatomic) int lastAction, lastAttackGun, lastMove;

@property (readonly, nonatomic) int *ammo;



@property (nonatomic) bool attacking;




// used by physics to signal ai
@property (nonatomic) bool blocked, moving;






@property (copy, nonatomic) OFString *name, *team;

+ (instancetype)entity;
- (OFData *)dataBySerializing;
- (void)setFromSerializedData:(OFData *)data;
- (void)resetMovement;
// reset player state not persistent accross spawns

Modified src/DynamicEntity.m from [1744394702] to [03f91e70ff].

1
2
3
4


5
6
7
8
9
10
11
#import "DynamicEntity.h"

#include "cube.h"



struct dynent {
	OFVector3D origin, velocity;
	float yaw, pitch, roll;
	float maxSpeed;
	bool outsideMap;
	bool inWater;
	bool onFloor, jumpNext;




>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
#import "DynamicEntity.h"

#include "cube.h"

#import "Monster.h"

struct dynent {
	OFVector3D origin, velocity;
	float yaw, pitch, roll;
	float maxSpeed;
	bool outsideMap;
	bool inWater;
	bool onFloor, jumpNext;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	copy->_lastAttackGun = _lastAttackGun;
	copy->_lastMove = _lastMove;
	copy->_attacking = _attacking;

	for (size_t i = 0; i < NUMGUNS; i++)
		copy->_ammo[i] = _ammo[i];

	copy->_monsterState = _monsterState;
	copy->_monsterType = _monsterType;
	copy->_enemy = _enemy;
	copy->_targetYaw = _targetYaw;
	copy->_blocked = _blocked;
	copy->_moving = _moving;
	copy->_trigger = _trigger;
	copy->_attackTarget = _attackTarget;
	copy->_anger = _anger;

	copy->_name = [_name copy];
	copy->_team = [_team copy];

	return copy;
}

- (OFData *)dataBySerializing







<
<
<
<


<
<
<
<







111
112
113
114
115
116
117




118
119




120
121
122
123
124
125
126
	copy->_lastAttackGun = _lastAttackGun;
	copy->_lastMove = _lastMove;
	copy->_attacking = _attacking;

	for (size_t i = 0; i < NUMGUNS; i++)
		copy->_ammo[i] = _ammo[i];





	copy->_blocked = _blocked;
	copy->_moving = _moving;




	copy->_name = [_name copy];
	copy->_team = [_team copy];

	return copy;
}

- (OFData *)dataBySerializing
165
166
167
168
169
170
171
172
173
174
175
176






177
178
179

180
181
182
183
184
185
186
		.quadMillis = _quadMillis,
		.gunSelect = _gunSelect,
		.gunWait = _gunWait,
		.lastAction = _lastAction,
		.lastAttackGun = _lastAttackGun,
		.lastMove = _lastMove,
		.attacking = _attacking,
		.monsterState = _monsterState,
		.monsterType = _monsterType,
		.targetYaw = _targetYaw,
		.blocked = _blocked,
		.moving = _moving,






		.trigger = _trigger,
		.attackTarget = _attackTarget,
		.anger = _anger };


	for (int i = 0; i < NUMGUNS; i++)
		data.ammo[i] = _ammo[i];

	memcpy(data.name, _name.UTF8String, min(_name.UTF8StringLength, 259));
	memcpy(data.team, _team.UTF8String, min(_team.UTF8StringLength, 259));








<
<
<

|
>
>
>
>
>
>
|
|
|
>







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
		.quadMillis = _quadMillis,
		.gunSelect = _gunSelect,
		.gunWait = _gunWait,
		.lastAction = _lastAction,
		.lastAttackGun = _lastAttackGun,
		.lastMove = _lastMove,
		.attacking = _attacking,



		.blocked = _blocked,
		.moving = _moving };

	if ([self isKindOfClass:Monster.class]) {
		Monster *monster = (Monster *)self;
		data.monsterState = monster.monsterState;
		data.monsterType = monster.monsterType;
		data.targetYaw = monster.targetYaw;
		data.trigger = monster.trigger;
		data.attackTarget = monster.attackTarget;
		data.anger = monster.anger;
	}

	for (int i = 0; i < NUMGUNS; i++)
		data.ammo[i] = _ammo[i];

	memcpy(data.name, _name.UTF8String, min(_name.UTF8StringLength, 259));
	memcpy(data.team, _team.UTF8String, min(_team.UTF8StringLength, 259));

232
233
234
235
236
237
238





239
240
241
242
243
244
245
246

247
248
249
250
251
252
253
	_lastAttackGun = d.lastAttackGun;
	_lastMove = d.lastMove;
	_attacking = d.attacking;

	for (int i = 0; i < NUMGUNS; i++)
		_ammo[i] = d.ammo[i];






	_monsterState = d.monsterState;
	_monsterType = d.monsterType;
	_targetYaw = d.targetYaw;
	_blocked = d.blocked;
	_moving = d.moving;
	_trigger = d.trigger;
	_attackTarget = d.attackTarget;
	_anger = d.anger;


	_name = [[OFString alloc] initWithUTF8String:d.name];
	_team = [[OFString alloc] initWithUTF8String:d.team];
}

- (void)resetMovement
{







>
>
>
>
>
|
|
|
<
<
|
|
|
>







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
	_lastAttackGun = d.lastAttackGun;
	_lastMove = d.lastMove;
	_attacking = d.attacking;

	for (int i = 0; i < NUMGUNS; i++)
		_ammo[i] = d.ammo[i];

	_blocked = d.blocked;
	_moving = d.moving;

	if ([self isKindOfClass:Monster.class]) {
		Monster *monster = (Monster *)self;
		monster.monsterState = d.monsterState;
		monster.monsterType = d.monsterType;
		monster.targetYaw = d.targetYaw;


		monster.trigger = d.trigger;
		monster.attackTarget = d.attackTarget;
		monster.anger = d.anger;
	}

	_name = [[OFString alloc] initWithUTF8String:d.name];
	_team = [[OFString alloc] initWithUTF8String:d.team];
}

- (void)resetMovement
{

Added src/Monster.h version [489882baef].

Renamed and modified src/monster.m [8b5a6bb4d3] to src/Monster.m [8c481c4403].

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
// monster.cpp: implements AI for single player monsters, currently client only



#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"

static OFMutableArray<DynamicEntity *> *monsters;
static int nextmonster, spawnremain, numkilled, monstertotal, mtimestart;

VARF(skill, 1, 3, 10, conoutf(@"skill is now %d", skill));




OFArray<DynamicEntity *> *
getmonsters()
{
	return monsters;
}
















// for savegames
void
restoremonsterstate()
{
	for (DynamicEntity *monster in monsters)
		if (monster.state == CS_DEAD)
			numkilled++;
}

#define TOTMFREQ 13
#define NUMMONSTERTYPES 8



>
>





|


|
>
|
>
>
|
|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
<

|







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
// monster.cpp: implements AI for single player monsters, currently client only

#import "Monster.h"

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"

static OFMutableArray<Monster *> *monsters;
static int nextmonster, spawnremain, numkilled, monstertotal, mtimestart;

@implementation Monster
+ (void)initialize
{
	monsters = [[OFMutableArray alloc] init];
}

+ (OFMutableArray<Monster *> *)monsters
{
	return monsters;
}

+ (instancetype)monsterWithType:(int)type
                            yaw:(int)yaw
                          state:(int)state
                        trigger:(int)trigger
                           move:(int)move
{
	return [[self alloc] initWithType:type
	                              yaw:yaw
	                            state:state
	                          trigger:trigger
	                             move:move];
}

VARF(skill, 1, 3, 10, conoutf(@"skill is now %d", skill));

// for savegames
+ (void)restoreAll

{
	for (Monster *monster in monsters)
		if (monster.state == CS_DEAD)
			numkilled++;
}

#define TOTMFREQ 13
#define NUMMONSTERTYPES 8

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
	    @"a hellpig", @"monster/hellpig" },
	{ GUN_ICEBALL, 12, 250, 1, 0, 10, 400, 6, 18, 18, S_PAINH, S_DEATHH,
	    @"a knight", @"monster/knight" },
	{ GUN_SLIMEBALL, 15, 100, 1, 0, 200, 400, 2, 13, 10, S_PAIND, S_DEATHD,
	    @"a goblin", @"monster/goblin" },
};


DynamicEntity *

basicmonster(int type, int yaw, int state, int trigger, int move)

{


	if (type >= NUMMONSTERTYPES) {
		conoutf(@"warning: unknown monster in spawn: %d", type);
		type = 0;
	}
	DynamicEntity *m = [DynamicEntity entity];
	struct monstertype *t = &monstertypes[(m.monsterType = type)];
	m.eyeHeight = 2.0f;
	m.aboveEye = 1.9f;
	m.radius *= t->bscale / 10.0f;
	m.eyeHeight *= t->bscale / 10.0f;
	m.aboveEye *= t->bscale / 10.0f;
	m.monsterState = state;

	if (state != M_SLEEP)
		spawnplayer(m);

	m.trigger = lastmillis + trigger;
	m.targetYaw = m.yaw = (float)yaw;
	m.move = move;
	m.enemy = player1;
	m.gunSelect = t->gun;
	m.maxSpeed = (float)t->speed;
	m.health = t->health;
	m.armour = 0;


	loopi(NUMGUNS) m.ammo[i] = 10000;

	m.pitch = 0;
	m.roll = 0;
	m.state = CS_ALIVE;
	m.anger = 0;
	m.name = t->name;



	if (monsters == nil)


		monsters = [[OFMutableArray alloc] init];


	[monsters addObject:m];






	return m;
}

void
spawnmonster() // spawn a random monster according to freq distribution in DMSP
{
	int n = rnd(TOTMFREQ), type;
	for (int i = 0;; i++) {
		if ((n -= monstertypes[i].freq) < 0) {
			type = i;
			break;
		}
	}
	basicmonster(type, rnd(360), M_SEARCH, 1000, 1);





}

// called after map start of when toggling edit mode to reset/spawn all
// monsters to initial state
void
monsterclear()
{
	[monsters removeAllObjects];

	numkilled = 0;
	monstertotal = 0;
	spawnremain = 0;

	if (m_dmsp) {
		nextmonster = mtimestart = lastmillis + 10000;
		monstertotal = spawnremain = gamemode < 0 ? skill * 10 : 0;
	} else if (m_classicsp) {
		mtimestart = lastmillis;

		for (Entity *e in ents) {
			if (e.type != MONSTER)
				continue;

			DynamicEntity *m =

			    basicmonster(e.attr2, e.attr1, M_SLEEP, 100, 0);


			m.origin = OFMakeVector3D(e.x, e.y, e.z);

			entinmap(m);
			monstertotal++;
		}
	}
}

// height-correct line of sight for monster shooting/seeing
bool
los(float lx, float ly, float lz, float bx, float by, float bz, OFVector3D *v)
{
	if (OUTBORD((int)lx, (int)ly) || OUTBORD((int)bx, (int)by))
		return false;
	float dx = bx - lx;
	float dy = by - ly;
	int steps = (int)(sqrt(dx * dx + dy * dy) / 0.9);







>
|
>
|
>

>
>




|
|
|
|
|
|
|
|
>

|
>
|
|
|
|
|
|
|
|
>
>
|
>
|
|
|
|
|

>
>
|
>
>
|

>
|
>
>
>
>
>

|


|









|
>
>
>
>
>


<
<
|
<


>



>










|
>
|
>
>

>







|







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
	    @"a hellpig", @"monster/hellpig" },
	{ GUN_ICEBALL, 12, 250, 1, 0, 10, 400, 6, 18, 18, S_PAINH, S_DEATHH,
	    @"a knight", @"monster/knight" },
	{ GUN_SLIMEBALL, 15, 100, 1, 0, 200, 400, 2, 13, 10, S_PAIND, S_DEATHD,
	    @"a goblin", @"monster/goblin" },
};

- (instancetype)initWithType:(int)type
                         yaw:(int)yaw
                       state:(int)state
                     trigger:(int)trigger
                        move:(int)move
{
	self = [super init];

	if (type >= NUMMONSTERTYPES) {
		conoutf(@"warning: unknown monster in spawn: %d", type);
		type = 0;
	}

	struct monstertype *t = &monstertypes[(self.monsterType = type)];
	self.eyeHeight = 2.0f;
	self.aboveEye = 1.9f;
	self.radius *= t->bscale / 10.0f;
	self.eyeHeight *= t->bscale / 10.0f;
	self.aboveEye *= t->bscale / 10.0f;
	self.monsterState = state;

	if (state != M_SLEEP)
		spawnplayer(self);

	self.trigger = lastmillis + trigger;
	self.targetYaw = self.yaw = (float)yaw;
	self.move = move;
	self.enemy = player1;
	self.gunSelect = t->gun;
	self.maxSpeed = (float)t->speed;
	self.health = t->health;
	self.armour = 0;

	for (size_t i = 0; i < NUMGUNS; i++)
		self.ammo[i] = 10000;

	self.pitch = 0;
	self.roll = 0;
	self.state = CS_ALIVE;
	self.anger = 0;
	self.name = t->name;

	return self;
}

- (id)copy
{
	Monster *copy = [super copy];

	copy->_monsterState = _monsterState;
	copy->_monsterType = _monsterType;
	copy->_enemy = _enemy;
	copy->_targetYaw = _targetYaw;
	copy->_trigger = _trigger;
	copy->_attackTarget = _attackTarget;
	copy->_anger = _anger;

	return copy;
}

static void
spawnmonster() // spawn a random monster according to freq distribution in DMSP
{
	int n = rnd(TOTMFREQ), type;
	for (int i = 0;; i++) {
		if ((n -= monstertypes[i].freq) < 0) {
			type = i;
			break;
		}
	}

	[monsters addObject:[Monster monsterWithType:type
	                                         yaw:rnd(360)
	                                       state:M_SEARCH
	                                     trigger:1000
	                                        move:1]];
}



+ (void)resetAll

{
	[monsters removeAllObjects];

	numkilled = 0;
	monstertotal = 0;
	spawnremain = 0;

	if (m_dmsp) {
		nextmonster = mtimestart = lastmillis + 10000;
		monstertotal = spawnremain = gamemode < 0 ? skill * 10 : 0;
	} else if (m_classicsp) {
		mtimestart = lastmillis;

		for (Entity *e in ents) {
			if (e.type != MONSTER)
				continue;

			Monster *m = [Monster monsterWithType:e.attr2
			                                  yaw:e.attr1
			                                state:M_SLEEP
			                              trigger:100
			                                 move:0];
			m.origin = OFMakeVector3D(e.x, e.y, e.z);
			[monsters addObject:m];
			entinmap(m);
			monstertotal++;
		}
	}
}

// height-correct line of sight for monster shooting/seeing
static bool
los(float lx, float ly, float lz, float bx, float by, float bz, OFVector3D *v)
{
	if (OUTBORD((int)lx, (int)ly) || OUTBORD((int)bx, (int)by))
		return false;
	float dx = bx - lx;
	float dy = by - ly;
	int steps = (int)(sqrt(dx * dx + dy * dy) / 0.9);
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
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

		x += dx / (float)steps;
		y += dy / (float)steps;
		i++;
	}
	return i >= steps;
}

bool
enemylos(DynamicEntity *m, OFVector3D *v)
{
	*v = m.origin;
	return los(m.origin.x, m.origin.y, m.origin.z, m.enemy.origin.x,
	    m.enemy.origin.y, m.enemy.origin.z, v);
}

// monster AI is sequenced using transitions: they are in a particular state
// where they execute a particular behaviour until the trigger time is hit, and
// then they reevaluate their situation based on the current state, the
// environment etc., and transition to the next state. Transition timeframes are
// parametrized by difficulty level (skill), faster transitions means quicker
// decision making means tougher AI.

// n = at skill 0, n/2 = at skill 10, r = added random factor
void
transition(DynamicEntity *m, int state, int moving, int n, int r)
{
	m.monsterState = state;
	m.move = moving;
	n = n * 130 / 100;
	m.trigger = lastmillis + n - skill * (n / 16) + rnd(r + 1);
}

void
normalise(DynamicEntity *m, float angle)
{
	while (m.yaw < angle - 180.0f)
		m.yaw += 360.0f;
	while (m.yaw > angle + 180.0f)
		m.yaw -= 360.0f;
}

// main AI thinking routine, called every frame for every monster
void
monsteraction(DynamicEntity *m)
{
	if (m.enemy.state == CS_DEAD) {
		m.enemy = player1;
		m.anger = 0;
	}
	normalise(m, m.targetYaw);
	// slowly turn monster towards his target
	if (m.targetYaw > m.yaw) {
		m.yaw += curtime * 0.5f;
		if (m.targetYaw < m.yaw)
			m.yaw = m.targetYaw;
	} else {
		m.yaw -= curtime * 0.5f;
		if (m.targetYaw > m.yaw)
			m.yaw = m.targetYaw;
	}

	vdist(disttoenemy, vectoenemy, m.origin, m.enemy.origin);

	m.pitch = atan2(m.enemy.origin.z - m.origin.z, disttoenemy) * 180 / PI;

	// special case: if we run into scenery
	if (m.blocked) {
		m.blocked = false;
		// try to jump over obstackle (rare)
		if (!rnd(20000 / monstertypes[m.monsterType].speed))
			m.jumpNext = true;
		// search for a way around (common)
		else if (m.trigger < lastmillis &&
		    (m.monsterState != M_HOME || !rnd(5))) {
			// patented "random walk" AI pathfinding (tm) ;)
			m.targetYaw += 180 + rnd(180);
			transition(m, M_SEARCH, 1, 400, 1000);



		}
	}

	float enemyYaw = -(float)atan2(m.enemy.origin.x - m.origin.x,
	                     m.enemy.origin.y - m.origin.y) /
	        PI * 180 +
	    180;

	switch (m.monsterState) {
	case M_PAIN:
	case M_ATTACKING:
	case M_SEARCH:
		if (m.trigger < lastmillis)
			transition(m, M_HOME, 1, 100, 200);
		break;

	case M_SLEEP: // state classic sp monster start in, wait for visual
	              // contact
	{
		OFVector3D target;
		if (editmode || !enemylos(m, &target))
			return; // skip running physics
		normalise(m, enemyYaw);
		float angle = (float)fabs(enemyYaw - m.yaw);
		if (disttoenemy < 8 // the better the angle to the player, the
		                    // further the monster can see/hear
		    || (disttoenemy < 16 && angle < 135) ||
		    (disttoenemy < 32 && angle < 90) ||
		    (disttoenemy < 64 && angle < 45) || angle < 10) {
			transition(m, M_HOME, 1, 500, 200);
			OFVector3D loc = m.origin;
			playsound(S_GRUNT1 + rnd(2), &loc);
		}
		break;
	}

	case M_AIMING:
		// this state is the delay between wanting to shoot and actually
		// firing
		if (m.trigger < lastmillis) {
			m.lastAction = 0;
			m.attacking = true;
			OFVector3D attackTarget = m.attackTarget;
			shoot(m, &attackTarget);
			transition(m, M_ATTACKING, 0, 600, 0);



		}
		break;

	case M_HOME:
		// monster has visual contact, heads straight for player and
		// may want to shoot at any time
		m.targetYaw = enemyYaw;
		if (m.trigger < lastmillis) {
			OFVector3D target;
			if (!enemylos(m, &target)) {
				// no visual contact anymore, let monster get
				// as close as possible then search for player
				transition(m, M_HOME, 1, 800, 500);



			} else {
				// the closer the monster is the more likely he
				// wants to shoot
				if (!rnd((int)disttoenemy / 3 + 1) &&
				    m.enemy.state == CS_ALIVE) {
					// get ready to fire
					m.attackTarget = target;
					transition(m, M_AIMING, 0,
					    monstertypes[m.monsterType].lag,



					    10);
				} else
					// track player some more
					transition(m, M_HOME, 1,

					    monstertypes[m.monsterType].rate,



					    0);

			}
		}
		break;
	}

	moveplayer(m, 1, false); // use physics to move monster
}

void
monsterpain(DynamicEntity *m, int damage, DynamicEntity *d)
{
	// a monster hit us


	if (d.monsterState) {
		// guard for RL guys shooting themselves :)
		if (m != d) {
			// don't attack straight away, first get angry
			m.anger++;
			int anger = m.monsterType == d.monsterType ? m.anger / 2

			                                           : m.anger;
			if (anger >= monstertypes[m.monsterType].loyalty)
				// monster infight if very angry
				m.enemy = d;
		}
	} else {
		// player hit us
		m.anger = 0;
		m.enemy = d;
	}

	// in this state monster won't attack


	transition(m, M_PAIN, 0, monstertypes[m.monsterType].pain, 200);


	if ((m.health -= damage) <= 0) {
		m.state = CS_DEAD;
		m.lastAction = lastmillis;
		numkilled++;
		player1.frags = numkilled;
		OFVector3D loc = m.origin;
		playsound(monstertypes[m.monsterType].diesound, &loc);
		int remain = monstertotal - numkilled;
		if (remain > 0 && remain <= 5)
			conoutf(@"only %d monster(s) remaining", remain);
	} else {
		OFVector3D loc = m.origin;
		playsound(monstertypes[m.monsterType].painsound, &loc);
	}
}

void
endsp(bool allkilled)
{
	conoutf(allkilled ? @"you have cleared the map!"
	                  : @"you reached the exit!");
	conoutf(@"score: %d kills in %d seconds", numkilled,
	    (lastmillis - mtimestart) / 1000);
	monstertotal = 0;
	startintermission();
}

void
monsterthink()
{
	if (m_dmsp && spawnremain && lastmillis > nextmonster) {
		if (spawnremain-- == monstertotal)
			conoutf(@"The invasion has begun!");
		nextmonster = lastmillis + 1000;
		spawnmonster();
	}

	if (monstertotal && !spawnremain && numkilled == monstertotal)
		endsp(true);

	// equivalent of player entity touch, but only teleports are used
	[ents enumerateObjectsUsingBlock:^(Entity *e, size_t i, bool *stop) {
		if (e.type != TELEPORT)
			return;

		if (OUTBORD(e.x, e.y))
			return;

		OFVector3D v =
		    OFMakeVector3D(e.x, e.y, (float)S(e.x, e.y)->floor);
		for (DynamicEntity *monster in monsters) {
			if (monster.state == CS_DEAD) {
				if (lastmillis - monster.lastAction < 2000) {
					monster.move = 0;
					moveplayer(monster, 1, false);
				}
			} else {
				v.z += monster.eyeHeight;
				vdist(dist, t, monster.origin, v);
				v.z -= monster.eyeHeight;

				if (dist < 4)
					teleport(i, monster);
			}
		}
	}];

	for (DynamicEntity *monster in monsters)
		if (monster.state == CS_ALIVE)
			monsteraction(monster);
}

void
monsterrender()
{
	for (DynamicEntity *monster in monsters)
		renderclient(monster, false,
		    monstertypes[monster.monsterType].mdlname,
		    monster.monsterType == 5,
		    monstertypes[monster.monsterType].mscale / 10.0f);
}








|
|














<
|

|
|

|


|
<

|
|
|
|



|
<

|
|
|

|

|
|
|
|

|
|
|


|
>
|


|
|

|
|

|
|

|
|
>
>
>



|
|



|



|
|






|

|
|





|
|








|
|
|
|
|
|
>
>
>






|
|

|


|
>
>
>




|

|
|
|
>
>
>
|
|

<
>
|
>
>
>
|
>





|


<
|


>
>
|

|

|
|
>
|
|

|



|
|

>

>
>
|
>
>
|
|
|


|
|




|
|



|
<

|







|
<









|











|
















|

|


|
<

|





>
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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

487
488
489
490
491
492
493
494
		x += dx / (float)steps;
		y += dy / (float)steps;
		i++;
	}
	return i >= steps;
}

static bool
enemylos(Monster *m, OFVector3D *v)
{
	*v = m.origin;
	return los(m.origin.x, m.origin.y, m.origin.z, m.enemy.origin.x,
	    m.enemy.origin.y, m.enemy.origin.z, v);
}

// monster AI is sequenced using transitions: they are in a particular state
// where they execute a particular behaviour until the trigger time is hit, and
// then they reevaluate their situation based on the current state, the
// environment etc., and transition to the next state. Transition timeframes are
// parametrized by difficulty level (skill), faster transitions means quicker
// decision making means tougher AI.

// n = at skill 0, n/2 = at skill 10, r = added random factor

- (void)transitionWithState:(int)state moving:(int)moving n:(int)n r:(int)r
{
	self.monsterState = state;
	self.move = moving;
	n = n * 130 / 100;
	self.trigger = lastmillis + n - skill * (n / 16) + rnd(r + 1);
}

- (void)normalizeWithAngle:(float)angle

{
	while (self.yaw < angle - 180.0f)
		self.yaw += 360.0f;
	while (self.yaw > angle + 180.0f)
		self.yaw -= 360.0f;
}

// main AI thinking routine, called every frame for every monster
- (void)performAction

{
	if (self.enemy.state == CS_DEAD) {
		self.enemy = player1;
		self.anger = 0;
	}
	[self normalizeWithAngle:self.targetYaw];
	// slowly turn monster towards his target
	if (self.targetYaw > self.yaw) {
		self.yaw += curtime * 0.5f;
		if (self.targetYaw < self.yaw)
			self.yaw = self.targetYaw;
	} else {
		self.yaw -= curtime * 0.5f;
		if (self.targetYaw > self.yaw)
			self.yaw = self.targetYaw;
	}

	vdist(disttoenemy, vectoenemy, self.origin, self.enemy.origin);
	self.pitch =
	    atan2(self.enemy.origin.z - self.origin.z, disttoenemy) * 180 / PI;

	// special case: if we run into scenery
	if (self.blocked) {
		self.blocked = false;
		// try to jump over obstackle (rare)
		if (!rnd(20000 / monstertypes[self.monsterType].speed))
			self.jumpNext = true;
		// search for a way around (common)
		else if (self.trigger < lastmillis &&
		    (self.monsterState != M_HOME || !rnd(5))) {
			// patented "random walk" AI pathfinding (tm) ;)
			self.targetYaw += 180 + rnd(180);
			[self transitionWithState:M_SEARCH
			                   moving:1
			                        n:400
			                        r:1000];
		}
	}

	float enemyYaw = -(float)atan2(self.enemy.origin.x - self.origin.x,
	                     self.enemy.origin.y - self.origin.y) /
	        PI * 180 +
	    180;

	switch (self.monsterState) {
	case M_PAIN:
	case M_ATTACKING:
	case M_SEARCH:
		if (self.trigger < lastmillis)
			[self transitionWithState:M_HOME moving:1 n:100 r:200];
		break;

	case M_SLEEP: // state classic sp monster start in, wait for visual
	              // contact
	{
		OFVector3D target;
		if (editmode || !enemylos(self, &target))
			return; // skip running physics
		[self normalizeWithAngle:enemyYaw];
		float angle = (float)fabs(enemyYaw - self.yaw);
		if (disttoenemy < 8 // the better the angle to the player, the
		                    // further the monster can see/hear
		    || (disttoenemy < 16 && angle < 135) ||
		    (disttoenemy < 32 && angle < 90) ||
		    (disttoenemy < 64 && angle < 45) || angle < 10) {
			[self transitionWithState:M_HOME moving:1 n:500 r:200];
			OFVector3D loc = self.origin;
			playsound(S_GRUNT1 + rnd(2), &loc);
		}
		break;
	}

	case M_AIMING:
		// this state is the delay between wanting to shoot and actually
		// firing
		if (self.trigger < lastmillis) {
			self.lastAction = 0;
			self.attacking = true;
			OFVector3D attackTarget = self.attackTarget;
			shoot(self, &attackTarget);
			[self transitionWithState:M_ATTACKING
			                   moving:0
			                        n:600
			                        r:0];
		}
		break;

	case M_HOME:
		// monster has visual contact, heads straight for player and
		// may want to shoot at any time
		self.targetYaw = enemyYaw;
		if (self.trigger < lastmillis) {
			OFVector3D target;
			if (!enemylos(self, &target)) {
				// no visual contact anymore, let monster get
				// as close as possible then search for player
				[self transitionWithState:M_HOME
				                   moving:1
				                        n:800
				                        r:500];
			} else {
				// the closer the monster is the more likely he
				// wants to shoot
				if (!rnd((int)disttoenemy / 3 + 1) &&
				    self.enemy.state == CS_ALIVE) {
					// get ready to fire
					self.attackTarget = target;
					int n =
					    monstertypes[self.monsterType].lag;
					[self transitionWithState:M_AIMING
					                   moving:0
					                        n:n
					                        r:10];
				} else {
					// track player some more

					int n =
					    monstertypes[self.monsterType].rate;
					[self transitionWithState:M_HOME
					                   moving:1
					                        n:n
					                        r:0];
				}
			}
		}
		break;
	}

	moveplayer(self, 1, false); // use physics to move monster
}


- (void)incurDamage:(int)damage fromEntity:(__kindof DynamicEntity *)d
{
	// a monster hit us
	if ([d isKindOfClass:Monster.class]) {
		Monster *m = (Monster *)d;

		// guard for RL guys shooting themselves :)
		if (self != m) {
			// don't attack straight away, first get angry
			self.anger++;
			int anger =
			    (self.monsterType == m.monsterType ? self.anger / 2
			                                       : self.anger);
			if (anger >= monstertypes[self.monsterType].loyalty)
				// monster infight if very angry
				self.enemy = m;
		}
	} else {
		// player hit us
		self.anger = 0;
		self.enemy = d;
	}

	// in this state monster won't attack
	[self transitionWithState:M_PAIN
	                   moving:0
	                        n:monstertypes[self.monsterType].pain
	                        r:200];

	if ((self.health -= damage) <= 0) {
		self.state = CS_DEAD;
		self.lastAction = lastmillis;
		numkilled++;
		player1.frags = numkilled;
		OFVector3D loc = self.origin;
		playsound(monstertypes[self.monsterType].diesound, &loc);
		int remain = monstertotal - numkilled;
		if (remain > 0 && remain <= 5)
			conoutf(@"only %d monster(s) remaining", remain);
	} else {
		OFVector3D loc = self.origin;
		playsound(monstertypes[self.monsterType].painsound, &loc);
	}
}

+ (void)endSinglePlayerWithAllKilled:(bool)allKilled

{
	conoutf(allKilled ? @"you have cleared the map!"
	                  : @"you reached the exit!");
	conoutf(@"score: %d kills in %d seconds", numkilled,
	    (lastmillis - mtimestart) / 1000);
	monstertotal = 0;
	startintermission();
}

+ (void)thinkAll

{
	if (m_dmsp && spawnremain && lastmillis > nextmonster) {
		if (spawnremain-- == monstertotal)
			conoutf(@"The invasion has begun!");
		nextmonster = lastmillis + 1000;
		spawnmonster();
	}

	if (monstertotal && !spawnremain && numkilled == monstertotal)
		[self endSinglePlayerWithAllKilled:true];

	// equivalent of player entity touch, but only teleports are used
	[ents enumerateObjectsUsingBlock:^(Entity *e, size_t i, bool *stop) {
		if (e.type != TELEPORT)
			return;

		if (OUTBORD(e.x, e.y))
			return;

		OFVector3D v =
		    OFMakeVector3D(e.x, e.y, (float)S(e.x, e.y)->floor);
		for (Monster *monster in monsters) {
			if (monster.state == CS_DEAD) {
				if (lastmillis - monster.lastAction < 2000) {
					monster.move = 0;
					moveplayer(monster, 1, false);
				}
			} else {
				v.z += monster.eyeHeight;
				vdist(dist, t, monster.origin, v);
				v.z -= monster.eyeHeight;

				if (dist < 4)
					teleport(i, monster);
			}
		}
	}];

	for (Monster *monster in monsters)
		if (monster.state == CS_ALIVE)
			[monster performAction];
}

+ (void)renderAll

{
	for (Monster *monster in monsters)
		renderclient(monster, false,
		    monstertypes[monster.monsterType].mdlname,
		    monster.monsterType == 5,
		    monstertypes[monster.monsterType].mscale / 10.0f);
}
@end

Modified src/clientextras.m from [ab4fb7df43] to [40507de7a2].

1
2
3
4
5

6
7
8
9
10
11
12
// clientextras.cpp: stuff that didn't fit in client.cpp or clientgame.cpp :)

#include "cube.h"

#import "DynamicEntity.h"


// render players & monsters
// very messy ad-hoc handling of animation frames, should be made more
// configurable

//              D    D    D    D'   D    D    D    D'   A   A'  P   P'  I   I'
//              R,  R'  E    L    J   J'





>







1
2
3
4
5
6
7
8
9
10
11
12
13
// clientextras.cpp: stuff that didn't fit in client.cpp or clientgame.cpp :)

#include "cube.h"

#import "DynamicEntity.h"
#import "Monster.h"

// render players & monsters
// very messy ad-hoc handling of animation frames, should be made more
// configurable

//              D    D    D    D'   D    D    D    D'   A   A'  P   P'  I   I'
//              R,  R'  E    L    J   J'
49
50
51
52
53
54
55

56
57

58
59
60
61
62
63
64
65
		// mdl = (((int)d>>6)&1)+1;
		// mz = d.o.z-d.eyeHeight+0.2f;
		// scale = 1.2f;
	} else if (d.state == CS_EDITING) {
		n = 16;
	} else if (d.state == CS_LAGGED) {
		n = 17;

	} else if (d.monsterState == M_ATTACKING) {
		n = 8;

	} else if (d.monsterState == M_PAIN) {
		n = 10;
	} else if ((!d.move && !d.strafe) || !d.moving) {
		n = 12;
	} else if (!d.onFloor && d.timeInAir > 100) {
		n = 18;
	} else {
		n = 14;







>
|

>
|







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
		// mdl = (((int)d>>6)&1)+1;
		// mz = d.o.z-d.eyeHeight+0.2f;
		// scale = 1.2f;
	} else if (d.state == CS_EDITING) {
		n = 16;
	} else if (d.state == CS_LAGGED) {
		n = 17;
	} else if ([d isKindOfClass:Monster.class] &&
	    ((Monster *)d).monsterState == M_ATTACKING) {
		n = 8;
	} else if ([d isKindOfClass:Monster.class] &&
	    ((Monster *)d).monsterState == M_PAIN) {
		n = 10;
	} else if ((!d.move && !d.strafe) || !d.moving) {
		n = 12;
	} else if (!d.onFloor && d.timeInAir > 100) {
		n = 18;
	} else {
		n = 14;

Modified src/clientgame.m from [f7cff8c331] to [d0a68a37a3].

1
2
3
4
5
6

7
8
9
10
11
12
13
// clientgame.cpp: core game related stuff

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"

#import "OFString+Cube.h"

int nextmode = 0; // nextmode becomes gamemode after next map load
VAR(gamemode, 1, 0, 0);

static void
mode(int n)






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
// clientgame.cpp: core game related stuff

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"
#import "Monster.h"
#import "OFString+Cube.h"

int nextmode = 0; // nextmode becomes gamemode after next map load
VAR(gamemode, 1, 0, 0);

static void
mode(int n)
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
				shoot(player1, &worldpos);
			// do this first, so we have most accurate information
			// when our player moves
			gets2c();
		}
		otherplayers();
		if (!demoplayback) {
			monsterthink();
			if (player1.state == CS_DEAD) {
				if (lastmillis - player1.lastAction < 2000) {
					player1.move = player1.strafe = 0;
					moveplayer(player1, 10, false);
				} else if (!m_arena && !m_sp &&
				    lastmillis - player1.lastAction > 10000)
					respawn();







|







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
				shoot(player1, &worldpos);
			// do this first, so we have most accurate information
			// when our player moves
			gets2c();
		}
		otherplayers();
		if (!demoplayback) {
			[Monster thinkAll];
			if (player1.state == CS_DEAD) {
				if (lastmillis - player1.lastAction < 2000) {
					player1.move = player1.strafe = 0;
					moveplayer(player1, 10, false);
				} else if (!m_arena && !m_sp &&
				    lastmillis - player1.lastAction > 10000)
					respawn();
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
startmap(OFString *name) // called just after a map load
{
	if (netmapstart() && m_sp) {
		gamemode = 0;
		conoutf(@"coop sp not supported yet");
	}
	sleepwait = 0;
	monsterclear();
	projreset();
	spawncycle = -1;
	spawnplayer(player1);
	player1.frags = 0;
	for (id player in players)
		if (player != [OFNull null])
			[player setFrags:0];







|







414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
startmap(OFString *name) // called just after a map load
{
	if (netmapstart() && m_sp) {
		gamemode = 0;
		conoutf(@"coop sp not supported yet");
	}
	sleepwait = 0;
	[Monster resetAll];
	projreset();
	spawncycle = -1;
	spawnplayer(player1);
	player1.frags = 0;
	for (id player in players)
		if (player != [OFNull null])
			[player setFrags:0];

Modified src/cube.h from [d202a9fdbd] to [0e2553a48d].

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
};

// bump if dynent/netprotocol changes or any other savegame/demo data
#define SAVEGAMEVERSION 4

enum { A_BLUE, A_GREEN, A_YELLOW }; // armour types... take 20/40/60 % off
enum {
	M_NONE = 0,
	M_SEARCH,
	M_HOME,
	M_ATTACKING,
	M_PAIN,
	M_SLEEP,
	M_AIMING
}; // monster states







<







122
123
124
125
126
127
128

129
130
131
132
133
134
135
};

// bump if dynent/netprotocol changes or any other savegame/demo data
#define SAVEGAMEVERSION 4

enum { A_BLUE, A_GREEN, A_YELLOW }; // armour types... take 20/40/60 % off
enum {

	M_SEARCH,
	M_HOME,
	M_ATTACKING,
	M_PAIN,
	M_SLEEP,
	M_AIMING
}; // monster states

Modified src/editing.m from [d8eb643406] to [85210314d4].

1
2
3
4
5
6

7
8
9
10
11
12
13
// editing.cpp: most map editing commands go here, entity editing commands are
// in world.cpp

#include "cube.h"

#import "DynamicEntity.h"

#import "OFString+Cube.h"

bool editmode = false;

// the current selection, used by almost all editing commands
// invariant: all code assumes that these are kept inside MINBORD distance of
// the edge of the map






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
// editing.cpp: most map editing commands go here, entity editing commands are
// in world.cpp

#include "cube.h"

#import "DynamicEntity.h"
#import "Monster.h"
#import "OFString+Cube.h"

bool editmode = false;

// the current selection, used by almost all editing commands
// invariant: all code assumes that these are kept inside MINBORD distance of
// the edge of the map
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		settagareas();     // reset triggers to allow quick playtesting
		entinmap(player1); // find spawn closest to current floating pos
	} else {
		resettagareas(); // clear trigger areas to allow them to be
		                 // edited
		player1.health = 100;
		if (m_classicsp)
			monsterclear(); // all monsters back at their spawns for
			                // editing
		projreset();
	}
	Cube.sharedInstance.repeatsKeys = editmode;
	selset = false;
	editing = editmode;
}
COMMANDN(edittoggle, toggleedit, ARG_NONE)







|
|







65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
		settagareas();     // reset triggers to allow quick playtesting
		entinmap(player1); // find spawn closest to current floating pos
	} else {
		resettagareas(); // clear trigger areas to allow them to be
		                 // edited
		player1.health = 100;
		if (m_classicsp)
			// all monsters back at their spawns for editing
			[Monster resetAll];
		projreset();
	}
	Cube.sharedInstance.repeatsKeys = editmode;
	selset = false;
	editing = editmode;
}
COMMANDN(edittoggle, toggleedit, ARG_NONE)

Modified src/meson.build from [d366589544] to [7b26dc3ecb].

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
    'Entity.m',
    'Identifier.m',
    'KeyMapping.m',
    'MD2.m',
    'MapModelInfo.m',
    'Menu.m',
    'MenuItem.m',

    'OFString+Cube.m',
    'Projectile.m',
    'ResolverResult.m',
    'ResolverThread.m',
    'ServerEntity.m',
    'ServerInfo.m',
    'Variable.m',
    'clients.m',
    'clientextras.m',
    'clientgame.m',
    'clients2c.m',
    'commands.m',
    'console.m',
    'editing.m',
    'entities.m',
    'init.m',
    'menus.m',
    'monster.m',
    'physics.m',
    'rendercubes.m',
    'renderextras.m',
    'rendergl.m',
    'rendermd2.m',
    'renderparticles.m',
    'rendertext.m',







>

















<







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
    'Entity.m',
    'Identifier.m',
    'KeyMapping.m',
    'MD2.m',
    'MapModelInfo.m',
    'Menu.m',
    'MenuItem.m',
    'Monster.m',
    'OFString+Cube.m',
    'Projectile.m',
    'ResolverResult.m',
    'ResolverThread.m',
    'ServerEntity.m',
    'ServerInfo.m',
    'Variable.m',
    'clients.m',
    'clientextras.m',
    'clientgame.m',
    'clients2c.m',
    'commands.m',
    'console.m',
    'editing.m',
    'entities.m',
    'init.m',
    'menus.m',

    'physics.m',
    'rendercubes.m',
    'renderextras.m',
    'rendergl.m',
    'rendermd2.m',
    'renderparticles.m',
    'rendertext.m',

Modified src/physics.m from [bb6edf173a] to [6e6b41ef3d].

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
// physics.cpp: no physics books were hurt nor consulted in the construction of
// this code. All physics computations and constants were invented on the fly
// and simply tweaked until they "felt right", and have no basis in reality.
// Collision detection is simplistic but very robust (uses discrete steps at
// fixed fps).

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"
#import "MapModelInfo.h"


// collide with player or monster
static bool
plcollide(
    DynamicEntity *d, DynamicEntity *o, float *headspace, float *hi, float *lo)
{
	if (o.state != CS_ALIVE)
		return true;

	const float r = o.radius + d.radius;
	if (fabs(o.origin.x - d.origin.x) < r &&
	    fabs(o.origin.y - d.origin.y) < r) {
		if (d.origin.z - d.eyeHeight < o.origin.z - o.eyeHeight) {
			if (o.origin.z - o.eyeHeight < *hi)
				*hi = o.origin.z - o.eyeHeight - 1;
		} else if (o.origin.z + o.aboveEye > *lo)
			*lo = o.origin.z + o.aboveEye + 1;

		if (fabs(o.origin.z - d.origin.z) < o.aboveEye + d.eyeHeight)
			return false;
		if (d.monsterState)
			return false; // hack
			              //
		*headspace = d.origin.z - o.origin.z - o.aboveEye - d.eyeHeight;
		if (*headspace < 0)
			*headspace = 10;
	}

	return true;
}











>




















|

|







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
// physics.cpp: no physics books were hurt nor consulted in the construction of
// this code. All physics computations and constants were invented on the fly
// and simply tweaked until they "felt right", and have no basis in reality.
// Collision detection is simplistic but very robust (uses discrete steps at
// fixed fps).

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"
#import "MapModelInfo.h"
#import "Monster.h"

// collide with player or monster
static bool
plcollide(
    DynamicEntity *d, DynamicEntity *o, float *headspace, float *hi, float *lo)
{
	if (o.state != CS_ALIVE)
		return true;

	const float r = o.radius + d.radius;
	if (fabs(o.origin.x - d.origin.x) < r &&
	    fabs(o.origin.y - d.origin.y) < r) {
		if (d.origin.z - d.eyeHeight < o.origin.z - o.eyeHeight) {
			if (o.origin.z - o.eyeHeight < *hi)
				*hi = o.origin.z - o.eyeHeight - 1;
		} else if (o.origin.z + o.aboveEye > *lo)
			*lo = o.origin.z + o.aboveEye + 1;

		if (fabs(o.origin.z - d.origin.z) < o.aboveEye + d.eyeHeight)
			return false;
		if ([d isKindOfClass:Monster.class])
			return false; // hack

		*headspace = d.origin.z - o.origin.z - o.aboveEye - d.eyeHeight;
		if (*headspace < 0)
			*headspace = 10;
	}

	return true;
}
101
102
103
104
105
106
107
108

109
110
111
112
113
114
115
116
117
	const float fy2 = d.origin.y + d.radius;
	const int x1 = fast_f2nat(fx1);
	const int y1 = fast_f2nat(fy1);
	const int x2 = fast_f2nat(fx2);
	const int y2 = fast_f2nat(fy2);
	float hi = 127, lo = -128;
	// big monsters are afraid of heights, unless angry :)
	float minfloor = (d.monsterState && !spawn && d.health > 100)

	    ? d.origin.z - d.eyeHeight - 4.5f
	    : -1000.0f;

	for (int x = x1; x <= x2; x++) {
		for (int y = y1; y <= y2; y++) {
			// collide with map
			if (OUTBORD(x, y))
				return false;
			struct sqr *s = S(x, y);







|
>
|
|







102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	const float fy2 = d.origin.y + d.radius;
	const int x1 = fast_f2nat(fx1);
	const int y1 = fast_f2nat(fy1);
	const int x2 = fast_f2nat(fx2);
	const int y2 = fast_f2nat(fy2);
	float hi = 127, lo = -128;
	// big monsters are afraid of heights, unless angry :)
	float minfloor =
	    ([d isKindOfClass:Monster.class] && !spawn && d.health > 100
	            ? d.origin.z - d.eyeHeight - 4.5f
	            : -1000.0f);

	for (int x = x1; x <= x2; x++) {
		for (int y = y1; y <= y2; y++) {
			// collide with map
			if (OUTBORD(x, y))
				return false;
			struct sqr *s = S(x, y);
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

	if (d != player1)
		if (!plcollide(d, player1, &headspace, &hi, &lo))
			return false;

	// this loop can be a performance bottleneck with many monster on a slow
	// cpu, should replace with a blockmap but seems mostly fast enough
	for (DynamicEntity *monster in getmonsters())
		if (!vreject(d.origin, monster.origin, 7.0f) && d != monster &&
		    !plcollide(d, monster, &headspace, &hi, &lo))
			return false;

	headspace -= 0.01f;

	mmcollide(d, &hi, &lo); // collide with map models







|







181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

	if (d != player1)
		if (!plcollide(d, player1, &headspace, &hi, &lo))
			return false;

	// this loop can be a performance bottleneck with many monster on a slow
	// cpu, should replace with a blockmap but seems mostly fast enough
	for (Monster *monster in Monster.monsters)
		if (!vreject(d.origin, monster.origin, 7.0f) && d != monster &&
		    !plcollide(d, monster, &headspace, &hi, &lo))
			return false;

	headspace -= 0.01f;

	mmcollide(d, &hi, &lo); // collide with map models
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
				// correct water feel
				if (water)
					pl.velocity = OFMakeVector3D(
					    pl.velocity.x / 8,
					    pl.velocity.y / 8, pl.velocity.z);
				if (local)
					playsoundc(S_JUMP);
				else if (pl.monsterState) {
					OFVector3D loc = pl.origin;
					playsound(S_JUMP, &loc);
				}
			} else if (pl.timeInAir > 800) {
				// if we land after long time must have been a
				// high jump, make thud sound
				if (local)
					playsoundc(S_LAND);
				else if (pl.monsterState) {
					OFVector3D loc = pl.origin;
					playsound(S_LAND, &loc);
				}
			}

			pl.timeInAir = 0;
		} else







|








|







321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
				// correct water feel
				if (water)
					pl.velocity = OFMakeVector3D(
					    pl.velocity.x / 8,
					    pl.velocity.y / 8, pl.velocity.z);
				if (local)
					playsoundc(S_JUMP);
				else if ([pl isKindOfClass:Monster.class]) {
					OFVector3D loc = pl.origin;
					playsound(S_JUMP, &loc);
				}
			} else if (pl.timeInAir > 800) {
				// if we land after long time must have been a
				// high jump, make thud sound
				if (local)
					playsoundc(S_LAND);
				else if ([pl isKindOfClass:Monster.class]) {
					OFVector3D loc = pl.origin;
					playsound(S_LAND, &loc);
				}
			}

			pl.timeInAir = 0;
		} else

Modified src/protos.h from [1cd5f32081] to [7c4924e6fb].

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    DynamicEntity *d, bool local);
extern void createrays(const OFVector3D *from, const OFVector3D *to);
extern void moveprojectiles(float time);
extern void projreset();
extern OFString *playerincrosshair();
extern int reloadtime(int gun);

// monster
extern void monsterclear();
extern void restoremonsterstate();
extern void monsterthink();
extern void monsterrender();
extern OFArray<DynamicEntity *> *getmonsters();
extern void monsterpain(DynamicEntity *m, int damage, DynamicEntity *d);
extern void endsp(bool allkilled);

// entities
extern void initEntities();
extern void renderents();
extern void putitems(uchar **p);
extern void checkquad(int time);
extern void checkitems();
extern void realpickup(int n, DynamicEntity *d);







<
<
<
<
<
<
<
<
<







246
247
248
249
250
251
252









253
254
255
256
257
258
259
    DynamicEntity *d, bool local);
extern void createrays(const OFVector3D *from, const OFVector3D *to);
extern void moveprojectiles(float time);
extern void projreset();
extern OFString *playerincrosshair();
extern int reloadtime(int gun);










// entities
extern void initEntities();
extern void renderents();
extern void putitems(uchar **p);
extern void checkquad(int time);
extern void checkitems();
extern void realpickup(int n, DynamicEntity *d);

Modified src/rendergl.m from [09aa28380f] to [d2bce2bd10].

1
2
3
4
5

6
7
8
9
10
11
12
// rendergl.cpp: core opengl rendering stuff

#include "cube.h"

#import "DynamicEntity.h"

#import "OFString+Cube.h"

#ifdef DARWIN
# define GL_COMBINE_EXT GL_COMBINE_ARB
# define GL_COMBINE_RGB_EXT GL_COMBINE_RGB_ARB
# define GL_SOURCE0_RBG_EXT GL_SOURCE0_RGB_ARB
# define GL_SOURCE1_RBG_EXT GL_SOURCE1_RGB_ARB





>







1
2
3
4
5
6
7
8
9
10
11
12
13
// rendergl.cpp: core opengl rendering stuff

#include "cube.h"

#import "DynamicEntity.h"
#import "Monster.h"
#import "OFString+Cube.h"

#ifdef DARWIN
# define GL_COMBINE_EXT GL_COMBINE_ARB
# define GL_COMBINE_RGB_EXT GL_COMBINE_RGB_ARB
# define GL_SOURCE0_RBG_EXT GL_SOURCE0_RGB_ARB
# define GL_SOURCE1_RBG_EXT GL_SOURCE1_RGB_ARB
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
	overbright(2);

	renderstrips();

	xtraverts = 0;

	renderclients();
	monsterrender();

	renderentities();

	renderspheres(curtime);
	renderents();

	glDisable(GL_CULL_FACE);







|







475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
	overbright(2);

	renderstrips();

	xtraverts = 0;

	renderclients();
	[Monster renderAll];

	renderentities();

	renderspheres(curtime);
	renderents();

	glDisable(GL_CULL_FACE);

Modified src/savegamedemo.m from [3f6725d365] to [fc99f9e9f6].

1
2
3
4
5
6
7

8
9
10
11
12
13
14
// loading and saving of savegames & demos, dumps the spawn state of all
// mapents, the full state of all dynents (monsters + player)

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"


#ifdef OF_BIG_ENDIAN
static const int islittleendian = 0;
#else
static const int islittleendian = 1;
#endif








>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// loading and saving of savegames & demos, dumps the spawn state of all
// mapents, the full state of all dynents (monsters + player)

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"
#import "Monster.h"

#ifdef OF_BIG_ENDIAN
static const int islittleendian = 0;
#else
static const int islittleendian = 1;
#endif

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	    min(getclientmap().UTF8StringLength, _MAXDEFSTR - 1));
	gzwrite(f, map, _MAXDEFSTR);
	gzputi(gamemode);
	gzputi(ents.count);
	for (Entity *e in ents)
		gzputc(f, e.spawned);
	gzwrite(f, data.items, data.count);
	OFArray<DynamicEntity *> *monsters = getmonsters();
	gzputi(monsters.count);
	for (DynamicEntity *monster in monsters) {
		data = [monster dataBySerializing];
		gzwrite(f, data.items, data.count);
	}
	gzputi(players.count);
	for (id player in players) {
		gzput(player == [OFNull null]);
		data = [player dataBySerializing];







|

|







111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	    min(getclientmap().UTF8StringLength, _MAXDEFSTR - 1));
	gzwrite(f, map, _MAXDEFSTR);
	gzputi(gamemode);
	gzputi(ents.count);
	for (Entity *e in ents)
		gzputc(f, e.spawned);
	gzwrite(f, data.items, data.count);
	OFArray<Monster *> *monsters = Monster.monsters;
	gzputi(monsters.count);
	for (Monster *monster in monsters) {
		data = [monster dataBySerializing];
		gzwrite(f, data.items, data.count);
	}
	gzputi(players.count);
	for (id player in players) {
		gzput(player == [OFNull null]);
		data = [player dataBySerializing];
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
	    [OFMutableData dataWithCapacity:DynamicEntity.serializedSize];
	[data increaseCountBy:DynamicEntity.serializedSize];
	gzread(f, data.mutableItems, data.count);
	[player1 setFromSerializedData:data];
	player1.lastAction = lastmillis;

	int nmonsters = gzgeti();
	OFArray<DynamicEntity *> *monsters = getmonsters();
	if (nmonsters != monsters.count)
		return loadgameout();

	for (DynamicEntity *monster in monsters) {
		gzread(f, data.mutableItems, data.count);
		[monster setFromSerializedData:data];
		// lazy, could save id of enemy instead
		monster.enemy = player1;
		// also lazy, but no real noticable effect on game
		monster.lastAction = monster.trigger = lastmillis + 500;
		if (monster.state == CS_DEAD)
			monster.lastAction = 0;
	}
	restoremonsterstate();

	int nplayers = gzgeti();
	loopi(nplayers) if (!gzget())
	{
		DynamicEntity *d = getclient(i);
		assert(d);
		gzread(f, data.mutableItems, data.count);







|



|









|







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
	    [OFMutableData dataWithCapacity:DynamicEntity.serializedSize];
	[data increaseCountBy:DynamicEntity.serializedSize];
	gzread(f, data.mutableItems, data.count);
	[player1 setFromSerializedData:data];
	player1.lastAction = lastmillis;

	int nmonsters = gzgeti();
	OFArray<Monster *> *monsters = Monster.monsters;
	if (nmonsters != monsters.count)
		return loadgameout();

	for (Monster *monster in monsters) {
		gzread(f, data.mutableItems, data.count);
		[monster setFromSerializedData:data];
		// lazy, could save id of enemy instead
		monster.enemy = player1;
		// also lazy, but no real noticable effect on game
		monster.lastAction = monster.trigger = lastmillis + 500;
		if (monster.state == CS_DEAD)
			monster.lastAction = 0;
	}
	[Monster restoreAll];

	int nplayers = gzgeti();
	loopi(nplayers) if (!gzget())
	{
		DynamicEntity *d = getclient(i);
		assert(d);
		gzread(f, data.mutableItems, data.count);

Modified src/weapon.m from [af10bcd44b] to [6b111961c9].

1
2
3
4
5

6
7
8
9
10
11
12
// weapon.cpp: all shooting and effects code

#include "cube.h"

#import "DynamicEntity.h"

#import "OFString+Cube.h"
#import "Projectile.h"

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





>







1
2
3
4
5
6
7
8
9
10
11
12
13
// weapon.cpp: all shooting and effects code

#include "cube.h"

#import "DynamicEntity.h"
#import "Monster.h"
#import "OFString+Cube.h"
#import "Projectile.h"

static const int MONSTERDAMAGEFACTOR = 4;
#define SGRAYS 20
static const float SGSPREAD = 2;
static OFVector3D sg[SGRAYS];
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
		p.owner = owner;
		p.gun = gun;
		return;
	}
}

void
hit(int target, int damage, DynamicEntity *d, DynamicEntity *at)
{
	OFVector3D o = d.origin;
	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), &o);
	}
	particle_splash(3, damage, 1000, &o);
	demodamage(damage, &o);
}







|




|
|







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
		p.owner = owner;
		p.gun = gun;
		return;
	}
}

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);
}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

			if (player == [OFNull null])
				return;

			radialeffect(player, v, i, qdam, p.owner);
		}];

		[getmonsters() enumerateObjectsUsingBlock:^(
		    DynamicEntity *monster, size_t i, bool *stop) {
			if (i != notthismonster)
				radialeffect(monster, v, i, qdam, p.owner);
		}];
	}
}

static inline void







|
|







232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

			if (player == [OFNull null])
				return;

			radialeffect(player, v, i, qdam, p.owner);
		}];

		[Monster.monsters enumerateObjectsUsingBlock:^(
		    Monster *monster, size_t i, bool *stop) {
			if (i != notthismonster)
				radialeffect(monster, v, i, qdam, p.owner);
		}];
	}
}

static inline void
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
	for (size_t i = 0; i < MAXPROJ; i++) {
		Projectile *p = projs[i];

		if (!p.inuse)
			continue;

		int qdam = guns[p.gun].damage * (p.owner.quadMillis ? 4 : 1);
		if (p.owner.monsterState)
			qdam /= MONSTERDAMAGEFACTOR;
		vdist(dist, v, p.o, p.to);
		float dtime = dist * 1000 / p.speed;
		if (time > dtime)
			dtime = time;
		vmul(v, time / dtime);
		vadd(v, p.o);
		if (p.local) {
			for (id player in players)
				if (player != [OFNull null])
					projdamage(player, p, &v, i, -1, qdam);

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

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








|















|







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
	for (size_t i = 0; i < MAXPROJ; i++) {
		Projectile *p = projs[i];

		if (!p.inuse)
			continue;

		int qdam = guns[p.gun].damage * (p.owner.quadMillis ? 4 : 1);
		if ([p.owner isKindOfClass:Monster.class])
			qdam /= MONSTERDAMAGEFACTOR;
		vdist(dist, v, p.o, p.to);
		float dtime = dist * 1000 / p.speed;
		if (time > dtime)
			dtime = time;
		vmul(v, time / dtime);
		vadd(v, p.o);
		if (p.local) {
			for (id player in players)
				if (player != [OFNull null])
					projdamage(player, p, &v, i, -1, qdam);

			if (p.owner != player1)
				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);
		}
		if (p.inuse) {
			OFVector3D po = p.o;

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
		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);







|







332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		break;

	case GUN_RL:
	case GUN_FIREBALL:
	case GUN_ICEBALL:
	case GUN_SLIMEBALL:
		pspeed = guns[gun].projspeed;
		if ([d isKindOfClass:Monster.class])
			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);
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    DynamicEntity *d, int i)
{
	if (o.state != CS_ALIVE)
		return;
	int qdam = guns[d.gunSelect].damage;
	if (d.quadMillis)
		qdam *= 4;
	if (d.monsterState)
		qdam /= MONSTERDAMAGEFACTOR;
	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))







|







363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    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;
		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))
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
	}
	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)
		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);
	}];

	for (DynamicEntity *monster in getmonsters())
		if (monster != d)
			raydamage(monster, &from, &to, d, -2);

	if (d.monsterState)
		raydamage(player1, &from, &to, d, -1);
}







|













|



|


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
	}
	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 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);
	}];

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

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

Modified src/world.m from [023ca9b6df] to [97ecb02030].

1
2
3
4
5
6

7
8
9
10
11
12
13
// world.cpp: core map management stuff

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"


extern OFString *entnames[]; // lookup from map entities above to strings

struct sqr *world = NULL;
int sfactor, ssize, cubicsize, mipsize;

struct header hdr;






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
// world.cpp: core map management stuff

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"
#import "Monster.h"

extern OFString *entnames[]; // lookup from map entities above to strings

struct sqr *world = NULL;
int sfactor, ssize, cubicsize, mipsize;

struct header hdr;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	OFString *aliasname =
	    [OFString stringWithFormat:@"level_trigger_%d", tag];

	if (identexists(aliasname))
		execute(aliasname, true);

	if (type == 2)
		endsp(false);
}
COMMAND(trigger, ARG_2INT)

// main geometric mipmapping routine, recursively rebuild mipmaps within block
// b. tries to produce cube out of 4 lower level mips as well as possible, sets
// defer to 0 if mipped cube is a perfect mip, i.e. can be rendered at this mip
// level indistinguishable from its constituent cubes (saves considerable







|







76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	OFString *aliasname =
	    [OFString stringWithFormat:@"level_trigger_%d", tag];

	if (identexists(aliasname))
		execute(aliasname, true);

	if (type == 2)
		[Monster endSinglePlayerWithAllKilled:false];
}
COMMAND(trigger, ARG_2INT)

// main geometric mipmapping routine, recursively rebuild mipmaps within block
// b. tries to produce cube out of 4 lower level mips as well as possible, sets
// defer to 0 if mipped cube is a perfect mip, i.e. can be rendered at this mip
// level indistinguishable from its constituent cubes (saves considerable

Modified src/worldlight.m from [d596ebb5c3] to [3113d251d8].

1
2
3
4
5
6

7
8
9
10
11
12
13
// worldlight.cpp

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"


extern bool hasoverbright;

VAR(lightscale, 1, 4, 100);

// done in realtime, needs to be fast
void






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
// worldlight.cpp

#include "cube.h"

#import "DynamicEntity.h"
#import "Entity.h"
#import "Monster.h"

extern bool hasoverbright;

VAR(lightscale, 1, 4, 100);

// done in realtime, needs to be fast
void
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

void
dodynlight(const OFVector3D *vold, const OFVector3D *v, int reach, int strength,
    DynamicEntity *owner)
{
	if (!reach)
		reach = dynlight;
	if (owner.monsterState)
		reach = reach / 2;
	if (!reach)
		return;
	if (v->x < 0 || v->y < 0 || v->x > ssize || v->y > ssize)
		return;

	int creach = reach + 16; // dependant on lightray random offsets!







|







202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

void
dodynlight(const OFVector3D *vold, const OFVector3D *v, int reach, int strength,
    DynamicEntity *owner)
{
	if (!reach)
		reach = dynlight;
	if ([owner isKindOfClass:Monster.class])
		reach = reach / 2;
	if (!reach)
		return;
	if (v->x < 0 || v->y < 0 || v->x > ssize || v->y > ssize)
		return;

	int creach = reach + 16; // dependant on lightray random offsets!