Cube  Diff

Differences From Artifact [106a45f580]:

To Artifact [ef13aa2477]:


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

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








-
+

+
+
-
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
-
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+

+
-
+

-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-


+
-
+

-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+

+
-
-
-
-
+
+
+
+
+
+
+
+
+


+
-
+
+

-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+


+
-
+

-
-
-
-
-
+
+
+
+
+

-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+






+
-
+

-
+
-
-
-
-
-
+
+
+
+


+
-
+

+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

+
-
-
-
-
+
+
+
+
+
+
+

-
+

+
-
-
-
+
+
+




+
-
+

-
-
-
-
-
+
+
+
+
+
+
+


+
-
+

-
-
-
+
+
+
+
+

-
-
// worldlight.cpp

#include "cube.h"

extern bool hasoverbright;

VAR(lightscale,1,4,100);
VAR(lightscale, 1, 4, 100);

void
lightray(float bx, float by,
void lightray(float bx, float by, persistent_entity &light)     // done in realtime, needs to be fast
    persistent_entity &light) // done in realtime, needs to be fast
{
    float lx = light.x+(rnd(21)-10)*0.1f;
    float ly = light.y+(rnd(21)-10)*0.1f;
    float dx = bx-lx;
    float dy = by-ly; 
    float dist = (float)sqrt(dx*dx+dy*dy);
    if(dist<1.0f) return;
    int reach = light.attr1;
    int steps = (int)(reach*reach*1.6f/dist); // can change this for speedup/quality?
    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
	float lx = light.x + (rnd(21) - 10) * 0.1f;
	float ly = light.y + (rnd(21) - 10) * 0.1f;
	float dx = bx - lx;
	float dy = by - ly;
	float dist = (float)sqrt(dx * dx + dy * dy);
	if (dist < 1.0f)
		return;
	int reach = light.attr1;
	int steps = (int)(reach * reach * 1.6f /
	                  dist); // can change this for speedup/quality?
	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)
	if (hasoverbright) {
    {
        l /= lightscale;
        stepl /= lightscale;
        
        if(light.attr3 || light.attr4)      // coloured light version, special case because most lights are white
        {
            int dimness = rnd((255-(light.attr2+light.attr3+light.attr4)/3)/16+1);  
            x += stepx*dimness;
            y += stepy*dimness;
		l /= lightscale;
		stepl /= lightscale;

		if (light.attr3 ||
		    light.attr4) // coloured light version, special case because
		                 // most lights are white
		{
			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))
            if(OUTBORD(x>>PRECBITS, y>>PRECBITS)) return;
				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;
            loopi(steps)
            {
                sqr *s = S(x>>PRECBITS, y>>PRECBITS); 
                int tl = (l>>PRECBITS)+s->r;
                s->r = tl>255 ? 255 : tl;
                tl = (g>>PRECBITS)+s->g;
                s->g = tl>255 ? 255 : tl;
                tl = (b>>PRECBITS)+s->b;
                s->b = tl>255 ? 255 : tl;
                if(SOLID(s)) return;
                x += stepx;
                y += stepy;
                l -= stepl;
                g -= stepg;
                b -= stepb;
                stepl -= 25;
                stepg -= 25;
                stepb -= 25;
            };
			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;
			loopi(steps)
			{
				sqr *s = S(x >> PRECBITS, y >> PRECBITS);
				int tl = (l >> PRECBITS) + s->r;
				s->r = tl > 255 ? 255 : tl;
				tl = (g >> PRECBITS) + s->g;
				s->g = tl > 255 ? 255 : tl;
				tl = (b >> PRECBITS) + s->b;
				s->b = tl > 255 ? 255 : tl;
				if (SOLID(s))
					return;
				x += stepx;
				y += stepy;
				l -= stepl;
				g -= stepg;
				b -= stepb;
				stepl -= 25;
				stepg -= 25;
				stepb -= 25;
			};
        }
        else        // white light, special optimized version
        {
            int dimness = rnd((255-light.attr2)/16+1);  
            x += stepx*dimness;
            y += stepy*dimness;
		} else // white light, special optimized version
		{
			int dimness = rnd((255 - light.attr2) / 16 + 1);
			x += stepx * dimness;
			y += stepy * dimness;

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

            loopi(steps)
            {
                sqr *s = S(x>>PRECBITS, y>>PRECBITS); 
                int tl = (l>>PRECBITS)+s->r;
                s->r = s->g = s->b = tl>255 ? 255 : tl;       
                if(SOLID(s)) return;
                x += stepx;
                y += stepy;
                l -= stepl;
                stepl -= 25;
            };
        };
			loopi(steps)
			{
				sqr *s = S(x >> PRECBITS, y >> PRECBITS);
				int tl = (l >> PRECBITS) + s->r;
				s->r = s->g = s->b = tl > 255 ? 255 : tl;
				if (SOLID(s))
					return;
				x += stepx;
				y += stepy;
				l -= stepl;
				stepl -= 25;
			};
		};
    }
    else        // the old (white) light code, here for the few people with old video cards that don't support overbright
    {
        loopi(steps)
        {
            sqr *s = S(x>>PRECBITS, y>>PRECBITS); 
            int light = l>>PRECBITS;
            if(light>s->r) s->r = s->g = s->b = (uchar)light;
            if(SOLID(s)) return;
            x += stepx;
            y += stepy;
            l -= stepl;
        };
    };
	} else // the old (white) light code, here for the few people with old
	       // video cards that don't support overbright
	{
		loopi(steps)
		{
			sqr *s = S(x >> PRECBITS, y >> PRECBITS);
			int light = l >> PRECBITS;
			if (light > s->r)
				s->r = s->g = s->b = (uchar)light;
			if (SOLID(s))
				return;
			x += stepx;
			y += stepy;
			l -= stepl;
		};
	};
    
};

void
void calclightsource(persistent_entity &l)
calclightsource(persistent_entity &l)
{
    int reach = l.attr1;
    int sx = l.x-reach;
    int ex = l.x+reach;
    int sy = l.y-reach;
    int ey = l.y+reach;
    
    rndreset();
    
    const float s = 0.8f;
	int reach = l.attr1;
	int sx = l.x - reach;
	int ex = l.x + reach;
	int sy = l.y - reach;
	int ey = l.y + reach;

	rndreset();

	const float s = 0.8f;

	for (float sx2 = (float)sx; sx2 <= ex; sx2 += s * 2) {
    for(float sx2 = (float)sx; sx2<=ex; sx2+=s*2) { lightray(sx2, (float)sy, l); lightray(sx2, (float)ey, l); };
    for(float sy2 = sy+s; sy2<=ey-s; sy2+=s*2)    { lightray((float)sx, sy2, l); lightray((float)ex, sy2, l); };
    
    rndtime();
		lightray(sx2, (float)sy, l);
		lightray(sx2, (float)ey, l);
	};
	for (float sy2 = sy + s; sy2 <= ey - s; sy2 += s * 2) {
		lightray((float)sx, sy2, l);
		lightray((float)ex, sy2, l);
	};

	rndtime();
};

void
void postlightarea(block &a)    // median filter, smooths out random noise in light and makes it more mipable
postlightarea(block &a) // median filter, smooths out random noise in light and
                        // makes it more mipable
{
    loop(x,a.xs) loop(y,a.ys)   // assumes area not on edge of world
    {
        sqr *s = S(x+a.x,y+a.y);
        #define median(m) s->m = (s->m*2 + SW(s,1,0)->m*2  + SW(s,0,1)->m*2 \
                                         + SW(s,-1,0)->m*2 + SW(s,0,-1)->m*2 \
                                         + SW(s,1,1)->m    + 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);
    };
	loop(x, a.xs) loop(y, a.ys) // assumes area not on edge of world
	{
		sqr *s = S(x + a.x, y + a.y);
#define median(m)                                                              \
	s->m =                                                                 \
	    (s->m * 2 + SW(s, 1, 0)->m * 2 + SW(s, 0, 1)->m * 2 +              \
	        SW(s, -1, 0)->m * 2 + SW(s, 0, -1)->m * 2 + SW(s, 1, 1)->m +   \
	        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);
	remip(a);
};

void
void calclight()
calclight()
{
    loop(x,ssize) loop(y,ssize)
    {
        sqr *s = S(x,y);
        s->r = s->g = s->b = 10;
    };
	loop(x, ssize) loop(y, ssize)
	{
		sqr *s = S(x, y);
		s->r = s->g = s->b = 10;
	};

    loopv(ents)
    {
        entity &e = ents[i];
        if(e.type==LIGHT) calclightsource(e);
    };
    
    block b = { 1, 1, ssize-2, ssize-2 };
    postlightarea(b);
    setvar("fullbright", 0);
	loopv(ents)
	{
		entity &e = ents[i];
		if (e.type == LIGHT)
			calclightsource(e);
	};

	block b = {1, 1, ssize - 2, ssize - 2};
	postlightarea(b);
	setvar("fullbright", 0);
};

VARP(dynlight, 0, 16, 32);

vector<block *> dlights;

void
void cleardlights()
cleardlights()
{
    while(!dlights.empty())
	while (!dlights.empty()) {
    {
        block *backup = dlights.pop();
        blockpaste(*backup);
        free(backup);    
    };
		block *backup = dlights.pop();
		blockpaste(*backup);
		free(backup);
	};
};

void
void dodynlight(vec &vold, vec &v, int reach, int strength, dynent *owner)
dodynlight(vec &vold, vec &v, int reach, int strength, dynent *owner)
{
	if (!reach)
    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 };
		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)
    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;
		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;

    dlights.add(blockcopy(b));      // backup area before rendering in dynlight
	dlights.add(blockcopy(b)); // backup area before rendering in dynlight

	persistent_entity l = {
    persistent_entity l = { (int)v.x, (int)v.y, (int)v.z, reach, LIGHT, strength, 0, 0 };
    calclightsource(l);
    postlightarea(b);
	    (int)v.x, (int)v.y, (int)v.z, reach, LIGHT, strength, 0, 0};
	calclightsource(l);
	postlightarea(b);
};

// utility functions also used by editing code

block *
block *blockcopy(block &s)
blockcopy(block &s)
{
    block *b = (block *)alloc(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;
	block *b = (block *)alloc(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
void blockpaste(block &b)
blockpaste(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);
	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);
};