Cube  Diff

Differences From Artifact [6c51021a97]:

To Artifact [293b2c8a10]:


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
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
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
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
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
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
470
471
472
473
474
475


476













477
478
479
480
481
482
483
484
485
486
487
488





489
490
491
492
493
494
495
496
497


498
499
500
501
502
503
504
505
506
507
508
509
510





-
+






+
-
+
+

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

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

+
-
+

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




-
-
-
-
+
+
+
+


+
-
+

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


+
-
+

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


+
-
+

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


+
-
+

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


+
-
+

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


+
-
+

-
-
-
+
+
+


+
-
+

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


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




+
-
+

-
-
-
+
+
+
+


+
-
+

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




+
-
+

-
-
-
+
+
+
+
+


-
+

+
-
+

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

+
+
-
+
+

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

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


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






-
// world.cpp: core map management stuff

#include "cube.h"

extern char *entnames[];                // lookup from map entities above to strings
extern char *entnames[]; // lookup from map entities above to strings

sqr *world = NULL;
int sfactor, ssize, cubicsize, mipsize;

header hdr;

void
void settag(int tag, int type)          // set all cubes with "tag" to space, if tag is 0 then reset ALL tagged cubes according to type
settag(int tag, int type) // set all cubes with "tag" to space, if tag is 0 then
                          // reset ALL tagged cubes according to type
{
    int maxx = 0, maxy = 0, minx = ssize, miny = ssize;
    loop(x, ssize) loop(y, ssize)
    {
        sqr *s = S(x, y);
        if(s->tag)
	int maxx = 0, maxy = 0, minx = ssize, miny = ssize;
	loop(x, ssize) loop(y, ssize)
	{
		sqr *s = S(x, y);
		if (s->tag) {
        {
            if(tag)
			if (tag) {
            {
                if(tag==s->tag) s->type = SPACE;
                else continue;
				if (tag == s->tag)
					s->type = SPACE;
				else
					continue;
            }
            else
			} else {
            {
                s->type = type ? SOLID : SPACE;
            };
            if(x>maxx) maxx = x;
            if(y>maxy) maxy = y;
            if(x<minx) 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
				s->type = type ? SOLID : SPACE;
			};
			if (x > maxx)
				maxx = x;
			if (y > maxy)
				maxy = y;
			if (x < minx)
				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()

void resettagareas() { settag(0, 0); };                                                         // reset for editing or map saving
void settagareas() { settag(0, 1); loopv(ents) if(ents[i].type==CARROT) setspawn(i, true); };   // set for playing
{
	settag(0, 0);
}; // reset for editing or map saving
void
settagareas()
{
	settag(0, 1);
	loopv(ents) if (ents[i].type == CARROT) setspawn(i, true);
}; // set for playing

void
void trigger(int tag, int type, bool savegame)
trigger(int tag, int type, bool savegame)
{
	if (!tag)
    if(!tag) return;
    settag(tag, type);
    if(!savegame && type!=3) playsound(S_RUMBLE);
    sprintf_sd(aliasname)("level_trigger_%d", tag);
    if(identexists(aliasname)) execute(aliasname);
    if(type==2) endsp(false);
		return;
	settag(tag, type);
	if (!savegame && type != 3)
		playsound(S_RUMBLE);
	sprintf_sd(aliasname)("level_trigger_%d", tag);
	if (identexists(aliasname))
		execute(aliasname);
	if (type == 2)
		endsp(false);
};

COMMAND(trigger, ARG_2INT);

// 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
// 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
void remip(block &b, int level)
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)
	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)
    {
        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))
		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)
				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))
						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
            };
				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
			} 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)->vdelta
                || o[0]->vdelta-o[3]->vdelta != o[3]->vdelta-SWS(w,x,y+2,ws)->vdelta
                || o[3]->vdelta-o[2]->vdelta != o[2]->vdelta-SWS(w,x+2,y+1,ws)->vdelta
                || o[1]->vdelta-o[2]->vdelta != 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);
				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)
					                ->vdelta ||
					    o[0]->vdelta - o[3]->vdelta !=
					        o[3]->vdelta -
					            SWS(w, x, y + 2, ws)
					                ->vdelta ||
					    o[3]->vdelta - o[2]->vdelta !=
					        o[2]->vdelta -
					            SWS(w, x + 2, y + 1, ws)
					                ->vdelta ||
					    o[1]->vdelta - o[2]->vdelta !=
					        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
void remipmore(block &b, int level)
remipmore(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);
	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
int closestent()        // used for delent and edit mode ent display
closestent() // used for delent and edit mode ent display
{
    if(noteditmode()) return -1;
    int best;
    float bdist = 99999;
    loopv(ents)
    {
        entity &e = ents[i];
        if(e.type==NOTUSED) continue;
        vec v = { e.x, e.y, e.z };
        vdist(dist, t, player1->o, v);
        if(dist<bdist)
	if (noteditmode())
		return -1;
	int best;
	float bdist = 99999;
	loopv(ents)
	{
		entity &e = ents[i];
		if (e.type == NOTUSED)
			continue;
		vec v = {e.x, e.y, e.z};
		vdist(dist, t, player1->o, v);
		if (dist < bdist) {
        {
            best = i;
            bdist = dist;
        };
    };
    return bdist==99999 ? -1 : best; 
			best = i;
			bdist = dist;
		};
	};
	return bdist == 99999 ? -1 : best;
};

void
void entproperty(int prop, int amount)
entproperty(int prop, int amount)
{
    int e = closestent();
    if(e<0) return;
    switch(prop)
	int e = closestent();
	if (e < 0)
		return;
	switch (prop) {
    {
        case 0: ents[e].attr1 += amount; break;
        case 1: ents[e].attr2 += amount; break;
        case 2: ents[e].attr3 += amount; break;
        case 3: ents[e].attr4 += amount; break;
    };
	case 0:
		ents[e].attr1 += amount;
		break;
	case 1:
		ents[e].attr2 += amount;
		break;
	case 2:
		ents[e].attr3 += amount;
		break;
	case 3:
		ents[e].attr4 += amount;
		break;
	};
};

void
void delent()
delent()
{
    int e = closestent();
    if(e<0) { conoutf("no more entities"); return; };
    int t = ents[e].type;
    conoutf("%s entity deleted", entnames[t]);
    ents[e].type = NOTUSED;
    addmsg(1, 10, SV_EDITENT, e, NOTUSED, 0, 0, 0, 0, 0, 0, 0);
    if(t==LIGHT) calclight();
	int e = closestent();
	if (e < 0) {
		conoutf("no more entities");
		return;
	};
	int t = ents[e].type;
	conoutf("%s entity deleted", entnames[t]);
	ents[e].type = NOTUSED;
	addmsg(1, 10, SV_EDITENT, e, NOTUSED, 0, 0, 0, 0, 0, 0, 0);
	if (t == LIGHT)
		calclight();
};

int
int findtype(char *what)
findtype(char *what)
{
    loopi(MAXENTTYPES) if(strcmp(what, entnames[i])==0) return i;
    conoutf("unknown entity type \"%s\"", what);
    return NOTUSED;
	loopi(MAXENTTYPES) if (strcmp(what, entnames[i]) == 0) return i;
	conoutf("unknown entity type \"%s\"", what);
	return NOTUSED;
}

entity *
entity *newentity(int x, int y, int z, char *what, int v1, int v2, int v3, int v4)
newentity(int x, int y, int z, char *what, int v1, int v2, int v3, int v4)
{
    int type = findtype(what);
    persistent_entity e = { x, y, z, v1, type, v2, v3, v4 };
    switch(type)
	int type = findtype(what);
	persistent_entity e = {x, y, z, v1, type, v2, v3, v4};
	switch (type) {
    {
        case LIGHT:
            if(v1>32) v1 = 32;
            if(!v1) e.attr1 = 16;
            if(!v2 && !v3 && !v4) e.attr2 = 255;          
            break;
            
        case MAPMODEL:
            e.attr4 = e.attr3;
            e.attr3 = e.attr2;
        case MONSTER:
        case TELEDEST:
            e.attr2 = (uchar)e.attr1;
        case PLAYERSTART:
            e.attr1 = (int)player1->yaw;
            break;
    };           
    addmsg(1, 10, SV_EDITENT, ents.length(), type, e.x, e.y, e.z, e.attr1, e.attr2, e.attr3, e.attr4);
    ents.add(*((entity *)&e)); // unsafe!
    if(type==LIGHT) calclight();
    return &ents.last();
	case LIGHT:
		if (v1 > 32)
			v1 = 32;
		if (!v1)
			e.attr1 = 16;
		if (!v2 && !v3 && !v4)
			e.attr2 = 255;
		break;

	case MAPMODEL:
		e.attr4 = e.attr3;
		e.attr3 = e.attr2;
	case MONSTER:
	case TELEDEST:
		e.attr2 = (uchar)e.attr1;
	case PLAYERSTART:
		e.attr1 = (int)player1->yaw;
		break;
	};
	addmsg(1, 10, SV_EDITENT, ents.length(), type, e.x, e.y, e.z, e.attr1,
	    e.attr2, e.attr3, e.attr4);
	ents.add(*((entity *)&e)); // unsafe!
	if (type == LIGHT)
		calclight();
	return &ents.last();
};

void
void clearents(char *name)
{  
    int type = findtype(name);
    if(noteditmode() || multiplayer()) return;
    loopv(ents)
    {
        entity &e = ents[i];
        if(e.type==type) e.type = NOTUSED;
    };
    if(type==LIGHT) calclight();
clearents(char *name)
{
	int type = findtype(name);
	if (noteditmode() || multiplayer())
		return;
	loopv(ents)
	{
		entity &e = ents[i];
		if (e.type == type)
			e.type = NOTUSED;
	};
	if (type == LIGHT)
		calclight();
};

COMMAND(clearents, ARG_1STR);

void
void scalecomp(uchar &c, int intens)
scalecomp(uchar &c, int intens)
{
    int n = c*intens/100;
    if(n>255) n = 255;
    c = n;
	int n = c * intens / 100;
	if (n > 255)
		n = 255;
	c = n;
};

void
void scalelights(int f, int intens)
scalelights(int f, int intens)
{
    loopv(ents)
    {
        entity &e = ents[i];
        if(e.type!=LIGHT) continue;
        e.attr1 = e.attr1*f/100;
        if(e.attr1<2) e.attr1 = 2;
        if(e.attr1>32) e.attr1 = 32;
        if(intens)
	loopv(ents)
	{
		entity &e = ents[i];
		if (e.type != LIGHT)
			continue;
		e.attr1 = e.attr1 * f / 100;
		if (e.attr1 < 2)
			e.attr1 = 2;
		if (e.attr1 > 32)
			e.attr1 = 32;
		if (intens) {
        {
            scalecomp(e.attr2, intens);
            scalecomp(e.attr3, intens);
            scalecomp(e.attr4, intens);
        };
    };
    calclight();
			scalecomp(e.attr2, intens);
			scalecomp(e.attr3, intens);
			scalecomp(e.attr4, intens);
		};
	};
	calclight();
};

COMMAND(scalelights, ARG_2INT);

int
int findentity(int type, int index)
findentity(int type, int index)
{
    for(int i = index; i<ents.length(); i++) if(ents[i].type==type) return i;
    loopj(index) if(ents[j].type==type) return j;
    return -1;
	for (int i = index; i < ents.length(); i++)
		if (ents[i].type == type)
			return i;
	loopj(index) if (ents[j].type == type) return j;
	return -1;
};

sqr *wmip[LARGEST_FACTOR*2];
sqr *wmip[LARGEST_FACTOR * 2];

void
void setupworld(int factor)
setupworld(int factor)
{
    ssize = 1<<(sfactor = factor);
    cubicsize = ssize*ssize;
    mipsize = cubicsize*134/100;
    sqr *w = world = (sqr *)alloc(mipsize*sizeof(sqr));
    loopi(LARGEST_FACTOR*2) { wmip[i] = w; w += cubicsize>>(i*2); };
};
	ssize = 1 << (sfactor = factor);
	cubicsize = ssize * ssize;
	mipsize = cubicsize * 134 / 100;
	sqr *w = world = (sqr *)alloc(mipsize * sizeof(sqr));
	loopi(LARGEST_FACTOR * 2)
	{
		wmip[i] = w;
		w += cubicsize >> (i * 2);
	};
};

void
empty_world(
void empty_world(int factor, bool force)    // main empty world creation routine, if passed factor -1 will enlarge old world by 1
    int factor, bool force) // main empty world creation routine, if passed
                            // factor -1 will enlarge old world by 1
{
    if(!force && noteditmode()) return; 
    cleardlights();
    pruneundos();
    sqr *oldworld = world;
    bool copy = false;
    if(oldworld && factor<0) { factor = sfactor+1; copy = true; };
    if(factor<SMALLEST_FACTOR) factor = SMALLEST_FACTOR;
    if(factor>LARGEST_FACTOR) factor = LARGEST_FACTOR;
    setupworld(factor);
    
    loop(x,ssize) loop(y,ssize)
    {
        sqr *s = S(x,y);
        s->r = s->g = s->b = 150;
        s->ftex = DEFAULT_FLOOR;
        s->ctex = DEFAULT_CEIL;
        s->wtex = s->utex = DEFAULT_WALL;
        s->type = SOLID;
        s->floor = 0;
        s->ceil = 16;
        s->vdelta = 0;
        s->defer = 0;
    };
    
    strncpy(hdr.head, "CUBE", 4);
    hdr.version = MAPVERSION;
    hdr.headersize = sizeof(header);
    hdr.sfactor = sfactor;
	if (!force && noteditmode())
		return;
	cleardlights();
	pruneundos();
	sqr *oldworld = world;
	bool copy = false;
	if (oldworld && factor < 0) {
		factor = sfactor + 1;
		copy = true;
	};
	if (factor < SMALLEST_FACTOR)
		factor = SMALLEST_FACTOR;
	if (factor > LARGEST_FACTOR)
		factor = LARGEST_FACTOR;
	setupworld(factor);

	loop(x, ssize) loop(y, ssize)
	{
		sqr *s = S(x, y);
		s->r = s->g = s->b = 150;
		s->ftex = DEFAULT_FLOOR;
		s->ctex = DEFAULT_CEIL;
		s->wtex = s->utex = DEFAULT_WALL;
		s->type = SOLID;
		s->floor = 0;
		s->ceil = 16;
		s->vdelta = 0;
		s->defer = 0;
	};

	strncpy(hdr.head, "CUBE", 4);
	hdr.version = MAPVERSION;
	hdr.headersize = sizeof(header);
	hdr.sfactor = sfactor;

    if(copy)
	if (copy) {
    {
        loop(x,ssize/2) loop(y,ssize/2)
        {
            *S(x+ssize/4, y+ssize/4) = *SWS(oldworld, x, y, ssize/2);
        };
        loopv(ents)
        {
            ents[i].x += ssize/4;
            ents[i].y += ssize/4;
        };
		loop(x, ssize / 2) loop(y, ssize / 2)
		{
			*S(x + ssize / 4, y + ssize / 4) =
			    *SWS(oldworld, x, y, ssize / 2);
		};
		loopv(ents)
		{
			ents[i].x += ssize / 4;
			ents[i].y += ssize / 4;
		};
    }
    else
	} else {
    {
        strn0cpy(hdr.maptitle, "Untitled Map by Unknown", 128);
        hdr.waterlevel = -100000;
        loopi(15) hdr.reserved[i] = 0;
        loopk(3) loopi(256) hdr.texlists[k][i] = i;
        ents.setsize(0);
        block b = { 8, 8, ssize-16, ssize-16 }; 
        edittypexy(SPACE, b);
    };
    
    calclight();
    startmap("base/unnamed");
    if(oldworld)
		strn0cpy(hdr.maptitle, "Untitled Map by Unknown", 128);
		hdr.waterlevel = -100000;
		loopi(15) hdr.reserved[i] = 0;
		loopk(3) loopi(256) hdr.texlists[k][i] = i;
		ents.setsize(0);
		block b = {8, 8, ssize - 16, ssize - 16};
		edittypexy(SPACE, b);
	};

	calclight();
	startmap("base/unnamed");
	if (oldworld) {
    {
        free(oldworld);
        toggleedit();
        execute("fullbright 1");
    };
		free(oldworld);
		toggleedit();
		execute("fullbright 1");
	};
};

void
mapenlarge()
{
void mapenlarge()  { empty_world(-1, false); };
void newmap(int i) { empty_world(i, false); };
	empty_world(-1, false);
};
void
newmap(int i)
{
	empty_world(i, false);
};

COMMAND(mapenlarge, ARG_NONE);
COMMAND(newmap, ARG_1INT);
COMMANDN(recalc, calclight, ARG_NONE);
COMMAND(delent, ARG_NONE);
COMMAND(entproperty, ARG_2INT);