Cube  Check-in [c38d75087b]

Overview
Comment:Convert all references to pointers in protos.h
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c38d75087b98b5b7a4c7963d119b8eb0bf6be2603bdf43a79e743300951a4d28
User & Date: js on 2025-03-20 20:11:37
Other Links: manifest | tags
Context
2025-03-20
20:38
Remove all default arguments from protos.h check-in: ef1d7e47f7 user: js tags: trunk
20:11
Convert all references to pointers in protos.h check-in: c38d75087b user: js tags: trunk
17:12
Remove last usage of vector check-in: 08c9d7b0fa user: js tags: trunk
Changes

Modified src/clientextras.mm from [6b3acabfcd] to [cdaa70e30e].

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	OFData *mapdata = readmap(mapname);
	if (mapdata == nil)
		return;
	ENetPacket *packet = enet_packet_create(
	    NULL, MAXTRANS + mapdata.count, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(p, SV_SENDMAP);
	sendstring(mapname, p);
	putint(p, mapdata.count);
	if (65535 - (p - start) < mapdata.count) {
		conoutf(@"map %@ is too large to send", mapname);
		enet_packet_destroy(packet);
		return;
	}
	memcpy(p, mapdata.items, mapdata.count);
	p += mapdata.count;







|
|
|







183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	OFData *mapdata = readmap(mapname);
	if (mapdata == nil)
		return;
	ENetPacket *packet = enet_packet_create(
	    NULL, MAXTRANS + mapdata.count, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(&p, SV_SENDMAP);
	sendstring(mapname, &p);
	putint(&p, mapdata.count);
	if (65535 - (p - start) < mapdata.count) {
		conoutf(@"map %@ is too large to send", mapname);
		enet_packet_destroy(packet);
		return;
	}
	memcpy(p, mapdata.items, mapdata.count);
	p += mapdata.count;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
void
getmap()
{
	ENetPacket *packet =
	    enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(p, SV_RECVMAP);
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	sendpackettoserv(packet);
	conoutf(@"requesting map from server...");
}

COMMAND(sendmap, ARG_1STR)
COMMAND(getmap, ARG_NONE)







|








211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
void
getmap()
{
	ENetPacket *packet =
	    enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(&p, SV_RECVMAP);
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	sendpackettoserv(packet);
	conoutf(@"requesting map from server...");
}

COMMAND(sendmap, ARG_1STR)
COMMAND(getmap, ARG_NONE)

Modified src/clientgame.mm from [a964a181aa] to [5aac2f8a02].

257
258
259
260
261
262
263
264
265

266
267

268
269
270
271
272
273
274
		checkquad(curtime);
		if (m_arena)
			arenarespawn();
		moveprojectiles((float)curtime);
		demoplaybackstep();
		if (!demoplayback) {
			if (getclientnum() >= 0)
				shoot(player1, worldpos); // only shoot when
				                          // connected to server

			gets2c(); // do this first, so we have most accurate
			          // information when our player moves

		}
		otherplayers();
		if (!demoplayback) {
			monsterthink();
			if (player1.state == CS_DEAD) {
				if (lastmillis - player1.lastaction < 2000) {
					player1.move = player1.strafe = 0;







<
|
>
|
|
>







257
258
259
260
261
262
263

264
265
266
267
268
269
270
271
272
273
274
275
		checkquad(curtime);
		if (m_arena)
			arenarespawn();
		moveprojectiles((float)curtime);
		demoplaybackstep();
		if (!demoplayback) {
			if (getclientnum() >= 0)

				// only shoot when connected to server
				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;

Modified src/clients.mm from [c63e414b28] to [9e4179b5d8].

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
	uchar *p = start + 2;
	bool serveriteminitdone = false;
	// suggest server to change map
	if (toservermap.length > 0) {
		// do this exclusively as map change may invalidate rest of
		// update
		packet->flags = ENET_PACKET_FLAG_RELIABLE;
		putint(p, SV_MAPCHANGE);
		sendstring(toservermap, p);
		toservermap = @"";
		putint(p, nextmode);
	} else {
		putint(p, SV_POS);
		putint(p, clientnum);
		// quantize coordinates to 1/16th of a cube, between 1 and 3
		// bytes
		putint(p, (int)(d.o.x * DMF));
		putint(p, (int)(d.o.y * DMF));
		putint(p, (int)(d.o.z * DMF));
		putint(p, (int)(d.yaw * DAF));
		putint(p, (int)(d.pitch * DAF));
		putint(p, (int)(d.roll * DAF));
		// quantize to 1/100, almost always 1 byte
		putint(p, (int)(d.vel.x * DVF));
		putint(p, (int)(d.vel.y * DVF));
		putint(p, (int)(d.vel.z * DVF));
		// pack rest in 1 byte: strafe:2, move:2, onfloor:1, state:3
		putint(p,
		    (d.strafe & 3) | ((d.move & 3) << 2) |
		        (((int)d.onfloor) << 4) |
		        ((editmode ? CS_EDITING : d.state) << 5));

		if (senditemstoserver) {
			packet->flags = ENET_PACKET_FLAG_RELIABLE;
			putint(p, SV_ITEMLIST);
			if (!m_noitems)
				putitems(p);
			putint(p, -1);
			senditemstoserver = false;
			serveriteminitdone = true;
		}
		// player chat, not flood protected for now
		if (ctext.length > 0) {
			packet->flags = ENET_PACKET_FLAG_RELIABLE;
			putint(p, SV_TEXT);
			sendstring(ctext, p);
			ctext = @"";
		}
		// tell other clients who I am
		if (!c2sinit) {
			packet->flags = ENET_PACKET_FLAG_RELIABLE;
			c2sinit = true;
			putint(p, SV_INITC2S);
			sendstring(player1.name, p);
			sendstring(player1.team, p);
			putint(p, player1.lifesequence);
		}
		for (OFData *msg in messages) {
			// send messages collected during the previous frames
			if (*(int *)[msg itemAtIndex:1])
				packet->flags = ENET_PACKET_FLAG_RELIABLE;
			loopi(*(int *)[msg itemAtIndex:0])
			    putint(p, *(int *)[msg itemAtIndex:i + 2]);
		}
		[messages removeAllObjects];
		if (lastmillis - lastping > 250) {
			putint(p, SV_PING);
			putint(p, lastmillis);
			lastping = lastmillis;
		}
	}
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	incomingdemodata(start, p - start, true);
	if (clienthost) {







|
|

|

|
|


|
|
|
|
|
|

|
|
|

|






|

|
|






|
|






|
|
|
|






|



|
|







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
	uchar *p = start + 2;
	bool serveriteminitdone = false;
	// suggest server to change map
	if (toservermap.length > 0) {
		// do this exclusively as map change may invalidate rest of
		// update
		packet->flags = ENET_PACKET_FLAG_RELIABLE;
		putint(&p, SV_MAPCHANGE);
		sendstring(toservermap, &p);
		toservermap = @"";
		putint(&p, nextmode);
	} else {
		putint(&p, SV_POS);
		putint(&p, clientnum);
		// quantize coordinates to 1/16th of a cube, between 1 and 3
		// bytes
		putint(&p, (int)(d.o.x * DMF));
		putint(&p, (int)(d.o.y * DMF));
		putint(&p, (int)(d.o.z * DMF));
		putint(&p, (int)(d.yaw * DAF));
		putint(&p, (int)(d.pitch * DAF));
		putint(&p, (int)(d.roll * DAF));
		// quantize to 1/100, almost always 1 byte
		putint(&p, (int)(d.vel.x * DVF));
		putint(&p, (int)(d.vel.y * DVF));
		putint(&p, (int)(d.vel.z * DVF));
		// pack rest in 1 byte: strafe:2, move:2, onfloor:1, state:3
		putint(&p,
		    (d.strafe & 3) | ((d.move & 3) << 2) |
		        (((int)d.onfloor) << 4) |
		        ((editmode ? CS_EDITING : d.state) << 5));

		if (senditemstoserver) {
			packet->flags = ENET_PACKET_FLAG_RELIABLE;
			putint(&p, SV_ITEMLIST);
			if (!m_noitems)
				putitems(&p);
			putint(&p, -1);
			senditemstoserver = false;
			serveriteminitdone = true;
		}
		// player chat, not flood protected for now
		if (ctext.length > 0) {
			packet->flags = ENET_PACKET_FLAG_RELIABLE;
			putint(&p, SV_TEXT);
			sendstring(ctext, &p);
			ctext = @"";
		}
		// tell other clients who I am
		if (!c2sinit) {
			packet->flags = ENET_PACKET_FLAG_RELIABLE;
			c2sinit = true;
			putint(&p, SV_INITC2S);
			sendstring(player1.name, &p);
			sendstring(player1.team, &p);
			putint(&p, player1.lifesequence);
		}
		for (OFData *msg in messages) {
			// send messages collected during the previous frames
			if (*(int *)[msg itemAtIndex:1])
				packet->flags = ENET_PACKET_FLAG_RELIABLE;
			loopi(*(int *)[msg itemAtIndex:0])
			    putint(&p, *(int *)[msg itemAtIndex:i + 2]);
		}
		[messages removeAllObjects];
		if (lastmillis - lastping > 250) {
			putint(&p, SV_PING);
			putint(&p, lastmillis);
			lastping = lastmillis;
		}
	}
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	incomingdemodata(start, p - start, true);
	if (clienthost) {

Modified src/clients2c.mm from [84894b97fe] to [2a4e83496a].

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
	uchar *p = buf + 2;
	char text[MAXTRANS];
	int cn = -1, type;
	DynamicEntity *d = nil;
	bool mapchanged = false;

	while (p < end)
		switch (type = getint(p)) {
		case SV_INITS2C: // welcome messsage from the server
		{
			cn = getint(p);
			int prot = getint(p);
			if (prot != PROTOCOL_VERSION) {
				conoutf(@"you are using a different game "
				        @"protocol (you: %d, server: %d)",
				    PROTOCOL_VERSION, prot);
				disconnect();
				return;
			}
			toservermap = @"";
			clientnum = cn; // we are now fully connected
			if (!getint(p))
				// we are the first client on this server, set
				// map
				toservermap = getclientmap();
			sgetstr();
			if (text[0] &&
			    strcmp(text, clientpassword.UTF8String)) {
				conoutf(@"you need to set the correct password "
				        @"to join this server!");
				disconnect();
				return;
			}
			if (getint(p) == 1)
				conoutf(@"server is FULL, disconnecting..");
			break;
		}

		case SV_POS: {
			// position of another client
			cn = getint(p);
			d = getclient(cn);
			if (d == nil)
				return;
			d.o = OFMakeVector3D(
			    getint(p) / DMF, getint(p) / DMF, getint(p) / DMF);



			d.yaw = getint(p) / DAF;
			d.pitch = getint(p) / DAF;
			d.roll = getint(p) / DAF;



			d.vel = OFMakeVector3D(
			    getint(p) / DVF, getint(p) / DVF, getint(p) / DVF);
			int f = getint(p);
			d.strafe = (f & 3) == 3 ? -1 : f & 3;
			f >>= 2;
			d.move = (f & 3) == 3 ? -1 : f & 3;
			d.onfloor = (f >> 2) & 1;
			int state = f >> 3;
			if (state == CS_DEAD && d.state != CS_DEAD)
				d.lastaction = lastmillis;
			d.state = state;
			if (!demoplayback)
				updatepos(d);
			break;
		}

		case SV_SOUND: {
			OFVector3D loc = d.o;
			playsound(getint(p), &loc);
			break;
		}

		case SV_TEXT:
			sgetstr();
			conoutf(@"%@:\f %s", d.name, text);
			break;

		case SV_MAPCHANGE:
			sgetstr();
			changemapserv(@(text), getint(p));
			mapchanged = true;
			break;

		case SV_ITEMLIST: {
			int n;
			if (mapchanged) {
				senditemstoserver = false;
				resetspawns();
			}
			while ((n = getint(p)) != -1)
				if (mapchanged)
					setspawn(n, true);
			break;
		}

		case SV_MAPRELOAD: // server requests next map
		{
			getint(p);
			OFString *nextmapalias = [OFString
			    stringWithFormat:@"nextmap_%@", getclientmap()];
			OFString *map =
			    getalias(nextmapalias); // look up map in the cycle
			changemap(map != nil ? map : getclientmap());
			break;
		}







|


|
|









|











|






|



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















|










|









|




|
|
<
|







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
	uchar *p = buf + 2;
	char text[MAXTRANS];
	int cn = -1, type;
	DynamicEntity *d = nil;
	bool mapchanged = false;

	while (p < end)
		switch (type = getint(&p)) {
		case SV_INITS2C: // welcome messsage from the server
		{
			cn = getint(&p);
			int prot = getint(&p);
			if (prot != PROTOCOL_VERSION) {
				conoutf(@"you are using a different game "
				        @"protocol (you: %d, server: %d)",
				    PROTOCOL_VERSION, prot);
				disconnect();
				return;
			}
			toservermap = @"";
			clientnum = cn; // we are now fully connected
			if (!getint(&p))
				// we are the first client on this server, set
				// map
				toservermap = getclientmap();
			sgetstr();
			if (text[0] &&
			    strcmp(text, clientpassword.UTF8String)) {
				conoutf(@"you need to set the correct password "
				        @"to join this server!");
				disconnect();
				return;
			}
			if (getint(&p) == 1)
				conoutf(@"server is FULL, disconnecting..");
			break;
		}

		case SV_POS: {
			// position of another client
			cn = getint(&p);
			d = getclient(cn);
			if (d == nil)
				return;
			OFVector3D tmp;
			tmp.x = getint(&p) / DMF;
			tmp.y = getint(&p) / DMF;
			tmp.z = getint(&p) / DMF;
			d.o = tmp;
			d.yaw = getint(&p) / DAF;
			d.pitch = getint(&p) / DAF;
			d.roll = getint(&p) / DAF;
			tmp.x = getint(&p) / DVF;
			tmp.y = getint(&p) / DVF;
			tmp.z = getint(&p) / DVF;
			d.vel = tmp;

			int f = getint(&p);
			d.strafe = (f & 3) == 3 ? -1 : f & 3;
			f >>= 2;
			d.move = (f & 3) == 3 ? -1 : f & 3;
			d.onfloor = (f >> 2) & 1;
			int state = f >> 3;
			if (state == CS_DEAD && d.state != CS_DEAD)
				d.lastaction = lastmillis;
			d.state = state;
			if (!demoplayback)
				updatepos(d);
			break;
		}

		case SV_SOUND: {
			OFVector3D loc = d.o;
			playsound(getint(&p), &loc);
			break;
		}

		case SV_TEXT:
			sgetstr();
			conoutf(@"%@:\f %s", d.name, text);
			break;

		case SV_MAPCHANGE:
			sgetstr();
			changemapserv(@(text), getint(&p));
			mapchanged = true;
			break;

		case SV_ITEMLIST: {
			int n;
			if (mapchanged) {
				senditemstoserver = false;
				resetspawns();
			}
			while ((n = getint(&p)) != -1)
				if (mapchanged)
					setspawn(n, true);
			break;
		}
		// server requests next map
		case SV_MAPRELOAD: {

			getint(&p);
			OFString *nextmapalias = [OFString
			    stringWithFormat:@"nextmap_%@", getclientmap()];
			OFString *map =
			    getalias(nextmapalias); // look up map in the cycle
			changemap(map != nil ? map : getclientmap());
			break;
		}
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
				// send new players my info again
				c2sinit = false;
				conoutf(@"connected: %s", text);
			}
			d.name = @(text);
			sgetstr();
			d.team = @(text);
			d.lifesequence = getint(p);
			break;
		}

		case SV_CDIS:
			cn = getint(p);
			if ((d = getclient(cn)) == nil)
				break;
			conoutf(@"player %@ disconnected",
			    d.name.length ? d.name : @"[incompatible client]");
			players[cn] = [OFNull null];
			break;

		case SV_SHOT: {
			int gun = getint(p);
			OFVector3D s, e;
			s.x = getint(p) / DMF;
			s.y = getint(p) / DMF;
			s.z = getint(p) / DMF;
			e.x = getint(p) / DMF;
			e.y = getint(p) / DMF;
			e.z = getint(p) / DMF;
			if (gun == GUN_SG)
				createrays(s, e);
			shootv(gun, s, e, d);
			break;
		}

		case SV_DAMAGE: {
			int target = getint(p);
			int damage = getint(p);
			int ls = getint(p);
			if (target == clientnum) {
				if (ls == player1.lifesequence)
					selfdamage(damage, cn, d);
			} else {
				OFVector3D loc = getclient(target).o;
				playsound(S_PAIN1 + rnd(5), &loc);
			}
			break;
		}

		case SV_DIED: {
			int actor = getint(p);
			if (actor == cn) {
				conoutf(@"%@ suicided", d.name);
			} else if (actor == clientnum) {
				int frags;
				if (isteam(player1.team, d.team)) {
					frags = -1;
					conoutf(@"you fragged a teammate (%@)",







|




|








|

|
|
|
|
|
|

|
|




|
|
|











|







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
				// send new players my info again
				c2sinit = false;
				conoutf(@"connected: %s", text);
			}
			d.name = @(text);
			sgetstr();
			d.team = @(text);
			d.lifesequence = getint(&p);
			break;
		}

		case SV_CDIS:
			cn = getint(&p);
			if ((d = getclient(cn)) == nil)
				break;
			conoutf(@"player %@ disconnected",
			    d.name.length ? d.name : @"[incompatible client]");
			players[cn] = [OFNull null];
			break;

		case SV_SHOT: {
			int gun = getint(&p);
			OFVector3D s, e;
			s.x = getint(&p) / DMF;
			s.y = getint(&p) / DMF;
			s.z = getint(&p) / DMF;
			e.x = getint(&p) / DMF;
			e.y = getint(&p) / DMF;
			e.z = getint(&p) / DMF;
			if (gun == GUN_SG)
				createrays(&s, &e);
			shootv(gun, &s, &e, d);
			break;
		}

		case SV_DAMAGE: {
			int target = getint(&p);
			int damage = getint(&p);
			int ls = getint(&p);
			if (target == clientnum) {
				if (ls == player1.lifesequence)
					selfdamage(damage, cn, d);
			} else {
				OFVector3D loc = getclient(target).o;
				playsound(S_PAIN1 + rnd(5), &loc);
			}
			break;
		}

		case SV_DIED: {
			int actor = getint(&p);
			if (actor == cn) {
				conoutf(@"%@ suicided", d.name);
			} else if (actor == clientnum) {
				int frags;
				if (isteam(player1.team, d.team)) {
					frags = -1;
					conoutf(@"you fragged a teammate (%@)",
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
			OFVector3D loc = d.o;
			playsound(S_DIE1 + rnd(2), &loc);
			d.lifesequence++;
			break;
		}

		case SV_FRAGS:
			[players[cn] setFrags:getint(p)];
			break;

		case SV_ITEMPICKUP:
			setspawn(getint(p), false);
			getint(p);
			break;

		case SV_ITEMSPAWN: {
			uint i = getint(p);
			setspawn(i, true);
			if (i >= (uint)ents.count)
				break;
			OFVector3D v =
			    OFMakeVector3D(ents[i].x, ents[i].y, ents[i].z);
			playsound(S_ITEMSPAWN, &v);
			break;
		}

		case SV_ITEMACC: // server acknowledges that I picked up this
		                 // item
			realpickup(getint(p), player1);
			break;

		case SV_EDITH: // coop editing messages, should be extended to
		               // include all possible editing ops
		case SV_EDITT:
		case SV_EDITS:
		case SV_EDITD:
		case SV_EDITE: {
			int x = getint(p);
			int y = getint(p);
			int xs = getint(p);
			int ys = getint(p);
			int v = getint(p);
			block b = { x, y, xs, ys };
			switch (type) {
			case SV_EDITH:
				editheightxy(v != 0, getint(p), b);
				break;
			case SV_EDITT:
				edittexxy(v, getint(p), b);
				break;
			case SV_EDITS:
				edittypexy(v, b);
				break;
			case SV_EDITD:
				setvdeltaxy(v, b);
				break;
			case SV_EDITE:
				editequalisexy(v != 0, b);
				break;
			}
			break;
		}

		case SV_EDITENT: // coop edit of ent
		{
			uint i = getint(p);

			while ((uint)ents.count <= i) {
				Entity *e = [Entity entity];
				e.type = NOTUSED;
				[ents addObject:e];
			}

			int to = ents[i].type;
			ents[i].type = getint(p);
			ents[i].x = getint(p);
			ents[i].y = getint(p);
			ents[i].z = getint(p);
			ents[i].attr1 = getint(p);
			ents[i].attr2 = getint(p);
			ents[i].attr3 = getint(p);
			ents[i].attr4 = getint(p);
			ents[i].spawned = false;
			if (ents[i].type == LIGHT || to == LIGHT)
				calclight();
			break;
		}

		case SV_PING:
			getint(p);
			break;

		case SV_PONG:
			addmsg(0, 2, SV_CLIENTPING,
			    player1.ping =
			        (player1.ping * 5 + lastmillis - getint(p)) /
			        6);
			break;

		case SV_CLIENTPING:
			[players[cn] setPing:getint(p)];
			break;

		case SV_GAMEMODE:
			nextmode = getint(p);
			break;

		case SV_TIMEUP:
			timeupdate(getint(p));
			break;

		case SV_RECVMAP: {
			sgetstr();
			conoutf(@"received map \"%s\" from server, reloading..",
			    text);
			int mapsize = getint(p);
			OFString *string = @(text);
			writemap(string, mapsize, p);
			p += mapsize;
			changemapserv(string, gamemode);
			break;
		}

		case SV_SERVMSG:
			sgetstr();
			conoutf(@"%s", text);
			break;

		case SV_EXT: // so we can messages without breaking previous
		             // clients/servers, if necessary
		{
			for (int n = getint(p); n; n--)
				getint(p);
			break;
		}

		default:
			neterr(@"type");
			return;
		}
}







|



|
|



|








|
|
<
|








|
|
|
|
|



|


|


|


|


|







|








|
|
|
|
|
|
|
|







|





|




|



|



|






|















|
|








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
			OFVector3D loc = d.o;
			playsound(S_DIE1 + rnd(2), &loc);
			d.lifesequence++;
			break;
		}

		case SV_FRAGS:
			[players[cn] setFrags:getint(&p)];
			break;

		case SV_ITEMPICKUP:
			setspawn(getint(&p), false);
			getint(&p);
			break;

		case SV_ITEMSPAWN: {
			uint i = getint(&p);
			setspawn(i, true);
			if (i >= (uint)ents.count)
				break;
			OFVector3D v =
			    OFMakeVector3D(ents[i].x, ents[i].y, ents[i].z);
			playsound(S_ITEMSPAWN, &v);
			break;
		}
		// server acknowledges that I picked up this item
		case SV_ITEMACC:

			realpickup(getint(&p), player1);
			break;

		case SV_EDITH: // coop editing messages, should be extended to
		               // include all possible editing ops
		case SV_EDITT:
		case SV_EDITS:
		case SV_EDITD:
		case SV_EDITE: {
			int x = getint(&p);
			int y = getint(&p);
			int xs = getint(&p);
			int ys = getint(&p);
			int v = getint(&p);
			block b = { x, y, xs, ys };
			switch (type) {
			case SV_EDITH:
				editheightxy(v != 0, getint(&p), &b);
				break;
			case SV_EDITT:
				edittexxy(v, getint(&p), &b);
				break;
			case SV_EDITS:
				edittypexy(v, &b);
				break;
			case SV_EDITD:
				setvdeltaxy(v, &b);
				break;
			case SV_EDITE:
				editequalisexy((v != 0), &b);
				break;
			}
			break;
		}

		case SV_EDITENT: // coop edit of ent
		{
			uint i = getint(&p);

			while ((uint)ents.count <= i) {
				Entity *e = [Entity entity];
				e.type = NOTUSED;
				[ents addObject:e];
			}

			int to = ents[i].type;
			ents[i].type = getint(&p);
			ents[i].x = getint(&p);
			ents[i].y = getint(&p);
			ents[i].z = getint(&p);
			ents[i].attr1 = getint(&p);
			ents[i].attr2 = getint(&p);
			ents[i].attr3 = getint(&p);
			ents[i].attr4 = getint(&p);
			ents[i].spawned = false;
			if (ents[i].type == LIGHT || to == LIGHT)
				calclight();
			break;
		}

		case SV_PING:
			getint(&p);
			break;

		case SV_PONG:
			addmsg(0, 2, SV_CLIENTPING,
			    player1.ping =
			        (player1.ping * 5 + lastmillis - getint(&p)) /
			        6);
			break;

		case SV_CLIENTPING:
			[players[cn] setPing:getint(&p)];
			break;

		case SV_GAMEMODE:
			nextmode = getint(&p);
			break;

		case SV_TIMEUP:
			timeupdate(getint(&p));
			break;

		case SV_RECVMAP: {
			sgetstr();
			conoutf(@"received map \"%s\" from server, reloading..",
			    text);
			int mapsize = getint(&p);
			OFString *string = @(text);
			writemap(string, mapsize, p);
			p += mapsize;
			changemapserv(string, gamemode);
			break;
		}

		case SV_SERVMSG:
			sgetstr();
			conoutf(@"%s", text);
			break;

		case SV_EXT: // so we can messages without breaking previous
		             // clients/servers, if necessary
		{
			for (int n = getint(&p); n; n--)
				getint(&p);
			break;
		}

		default:
			neterr(@"type");
			return;
		}
}

Modified src/cube.h from [b293c88539] to [6d45041e84].

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#define vlinterp(v, f, u, g)                   \
	{                                      \
		(v).x = (v).x * f + (u).x * g; \
		(v).y = (v).y * f + (u).y * g; \
		(v).z = (v).z * f + (u).z * g; \
	}

#define sgetstr()                       \
	{                               \
		char *t = text;         \
		do {                    \
			*t = getint(p); \
		} while (*t++);         \
	} // used by networking

#define m_noitems (gamemode >= 4)
#define m_noitemsrail (gamemode <= 5)
#define m_arena (gamemode >= 8)
#define m_tarena (gamemode >= 10)
#define m_teammode (gamemode & 1 && gamemode > 2)







|
|
|
|
|
|







311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#define vlinterp(v, f, u, g)                   \
	{                                      \
		(v).x = (v).x * f + (u).x * g; \
		(v).y = (v).y * f + (u).y * g; \
		(v).z = (v).z * f + (u).z * g; \
	}

#define sgetstr()                        \
	{                                \
		char *t = text;          \
		do {                     \
			*t = getint(&p); \
		} while (*t++);          \
	} // used by networking

#define m_noitems (gamemode >= 4)
#define m_noitemsrail (gamemode <= 5)
#define m_arena (gamemode >= 8)
#define m_tarena (gamemode >= 10)
#define m_teammode (gamemode & 1 && gamemode > 2)

Modified src/editing.mm from [8eeba4e2ca] to [cc23cfc264].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		};
	});
}

int selh = 0;
bool selset = false;

#define loopselxy(b)                                      \
	{                                                 \
		makeundo();                               \
		loop(x, sel.xs) loop(y, sel.ys)           \
		{                                         \
			sqr *s = S(sel.x + x, sel.y + y); \
			b;                                \
		}                                         \
		remip(sel);                               \
	}

int cx, cy, ch;

int curedittex[] = { -1, -1, -1 };

bool dragging = false;







|
|
|
|
|
|
|
|
|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		};
	});
}

int selh = 0;
bool selset = false;

#define loopselxy(b)                                        \
	{                                                   \
		makeundo();                                 \
		loop(x, sel->xs) loop(y, sel->ys)           \
		{                                           \
			sqr *s = S(sel->x + x, sel->y + y); \
			b;                                  \
		}                                           \
		remip(sel);                                 \
	}

int cx, cy, ch;

int curedittex[] = { -1, -1, -1 };

bool dragging = false;
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
			if (s->tag)
				linestyle(GRIDW, 0xFF, 0x40, 0x40);
			else if (s->type == FHF || s->type == CHF)
				linestyle(GRIDW, 0x80, 0xFF, 0x80);
			else
				linestyle(GRIDW, 0x80, 0x80, 0x80);
			block b = { ix, iy, 1, 1 };
			box(b, h1, h2, h3, h4);
			linestyle(GRID8, 0x40, 0x40, 0xFF);
			if (!(ix & GRIDM))
				line(ix, iy, h1, ix, iy + 1, h4);
			if (!(ix + 1 & GRIDM))
				line(ix + 1, iy, h2, ix + 1, iy + 1, h3);
			if (!(iy & GRIDM))
				line(ix, iy, h1, ix + 1, iy, h2);
			if (!(iy + 1 & GRIDM))
				line(ix, iy + 1, h4, ix + 1, iy + 1, h3);
		}
	}

	if (!SOLID(s)) {
		float ih = sheight(s, s, z);
		linestyle(GRIDS, 0xFF, 0xFF, 0xFF);
		block b = { cx, cy, 1, 1 };
		box(b, ih, sheight(s, SWS(s, 1, 0, ssize), z),
		    sheight(s, SWS(s, 1, 1, ssize), z),
		    sheight(s, SWS(s, 0, 1, ssize), z));
		linestyle(GRIDS, 0xFF, 0x00, 0x00);
		dot(cx, cy, ih);
		ch = (int)ih;
	}

	if (selset) {
		linestyle(GRIDS, 0xFF, 0x40, 0x40);
		box(sel, (float)selh, (float)selh, (float)selh, (float)selh);
	}
}

static OFMutableData *undos; // unlimited undo
VARP(undomegs, 0, 1, 10);    // bounded by n megs

void







|
















|









|







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
			if (s->tag)
				linestyle(GRIDW, 0xFF, 0x40, 0x40);
			else if (s->type == FHF || s->type == CHF)
				linestyle(GRIDW, 0x80, 0xFF, 0x80);
			else
				linestyle(GRIDW, 0x80, 0x80, 0x80);
			block b = { ix, iy, 1, 1 };
			box(&b, h1, h2, h3, h4);
			linestyle(GRID8, 0x40, 0x40, 0xFF);
			if (!(ix & GRIDM))
				line(ix, iy, h1, ix, iy + 1, h4);
			if (!(ix + 1 & GRIDM))
				line(ix + 1, iy, h2, ix + 1, iy + 1, h3);
			if (!(iy & GRIDM))
				line(ix, iy, h1, ix + 1, iy, h2);
			if (!(iy + 1 & GRIDM))
				line(ix, iy + 1, h4, ix + 1, iy + 1, h3);
		}
	}

	if (!SOLID(s)) {
		float ih = sheight(s, s, z);
		linestyle(GRIDS, 0xFF, 0xFF, 0xFF);
		block b = { cx, cy, 1, 1 };
		box(&b, ih, sheight(s, SWS(s, 1, 0, ssize), z),
		    sheight(s, SWS(s, 1, 1, ssize), z),
		    sheight(s, SWS(s, 0, 1, ssize), z));
		linestyle(GRIDS, 0xFF, 0x00, 0x00);
		dot(cx, cy, ih);
		ch = (int)ih;
	}

	if (selset) {
		linestyle(GRIDS, 0xFF, 0x40, 0x40);
		box(&sel, (float)selh, (float)selh, (float)selh, (float)selh);
	}
}

static OFMutableData *undos; // unlimited undo
VARP(undomegs, 0, 1, 10);    // bounded by n megs

void
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
void
makeundo()
{
	if (undos == nil)
		undos =
		    [[OFMutableData alloc] initWithItemSize:sizeof(block *)];

	block *copy = blockcopy(sel);
	[undos addItem:&copy];
	pruneundos(undomegs << 20);
}

void
editundo()
{
	EDITMP;
	if (undos.count == 0) {
		conoutf(@"nothing more to undo");
		return;
	}
	block *p = *(block **)undos.lastItem;
	[undos removeLastItem];
	blockpaste(*p);
	OFFreeMemory(p);
}

block *copybuf = NULL;

void
copy()
{
	EDITSELMP;
	if (copybuf)
		OFFreeMemory(copybuf);
	copybuf = blockcopy(sel);
}

void
paste()
{
	EDITMP;
	if (!copybuf) {
		conoutf(@"nothing to paste");
		return;
	}
	sel.xs = copybuf->xs;
	sel.ys = copybuf->ys;
	correctsel();
	if (!selset || sel.xs != copybuf->xs || sel.ys != copybuf->ys) {
		conoutf(@"incorrect selection");
		return;
	}
	makeundo();
	copybuf->x = sel.x;
	copybuf->y = sel.y;
	blockpaste(*copybuf);
}

void
tofronttex() // maintain most recently used of the texture lists when applying
             // texture
{
	loopi(3)







|














|











|




















|







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
void
makeundo()
{
	if (undos == nil)
		undos =
		    [[OFMutableData alloc] initWithItemSize:sizeof(block *)];

	block *copy = blockcopy(&sel);
	[undos addItem:&copy];
	pruneundos(undomegs << 20);
}

void
editundo()
{
	EDITMP;
	if (undos.count == 0) {
		conoutf(@"nothing more to undo");
		return;
	}
	block *p = *(block **)undos.lastItem;
	[undos removeLastItem];
	blockpaste(p);
	OFFreeMemory(p);
}

block *copybuf = NULL;

void
copy()
{
	EDITSELMP;
	if (copybuf)
		OFFreeMemory(copybuf);
	copybuf = blockcopy(&sel);
}

void
paste()
{
	EDITMP;
	if (!copybuf) {
		conoutf(@"nothing to paste");
		return;
	}
	sel.xs = copybuf->xs;
	sel.ys = copybuf->ys;
	correctsel();
	if (!selset || sel.xs != copybuf->xs || sel.ys != copybuf->ys) {
		conoutf(@"incorrect selection");
		return;
	}
	makeundo();
	copybuf->x = sel.x;
	copybuf->y = sel.y;
	blockpaste(copybuf);
}

void
tofronttex() // maintain most recently used of the texture lists when applying
             // texture
{
	loopi(3)
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
}

// the core editing function. all the *xy functions perform the core operations
// and are also called directly from the network, the function below it is
// strictly triggered locally. They all have very similar structure.

void
editheightxy(bool isfloor, int amount, block &sel)
{
	loopselxy(
	    if (isfloor) {
		    s->floor += amount;
		    if (s->floor >= s->ceil)
			    s->floor = s->ceil - 1;
	    } else {
		    s->ceil += amount;
		    if (s->ceil <= s->floor)
			    s->ceil = s->floor + 1;
	    });
}

void
editheight(int flr, int amount)
{
	EDITSEL;
	bool isfloor = flr == 0;
	editheightxy(isfloor, amount, sel);
	addmsg(1, 7, SV_EDITH, sel.x, sel.y, sel.xs, sel.ys, isfloor, amount);
}
COMMAND(editheight, ARG_2INT)

void
edittexxy(int type, int t, block &sel)
{
	loopselxy(switch (type) {
	        case 0:
		        s->ftex = t;
		        break;
	        case 1:
		        s->wtex = t;







|


















|





|







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
}

// the core editing function. all the *xy functions perform the core operations
// and are also called directly from the network, the function below it is
// strictly triggered locally. They all have very similar structure.

void
editheightxy(bool isfloor, int amount, const block *sel)
{
	loopselxy(
	    if (isfloor) {
		    s->floor += amount;
		    if (s->floor >= s->ceil)
			    s->floor = s->ceil - 1;
	    } else {
		    s->ceil += amount;
		    if (s->ceil <= s->floor)
			    s->ceil = s->floor + 1;
	    });
}

void
editheight(int flr, int amount)
{
	EDITSEL;
	bool isfloor = flr == 0;
	editheightxy(isfloor, amount, &sel);
	addmsg(1, 7, SV_EDITH, sel.x, sel.y, sel.xs, sel.ys, isfloor, amount);
}
COMMAND(editheight, ARG_2INT)

void
edittexxy(int type, int t, const block *sel)
{
	loopselxy(switch (type) {
	        case 0:
		        s->ftex = t;
		        break;
	        case 1:
		        s->wtex = t;
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
		lasttype = type;
	}
	int atype = type == 3 ? 1 : type;
	int i = curedittex[atype];
	i = i < 0 ? 0 : i + dir;
	curedittex[atype] = i = min(max(i, 0), 255);
	int t = lasttex = hdr.texlists[atype][i];
	edittexxy(type, t, sel);
	addmsg(1, 7, SV_EDITT, sel.x, sel.y, sel.xs, sel.ys, type, t);
}

void
replace()
{
	EDITSELMP;







|







405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
		lasttype = type;
	}
	int atype = type == 3 ? 1 : type;
	int i = curedittex[atype];
	i = i < 0 ? 0 : i + dir;
	curedittex[atype] = i = min(max(i, 0), 255);
	int t = lasttex = hdr.texlists[atype][i];
	edittexxy(type, t, &sel);
	addmsg(1, 7, SV_EDITT, sel.x, sel.y, sel.xs, sel.ys, type, t);
}

void
replace()
{
	EDITSELMP;
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
		case 3:
			if (s->utex == rtex.utex)
				s->utex = lasttex;
			break;
		}
	}
	block b = { 0, 0, ssize, ssize };
	remip(b);
}

void
edittypexy(int type, block &sel)
{
	loopselxy(s->type = type);
}

void
edittype(int type)
{
	EDITSEL;
	if (type == CORNER &&
	    (sel.xs != sel.ys || sel.xs == 3 || sel.xs > 4 && sel.xs != 8 ||
	        sel.x & ~-sel.xs || sel.y & ~-sel.ys)) {
		conoutf(@"corner selection must be power of 2 aligned");
		return;
	}
	edittypexy(type, sel);
	addmsg(1, 6, SV_EDITS, sel.x, sel.y, sel.xs, sel.ys, type);
}

void
heightfield(int t)
{
	edittype(t == 0 ? FHF : CHF);







|



|














|







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
		case 3:
			if (s->utex == rtex.utex)
				s->utex = lasttex;
			break;
		}
	}
	block b = { 0, 0, ssize, ssize };
	remip(&b);
}

void
edittypexy(int type, const block *sel)
{
	loopselxy(s->type = type);
}

void
edittype(int type)
{
	EDITSEL;
	if (type == CORNER &&
	    (sel.xs != sel.ys || sel.xs == 3 || sel.xs > 4 && sel.xs != 8 ||
	        sel.x & ~-sel.xs || sel.y & ~-sel.ys)) {
		conoutf(@"corner selection must be power of 2 aligned");
		return;
	}
	edittypexy(type, &sel);
	addmsg(1, 6, SV_EDITS, sel.x, sel.y, sel.xs, sel.ys, type);
}

void
heightfield(int t)
{
	edittype(t == 0 ? FHF : CHF);
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
corner()
{
	edittype(CORNER);
}
COMMAND(corner, ARG_NONE)

void
editequalisexy(bool isfloor, block &sel)
{
	int low = 127, hi = -128;
	loopselxy({
		if (s->floor < low)
			low = s->floor;
		if (s->ceil > hi)
			hi = s->ceil;







|







481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
corner()
{
	edittype(CORNER);
}
COMMAND(corner, ARG_NONE)

void
editequalisexy(bool isfloor, const block *sel)
{
	int low = 127, hi = -128;
	loopselxy({
		if (s->floor < low)
			low = s->floor;
		if (s->ceil > hi)
			hi = s->ceil;
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}

void
equalize(int flr)
{
	bool isfloor = flr == 0;
	EDITSEL;
	editequalisexy(isfloor, sel);
	addmsg(1, 6, SV_EDITE, sel.x, sel.y, sel.xs, sel.ys, isfloor);
}
COMMAND(equalize, ARG_1INT)

void
setvdeltaxy(int delta, block &sel)
{
	loopselxy(s->vdelta = max(s->vdelta + delta, 0));
	remipmore(sel);
}

void
setvdelta(int delta)
{
	EDITSEL;
	setvdeltaxy(delta, sel);
	addmsg(1, 6, SV_EDITD, sel.x, sel.y, sel.xs, sel.ys, delta);
}

const int MAXARCHVERT = 50;
int archverts[MAXARCHVERT][MAXARCHVERT];
bool archvinit = false;








|





|









|







505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}

void
equalize(int flr)
{
	bool isfloor = flr == 0;
	EDITSEL;
	editequalisexy(isfloor, &sel);
	addmsg(1, 6, SV_EDITE, sel.x, sel.y, sel.xs, sel.ys, isfloor);
}
COMMAND(equalize, ARG_1INT)

void
setvdeltaxy(int delta, const block *sel)
{
	loopselxy(s->vdelta = max(s->vdelta + delta, 0));
	remipmore(sel);
}

void
setvdelta(int delta)
{
	EDITSEL;
	setvdeltaxy(delta, &sel);
	addmsg(1, 6, SV_EDITD, sel.x, sel.y, sel.xs, sel.ys, delta);
}

const int MAXARCHVERT = 50;
int archverts[MAXARCHVERT][MAXARCHVERT];
bool archvinit = false;

551
552
553
554
555
556
557



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608



609
610
611
612
613
614
615
	EDITSELMP;
	sel.xs++;
	sel.ys++;
	if (sel.xs > MAXARCHVERT)
		sel.xs = MAXARCHVERT;
	if (sel.ys > MAXARCHVERT)
		sel.ys = MAXARCHVERT;



	loopselxy(s->vdelta = sel.xs > sel.ys
	        ? (archverts[sel.xs - 1][x] +
	              (y == 0 || y == sel.ys - 1 ? sidedelta : 0))
	        : (archverts[sel.ys - 1][y] +
	              (x == 0 || x == sel.xs - 1 ? sidedelta : 0)));
	remipmore(sel);
}

void
slope(int xd, int yd)
{
	EDITSELMP;
	int off = 0;
	if (xd < 0)
		off -= xd * sel.xs;
	if (yd < 0)
		off -= yd * sel.ys;
	sel.xs++;
	sel.ys++;



	loopselxy(s->vdelta = xd * x + yd * y + off);
	remipmore(sel);
}

void
perlin(int scale, int seed, int psize)
{
	EDITSELMP;
	sel.xs++;
	sel.ys++;
	makeundo();
	sel.xs--;
	sel.ys--;
	perlinarea(sel, scale, seed, psize);
	sel.xs++;
	sel.ys++;
	remipmore(sel);
	sel.xs--;
	sel.ys--;
}

VARF(
    fullbright, 0, 0, 1, if (fullbright) {
	    if (noteditmode())
		    return;
	    loopi(mipsize) world[i].r = world[i].g = world[i].b = 176;
    });

void
edittag(int tag)
{
	EDITSELMP;



	loopselxy(s->tag = tag);
}

void
newent(OFString *what, OFString *a1, OFString *a2, OFString *a3, OFString *a4)
{
	EDITSEL;







>
>
>
|
|
|
|
|














>
>
>













|


|















>
>
>







551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
	EDITSELMP;
	sel.xs++;
	sel.ys++;
	if (sel.xs > MAXARCHVERT)
		sel.xs = MAXARCHVERT;
	if (sel.ys > MAXARCHVERT)
		sel.ys = MAXARCHVERT;
	block *sel_ = &sel;
	// Ugly hack to make the macro work.
	block *sel = sel_;
	loopselxy(s->vdelta = sel->xs > sel->ys
	        ? (archverts[sel->xs - 1][x] +
	              (y == 0 || y == sel->ys - 1 ? sidedelta : 0))
	        : (archverts[sel->ys - 1][y] +
	              (x == 0 || x == sel->xs - 1 ? sidedelta : 0)));
	remipmore(sel);
}

void
slope(int xd, int yd)
{
	EDITSELMP;
	int off = 0;
	if (xd < 0)
		off -= xd * sel.xs;
	if (yd < 0)
		off -= yd * sel.ys;
	sel.xs++;
	sel.ys++;
	block *sel_ = &sel;
	// Ugly hack to make the macro work.
	block *sel = sel_;
	loopselxy(s->vdelta = xd * x + yd * y + off);
	remipmore(sel);
}

void
perlin(int scale, int seed, int psize)
{
	EDITSELMP;
	sel.xs++;
	sel.ys++;
	makeundo();
	sel.xs--;
	sel.ys--;
	perlinarea(&sel, scale, seed, psize);
	sel.xs++;
	sel.ys++;
	remipmore(&sel);
	sel.xs--;
	sel.ys--;
}

VARF(
    fullbright, 0, 0, 1, if (fullbright) {
	    if (noteditmode())
		    return;
	    loopi(mipsize) world[i].r = world[i].g = world[i].b = 176;
    });

void
edittag(int tag)
{
	EDITSELMP;
	block *sel_ = &sel;
	// Ugly hack to make the macro work.
	block *sel = sel_;
	loopselxy(s->tag = tag);
}

void
newent(OFString *what, OFString *a1, OFString *a2, OFString *a3, OFString *a4)
{
	EDITSEL;

Modified src/entities.mm from [e562f26211] to [115703834e].

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		player1.quadmillis = 0;
		playsoundc(S_PUPOUT);
		conoutf(@"quad damage is over");
	}
}

void
putitems(uchar *&p) // puts items in network stream and also spawns them locally
{
	[ents enumerateObjectsUsingBlock:^(Entity *e, size_t i, bool *stop) {
		if ((e.type >= I_SHELLS && e.type <= I_QUAD) ||
		    e.type == CARROT) {
			putint(p, i);
			e.spawned = true;
		}







|







332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		player1.quadmillis = 0;
		playsoundc(S_PUPOUT);
		conoutf(@"quad damage is over");
	}
}

void
putitems(uchar **p) // puts items in network stream and also spawns them locally
{
	[ents enumerateObjectsUsingBlock:^(Entity *e, size_t i, bool *stop) {
		if ((e.type >= I_SHELLS && e.type <= I_QUAD) ||
		    e.type == CARROT) {
			putint(p, i);
			e.spawned = true;
		}

Modified src/monster.mm from [7bb8f140a9] to [f91ea743e3].

279
280
281
282
283
284
285

286
287
288
289
290
291
292
293

	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;

			shoot(m, 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







>
|







279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

	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

Modified src/protos.h from [155e863644] to [8ea505a066].

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
    DynamicEntity *d, bool team, OFString *mdlname, bool hellpig, float scale);
void showscores(bool on);
extern void renderscores();

// world
extern void setupworld(int factor);
extern void empty_world(int factor, bool force);
extern void remip(block &b, int level = 0);
extern void remipmore(const block &b, int level = 0);
extern int closestent();
extern int findentity(int type, int index = 0);
extern void trigger(int tag, int type, bool savegame);
extern void resettagareas();
extern void settagareas();
extern Entity *newentity(
    int x, int y, int z, OFString *what, int v1, int v2, int v3, int v4);

// worldlight
extern void calclight();
extern void dodynlight(const OFVector3D &vold, const OFVector3D &v, int reach,
    int strength, DynamicEntity *owner);
extern void cleardlights();
extern block *blockcopy(block &b);
extern void blockpaste(const block &b);

// worldrender
extern void render_world(float vx, float vy, float vh, int yaw, int pitch,
    float widef, int w, int h);

// worldocull
extern void computeraytable(float vx, float vy);







|
|










|


|
|







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
    DynamicEntity *d, bool team, OFString *mdlname, bool hellpig, float scale);
void showscores(bool on);
extern void renderscores();

// world
extern void setupworld(int factor);
extern void empty_world(int factor, bool force);
extern void remip(const block *b, int level = 0);
extern void remipmore(const block *b, int level = 0);
extern int closestent();
extern int findentity(int type, int index = 0);
extern void trigger(int tag, int type, bool savegame);
extern void resettagareas();
extern void settagareas();
extern Entity *newentity(
    int x, int y, int z, OFString *what, int v1, int v2, int v3, int v4);

// worldlight
extern void calclight();
extern void dodynlight(const OFVector3D *vold, const OFVector3D *v, int reach,
    int strength, DynamicEntity *owner);
extern void cleardlights();
extern block *blockcopy(const block *b);
extern void blockpaste(const block *b);

// worldrender
extern void render_world(float vx, float vy, float vh, int yaw, int pitch,
    float widef, int w, int h);

// worldocull
extern void computeraytable(float vx, float vy);
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
extern int text_width(OFString *string);
extern void draw_envbox(int t, int fogdist);

// editing
extern void cursorupdate();
extern void toggleedit();
extern void editdrag(bool isdown);
extern void setvdeltaxy(int delta, block &sel);
extern void editequalisexy(bool isfloor, block &sel);
extern void edittypexy(int type, block &sel);
extern void edittexxy(int type, int t, block &sel);
extern void editheightxy(bool isfloor, int amount, block &sel);
extern bool noteditmode();
extern void pruneundos(int maxremain = 0);

// renderextras
extern void line(int x1, int y1, float z1, int x2, int y2, float z2);
extern void box(block &b, float z1, float z2, float z3, float z4);
extern void dot(int x, int y, float z);
extern void linestyle(float width, int r, int g, int b);
extern void newsphere(const OFVector3D &o, float max, int type);
extern void renderspheres(int time);
extern void gl_drawhud(
    int w, int h, int curfps, int nquads, int curvert, bool underwater);
extern void readdepth(int w, int h);
extern void blendbox(int x1, int y1, int x2, int y2, bool border);
extern void damageblend(int n);

// renderparticles
extern void setorient(OFVector3D &r, OFVector3D &u);
extern void particle_splash(int type, int num, int fade, const OFVector3D &p);
extern void particle_trail(
    int type, int fade, OFVector3D &from, OFVector3D &to);
extern void render_particles(int time);

// worldio
extern void save_world(OFString *fname);
extern void load_world(OFString *mname);
extern void writemap(OFString *mname, int msize, uchar *mdata);
extern OFData *readmap(OFString *mname);
extern void loadgamerest();
extern void incomingdemodata(uchar *buf, int len, bool extras = false);
extern void demoplaybackstep();
extern void stop();
extern void stopifrecording();
extern void demodamage(int damage, const OFVector3D &o);
extern void demoblend(int damage);

// physics
extern void moveplayer(DynamicEntity *pl, int moveres, bool local);
extern bool collide(DynamicEntity *d, bool spawn, float drop, float rise);
extern void entinmap(DynamicEntity *d);
extern void setentphysics(int mml, int mmr);







|
|
|
|
|





|


|








|
|

|












|







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
extern int text_width(OFString *string);
extern void draw_envbox(int t, int fogdist);

// editing
extern void cursorupdate();
extern void toggleedit();
extern void editdrag(bool isdown);
extern void setvdeltaxy(int delta, const block *sel);
extern void editequalisexy(bool isfloor, const block *sel);
extern void edittypexy(int type, const block *sel);
extern void edittexxy(int type, int t, const block *sel);
extern void editheightxy(bool isfloor, int amount, const block *sel);
extern bool noteditmode();
extern void pruneundos(int maxremain = 0);

// renderextras
extern void line(int x1, int y1, float z1, int x2, int y2, float z2);
extern void box(const block *b, float z1, float z2, float z3, float z4);
extern void dot(int x, int y, float z);
extern void linestyle(float width, int r, int g, int b);
extern void newsphere(const OFVector3D *o, float max, int type);
extern void renderspheres(int time);
extern void gl_drawhud(
    int w, int h, int curfps, int nquads, int curvert, bool underwater);
extern void readdepth(int w, int h);
extern void blendbox(int x1, int y1, int x2, int y2, bool border);
extern void damageblend(int n);

// renderparticles
extern void setorient(const OFVector3D *r, const OFVector3D *u);
extern void particle_splash(int type, int num, int fade, const OFVector3D *p);
extern void particle_trail(
    int type, int fade, const OFVector3D *from, const OFVector3D *to);
extern void render_particles(int time);

// worldio
extern void save_world(OFString *fname);
extern void load_world(OFString *mname);
extern void writemap(OFString *mname, int msize, uchar *mdata);
extern OFData *readmap(OFString *mname);
extern void loadgamerest();
extern void incomingdemodata(uchar *buf, int len, bool extras = false);
extern void demoplaybackstep();
extern void stop();
extern void stopifrecording();
extern void demodamage(int damage, const OFVector3D *o);
extern void demoblend(int damage);

// physics
extern void moveplayer(DynamicEntity *pl, int moveres, bool local);
extern bool collide(DynamicEntity *d, bool spawn, float drop, float rise);
extern void entinmap(DynamicEntity *d);
extern void setentphysics(int mml, int mmr);
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
extern void initserver(bool dedicated, int uprate, OFString *sdesc,
    OFString *ip, OFString *master, OFString *passwd, int maxcl);
extern void cleanupserver();
extern void localconnect();
extern void localdisconnect();
extern void localclienttoserver(struct _ENetPacket *);
extern void serverslice(int seconds, unsigned int timeout);
extern void putint(uchar *&p, int n);
extern int getint(uchar *&p);
extern void sendstring(OFString *t, uchar *&p);
extern void startintermission();
extern void restoreserverstate(OFArray<Entity *> *ents);
extern uchar *retrieveservers(uchar *buf, int buflen);
extern char msgsizelookup(int msg);
extern void serverms(int mode, int numplayers, int minremain,
    OFString *smapname, int seconds, bool isfull);
extern void servermsinit(OFString *master, OFString *sdesc, bool listen);
extern void sendmaps(int n, OFString *mapname, int mapsize, uchar *mapdata);
extern ENetPacket *recvmap(int n);

// weapon
extern void selectgun(int a = -1, int b = -1, int c = -1);
extern void shoot(DynamicEntity *d, const OFVector3D &to);
extern void shootv(int gun, OFVector3D &from, OFVector3D &to,
    DynamicEntity *d = 0, bool local = false);
extern void createrays(OFVector3D &from, 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);
extern void renderentities();
extern void resetspawns();
extern void setspawn(uint i, bool on);
extern void teleport(int n, DynamicEntity *d);
extern void baseammo(int gun);

// rndmap
extern void perlinarea(block &b, int scale, int seed, int psize);







|
|
|












|
|

|

















|










|
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
extern void initserver(bool dedicated, int uprate, OFString *sdesc,
    OFString *ip, OFString *master, OFString *passwd, int maxcl);
extern void cleanupserver();
extern void localconnect();
extern void localdisconnect();
extern void localclienttoserver(struct _ENetPacket *);
extern void serverslice(int seconds, unsigned int timeout);
extern void putint(uchar **p, int n);
extern int getint(uchar **p);
extern void sendstring(OFString *t, uchar **p);
extern void startintermission();
extern void restoreserverstate(OFArray<Entity *> *ents);
extern uchar *retrieveservers(uchar *buf, int buflen);
extern char msgsizelookup(int msg);
extern void serverms(int mode, int numplayers, int minremain,
    OFString *smapname, int seconds, bool isfull);
extern void servermsinit(OFString *master, OFString *sdesc, bool listen);
extern void sendmaps(int n, OFString *mapname, int mapsize, uchar *mapdata);
extern ENetPacket *recvmap(int n);

// weapon
extern void selectgun(int a = -1, int b = -1, int c = -1);
extern void shoot(DynamicEntity *d, const OFVector3D *to);
extern void shootv(int gun, const OFVector3D *from, const OFVector3D *to,
    DynamicEntity *d = 0, bool local = false);
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);
extern void renderentities();
extern void resetspawns();
extern void setspawn(uint i, bool on);
extern void teleport(int n, DynamicEntity *d);
extern void baseammo(int gun);

// rndmap
extern void perlinarea(const block *b, int scale, int seed, int psize);

Modified src/renderextras.mm from [32dfc98d1d] to [5a2fb82c3f].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
linestyle(float width, int r, int g, int b)
{
	glLineWidth(width);
	glColor3ub(r, g, b);
}

void
box(block &b, float z1, float z2, float z3, float z4)
{
	glBegin(GL_POLYGON);
	glVertex3f((float)b.x, z1, (float)b.y);
	glVertex3f((float)b.x + b.xs, z2, (float)b.y);
	glVertex3f((float)b.x + b.xs, z3, (float)b.y + b.ys);
	glVertex3f((float)b.x, z4, (float)b.y + b.ys);
	glEnd();
	xtraverts += 4;
}

void
dot(int x, int y, float z)
{







|


|
|
|
|







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
linestyle(float width, int r, int g, int b)
{
	glLineWidth(width);
	glColor3ub(r, g, b);
}

void
box(const block *b, float z1, float z2, float z3, float z4)
{
	glBegin(GL_POLYGON);
	glVertex3f((float)b->x, z1, (float)b->y);
	glVertex3f((float)b->x + b->xs, z2, (float)b->y);
	glVertex3f((float)b->x + b->xs, z3, (float)b->y + b->ys);
	glVertex3f((float)b->x, z4, (float)b->y + b->ys);
	glEnd();
	xtraverts += 4;
}

void
dot(int x, int y, float z)
{
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
	int type;
	sphere *next;
};
sphere spheres[MAXSPHERES], *slist = NULL, *sempty = NULL;
bool sinit = false;

void
newsphere(const OFVector3D &o, float max, int type)
{
	if (!sinit) {
		loopi(MAXSPHERES)
		{
			spheres[i].next = sempty;
			sempty = &spheres[i];
		}
		sinit = true;
	}
	if (sempty) {
		sphere *p = sempty;
		sempty = p->next;
		p->o = o;
		p->max = max;
		p->size = 1;
		p->type = type;
		p->next = slist;
		slist = p;
	}
}







|












|







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
	int type;
	sphere *next;
};
sphere spheres[MAXSPHERES], *slist = NULL, *sempty = NULL;
bool sinit = false;

void
newsphere(const OFVector3D *o, float max, int type)
{
	if (!sinit) {
		loopi(MAXSPHERES)
		{
			spheres[i].next = sempty;
			sempty = &spheres[i];
		}
		sinit = true;
	}
	if (sempty) {
		sphere *p = sempty;
		sempty = p->next;
		p->o = *o;
		p->max = max;
		p->size = 1;
		p->type = type;
		p->next = slist;
		slist = p;
	}
}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
		return;

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

		OFVector3D v = OFMakeVector3D(e.x, e.y, e.z);
		particle_splash(2, 2, 40, v);
	}

	int e = closestent();
	if (e >= 0) {
		Entity *c = ents[e];
		closeent =
		    [OFString stringWithFormat:@"closest entity = %@ (%d, %d, "







|







186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
		return;

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

		OFVector3D v = OFMakeVector3D(e.x, e.y, e.z);
		particle_splash(2, 2, 40, &v);
	}

	int e = closestent();
	if (e >= 0) {
		Entity *c = ents[e];
		closeent =
		    [OFString stringWithFormat:@"closest entity = %@ (%d, %d, "
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
	gluUnProject(w / 2, h / 2, depthcorrect(cursordepth), mm, pm, viewport,
	    &worldx, &worldz, &worldy);
	worldpos.x = (float)worldx;
	worldpos.y = (float)worldy;
	worldpos.z = (float)worldz;
	OFVector3D r = OFMakeVector3D(mm[0], mm[4], mm[8]);
	OFVector3D u = OFMakeVector3D(mm[1], mm[5], mm[9]);
	setorient(r, u);
}

void
drawicon(float tx, float ty, int x, int y)
{
	glBindTexture(GL_TEXTURE_2D, 5);
	glBegin(GL_QUADS);







|







272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
	gluUnProject(w / 2, h / 2, depthcorrect(cursordepth), mm, pm, viewport,
	    &worldx, &worldz, &worldy);
	worldpos.x = (float)worldx;
	worldpos.y = (float)worldy;
	worldpos.z = (float)worldz;
	OFVector3D r = OFMakeVector3D(mm[0], mm[4], mm[8]);
	OFVector3D u = OFMakeVector3D(mm[1], mm[5], mm[9]);
	setorient(&r, &u);
}

void
drawicon(float tx, float ty, int x, int y)
{
	glBindTexture(GL_TEXTURE_2D, 5);
	glBegin(GL_QUADS);

Modified src/renderparticles.mm from [1d20594b8c] to [a86860234b].

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

60
61
62
63
64
65
66
67
68
};
particle particles[MAXPARTICLES], *parlist = NULL, *parempty = NULL;
bool parinit = false;

VARP(maxparticles, 100, 2000, MAXPARTICLES - 500);

static void
newparticle(const OFVector3D &o, const OFVector3D &d, int fade, int type)
{
	if (!parinit) {
		loopi(MAXPARTICLES)
		{
			particles[i].next = parempty;
			parempty = &particles[i];
		}
		parinit = true;
	}
	if (parempty) {
		particle *p = parempty;
		parempty = p->next;
		p->o = o;
		p->d = d;
		p->fade = fade;
		p->type = type;
		p->millis = lastmillis;
		p->next = parlist;
		parlist = p;
	}
}

VAR(demotracking, 0, 0, 1);
VARP(particlesize, 20, 100, 500);

OFVector3D right, up;

void
setorient(OFVector3D &r, OFVector3D &u)
{
	right = r;
	up = u;
}

void
render_particles(int time)
{
	if (demoplayback && demotracking) {

		OFVector3D nom = OFMakeVector3D(0, 0, 0);
		newparticle(player1.o, nom, 100000000, 8);
	}

	glDepthMask(GL_FALSE);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);
	glDisable(GL_FOG);








|












|
|














|

|
|






>

|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
};
particle particles[MAXPARTICLES], *parlist = NULL, *parempty = NULL;
bool parinit = false;

VARP(maxparticles, 100, 2000, MAXPARTICLES - 500);

static void
newparticle(const OFVector3D *o, const OFVector3D *d, int fade, int type)
{
	if (!parinit) {
		loopi(MAXPARTICLES)
		{
			particles[i].next = parempty;
			parempty = &particles[i];
		}
		parinit = true;
	}
	if (parempty) {
		particle *p = parempty;
		parempty = p->next;
		p->o = *o;
		p->d = *d;
		p->fade = fade;
		p->type = type;
		p->millis = lastmillis;
		p->next = parlist;
		parlist = p;
	}
}

VAR(demotracking, 0, 0, 1);
VARP(particlesize, 20, 100, 500);

OFVector3D right, up;

void
setorient(const OFVector3D *r, const OFVector3D *u)
{
	right = *r;
	up = *u;
}

void
render_particles(int time)
{
	if (demoplayback && demotracking) {
		OFVector3D o = player1.o;
		OFVector3D nom = OFMakeVector3D(0, 0, 0);
		newparticle(&o, &nom, 100000000, 8);
	}

	glDepthMask(GL_FALSE);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);
	glDisable(GL_FOG);

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

	glEnable(GL_FOG);
	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
}

void
particle_splash(int type, int num, int fade, const OFVector3D &p)
{
	loopi(num)
	{
		const int radius = type == 5 ? 50 : 150;
		int x, y, z;
		do {
			x = rnd(radius * 2) - radius;
			y = rnd(radius * 2) - radius;
			z = rnd(radius * 2) - radius;
		} while (x * x + y * y + z * z > radius * radius);
		newparticle(p, OFMakeVector3D(x, y, z), rnd(fade * 3), type);

	}
}

void
particle_trail(int type, int fade, OFVector3D &s, OFVector3D &e)
{
	vdist(d, v, s, e);
	vdiv(v, d * 2 + 0.1f);
	OFVector3D p = s;
	loopi((int)d * 2)
	{
		vadd(p, v);
		OFVector3D d =
		    OFMakeVector3D(rnd(11) - 5, rnd(11) - 5, rnd(11) - 5);
		newparticle(p, d, rnd(fade) + fade, type);
	}
}







|










|
>




|

|

|





|


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

	glEnable(GL_FOG);
	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
}

void
particle_splash(int type, int num, int fade, const OFVector3D *p)
{
	loopi(num)
	{
		const int radius = type == 5 ? 50 : 150;
		int x, y, z;
		do {
			x = rnd(radius * 2) - radius;
			y = rnd(radius * 2) - radius;
			z = rnd(radius * 2) - radius;
		} while (x * x + y * y + z * z > radius * radius);
		OFVector3D d = OFMakeVector3D(x, y, z);
		newparticle(p, &d, rnd(fade * 3), type);
	}
}

void
particle_trail(int type, int fade, const OFVector3D *s, const OFVector3D *e)
{
	vdist(d, v, *s, *e);
	vdiv(v, d * 2 + 0.1f);
	OFVector3D p = *s;
	loopi((int)d * 2)
	{
		vadd(p, v);
		OFVector3D d =
		    OFMakeVector3D(rnd(11) - 5, rnd(11) - 5, rnd(11) - 5);
		newparticle(&p, &d, rnd(fade) + fade, type);
	}
}

Modified src/rndmap.mm from [3e8d55fcae] to [268a907b0c].

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
		    amplitude;
		seed += seedstep;
	}
	return total;
}

void
perlinarea(block &b, int scale, int seed, int psize)
{
	srand(seed);
	seed = rnd(10000);
	if (!scale)
		scale = 10;
	for (int x = b.x; x <= b.x + b.xs; x++) {
		for (int y = b.y; y <= b.y + b.ys; y++) {
			sqr *s = S(x, y);
			if (!SOLID(s) && x != b.x + b.xs && y != b.y + b.ys)
				s->type = FHF;
			s->vdelta =
			    (int)(perlinnoise_2D(x / ((float)scale) + seed,
			              y / ((float)scale) + seed, 1000, 0.01f) *
			            50 +
			        25);
			if (s->vdelta > 128)







|





|
|

|







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
		    amplitude;
		seed += seedstep;
	}
	return total;
}

void
perlinarea(const block *b, int scale, int seed, int psize)
{
	srand(seed);
	seed = rnd(10000);
	if (!scale)
		scale = 10;
	for (int x = b->x; x <= b->x + b->xs; x++) {
		for (int y = b->y; y <= b->y + b->ys; y++) {
			sqr *s = S(x, y);
			if (!SOLID(s) && x != b->x + b->xs && y != b->y + b->ys)
				s->type = FHF;
			s->vdelta =
			    (int)(perlinnoise_2D(x / ((float)scale) + seed,
			              y / ((float)scale) + seed, 1000, 0.01f) *
			            50 +
			        25);
			if (s->vdelta > 128)

Modified src/savegamedemo.mm from [58839045b8] to [f1dd561ea3].

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	demorecording = true;
	starttime = lastmillis;
	ddamage = bdamage = 0;
}
COMMAND(record, ARG_1STR)

void
demodamage(int damage, const OFVector3D &o)
{
	ddamage = damage;
	dorig = o;
}

void
demoblend(int damage)
{
	bdamage = damage;
}







|


|







287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	demorecording = true;
	starttime = lastmillis;
	ddamage = bdamage = 0;
}
COMMAND(record, ARG_1STR)

void
demodamage(int damage, const OFVector3D *o)
{
	ddamage = damage;
	dorig = *o;
}

void
demoblend(int damage)
{
	bdamage = damage;
}
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
			loopi(NUMGUNS) target.ammo[i] = gzget();
			target.state = gzget();
			target.lastmove = playbacktime;
			if ((bdamage = gzgeti()))
				damageblend(bdamage);
			if ((ddamage = gzgeti())) {
				gzgetv(dorig);
				particle_splash(3, ddamage, 1000, dorig);
			}
			// FIXME: set more client state here
		}

		// insert latest copy of player into history
		if (extras &&
		    (playerhistory.count == 0 ||







|







450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
			loopi(NUMGUNS) target.ammo[i] = gzget();
			target.state = gzget();
			target.lastmove = playbacktime;
			if ((bdamage = gzgeti()))
				damageblend(bdamage);
			if ((ddamage = gzgeti())) {
				gzgetv(dorig);
				particle_splash(3, ddamage, 1000, &dorig);
			}
			// FIXME: set more client state here
		}

		// insert latest copy of player into history
		if (extras &&
		    (playerhistory.count == 0 ||

Modified src/server.mm from [9e76792f10] to [e4950a7889].

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
void
send2(bool rel, int cn, int a, int b)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, 32, rel ? ENET_PACKET_FLAG_RELIABLE : 0);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(p, a);
	putint(p, b);
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	if (cn < 0)
		process(packet, -1);
	else
		send(cn, packet);
	if (packet->referenceCount == 0)
		enet_packet_destroy(packet);
}

void
sendservmsg(OFString *msg)
{
	ENetPacket *packet = enet_packet_create(
	    NULL, _MAXDEFSTR + 10, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(p, SV_SERVMSG);
	sendstring(msg, p);
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	multicast(packet, -1);
	if (packet->referenceCount == 0)
		enet_packet_destroy(packet);
}








|
|

















|
|







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
void
send2(bool rel, int cn, int a, int b)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, 32, rel ? ENET_PACKET_FLAG_RELIABLE : 0);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(&p, a);
	putint(&p, b);
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	if (cn < 0)
		process(packet, -1);
	else
		send(cn, packet);
	if (packet->referenceCount == 0)
		enet_packet_destroy(packet);
}

void
sendservmsg(OFString *msg)
{
	ENetPacket *packet = enet_packet_create(
	    NULL, _MAXDEFSTR + 10, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(&p, SV_SERVMSG);
	sendstring(msg, &p);
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	multicast(packet, -1);
	if (packet->referenceCount == 0)
		enet_packet_destroy(packet);
}

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

	uchar *end = packet->data + packet->dataLength;
	uchar *p = packet->data + 2;
	char text[MAXTRANS];
	int cn = -1, type;

	while (p < end) {
		switch ((type = getint(p))) {
		case SV_TEXT:
			sgetstr();
			break;

		case SV_INITC2S:
			sgetstr();
			clients[cn].name = @(text);
			sgetstr();
			getint(p);
			break;

		case SV_MAPCHANGE: {
			sgetstr();
			int reqmode = getint(p);
			if (reqmode < 0)
				reqmode = 0;
			if (smapname.length > 0 && !mapreload &&
			    !vote(@(text), reqmode, sender))
				return;
			mapreload = false;
			mode = reqmode;
			minremain = mode & 1 ? 15 : 10;
			mapend = lastsec + minremain * 60;
			interm = 0;
			smapname = @(text);
			resetitems();
			sender = -1;
			break;
		}

		case SV_ITEMLIST: {
			int n;
			while ((n = getint(p)) != -1)
				if (notgotitems) {
					while (sents.count <= n)
						[sents addObject:[ServerEntity
						                     entity]];
					sents[n].spawned = true;
				}
			notgotitems = false;
			break;
		}

		case SV_ITEMPICKUP: {
			int n = getint(p);
			pickup(n, getint(p), sender);
			break;
		}

		case SV_PING:
			send2(false, cn, SV_PONG, getint(p));
			break;

		case SV_POS: {
			cn = getint(p);
			if (cn < 0 || cn >= clients.count ||
			    clients[cn].type == ST_EMPTY) {
				disconnect_client(sender, @"client num");
				return;
			}
			int size = msgsizelookup(type);
			assert(size != -1);
			loopi(size - 2) getint(p);
			break;
		}

		case SV_SENDMAP: {
			sgetstr();
			int mapsize = getint(p);
			sendmaps(sender, @(text), mapsize, p);
			return;
		}

		case SV_RECVMAP:
			send(sender, recvmap(sender));
			return;

		// allows for new features that require no server updates
		case SV_EXT:
			for (int n = getint(p); n; n--)
				getint(p);
			break;

		default: {
			int size = msgsizelookup(type);
			if (size == -1) {
				disconnect_client(sender, @"tag type");
				return;
			}
			loopi(size - 1) getint(p);
		}
		}
	}

	if (p > end) {
		disconnect_client(sender, @"end of packet");
		return;
	}

	multicast(packet, sender);
}

void
send_welcome(int n)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	__block uchar *p = start + 2;
	putint(p, SV_INITS2C);
	putint(p, n);
	putint(p, PROTOCOL_VERSION);
	putint(p, *smapname.UTF8String);
	sendstring(serverpassword, p);
	putint(p, clients.count > maxclients);
	if (smapname.length > 0) {
		putint(p, SV_MAPCHANGE);
		sendstring(smapname, p);
		putint(p, mode);
		putint(p, SV_ITEMLIST);
		[sents enumerateObjectsUsingBlock:^(
		    ServerEntity *e, size_t i, bool *stop) {
			if (e.spawned)
				putint(p, i);
		}];
		putint(p, -1);
	}
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	send(n, packet);
}

void







|








|




|


















|











|
|




|



|







|





|










|
|








|



















|
|
|
|
|
|

|
|
|
|



|

|







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

	uchar *end = packet->data + packet->dataLength;
	uchar *p = packet->data + 2;
	char text[MAXTRANS];
	int cn = -1, type;

	while (p < end) {
		switch ((type = getint(&p))) {
		case SV_TEXT:
			sgetstr();
			break;

		case SV_INITC2S:
			sgetstr();
			clients[cn].name = @(text);
			sgetstr();
			getint(&p);
			break;

		case SV_MAPCHANGE: {
			sgetstr();
			int reqmode = getint(&p);
			if (reqmode < 0)
				reqmode = 0;
			if (smapname.length > 0 && !mapreload &&
			    !vote(@(text), reqmode, sender))
				return;
			mapreload = false;
			mode = reqmode;
			minremain = mode & 1 ? 15 : 10;
			mapend = lastsec + minremain * 60;
			interm = 0;
			smapname = @(text);
			resetitems();
			sender = -1;
			break;
		}

		case SV_ITEMLIST: {
			int n;
			while ((n = getint(&p)) != -1)
				if (notgotitems) {
					while (sents.count <= n)
						[sents addObject:[ServerEntity
						                     entity]];
					sents[n].spawned = true;
				}
			notgotitems = false;
			break;
		}

		case SV_ITEMPICKUP: {
			int n = getint(&p);
			pickup(n, getint(&p), sender);
			break;
		}

		case SV_PING:
			send2(false, cn, SV_PONG, getint(&p));
			break;

		case SV_POS: {
			cn = getint(&p);
			if (cn < 0 || cn >= clients.count ||
			    clients[cn].type == ST_EMPTY) {
				disconnect_client(sender, @"client num");
				return;
			}
			int size = msgsizelookup(type);
			assert(size != -1);
			loopi(size - 2) getint(&p);
			break;
		}

		case SV_SENDMAP: {
			sgetstr();
			int mapsize = getint(&p);
			sendmaps(sender, @(text), mapsize, p);
			return;
		}

		case SV_RECVMAP:
			send(sender, recvmap(sender));
			return;

		// allows for new features that require no server updates
		case SV_EXT:
			for (int n = getint(&p); n; n--)
				getint(&p);
			break;

		default: {
			int size = msgsizelookup(type);
			if (size == -1) {
				disconnect_client(sender, @"tag type");
				return;
			}
			loopi(size - 1) getint(&p);
		}
		}
	}

	if (p > end) {
		disconnect_client(sender, @"end of packet");
		return;
	}

	multicast(packet, sender);
}

void
send_welcome(int n)
{
	ENetPacket *packet =
	    enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	__block uchar *p = start + 2;
	putint(&p, SV_INITS2C);
	putint(&p, n);
	putint(&p, PROTOCOL_VERSION);
	putint(&p, *smapname.UTF8String);
	sendstring(serverpassword, &p);
	putint(&p, clients.count > maxclients);
	if (smapname.length > 0) {
		putint(&p, SV_MAPCHANGE);
		sendstring(smapname, &p);
		putint(&p, mode);
		putint(&p, SV_ITEMLIST);
		[sents enumerateObjectsUsingBlock:^(
		    ServerEntity *e, size_t i, bool *stop) {
			if (e.spawned)
				putint(&p, i);
		}];
		putint(&p, -1);
	}
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	send(n, packet);
}

void

Modified src/serverbrowser.mm from [40331e8345] to [3253e95f9b].

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	uchar *p;

	for (ServerInfo *si in servers) {
		if (si.address.host == ENET_HOST_ANY)
			continue;

		p = ping;
		putint(p, lastmillis);
		buf.data = ping;
		buf.dataLength = p - ping;
		ENetAddress address = si.address;
		enet_socket_send(pingsock, &address, &buf, 1);
	}

	lastinfo = lastmillis;







|







134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	uchar *p;

	for (ServerInfo *si in servers) {
		if (si.address.host == ENET_HOST_ANY)
			continue;

		p = ping;
		putint(&p, lastmillis);
		buf.data = ping;
		buf.dataLength = p - ping;
		ENetAddress address = si.address;
		enet_socket_send(pingsock, &address, &buf, 1);
	}

	lastinfo = lastmillis;
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
	while (enet_socket_wait(pingsock, &events, 0) >= 0 && events) {
		if (enet_socket_receive(pingsock, &addr, &buf, 1) <= 0)
			return;

		for (ServerInfo *si in servers) {
			if (addr.host == si.address.host) {
				p = ping;
				si.ping = lastmillis - getint(p);
				si.protocol = getint(p);
				if (si.protocol != PROTOCOL_VERSION)
					si.ping = 9998;
				si.mode = getint(p);
				si.numplayers = getint(p);
				si.minremain = getint(p);
				sgetstr();
				si.map = @(text);
				sgetstr();
				si.sdesc = @(text);
				break;
			}
		}







|
|


|
|
|







181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
	while (enet_socket_wait(pingsock, &events, 0) >= 0 && events) {
		if (enet_socket_receive(pingsock, &addr, &buf, 1) <= 0)
			return;

		for (ServerInfo *si in servers) {
			if (addr.host == si.address.host) {
				p = ping;
				si.ping = lastmillis - getint(&p);
				si.protocol = getint(&p);
				if (si.protocol != PROTOCOL_VERSION)
					si.ping = 9998;
				si.mode = getint(&p);
				si.numplayers = getint(&p);
				si.minremain = getint(&p);
				sgetstr();
				si.map = @(text);
				sgetstr();
				si.sdesc = @(text);
				break;
			}
		}

Modified src/serverms.mm from [0e5451e8b8] to [67d3e10cf2].

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	buf.data = pong;
	while (enet_socket_wait(pongsock, &events, 0) >= 0 && events) {
		buf.dataLength = sizeof(pong);
		len = enet_socket_receive(pongsock, &addr, &buf, 1);
		if (len < 0)
			return;
		p = &pong[len];
		putint(p, PROTOCOL_VERSION);
		putint(p, mode);
		putint(p, numplayers);
		putint(p, minremain);
		OFString *mname =
		    [OFString stringWithFormat:@"%@%@",
		              (isfull ? @"[FULL] " : @""), smapname];
		sendstring(mname, p);
		sendstring(serverdesc, p);
		buf.dataLength = p - pong;
		enet_socket_send(pongsock, &addr, &buf, 1);
	}
}

void
servermsinit(OFString *master_, OFString *sdesc, bool listen)







|
|
|
|



|
|







132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	buf.data = pong;
	while (enet_socket_wait(pongsock, &events, 0) >= 0 && events) {
		buf.dataLength = sizeof(pong);
		len = enet_socket_receive(pongsock, &addr, &buf, 1);
		if (len < 0)
			return;
		p = &pong[len];
		putint(&p, PROTOCOL_VERSION);
		putint(&p, mode);
		putint(&p, numplayers);
		putint(&p, minremain);
		OFString *mname =
		    [OFString stringWithFormat:@"%@%@",
		              (isfull ? @"[FULL] " : @""), smapname];
		sendstring(mname, &p);
		sendstring(serverdesc, &p);
		buf.dataLength = p - pong;
		enet_socket_send(pongsock, &addr, &buf, 1);
	}
}

void
servermsinit(OFString *master_, OFString *sdesc, bool listen)

Modified src/serverutil.mm from [0ddeef8a46] to [3bd542851a].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// misc useful functions used by the server

#include "cube.h"

// all network traffic is in 32bit ints, which are then compressed using the
// following simple scheme (assumes that most values are small).

void
putint(uchar *&p, int n)
{
	if (n < 128 && n > -127) {
		*p++ = n;
	} else if (n < 0x8000 && n >= -0x8000) {
		*p++ = 0x80;
		*p++ = n;
		*p++ = n >> 8;
	} else {
		*p++ = 0x81;
		*p++ = n;
		*p++ = n >> 8;
		*p++ = n >> 16;
		*p++ = n >> 24;
	}
}

int
getint(uchar *&p)
{
	int c = *((char *)p);
	p++;
	if (c == -128) {
		int n = *p++;
		n |= *((char *)p) << 8;
		p++;
		return n;
	} else if (c == -127) {
		int n = *p++;
		n |= *p++ << 8;
		n |= *p++ << 16;
		return n | (*p++ << 24);
	} else
		return c;
}

void
sendstring(OFString *t_, uchar *&p)
{
	const char *t = t_.UTF8String;

	for (size_t i = 0; i < _MAXDEFSTR && *t != '\0'; i++)
		putint(p, *t++);

	putint(p, 0);








|


|

|
|
|

|
|
|
|
|




|

|
|

|
|
|


|
|
|
|





|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// misc useful functions used by the server

#include "cube.h"

// all network traffic is in 32bit ints, which are then compressed using the
// following simple scheme (assumes that most values are small).

void
putint(uchar **p, int n)
{
	if (n < 128 && n > -127) {
		*(*p)++ = n;
	} else if (n < 0x8000 && n >= -0x8000) {
		*(*p)++ = 0x80;
		*(*p)++ = n;
		*(*p)++ = n >> 8;
	} else {
		*(*p)++ = 0x81;
		*(*p)++ = n;
		*(*p)++ = n >> 8;
		*(*p)++ = n >> 16;
		*(*p)++ = n >> 24;
	}
}

int
getint(uchar **p)
{
	int c = *((char *)*p);
	(*p)++;
	if (c == -128) {
		int n = *(*p)++;
		n |= *((char *)*p) << 8;
		(*p)++;
		return n;
	} else if (c == -127) {
		int n = *(*p)++;
		n |= *(*p)++ << 8;
		n |= *(*p)++ << 16;
		return n | (*(*p)++ << 24);
	} else
		return c;
}

void
sendstring(OFString *t_, uchar **p)
{
	const char *t = t_.UTF8String;

	for (size_t i = 0; i < _MAXDEFSTR && *t != '\0'; i++)
		putint(p, *t++);

	putint(p, 0);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
	if (!copydata)
		return NULL;
	ENetPacket *packet = enet_packet_create(
	    NULL, MAXTRANS + copysize, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(p, SV_RECVMAP);
	sendstring(copyname, p);
	putint(p, copysize);
	memcpy(p, copydata, copysize);
	p += copysize;
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	return packet;
}








|
|
|







118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
	if (!copydata)
		return NULL;
	ENetPacket *packet = enet_packet_create(
	    NULL, MAXTRANS + copysize, ENET_PACKET_FLAG_RELIABLE);
	uchar *start = packet->data;
	uchar *p = start + 2;
	putint(&p, SV_RECVMAP);
	sendstring(copyname, &p);
	putint(&p, copysize);
	memcpy(p, copydata, copysize);
	p += copysize;
	*(ushort *)start = ENET_HOST_TO_NET_16(p - start);
	enet_packet_resize(packet, p - start);
	return packet;
}

Modified src/weapon.mm from [ec6a711714] to [b2ede2d0f5].

66
67
68
69
70
71
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{
	selectgun((a1.length > 0 ? a1.cube_intValue : -1),
	    (a2.length > 0 ? a2.cube_intValue : -1),
	    (a3.length > 0 ? a3.cube_intValue : -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);
	}
}

// if lineseg hits entity bounding box
bool
intersect(DynamicEntity *d, const OFVector3D &from, const OFVector3D &to)
{
	OFVector3D v = to, w = d.o;
	const OFVector3D *p;
	vsub(v, from);
	vsub(w, from);
	float c1 = dotprod(w, v);

	if (c1 <= 0)
		p = &from;
	else {
		float c2 = dotprod(v, v);
		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);
}

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

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


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

	return nil;
}

static const size_t MAXPROJ = 100;
static Projectile *projs[MAXPROJ];

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

void
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.speed = speed;
		p.local = local;
		p.owner = owner;
		p.gun = gun;
		return;
	}
}

void
hit(int target, int damage, DynamicEntity *d, DynamicEntity *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);
		OFVector3D loc = d.o;
		playsound(S_PAIN1 + rnd(5), &loc);
	}
	particle_splash(3, damage, 1000, d.o);
	demodamage(damage, d.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)
{
	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, const OFVector3D &v, const 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);
		// no push?
	} else {
		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);







>

|
<

|





|





|
|

|

|
|



|



|



|



















>
|

















|
|











|
|











>






<
|

|
|







|



|












|






|


|







66
67
68
69
70
71
72
73
74
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
	selectgun((a1.length > 0 ? a1.cube_intValue : -1),
	    (a2.length > 0 ? a2.cube_intValue : -1),
	    (a3.length > 0 ? a3.cube_intValue : -1));
}
COMMAND(weapon, ARG_3STR)

// create random spread of rays for the shotgun
void
createrays(const OFVector3D *from, const OFVector3D *to)

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

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

	if (c1 <= 0)
		p = from;
	else {
		float c2 = dotprod(v, v);
		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);
}

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

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

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

	return nil;
}

static const size_t 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)
{
	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.speed = speed;
		p.local = local;
		p.owner = owner;
		p.gun = gun;
		return;
	}
}

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

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)
{
	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, const OFVector3D *v, const 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);
		// no push?
	} else {
		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);
238
239
240
241
242
243
244
245
246
247
248
249
250

251
252
253
254
255
256
257
258
259
			if (i != notthismonster)
				radialeffect(monster, v, i, qdam, p.owner);
		}];
	}
}

inline void
projdamage(
    DynamicEntity *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)
{







|
|




>
|
|







239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
			if (i != notthismonster)
				radialeffect(monster, v, i, qdam, p.owner);
		}];
	}
}

inline void
projdamage(DynamicEntity *o, Projectile *p, const 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);
		hit(i, qdam, o, p.owner);
	}
}

void
moveprojectiles(float time)
{
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
		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.o, v, 10.0f) &&
				    monster != p.owner)
					projdamage(monster, p, v, -1, i, qdam);
		}
		if (p.inuse) {


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

// create visual effect from a shot
void

shootv(int gun, OFVector3D &from, OFVector3D &to, DynamicEntity *d, bool local)
{
	OFVector3D loc = d.o;
	playsound(guns[gun].sound, d == player1 ? NULL : &loc);
	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;







|


|




|


>
>

|


|
|

|

|









>
|









|







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
		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.o, v, 10.0f) &&
				    monster != p.owner)
					projdamage(monster, p, &v, -1, i, qdam);
		}
		if (p.inuse) {
			OFVector3D po = p.o;

			if (time == dtime)
				splash(p, &v, &po, -1, -1, qdam);
			else {
				if (p.gun == GUN_RL) {
					dodynlight(&po, &v, 0, 255, p.owner);
					particle_splash(5, 2, 200, &v);
				} else {
					particle_splash(1, 1, 200, &v);
					particle_splash(
					    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)
{
	OFVector3D loc = d.o;
	playsound(guns[gun].sound, d == player1 ? NULL : &loc);
	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;
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
		particle_trail(1, 500, from, to);
		break;
	}
}

void
hitpush(int target, int damage, DynamicEntity *d, DynamicEntity *at,
    const OFVector3D &from, const OFVector3D &to)
{
	hit(target, damage, d, at);
	vdist(dist, v, from, to);
	vmul(v, damage / dist / 50);
	vadd(d.vel, v);
}

void
raydamage(DynamicEntity *o, const OFVector3D &from, const 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.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))
		hitpush(i, qdam, o, d, from, to);
}

void
shoot(DynamicEntity *d, const 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)
		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);
}







|


|





|











|







|


















|
















|



|











|




|


|

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
		particle_trail(1, 500, from, to);
		break;
	}
}

void
hitpush(int target, int damage, DynamicEntity *d, DynamicEntity *at,
    const OFVector3D *from, const OFVector3D *to)
{
	hit(target, damage, d, at);
	vdist(dist, v, *from, *to);
	vmul(v, damage / dist / 50);
	vadd(d.vel, v);
}

void
raydamage(DynamicEntity *o, const OFVector3D *from, const 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.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))
		hitpush(i, qdam, o, d, from, to);
}

void
shoot(DynamicEntity *d, const 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)
		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);
}

Modified src/world.mm from [742d4e7bcd] to [96ee092ff1].

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
				minx = x;
			if (y < miny)
				miny = y;
		}
	}
	block b = { minx, miny, maxx - minx + 1, maxy - miny + 1 };
	if (maxx)
		remip(b); // remip minimal area of changed geometry
}

void
resettagareas()
{
	settag(0, 0);
} // reset for editing or map saving







|







38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
				minx = x;
			if (y < miny)
				miny = y;
		}
	}
	block b = { minx, miny, maxx - minx + 1, maxy - miny + 1 };
	if (maxx)
		remip(&b); // remip minimal area of changed geometry
}

void
resettagareas()
{
	settag(0, 0);
} // reset for editing or map saving
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
// 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
// rendering time if this is possible).

void
remip(block &b, int level)
{
	if (level >= SMALLEST_FACTOR)
		return;

	int lighterr = getvar(@"lighterror") * 3;
	sqr *w = wmip[level];
	sqr *v = wmip[level + 1];
	int ws = ssize >> level;
	int vs = ssize >> (level + 1);
	block s = b;
	if (s.x & 1) {
		s.x--;
		s.xs++;
	}
	if (s.y & 1) {
		s.y--;
		s.ys++;
	}
	s.xs = (s.xs + 1) & ~1;
	s.ys = (s.ys + 1) & ~1;
	for (int x = s.x; x < s.x + s.xs; x += 2)
		for (int y = s.y; y < s.y + s.ys; y += 2) {
			sqr *o[4];
			o[0] = SWS(w, x, y, ws); // the 4 constituent cubes
			o[1] = SWS(w, x + 1, y, ws);
			o[2] = SWS(w, x + 1, y + 1, ws);
			o[3] = SWS(w, x, y + 1, ws);
			sqr *r = SWS(v, x / 2, y / 2,
			    vs); // the target cube in the higher mip level

			*r = *o[0];
			uchar nums[MAXTYPE];
			loopi(MAXTYPE) nums[i] = 0;
			loopj(4) nums[o[j]->type]++;
			r->type =
			    SEMISOLID; // cube contains both solid and space,
			               // treated specially in the renderer

			loopk(MAXTYPE) if (nums[k] == 4) r->type = k;
			if (!SOLID(r)) {
				int floor = 127, ceil = -128, num = 0;
				loopi(4) if (!SOLID(o[i]))
				{
					num++;
					int fh = o[i]->floor;
					int ch = o[i]->ceil;
					if (r->type == SEMISOLID) {
						if (o[i]->type == FHF)
							fh -= o[i]->vdelta / 4 +
							    2; // crap hack,
							       // needed for
							       // rendering
							       // large mips
							       // next to hfs


						if (o[i]->type == CHF)
							ch += o[i]->vdelta / 4 +
							    2; // FIXME: needs
							       // to somehow
							       // take into
							       // account middle
							       // vertices on
							       // higher mips


					}
					if (fh < floor)
						floor =
						    fh; // take lowest floor and
						        // highest ceil, so we
						        // never have to see
						        // missing lower/upper
						        // from the side
					if (ch > ceil)
						ceil = ch;
				}
				r->floor = floor;
				r->ceil = ceil;
			}
			if (r->type == CORNER)
				goto mip; // special case: don't ever split even
				          // if textures etc are different

			r->defer = 1;
			if (SOLID(r)) {
				loopi(3)
				{
					if (o[i]->wtex != o[3]->wtex)
						goto c; // on an all solid cube,
						        // only thing that needs
						        // to be equal for a
						        // perfect mip is the
						        // wall texture

				}
			} else {
				loopi(3)
				{


					if (o[i]->type != o[3]->type ||
					    o[i]->floor != o[3]->floor ||
					    o[i]->ceil != o[3]->ceil ||
					    o[i]->ftex != o[3]->ftex ||
					    o[i]->ctex != o[3]->ctex ||
					    abs(o[i + 1]->r - o[0]->r) >
					        lighterr // perfect mip even if
					                 // light is not exactly
					                 // equal
					    || abs(o[i + 1]->g - o[0]->g) >
					        lighterr ||
					    abs(o[i + 1]->b - o[0]->b) >
					        lighterr ||
					    o[i]->utex != o[3]->utex ||
					    o[i]->wtex != o[3]->wtex)
						goto c;
				}
				if (r->type == CHF ||
				    r->type ==
				        FHF) // can make a perfect mip out of a
				             // hf if slopes lie on one line
				{

					if (o[0]->vdelta - o[1]->vdelta !=
					        o[1]->vdelta -
					            SWS(w, x + 2, y, ws)
					                ->vdelta ||
					    o[0]->vdelta - o[2]->vdelta !=
					        o[2]->vdelta -
					            SWS(w, x + 2, y + 2, ws)







|



>





|

















<
|
>




<
|
|
>










<
|
<
|
<
|
>
>

<
|
<
|
|
|
|
>
>


<
|
|
|
|
|







|
|
>





|
|
<
|
|
>




>
>






|
<
<
|







|
<
|
|
<
>







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
// 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
// rendering time if this is possible).

void
remip(const block *b, int level)
{
	if (level >= SMALLEST_FACTOR)
		return;

	int lighterr = getvar(@"lighterror") * 3;
	sqr *w = wmip[level];
	sqr *v = wmip[level + 1];
	int ws = ssize >> level;
	int vs = ssize >> (level + 1);
	block s = *b;
	if (s.x & 1) {
		s.x--;
		s.xs++;
	}
	if (s.y & 1) {
		s.y--;
		s.ys++;
	}
	s.xs = (s.xs + 1) & ~1;
	s.ys = (s.ys + 1) & ~1;
	for (int x = s.x; x < s.x + s.xs; x += 2)
		for (int y = s.y; y < s.y + s.ys; y += 2) {
			sqr *o[4];
			o[0] = SWS(w, x, y, ws); // the 4 constituent cubes
			o[1] = SWS(w, x + 1, y, ws);
			o[2] = SWS(w, x + 1, y + 1, ws);
			o[3] = SWS(w, x, y + 1, ws);

			// the target cube in the higher mip level
			sqr *r = SWS(v, x / 2, y / 2, vs);
			*r = *o[0];
			uchar nums[MAXTYPE];
			loopi(MAXTYPE) nums[i] = 0;
			loopj(4) nums[o[j]->type]++;

			// cube contains both solid and space, treated
			// specially in the renderer
			r->type = SEMISOLID;
			loopk(MAXTYPE) if (nums[k] == 4) r->type = k;
			if (!SOLID(r)) {
				int floor = 127, ceil = -128, num = 0;
				loopi(4) if (!SOLID(o[i]))
				{
					num++;
					int fh = o[i]->floor;
					int ch = o[i]->ceil;
					if (r->type == SEMISOLID) {
						if (o[i]->type == FHF)

							// crap hack, needed

							// for rendering large

							// mips next to hfs
							fh -= o[i]->vdelta / 4 +
							    2;
						if (o[i]->type == CHF)

							// FIXME: needs to

							// somehow take into
							// account middle
							// vertices on higher
							// mips
							ch += o[i]->vdelta / 4 +
							    2;
					}
					if (fh < floor)

						// take lowest floor and
						// highest ceil, so we never
						// have to see missing
						// lower/upper from the side
						floor = fh;
					if (ch > ceil)
						ceil = ch;
				}
				r->floor = floor;
				r->ceil = ceil;
			}
			if (r->type == CORNER)
				// special case: don't ever split even if
				// textures etc are different
				goto mip;
			r->defer = 1;
			if (SOLID(r)) {
				loopi(3)
				{
					if (o[i]->wtex != o[3]->wtex)
						// on an all solid cube, only
						// thing that needs to be equal

						// for a perfect mip is the
						// wall texture
						goto c;
				}
			} else {
				loopi(3)
				{
					// perfect mip even if light is not
					// exactly equal
					if (o[i]->type != o[3]->type ||
					    o[i]->floor != o[3]->floor ||
					    o[i]->ceil != o[3]->ceil ||
					    o[i]->ftex != o[3]->ftex ||
					    o[i]->ctex != o[3]->ctex ||
					    abs(o[i + 1]->r - o[0]->r) >
					        lighterr ||


					    abs(o[i + 1]->g - o[0]->g) >
					        lighterr ||
					    abs(o[i + 1]->b - o[0]->b) >
					        lighterr ||
					    o[i]->utex != o[3]->utex ||
					    o[i]->wtex != o[3]->wtex)
						goto c;
				}


				// can make a perfect mip out of a hf if slopes
				// lie on one line

				if (r->type == CHF || r->type == FHF) {
					if (o[0]->vdelta - o[1]->vdelta !=
					        o[1]->vdelta -
					            SWS(w, x + 2, y, ws)
					                ->vdelta ||
					    o[0]->vdelta - o[2]->vdelta !=
					        o[2]->vdelta -
					            SWS(w, x + 2, y + 2, ws)
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
					        o[2]->vdelta -
					            SWS(w, x + 1, y + 2, ws)
					                ->vdelta)
						goto c;
				}
			}
			{
				loopi(4) if (o[i]->defer) goto c;
			} // if any of the constituents is not perfect, then
			  // this one isn't either


		mip:
			r->defer = 0;
		c:;
		}
	s.x /= 2;
	s.y /= 2;
	s.xs /= 2;
	s.ys /= 2;
	remip(s, level + 1);
}

void
remipmore(const block &b, int level)
{
	block bb = b;

	if (bb.x > 1)
		bb.x--;
	if (bb.y > 1)
		bb.y--;
	if (bb.xs < ssize - 3)
		bb.xs++;
	if (bb.ys < ssize - 3)
		bb.ys++;

	remip(bb, level);
}

int
closestent() // used for delent and edit mode ent display
{
	if (noteditmode())
		return -1;







<
|
|
>
>








|



|

|
>








>
|







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
					        o[2]->vdelta -
					            SWS(w, x + 1, y + 2, ws)
					                ->vdelta)
						goto c;
				}
			}
			{

				// if any of the constituents is not perfect,
				// then this one isn't either
				loopi(4) if (o[i]->defer) goto c;
			}
		mip:
			r->defer = 0;
		c:;
		}
	s.x /= 2;
	s.y /= 2;
	s.xs /= 2;
	s.ys /= 2;
	remip(&s, level + 1);
}

void
remipmore(const block *b, int level)
{
	block bb = *b;

	if (bb.x > 1)
		bb.x--;
	if (bb.y > 1)
		bb.y--;
	if (bb.xs < ssize - 3)
		bb.xs++;
	if (bb.ys < ssize - 3)
		bb.ys++;

	remip(&bb, level);
}

int
closestent() // used for delent and edit mode ent display
{
	if (noteditmode())
		return -1;
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
		char buffer[128] = "Untitled Map by Unknown";
		memcpy(hdr.maptitle, buffer, 128);
		hdr.waterlevel = -100000;
		loopi(15) hdr.reserved[i] = 0;
		loopk(3) loopi(256) hdr.texlists[k][i] = i;
		[ents removeAllObjects];
		block b = { 8, 8, ssize - 16, ssize - 16 };
		edittypexy(SPACE, b);
	}

	calclight();
	startmap(@"base/unnamed");
	if (oldworld) {
		OFFreeMemory(oldworld);
		toggleedit();







|







504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
		char buffer[128] = "Untitled Map by Unknown";
		memcpy(hdr.maptitle, buffer, 128);
		hdr.waterlevel = -100000;
		loopi(15) hdr.reserved[i] = 0;
		loopk(3) loopi(256) hdr.texlists[k][i] = i;
		[ents removeAllObjects];
		block b = { 8, 8, ssize - 16, ssize - 16 };
		edittypexy(SPACE, &b);
	}

	calclight();
	startmap(@"base/unnamed");
	if (oldworld) {
		OFFreeMemory(oldworld);
		toggleedit();

Modified src/worldlight.mm from [5df422a1d9] to [ecc6786def].

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	        SW(s, 1, -1)->m + SW(s, -1, 1)->m + SW(s, -1, -1)->m) /      \
	    14; // median is 4/2/1 instead
		median(r);
		median(g);
		median(b);
	}

	remip(a);
}

void
calclight()
{
	loop(x, ssize) loop(y, ssize)
	{







|







161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	        SW(s, 1, -1)->m + SW(s, -1, 1)->m + SW(s, -1, -1)->m) /      \
	    14; // median is 4/2/1 instead
		median(r);
		median(g);
		median(b);
	}

	remip(&a);
}

void
calclight()
{
	loop(x, ssize) loop(y, ssize)
	{
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

void
cleardlights()
{
	while (dlights.count > 0) {
		block *backup = *(block **)[dlights lastItem];
		[dlights removeLastItem];
		blockpaste(*backup);
		OFFreeMemory(backup);
	}
}

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!
	block b = { (int)v.x - creach, (int)v.y - creach, creach * 2 + 1,
		creach * 2 + 1 };

	if (b.x < 1)
		b.x = 1;
	if (b.y < 1)
		b.y = 1;
	if (b.xs + b.x > ssize - 2)
		b.xs = ssize - 2 - b.x;
	if (b.ys + b.y > ssize - 2)
		b.ys = ssize - 2 - b.y;

	if (dlights == nil)
		dlights =
		    [[OFMutableData alloc] initWithItemSize:sizeof(block *)];

	// backup area before rendering in dynlight
	block *copy = blockcopy(b);
	[dlights addItem:&copy];

	PersistentEntity *l = [Entity entity];
	l.x = v.x;
	l.y = v.y;
	l.z = v.z;
	l.attr1 = reach;
	l.type = LIGHT;
	l.attr2 = strength;
	calclightsource(l);
	postlightarea(b);
}

// utility functions also used by editing code

block *
blockcopy(block &s)
{
	block *b = (block *)OFAllocZeroedMemory(
	    1, sizeof(block) + s.xs * s.ys * sizeof(sqr));
	*b = s;
	sqr *q = (sqr *)(b + 1);
	for (int x = s.x; x < s.xs + s.x; x++)
		for (int y = s.y; y < s.ys + s.y; y++)
			*q++ = *S(x, y);
	return b;
}

void
blockpaste(const block &b)
{
	sqr *q = (sqr *)((&b) + 1);
	for (int x = b.x; x < b.xs + b.x; x++)
		for (int y = b.y; y < b.ys + b.y; y++)
			*S(x, y) = *q++;
	remipmore(b);
}







|





|








|



|
















|



|
|
|










|


|
|

|
|





|

|
|
|



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

void
cleardlights()
{
	while (dlights.count > 0) {
		block *backup = *(block **)[dlights lastItem];
		[dlights removeLastItem];
		blockpaste(backup);
		OFFreeMemory(backup);
	}
}

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!
	block b = { (int)v->x - creach, (int)v->y - creach, creach * 2 + 1,
		creach * 2 + 1 };

	if (b.x < 1)
		b.x = 1;
	if (b.y < 1)
		b.y = 1;
	if (b.xs + b.x > ssize - 2)
		b.xs = ssize - 2 - b.x;
	if (b.ys + b.y > ssize - 2)
		b.ys = ssize - 2 - b.y;

	if (dlights == nil)
		dlights =
		    [[OFMutableData alloc] initWithItemSize:sizeof(block *)];

	// backup area before rendering in dynlight
	block *copy = blockcopy(&b);
	[dlights addItem:&copy];

	PersistentEntity *l = [Entity entity];
	l.x = v->x;
	l.y = v->y;
	l.z = v->z;
	l.attr1 = reach;
	l.type = LIGHT;
	l.attr2 = strength;
	calclightsource(l);
	postlightarea(b);
}

// utility functions also used by editing code

block *
blockcopy(const block *s)
{
	block *b = (block *)OFAllocZeroedMemory(
	    1, sizeof(block) + s->xs * s->ys * sizeof(sqr));
	*b = *s;
	sqr *q = (sqr *)(b + 1);
	for (int x = s->x; x < s->xs + s->x; x++)
		for (int y = s->y; y < s->ys + s->y; y++)
			*q++ = *S(x, y);
	return b;
}

void
blockpaste(const block *b)
{
	sqr *q = (sqr *)(b + 1);
	for (int x = b->x; x < b->xs + b->x; x++)
		for (int y = b->y; y < b->ys + b->y; y++)
			*S(x, y) = *q++;
	remipmore(b);
}