Cube  Diff

Differences From Artifact [c9005101f7]:

To Artifact [1c146ff92c]:


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



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













-
+

-
+






-
-
-
-
+
+
+
+





-
-
-
+
+
+




+
-
-
+
+
-

-
+







-
-
-
+
+
+





+
-
-
+
+















-
-
-
-
+
+
+
+







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

#include "cube.h"

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

// collide with player or monster
bool
static bool
plcollide(
    DynamicEntity *d, DynamicEntity *o, float &headspace, float &hi, float &lo)
    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 (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;
		*headspace = d.o.z - o.o.z - o.aboveeye - d.eyeheight;
		if (*headspace < 0)
			*headspace = 10;
	}
	return true;
}

// recursively collide with a mipmapped corner cube
bool
cornertest(int mip, int x, int y, int dx, int dy, int &bx, int &by,
static bool
cornertest(int mip, int x, int y, int dx, int dy, int *bx, int *by, int *bs)
    int &bs) // recursively collide with a mipmapped corner cube
{
	sqr *w = wmip[mip];
	struct sqr *w = wmip[mip];
	int sz = ssize >> mip;
	bool stest =
	    SOLID(SWS(w, x + dx, y, sz)) && SOLID(SWS(w, x, y + dy, sz));
	mip++;
	x /= 2;
	y /= 2;
	if (SWS(wmip[mip], x, y, ssize >> mip)->type == CORNER) {
		bx = x << mip;
		by = y << mip;
		bs = 1 << mip;
		*bx = x << mip;
		*by = y << mip;
		*bs = 1 << mip;
		return cornertest(mip, x, y, dx, dy, bx, by, bs);
	}
	return stest;
}

// collide with a mapmodel
void
mmcollide(DynamicEntity *d, float &hi, float &lo) // collide with a mapmodel
static void
mmcollide(DynamicEntity *d, float *hi, float *lo)
{
	for (Entity *e in ents) {
		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;
				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
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
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







-
+










-
+



-
+



-
+


-
+
+







	    : -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);
			struct sqr *s = S(x, y);
			float ceil = s->ceil;
			float floor = s->floor;
			switch (s->type) {
			case SOLID:
				return false;

			case CORNER: {
				int bx = x, by = y, bs = 1;
				if (x == x1 && y == y1 &&
				        cornertest(
				            0, x, y, -1, -1, bx, by, bs) &&
				            0, x, y, -1, -1, &bx, &by, &bs) &&
				        fx1 - bx + fy1 - by <= bs ||
				    x == x2 && y == y1 &&
				        cornertest(
				            0, x, y, 1, -1, bx, by, bs) &&
				            0, x, y, 1, -1, &bx, &by, &bs) &&
				        fx2 - bx >= fy1 - by ||
				    x == x1 && y == y2 &&
				        cornertest(
				            0, x, y, -1, 1, bx, by, bs) &&
				            0, x, y, -1, 1, &bx, &by, &bs) &&
				        fx1 - bx <= fy2 - by ||
				    x == x2 && y == y2 &&
				        cornertest(0, x, y, 1, 1, bx, by, bs) &&
				        cornertest(
				            0, x, y, 1, 1, &bx, &by, &bs) &&
				        fx2 - bx + fy2 - by >= bs)
					return false;
				break;
			}

			case FHF: // FIXME: too simplistic collision with
			          // slopes, makes it feels like tiny stairs
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
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







-
+



-
+





-
+



-
+







	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))
		if (!plcollide(d, player, &headspace, &hi, &lo))
			return false;
	}
	if (d != player1)
		if (!plcollide(d, player1, headspace, hi, lo))
		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))
		    !plcollide(d, monster, &headspace, &hi, &lo))
			return false;
	headspace -= 0.01f;

	mmcollide(d, hi, lo); // collide with map models
	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;
242
243
244
245
246
247
248
249
250


251
252
253
254
255
256
257
244
245
246
247
248
249
250


251
252
253
254
255
256
257
258
259







-
-
+
+







	}
}

// 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)
static void
moveplayer4(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)));
373
374
375
376
377
378
379
380

381
382
383
384
385
386
387
375
376
377
378
379
380
381

382
383
384
385
386
387
388
389







-
+








	// 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);
		struct 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

407
408
409
410
411
412
413
414

415
416
417
409
410
411
412
413
414
415

416
417
418
419







-
+



	}
	pl.inwater = water;
}

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