Cube  Diff

Differences From Artifact [cbd2b108e1]:

To Artifact [850bd490e8]:


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

#include "cube.h"


#import "MapModelInfo.h"


bool
plcollide(dynent *d, dynent *o, float &headspace, float &hi,
    float &lo) // collide with player or monster
{
	if (o->state != CS_ALIVE)
		return true;
	const float r = o->radius + d->radius;
	if (fabs(o->o.x - d->o.x) < r && fabs(o->o.y - d->o.y) < r) {
		if (d->o.z - d->eyeheight < o->o.z - o->eyeheight) {
			if (o->o.z - o->eyeheight < hi)
				hi = o->o.z - o->eyeheight - 1;
		} else if (o->o.z + o->aboveeye > lo)
			lo = o->o.z + o->aboveeye + 1;

		if (fabs(o->o.z - d->o.z) < o->aboveeye + d->eyeheight)
			return false;
		if (d->monsterstate)
			return false; // hack
		headspace = d->o.z - o->o.z - o->aboveeye - d->eyeheight;
		if (headspace < 0)
			headspace = 10;
	}
	return true;
}

bool








>


>

|
|

|

|
|
|
|
|
|
|

|

|

|







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

#include "cube.h"

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

// collide with player or monster
bool
plcollide(
    DynamicEntity *d, DynamicEntity *o, float &headspace, float &hi, float &lo)
{
	if (o.state != CS_ALIVE)
		return true;
	const float r = o.radius + d.radius;
	if (fabs(o.o.x - d.o.x) < r && fabs(o.o.y - d.o.y) < r) {
		if (d.o.z - d.eyeheight < o.o.z - o.eyeheight) {
			if (o.o.z - o.eyeheight < hi)
				hi = o.o.z - o.eyeheight - 1;
		} else if (o.o.z + o.aboveeye > lo)
			lo = o.o.z + o.aboveeye + 1;

		if (fabs(o.o.z - d.o.z) < o.aboveeye + d.eyeheight)
			return false;
		if (d.monsterstate)
			return false; // hack
		headspace = d.o.z - o.o.z - o.aboveeye - d.eyeheight;
		if (headspace < 0)
			headspace = 10;
	}
	return true;
}

bool
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95
96
97
98

99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
		bs = 1 << mip;
		return cornertest(mip, x, y, dx, dy, bx, by, bs);
	}
	return stest;
}

void
mmcollide(dynent *d, float &hi, float &lo) // collide with a mapmodel
{
	loopv(ents)
	{
		entity &e = ents[i];
		if (e.type != MAPMODEL)
			continue;
		MapModelInfo *mmi = getmminfo(e.attr2);
		if (mmi == nil || !mmi.h)
			continue;
		const float r = mmi.rad + d->radius;
		if (fabs(e.x - d->o.x) < r && fabs(e.y - d->o.y) < r) {
			float mmz =
			    (float)(S(e.x, e.y)->floor + mmi.zoff + e.attr3);
			if (d->o.z - d->eyeheight < mmz) {
				if (mmz < hi)
					hi = mmz;
			} else if (mmz + mmi.h > lo)
				lo = mmz + mmi.h;
		}
	}
}

// all collision happens here
// spawn is a dirty side effect used in spawning
// drop & rise are supplied by the physics below to indicate gravity/push for
// current mini-timestep

bool
collide(dynent *d, bool spawn, float drop, float rise)
{

	const float fx1 =
	    d->o.x - d->radius; // figure out integer cube rectangle this entity
	                        // covers in map
	const float fy1 = d->o.y - d->radius;
	const float fx2 = d->o.x + d->radius;
	const float fy2 = d->o.y + d->radius;
	const int x1 = fast_f2nat(fx1);
	const int y1 = fast_f2nat(fy1);
	const int x2 = fast_f2nat(fx2);
	const int y2 = fast_f2nat(fy2);
	float hi = 127, lo = -128;

	float minfloor = (d->monsterstate && !spawn && d->health > 100)
	    ? d->o.z - d->eyeheight - 4.5f
	    : -1000.0f; // big monsters are afraid of heights,
	                // unless angry :)

	for (int x = x1; x <= x2; x++)
		for (int y = y1; y <= y2; y++) // collide with map
		{

			if (OUTBORD(x, y))
				return false;
			sqr *s = S(x, y);
			float ceil = s->ceil;
			float floor = s->floor;
			switch (s->type) {
			case SOLID:







|









|
|


|














|

>
|
<
<
|
|
|





>
|
|
|
<


|
<
>







52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


92
93
94
95
96
97
98
99
100
101
102
103

104
105
106

107
108
109
110
111
112
113
114
		bs = 1 << mip;
		return cornertest(mip, x, y, dx, dy, bx, by, bs);
	}
	return stest;
}

void
mmcollide(DynamicEntity *d, float &hi, float &lo) // collide with a mapmodel
{
	loopv(ents)
	{
		entity &e = ents[i];
		if (e.type != MAPMODEL)
			continue;
		MapModelInfo *mmi = getmminfo(e.attr2);
		if (mmi == nil || !mmi.h)
			continue;
		const float r = mmi.rad + d.radius;
		if (fabs(e.x - d.o.x) < r && fabs(e.y - d.o.y) < r) {
			float mmz =
			    (float)(S(e.x, e.y)->floor + mmi.zoff + e.attr3);
			if (d.o.z - d.eyeheight < mmz) {
				if (mmz < hi)
					hi = mmz;
			} else if (mmz + mmi.h > lo)
				lo = mmz + mmi.h;
		}
	}
}

// all collision happens here
// spawn is a dirty side effect used in spawning
// drop & rise are supplied by the physics below to indicate gravity/push for
// current mini-timestep

bool
collide(DynamicEntity *d, bool spawn, float drop, float rise)
{
	// figure out integer cube rectangle this entity covers in map
	const float fx1 = d.o.x - d.radius;


	const float fy1 = d.o.y - d.radius;
	const float fx2 = d.o.x + d.radius;
	const float fy2 = d.o.y + d.radius;
	const int x1 = fast_f2nat(fx1);
	const int y1 = fast_f2nat(fy1);
	const int x2 = fast_f2nat(fx2);
	const int y2 = fast_f2nat(fy2);
	float hi = 127, lo = -128;
	// big monsters are afraid of heights, unless angry :)
	float minfloor = (d.monsterstate && !spawn && d.health > 100)
	    ? d.o.z - d.eyeheight - 4.5f
	    : -1000.0f;


	for (int x = x1; x <= x2; x++)
		for (int y = y1; y <= y2; y++) {

			// collide with map
			if (OUTBORD(x, y))
				return false;
			sqr *s = S(x, y);
			float ceil = s->ceil;
			float floor = s->floor;
			switch (s->type) {
			case SOLID:
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
				hi = ceil;
			if (floor > lo)
				lo = floor;
			if (floor < minfloor)
				return false;
		}

	if (hi - lo < d->eyeheight + d->aboveeye)
		return false;

	float headspace = 10;
	loopv(players) // collide with other players
	{
		dynent *o = players[i];
		if (!o || o == d)
			continue;
		if (!plcollide(d, o, headspace, hi, lo))
			return false;
	}
	if (d != player1)
		if (!plcollide(d, player1, headspace, hi, lo))
			return false;
	dvector &v = getmonsters();
	// this loop can be a performance bottleneck with many monster on a slow
	// cpu, should replace with a blockmap but seems mostly fast enough

	loopv(v) if (!vreject(d->o, v[i]->o, 7.0f) && d != v[i] &&
	    !plcollide(d, v[i], headspace, hi, lo)) return false;

	headspace -= 0.01f;

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

	if (spawn) {
		d->o.z = lo + d->eyeheight; // just drop to floor (sideeffect)

		d->onfloor = true;
	} else {
		const float space = d->o.z - d->eyeheight - lo;
		if (space < 0) {
			if (space > -0.01)
				d->o.z = lo + d->eyeheight; // stick on step


			else if (space > -1.26f)
				d->o.z += rise; // rise thru stair


			else
				return false;
		} else {


			d->o.z -= min(min(drop, space), headspace); // gravity
		}

		const float space2 = hi - (d->o.z + d->aboveeye);
		if (space2 < 0) {
			if (space2 < -0.1)
				return false;      // hack alert!
			d->o.z = hi - d->aboveeye; // glue to ceiling

			d->vel.z = 0; // cancel out jumping velocity

		}

		d->onfloor = d->o.z - d->eyeheight - lo < 0.001f;
	}
	return true;
}

float
rad(float x)
{







|



|
<
|
<

|





<


>
|
|
>





|
>
|

|


|
>
>

|
>
>


|
>
>
|
|
<
|


|
|
>
|
>


|







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
				hi = ceil;
			if (floor > lo)
				lo = floor;
			if (floor < minfloor)
				return false;
		}

	if (hi - lo < d.eyeheight + d.aboveeye)
		return false;

	float headspace = 10;
	for (id player in players) {

		if (player == [OFNull null] || player == d)

			continue;
		if (!plcollide(d, player, headspace, hi, lo))
			return false;
	}
	if (d != player1)
		if (!plcollide(d, player1, headspace, hi, lo))
			return false;

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

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

	if (spawn) {
		// just drop to floor (sideeffect)
		d.o = OFMakeVector3D(d.o.x, d.o.y, lo + d.eyeheight);
		d.onfloor = true;
	} else {
		const float space = d.o.z - d.eyeheight - lo;
		if (space < 0) {
			if (space > -0.01)
				// stick on step
				d.o = OFMakeVector3D(
				    d.o.x, d.o.y, lo + d.eyeheight);
			else if (space > -1.26f)
				// rise thru stair
				d.o =
				    OFMakeVector3D(d.o.x, d.o.y, d.o.z + rise);
			else
				return false;
		} else
			// gravity
			d.o = OFMakeVector3D(d.o.x, d.o.y,
			    d.o.z - min(min(drop, space), headspace));


		const float space2 = hi - (d.o.z + d.aboveeye);
		if (space2 < 0) {
			if (space2 < -0.1)
				return false; // hack alert!
			// glue to ceiling
			d.o = OFMakeVector3D(d.o.x, d.o.y, hi - d.aboveeye);
			// cancel out jumping velocity
			d.vel = OFMakeVector3D(d.vel.x, d.vel.y, 0);
		}

		d.onfloor = d.o.z - d.eyeheight - lo < 0.001f;
	}
	return true;
}

float
rad(float x)
{
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

269
270
271
272
273
274
275
276
277
278

279
280
281
282
283

284
285
286
287
288
289


290
291
292
293
294
295
296
297
298

299

300
301
302
303
304
305

306
307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

390
391
392

393

394
395
396
397
398
399
400
401
402
403
}

// main physics routine, moves a player/monster for a curtime step
// moveres indicated the physics precision (which is lower for monsters and
// multiplayer prediction) local is false for multiplayer prediction

void
moveplayer(dynent *pl, int moveres, bool local, int curtime)
{
	const bool water = hdr.waterlevel > pl->o.z - 0.5f;
	const bool floating = (editmode && local) || pl->state == CS_EDITING;

	OFVector3D d; // vector of direction we ideally want to move in

	d.x = (float)(pl->move * cos(rad(pl->yaw - 90)));
	d.y = (float)(pl->move * sin(rad(pl->yaw - 90)));
	d.z = 0;

	if (floating || water) {
		d.x *= (float)cos(rad(pl->pitch));
		d.y *= (float)cos(rad(pl->pitch));
		d.z = (float)(pl->move * sin(rad(pl->pitch)));
	}

	d.x += (float)(pl->strafe * cos(rad(pl->yaw - 180)));
	d.y += (float)(pl->strafe * sin(rad(pl->yaw - 180)));

	const float speed =
	    curtime / (water ? 2000.0f : 1000.0f) * pl->maxspeed;
	const float friction =
	    water ? 20.0f : (pl->onfloor || floating ? 6.0f : 30.0f);

	const float fpsfric = friction / curtime * 20.0f;

	vmul(pl->vel, fpsfric - 1); // slowly apply friction and direction to
	                            // velocity, gives a smooth movement

	vadd(pl->vel, d);
	vdiv(pl->vel, fpsfric);
	d = pl->vel;
	vmul(d, speed); // d is now frametime based velocity vector

	pl->blocked = false;
	pl->moving = true;

	if (floating) // just apply velocity
	{

		vadd(pl->o, d);
		if (pl->jumpnext) {
			pl->jumpnext = false;
			pl->vel.z = 2;
		}

	} else // apply velocity with collision
	{
		if (pl->onfloor || water) {
			if (pl->jumpnext) {
				pl->jumpnext = false;
				pl->vel.z = 1.7f; // physics impulse upwards


				// dampen velocity change even harder, gives
				// correct water feel
				if (water) {
					pl->vel.x /= 8;
					pl->vel.y /= 8;
				}
				if (local)
					playsoundc(S_JUMP);
				else if (pl->monsterstate)

					playsound(S_JUMP, &pl->o);

			} else if (pl->timeinair > 800) {
				// if we land after long time must have been a
				// high jump, make thud sound
				if (local)
					playsoundc(S_LAND);
				else if (pl->monsterstate)

					playsound(S_LAND, &pl->o);
			}

			pl->timeinair = 0;
		} else {
			pl->timeinair += curtime;
		}

		const float gravity = 20;
		const float f = 1.0f / moveres;
		// incorrect, but works fine
		float dropf = ((gravity - 1) + pl->timeinair / 15.0f);
		// float slowly down in water
		if (water) {
			dropf = 5;
			pl->timeinair = 0;
		}
		const float drop = dropf * curtime / gravity / 100 /
		    moveres; // at high fps, gravity kicks in too fast
		const float rise = speed / moveres /
		    1.2f; // extra smoothness when lifting up stairs


		loopi(moveres) // discrete steps collision detection & sliding
		{
			// try move forward
			pl->o.x += f * d.x;
			pl->o.y += f * d.y;
			pl->o.z += f * d.z;
			if (collide(pl, false, drop, rise))
				continue;
			// player stuck, try slide along y axis
			pl->blocked = true;
			pl->o.x -= f * d.x;
			if (collide(pl, false, drop, rise)) {
				d.x = 0;
				continue;
			}
			pl->o.x += f * d.x;
			// still stuck, try x axis

			pl->o.y -= f * d.y;
			if (collide(pl, false, drop, rise)) {
				d.y = 0;
				continue;
			}
			pl->o.y += f * d.y;
			// try just dropping down
			pl->moving = false;
			pl->o.x -= f * d.x;
			pl->o.y -= f * d.y;
			if (collide(pl, false, drop, rise)) {
				d.y = d.x = 0;
				continue;
			}
			pl->o.z -= f * d.z;
			break;
		}
	}

	// detect wether player is outside map, used for skipping zbuffer clear
	// mostly

	if (pl->o.x < 0 || pl->o.x >= ssize || pl->o.y < 0 || pl->o.y > ssize) {
		pl->outsidemap = true;
	} else {
		sqr *s = S((int)pl->o.x, (int)pl->o.y);
		pl->outsidemap = SOLID(s) ||
		    pl->o.z < s->floor - (s->type == FHF ? s->vdelta / 4 : 0) ||
		    pl->o.z > s->ceil + (s->type == CHF ? s->vdelta / 4 : 0);
	}

	// automatically apply smooth roll when strafing

	if (pl->strafe == 0) {
		pl->roll = pl->roll / (1 + (float)sqrt((float)curtime) / 25);
	} else {
		pl->roll += pl->strafe * curtime / -30.0f;
		if (pl->roll > maxroll)
			pl->roll = (float)maxroll;
		if (pl->roll < -maxroll)
			pl->roll = (float)-maxroll;
	}

	// play sounds on water transitions

	if (!pl->inwater && water) {

		playsound(S_SPLASH2, &pl->o);
		pl->vel.z = 0;
	} else if (pl->inwater && !water)

		playsound(S_SPLASH1, &pl->o);

	pl->inwater = water;
}

void
moveplayer(dynent *pl, int moveres, bool local)
{
	loopi(physicsrepeat) moveplayer(pl, moveres, local,
	    i ? curtime / physicsrepeat
	      : curtime - curtime / physicsrepeat * (physicsrepeat - 1));
}







|

|
|



|
|



|
|
|


|
|

<
|

|



|
|
>
|
|
|


|
|

|
<
>
|
|
|
|

>
|
<
|
|
|
|
>
>


|
|
|
<


|
>
|
>
|




|
>
|
|
>
|
|
|
<




|



|

<
|
|
|
>




|
|
<



|
|




<

>
|




<

|
|
<




|







|
|
|
|
|
|
|




|
|
|
|
|
|
|
|




|
>
|
|
|
>
|
>
|



|





241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

286
287
288
289
290
291
292
293

294
295
296
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

324
325
326
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341
342
343

344
345
346
347
348
349
350
351
352

353
354
355
356
357
358
359

360
361
362

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
}

// main physics routine, moves a player/monster for a curtime step
// moveres indicated the physics precision (which is lower for monsters and
// multiplayer prediction) local is false for multiplayer prediction

void
moveplayer(DynamicEntity *pl, int moveres, bool local, int curtime)
{
	const bool water = hdr.waterlevel > pl.o.z - 0.5f;
	const bool floating = (editmode && local) || pl.state == CS_EDITING;

	OFVector3D d; // vector of direction we ideally want to move in

	d.x = (float)(pl.move * cos(rad(pl.yaw - 90)));
	d.y = (float)(pl.move * sin(rad(pl.yaw - 90)));
	d.z = 0;

	if (floating || water) {
		d.x *= (float)cos(rad(pl.pitch));
		d.y *= (float)cos(rad(pl.pitch));
		d.z = (float)(pl.move * sin(rad(pl.pitch)));
	}

	d.x += (float)(pl.strafe * cos(rad(pl.yaw - 180)));
	d.y += (float)(pl.strafe * sin(rad(pl.yaw - 180)));


	const float speed = curtime / (water ? 2000.0f : 1000.0f) * pl.maxspeed;
	const float friction =
	    water ? 20.0f : (pl.onfloor || floating ? 6.0f : 30.0f);

	const float fpsfric = friction / curtime * 20.0f;

	// slowly apply friction and direction to
	// velocity, gives a smooth movement
	vmul(pl.vel, fpsfric - 1);
	vadd(pl.vel, d);
	vdiv(pl.vel, fpsfric);
	d = pl.vel;
	vmul(d, speed); // d is now frametime based velocity vector

	pl.blocked = false;
	pl.moving = true;

	if (floating) {

		// just apply velocity
		vadd(pl.o, d);
		if (pl.jumpnext) {
			pl.jumpnext = false;
			pl.vel = OFMakeVector3D(pl.vel.x, pl.vel.y, 2);
		}
	} else {
		// apply velocity with collision

		if (pl.onfloor || water) {
			if (pl.jumpnext) {
				pl.jumpnext = false;
				// physics impulse upwards
				pl.vel =
				    OFMakeVector3D(pl.vel.x, pl.vel.y, 1.7);
				// dampen velocity change even harder, gives
				// correct water feel
				if (water)
					pl.vel = OFMakeVector3D(pl.vel.x / 8,
					    pl.vel.y / 8, pl.vel.z);

				if (local)
					playsoundc(S_JUMP);
				else if (pl.monsterstate) {
					OFVector3D loc = pl.o;
					playsound(S_JUMP, &loc);
				}
			} else if (pl.timeinair > 800) {
				// if we land after long time must have been a
				// high jump, make thud sound
				if (local)
					playsoundc(S_LAND);
				else if (pl.monsterstate) {
					OFVector3D loc = pl.o;
					playsound(S_LAND, &loc);
				}
			}
			pl.timeinair = 0;
		} else
			pl.timeinair += curtime;


		const float gravity = 20;
		const float f = 1.0f / moveres;
		// incorrect, but works fine
		float dropf = ((gravity - 1) + pl.timeinair / 15.0f);
		// float slowly down in water
		if (water) {
			dropf = 5;
			pl.timeinair = 0;
		}

		// at high fps, gravity kicks in too fast
		const float drop = dropf * curtime / gravity / 100 / moveres;
		// extra smoothness when lifting up stairs
		const float rise = speed / moveres / 1.2f;

		loopi(moveres) // discrete steps collision detection & sliding
		{
			// try move forward
			pl.o = OFMakeVector3D(pl.o.x + f * d.x,
			    pl.o.y + f * d.y, pl.o.z + f * d.z);

			if (collide(pl, false, drop, rise))
				continue;
			// player stuck, try slide along y axis
			pl.blocked = true;
			pl.o = OFMakeVector3D(pl.o.x - f * d.x, pl.o.y, pl.o.z);
			if (collide(pl, false, drop, rise)) {
				d.x = 0;
				continue;
			}

			// still stuck, try x axis
			pl.o = OFMakeVector3D(
			    pl.o.x + f * d.x, pl.o.y - f * d.y, pl.o.z);
			if (collide(pl, false, drop, rise)) {
				d.y = 0;
				continue;
			}

			// try just dropping down
			pl.moving = false;
			pl.o = OFMakeVector3D(pl.o.x - f * d.x, pl.o.y, pl.o.z);

			if (collide(pl, false, drop, rise)) {
				d.y = d.x = 0;
				continue;
			}
			pl.o = OFMakeVector3D(pl.o.x, pl.o.y, pl.o.z - f * d.z);
			break;
		}
	}

	// detect wether player is outside map, used for skipping zbuffer clear
	// mostly

	if (pl.o.x < 0 || pl.o.x >= ssize || pl.o.y < 0 || pl.o.y > ssize)
		pl.outsidemap = true;
	else {
		sqr *s = S((int)pl.o.x, (int)pl.o.y);
		pl.outsidemap = SOLID(s) ||
		    pl.o.z < s->floor - (s->type == FHF ? s->vdelta / 4 : 0) ||
		    pl.o.z > s->ceil + (s->type == CHF ? s->vdelta / 4 : 0);
	}

	// automatically apply smooth roll when strafing

	if (pl.strafe == 0)
		pl.roll = pl.roll / (1 + (float)sqrt((float)curtime) / 25);
	else {
		pl.roll += pl.strafe * curtime / -30.0f;
		if (pl.roll > maxroll)
			pl.roll = (float)maxroll;
		if (pl.roll < -maxroll)
			pl.roll = (float)-maxroll;
	}

	// play sounds on water transitions

	if (!pl.inwater && water) {
		OFVector3D loc = pl.o;
		playsound(S_SPLASH2, &loc);
		pl.vel = OFMakeVector3D(pl.vel.x, pl.vel.y, 0);
	} else if (pl.inwater && !water) {
		OFVector3D loc = pl.o;
		playsound(S_SPLASH1, &loc);
	}
	pl.inwater = water;
}

void
moveplayer(DynamicEntity *pl, int moveres, bool local)
{
	loopi(physicsrepeat) moveplayer(pl, moveres, local,
	    i ? curtime / physicsrepeat
	      : curtime - curtime / physicsrepeat * (physicsrepeat - 1));
}