Cube  Diff

Differences From Artifact [97ecb02030]:

To Artifact [9e368d7a28]:


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







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




+
-
+
+









-
+








// set all cubes with "tag" to space, if tag is 0 then reset ALL tagged cubes
// according to type
void
settag(int tag, int type)
{
	int maxx = 0, maxy = 0, minx = ssize, miny = ssize;
	loop(x, ssize) loop(y, ssize)
	for (int x = 0; x < ssize; x++) {
	{
		struct sqr *s = S(x, y);
		if (s->tag) {
			if (tag) {
				if (tag == s->tag)
					s->type = SPACE;
				else
					continue;
			} 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;
		}
	}
	struct block b = { minx, miny, maxx - minx + 1, maxy - miny + 1 };
	if (maxx)
		remip(&b, 0); // remip minimal area of changed geometry
}

		for (int y = 0; y < ssize; y++) {
			struct sqr *s = S(x, y);

			if (s->tag) {
				if (tag) {
					if (tag == s->tag)
						s->type = SPACE;
					else
						continue;
				} 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;
			}
		}
	}

	if (maxx) {
		// remip minimal area of changed geometry
		struct block b = { minx, miny, maxx - minx + 1,
			maxy - miny + 1 };
		remip(&b, 0);
	}
}

// reset for editing or map saving
void
resettagareas()
{
	settag(0, 0);
}
} // reset for editing or map saving

// set for playing
void
settagareas()
{
	settag(0, 1);

	[ents enumerateObjectsUsingBlock:^(Entity *e, size_t i, bool *stop) {
		if (ents[i].type == CARROT)
			setspawn(i, true);
	}];
} // set for playing
}

void
trigger(int tag, int type, bool savegame)
{
	if (!tag)
		return;

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







+
-
-
+
+
+



+
-
+
+


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










-
+
-








-
+
-







			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);
			// the target cube in the higher mip level
			struct sqr *r = SWS(v, x / 2, y / 2, vs);
			*r = *o[0];
			uchar nums[MAXTYPE];
			for (int i = 0; i < MAXTYPE; i++)
			loopi(MAXTYPE) nums[i] = 0;
			loopj(4) nums[o[j]->type]++;
				nums[i] = 0;
			for (int j = 0; j < 4; j++)
				nums[o[j]->type]++;
			// cube contains both solid and space, treated
			// specially in the renderer
			r->type = SEMISOLID;
			for (int k = 0; k < MAXTYPE; k++)
			loopk(MAXTYPE) if (nums[k] == 4) r->type = k;
				if (nums[k] == 4)
					r->type = k;
			if (!SOLID(r)) {
				int floor = 127, ceil = -128;
				for (int i = 0; i < 4; i++) {
				loopi(4) if (!SOLID(o[i]))
					if (!SOLID(o[i])) {
				{
					int fh = o[i]->floor;
					int ch = o[i]->ceil;
					if (r->type == SEMISOLID) {
						if (o[i]->type == FHF)
							// crap hack, needed
							// for rendering large
							// mips next to hfs
							fh -= o[i]->vdelta / 4 +
							    2;
						if (o[i]->type == CHF)
							// FIXME: needs to
							// somehow take into
							// account middle
							// vertices on higher
							// mips
							ch += o[i]->vdelta / 4 +
							    2;
					}
					if (fh < floor)
						// take lowest floor and
						// highest ceil, so we never
						// have to see missing
						// lower/upper from the side
						floor = fh;
					if (ch > ceil)
						ceil = ch;
						int fh = o[i]->floor;
						int ch = o[i]->ceil;
						if (r->type == SEMISOLID) {
							if (o[i]->type == FHF)
								// crap hack,
								// needed for
								// rendering
								// large mips
								// next to hfs
								fh -=
								    o[i]->vdelta /
								        4 +
								    2;
							if (o[i]->type == CHF)
								// FIXME: needs
								// to somehow
								// take into
								// account
								// middle
								// vertices on
								// higher mips
								ch +=
								    o[i]->vdelta /
								        4 +
								    2;
						}
						if (fh < floor)
							// take lowest floor and
							// highest ceil, so we
							// never have to see
							// missing lower/upper
							// from the side
							floor = fh;
						if (ch > ceil)
							ceil = ch;
					}
				}
				r->floor = floor;
				r->ceil = ceil;
			}
			if (r->type == CORNER)
				// special case: don't ever split even if
				// textures etc are different
				goto mip;
			r->defer = 1;
			if (SOLID(r)) {
				loopi(3)
				for (int i = 0; i < 3; i++) {
				{
					if (o[i]->wtex != o[3]->wtex)
						// on an all solid cube, only
						// thing that needs to be equal
						// for a perfect mip is the
						// wall texture
						goto c;
				}
			} else {
				loopi(3)
				for (int i = 0; i < 3; i++) {
				{
					// perfect mip even if light is not
					// exactly equal
					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 ||
224
225
226
227
228
229
230

231


232
233
234
235
236
237
238
245
246
247
248
249
250
251
252

253
254
255
256
257
258
259
260
261







+
-
+
+







					                ->vdelta)
						goto c;
				}
			}
			{
				// if any of the constituents is not perfect,
				// then this one isn't either
				for (int i = 0; i < 4; i++)
				loopi(4) if (o[i]->defer) goto c;
					if (o[i]->defer)
						goto c;
			}
		mip:
			r->defer = 0;
		c:;
		}
	s.x /= 2;
	s.y /= 2;
318
319
320
321
322
323
324

325


326
327
328
329
330
331
332
341
342
343
344
345
346
347
348

349
350
351
352
353
354
355
356
357







+
-
+
+







	if (t == LIGHT)
		calclight();
}

int
findtype(OFString *what)
{
	for (int i = 0; i < MAXENTTYPES; i++)
	loopi(MAXENTTYPES) if ([what isEqual:entnames[i]]) return i;
		if ([what isEqual:entnames[i]])
			return i;
	conoutf(@"unknown entity type \"%@\"", what);
	return NOTUSED;
}

Entity *
newentity(int x, int y, int z, OFString *what, int v1, int v2, int v3, int v4)
{
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
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







+
-
+
+













-
+
-








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

struct sqr *wmip[LARGEST_FACTOR * 2];

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

// main empty world creation routine, if passed factor -1 will enlarge old
// world by 1
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
511
512
513
514
515
492
493
494
495
496
497
498

499











500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

520



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535


536
537
538
539
540
541
542
543
544
545
546







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








-
+
-
-
-
+
+
+
+










+
-
-
+
+
+
+







	}
	if (factor < SMALLEST_FACTOR)
		factor = SMALLEST_FACTOR;
	if (factor > LARGEST_FACTOR)
		factor = LARGEST_FACTOR;
	setupworld(factor);

	loop(x, ssize) loop(y, ssize)
	for (int x = 0; x < ssize; x++) {
	{
		struct 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;
		for (int y = 0; y < ssize; y++) {
			struct 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(struct header);
	hdr.sfactor = sfactor;

	if (copy) {
		loop(x, ssize / 2) loop(y, ssize / 2)
		for (int x = 0; x < ssize / 2; x++) {
		{
			*S(x + ssize / 4, y + ssize / 4) =
			    *SWS(oldworld, x, y, ssize / 2);
			for (int y = 0; y < ssize / 2; y++) {
				*S(x + ssize / 4, y + ssize / 4) =
				    *SWS(oldworld, x, y, ssize / 2);
			}
		}

		for (Entity *e in ents) {
			e.x += ssize / 4;
			e.y += ssize / 4;
		}
	} else {
		char buffer[128] = "Untitled Map by Unknown";
		memcpy(hdr.maptitle, buffer, 128);
		hdr.waterlevel = -100000;
		for (int i = 0; i < 15; i++)
		loopi(15) hdr.reserved[i] = 0;
		loopk(3) loopi(256) hdr.texlists[k][i] = i;
			hdr.reserved[i] = 0;
		for (int k = 0; k < 3; k++)
			for (int i = 0; i < 256; i++)
				hdr.texlists[k][i] = i;
		[ents removeAllObjects];
		struct block b = { 8, 8, ssize - 16, ssize - 16 };
		edittypexy(SPACE, &b);
	}

	calclight();
	startmap(@"base/unnamed");