Cube  Check-in [51fb59fc93]

Overview
Comment:Remove fast_f2nat
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 51fb59fc934df4829a82f7992f10435519dc8deb7098c3be1b17642fc6c05a44
User & Date: js on 2025-03-23 17:45:28.982
Other Links: manifest | tags
Context
2025-03-23
19:40
Clean up identifiers, use blocks for commands check-in: d7661be1b1 user: js tags: trunk
17:45
Remove fast_f2nat check-in: 51fb59fc93 user: js tags: trunk
17:41
Remove dotprod check-in: da90479adf user: js tags: trunk
Changes
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	int steps = (int)(sqrt(dx * dx + dy * dy) / 0.9);
	if (!steps)
		return false;
	float x = lx;
	float y = ly;
	int i = 0;
	for (;;) {
		struct sqr *s = S(fast_f2nat(x), fast_f2nat(y));
		if (SOLID(s))
			break;
		float floor = s->floor;
		if (s->type == FHF)
			floor -= s->vdelta / 4.0f;
		float ceil = s->ceil;
		if (s->type == CHF)







|







194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	int steps = (int)(sqrt(dx * dx + dy * dy) / 0.9);
	if (!steps)
		return false;
	float x = lx;
	float y = ly;
	int i = 0;
	for (;;) {
		struct sqr *s = S((int)x, (int)y);
		if (SOLID(s))
			break;
		float floor = s->floor;
		if (s->type == FHF)
			floor -= s->vdelta / 4.0f;
		float ceil = s->ceil;
		if (s->type == CHF)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
collide(DynamicEntity *d, bool spawn, float drop, float rise)
{
	// figure out integer cube rectangle this entity covers in map
	const float fx1 = d.origin.x - d.radius;
	const float fy1 = d.origin.y - d.radius;
	const float fx2 = d.origin.x + d.radius;
	const float fy2 = d.origin.y + d.radius;
	const int x1 = fast_f2nat(fx1);
	const int y1 = fast_f2nat(fy1);
	const int x2 = fast_f2nat(fx2);
	const int y2 = fast_f2nat(fy2);
	float hi = 127, lo = -128;
	// big monsters are afraid of heights, unless angry :)
	float minfloor =
	    ([d isKindOfClass:Monster.class] && !spawn && d.health > 100
	            ? d.origin.z - d.eyeHeight - 4.5f
	            : -1000.0f);








|
|
|
|







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
collide(DynamicEntity *d, bool spawn, float drop, float rise)
{
	// figure out integer cube rectangle this entity covers in map
	const float fx1 = d.origin.x - d.radius;
	const float fy1 = d.origin.y - d.radius;
	const float fx2 = d.origin.x + d.radius;
	const float fy2 = d.origin.y + d.radius;
	const int x1 = fx1;
	const int y1 = fy1;
	const int x2 = fx2;
	const int y2 = fy2;
	float hi = 127, lo = -128;
	// big monsters are afraid of heights, unless angry :)
	float minfloor =
	    ([d isKindOfClass:Monster.class] && !spawn && d.health > 100
	            ? d.origin.z - d.eyeHeight - 4.5f
	            : -1000.0f);

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
			rnd(i + 1);                          \
	}

#ifndef OF_WINDOWS
# define __cdecl
#endif

#define fast_f2nat(val) ((int)(val))

#ifdef __cplusplus
extern "C" {
#endif
extern void endianswap(void *, int, int);
#ifdef __cplusplus
}
#endif







<
<







32
33
34
35
36
37
38


39
40
41
42
43
44
45
			rnd(i + 1);                          \
	}

#ifndef OF_WINDOWS
# define __cdecl
#endif



#ifdef __cplusplus
extern "C" {
#endif
extern void endianswap(void *, int, int);
#ifdef __cplusplus
}
#endif
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
	const int PRECBITS = 12;
	const float PRECF = 4096.0f;
	int x = (int)(lx * PRECF);
	int y = (int)(ly * PRECF);
	int l = light.attr2 << PRECBITS;
	int stepx = (int)(dx / (float)steps * PRECF);
	int stepy = (int)(dy / (float)steps * PRECF);

	int stepl =
	    fast_f2nat(l / (float)steps); // incorrect: light will fade quicker
	                                  // if near edge of the world

	if (hasoverbright) {
		l /= lightscale;
		stepl /= lightscale;

		// coloured light version, special case because most lights are
		// white
		if (light.attr3 || light.attr4) {
			int dimness = rnd(
			    (255 -
			        (light.attr2 + light.attr3 + light.attr4) / 3) /
			        16 +
			    1);
			x += stepx * dimness;
			y += stepy * dimness;

			if (OUTBORD(x >> PRECBITS, y >> PRECBITS))
				return;

			int g = light.attr3 << PRECBITS;
			int stepg = fast_f2nat(g / (float)steps);
			int b = light.attr4 << PRECBITS;
			int stepb = fast_f2nat(b / (float)steps);
			g /= lightscale;
			stepg /= lightscale;
			b /= lightscale;
			stepb /= lightscale;
			for (int i = 0; i < steps; i++) {
				struct sqr *s = S(x >> PRECBITS, y >> PRECBITS);
				int tl = (l >> PRECBITS) + s->r;







>
|
<
<




















|

|







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
	const int PRECBITS = 12;
	const float PRECF = 4096.0f;
	int x = (int)(lx * PRECF);
	int y = (int)(ly * PRECF);
	int l = light.attr2 << PRECBITS;
	int stepx = (int)(dx / (float)steps * PRECF);
	int stepy = (int)(dy / (float)steps * PRECF);
	// incorrect: light will fade quicker if near edge of the world
	int stepl = l / (float)steps;



	if (hasoverbright) {
		l /= lightscale;
		stepl /= lightscale;

		// coloured light version, special case because most lights are
		// white
		if (light.attr3 || light.attr4) {
			int dimness = rnd(
			    (255 -
			        (light.attr2 + light.attr3 + light.attr4) / 3) /
			        16 +
			    1);
			x += stepx * dimness;
			y += stepy * dimness;

			if (OUTBORD(x >> PRECBITS, y >> PRECBITS))
				return;

			int g = light.attr3 << PRECBITS;
			int stepg = g / (float)steps;
			int b = light.attr4 << PRECBITS;
			int stepb = b / (float)steps;
			g /= lightscale;
			stepg /= lightscale;
			b /= lightscale;
			stepb /= lightscale;
			for (int i = 0; i < steps; i++) {
				struct sqr *s = S(x >> PRECBITS, y >> PRECBITS);
				int tl = (l >> PRECBITS) + s->r;
31
32
33
34
35
36
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	float apitch = (float)fabs(player1.pitch);
	float af = getvar(@"fov") / 2 + apitch / 1.5f + 3;
	float byaw = (player1.yaw - 90 + af) / 360 * PI2;
	float syaw = (player1.yaw - 90 - af) / 360 * PI2;

	for (int i = 0; i < NUMRAYS; i++) {
		float angle = i * PI2 / NUMRAYS;

		if ((apitch > 45 // must be bigger if fov>120
		        || (angle < byaw && angle > syaw) ||
		        (angle < byaw - PI2 && angle > syaw - PI2) ||
		        (angle < byaw + PI2 && angle > syaw + PI2)) &&
		    !OUTBORD(vx, vy) &&
		    !SOLID(S(fast_f2nat(vx),
		        fast_f2nat(vy)))) // try to avoid tracing ray if outside
		                          // of frustrum
		{
			float ray = i * 8 / (float)NUMRAYS;
			float dx, dy;
			if (ray > 1 && ray < 3) {
				dx = -(ray - 2);
				dy = 1;
			} else if (ray >= 3 && ray < 5) {
				dx = -1;







>
|
|


|
<
<
<
<







31
32
33
34
35
36
37
38
39
40
41
42
43




44
45
46
47
48
49
50
	float apitch = (float)fabs(player1.pitch);
	float af = getvar(@"fov") / 2 + apitch / 1.5f + 3;
	float byaw = (player1.yaw - 90 + af) / 360 * PI2;
	float syaw = (player1.yaw - 90 - af) / 360 * PI2;

	for (int i = 0; i < NUMRAYS; i++) {
		float angle = i * PI2 / NUMRAYS;
		// try to avoid tracing ray if outside of frustrum
		// apitch must be bigger if fov > 120
		if ((apitch > 45 || (angle < byaw && angle > syaw) ||
		        (angle < byaw - PI2 && angle > syaw - PI2) ||
		        (angle < byaw + PI2 && angle > syaw + PI2)) &&
		    !OUTBORD(vx, vy) && !SOLID(S((int)vx, (int)vy))) {




			float ray = i * 8 / (float)NUMRAYS;
			float dx, dy;
			if (ray > 1 && ray < 3) {
				dx = -(ray - 2);
				dy = 1;
			} else if (ray >= 3 && ray < 5) {
				dx = -1;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
			float sx = vx;
			float sy = vy;
			for (;;) {
				sx += dx;
				sy += dy;
				// 90% of time spend in this function is on this
				// line
				if (SOLID(S(fast_f2nat(sx), fast_f2nat(sy)))) {
					rdist[i] = (float)(fabs(sx - vx) +
					    fabs(sy - vy));
					break;
				}
			}
		} else
			rdist[i] = 2;







|







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
			float sx = vx;
			float sy = vy;
			for (;;) {
				sx += dx;
				sy += dy;
				// 90% of time spend in this function is on this
				// line
				if (SOLID(S((int)sx, (int)sy))) {
					rdist[i] = (float)(fabs(sx - vx) +
					    fabs(sy - vy));
					break;
				}
			}
		} else
			rdist[i] = 2;
179
180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
195
196
197
198
199
200
				l = ma(cx - vx, cy + csize - vy);
			} // E
		} else {
			h = ca(cx + csize - vx, cy - vy);
			l = ca(cx - vx, cy + csize - vy);
		} // H
	}
	int si = fast_f2nat(h * (NUMRAYS / 8)) +
	    NUMRAYS; // get indexes into occlusion map from angles

	int ei = fast_f2nat(l * (NUMRAYS / 8)) + NUMRAYS + 1;
	if (ei <= si)
		ei += NUMRAYS;

	for (int i = si; i <= ei; i++) {
		if (dist < rdist[i & (NUMRAYS - 1)])
			// if any value in this segment of the occlusion map is
			// further away then cube is not occluded
			return 0;
	}

	return 1; // cube is entirely occluded
}







<
|
>
|












176
177
178
179
180
181
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
				l = ma(cx - vx, cy + csize - vy);
			} // E
		} else {
			h = ca(cx + csize - vx, cy - vy);
			l = ca(cx - vx, cy + csize - vy);
		} // H
	}

	// get indexes into occlusion map from angles
	int si = h * (NUMRAYS / 8) + NUMRAYS;
	int ei = l * (NUMRAYS / 8) + NUMRAYS + 1;
	if (ei <= si)
		ei += NUMRAYS;

	for (int i = si; i <= ei; i++) {
		if (dist < rdist[i & (NUMRAYS - 1)])
			// if any value in this segment of the occlusion map is
			// further away then cube is not occluded
			return 0;
	}

	return 1; // cube is entirely occluded
}