Cube  Diff

Differences From Artifact [6ba0a980d9]:

To Artifact [696569b661]:


1
2
3
4
5

6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
13





+







// editing.cpp: most map editing commands go here, entity editing commands are
// in world.cpp

#include "cube.h"

#import "Command.h"
#import "DynamicEntity.h"
#import "Monster.h"
#import "OFString+Cube.h"

bool editmode = false;

// the current selection, used by almost all editing commands
74
75
76
77
78
79
80


81


82
83
84
85
86
87
88
75
76
77
78
79
80
81
82
83

84
85
86
87
88
89
90
91
92







+
+
-
+
+







			[Monster resetAll];
		projreset();
	}
	Cube.sharedInstance.repeatsKeys = editmode;
	selset = false;
	editing = editmode;
}

COMMAND(edittoggle, ARG_NONE, ^{
COMMANDN(edittoggle, toggleedit, ARG_NONE)
	toggleedit();
})

void
correctsel() // ensures above invariant
{
	selset = !OUTBORD(sel.x, sel.y);
	int bsize = ssize - MINBORD;
	if (sel.xs + sel.x > bsize)
116
117
118
119
120
121
122
123
124

125
126
127
128
129
130

131
132
133
134
135
136
137
120
121
122
123
124
125
126


127

128
129
130
131

132
133
134
135
136
137
138
139







-
-
+
-




-
+







#define EDITSELMP                                            \
	if (noteditmode() || noselection() || multiplayer()) \
		return;
#define EDITMP                              \
	if (noteditmode() || multiplayer()) \
		return;

void
selectpos(int x, int y, int xs, int ys)
COMMAND(select, ARG_4INT, (^(int x, int y, int xs, int ys) {
{
	struct block s = { x, y, xs, ys };
	sel = s;
	selh = 0;
	correctsel();
}
}))

void
makesel()
{
	struct block s = { min(lastx, cx), min(lasty, cy), abs(lastx - cx) + 1,
		abs(lasty - cy) + 1 };
	sel = s;
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
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







-
-
+
-









-
+



-
-
+
-






-
+

-
-
+
-
















-
+







		    initWithItemSize:sizeof(struct block *)];

	struct block *copy = blockcopy(&sel);
	[undos addItem:&copy];
	pruneundos(undomegs << 20);
}

void
editundo()
COMMAND(undo, ARG_NONE, ^{
{
	EDITMP;
	if (undos.count == 0) {
		conoutf(@"nothing more to undo");
		return;
	}
	struct block *p = undos.mutableLastItem;
	[undos removeLastItem];
	blockpaste(p);
	OFFreeMemory(p);
}
})

static struct block *copybuf = NULL;

void
copy()
COMMAND(copy, ARG_NONE, ^{
{
	EDITSELMP;

	if (copybuf)
		OFFreeMemory(copybuf);

	copybuf = blockcopy(&sel);
}
})

void
paste()
COMMAND(paste, ARG_NONE, ^{
{
	EDITMP;
	if (!copybuf) {
		conoutf(@"nothing to paste");
		return;
	}
	sel.xs = copybuf->xs;
	sel.ys = copybuf->ys;
	correctsel();
	if (!selset || sel.xs != copybuf->xs || sel.ys != copybuf->ys) {
		conoutf(@"incorrect selection");
		return;
	}
	makeundo();
	copybuf->x = sel.x;
	copybuf->y = sel.y;
	blockpaste(copybuf);
}
})

void
tofronttex() // maintain most recently used of the texture lists when applying
             // texture
{
	for (int i = 0; i < 3; i++) {
		int c = curedittex[i];
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
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







+
-
+
+
+




















-
-
+
-

















-
+

-
-
+
-







{
	EDITSEL;

	bool isfloor = flr == 0;
	editheightxy(isfloor, amount, &sel);
	addmsg(1, 7, SV_EDITH, sel.x, sel.y, sel.xs, sel.ys, isfloor, amount);
}

COMMAND(editheight, ARG_2INT)
COMMAND(editheight, ARG_2INT, ^(int flr, int amount) {
	editheight(flr, amount);
})

void
edittexxy(int type, int t, const struct block *sel)
{
	loopselxy(switch (type) {
	        case 0:
		        s->ftex = t;
		        break;
	        case 1:
		        s->wtex = t;
		        break;
	        case 2:
		        s->ctex = t;
		        break;
	        case 3:
		        s->utex = t;
		        break;
	});
}

void
edittex(int type, int dir)
COMMAND(edittex, ARG_2INT, ^(int type, int dir) {
{
	EDITSEL;

	if (type < 0 || type > 3)
		return;

	if (type != lasttype) {
		tofronttex();
		lasttype = type;
	}

	int atype = type == 3 ? 1 : type;
	int i = curedittex[atype];
	i = i < 0 ? 0 : i + dir;
	curedittex[atype] = i = min(max(i, 0), 255);
	int t = lasttex = hdr.texlists[atype][i];
	edittexxy(type, t, &sel);
	addmsg(1, 7, SV_EDITT, sel.x, sel.y, sel.xs, sel.ys, type, t);
}
})

void
replace()
COMMAND(replace, ARG_NONE, (^{
{
	EDITSELMP;

	for (int x = 0; x < ssize; x++) {
		for (int y = 0; y < ssize; y++) {
			struct sqr *s = S(x, y);
			switch (lasttype) {
			case 0:
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
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







-
+







-
+















-
-
+
-

-
+
-

-
-
+
-

-
+
-

-
-
+
-

-
+
-







				break;
			}
		}
	}

	struct block b = { 0, 0, ssize, ssize };
	remip(&b, 0);
}
}))

void
edittypexy(int type, const struct block *sel)
{
	loopselxy(s->type = type);
}

void
static void
edittype(int type)
{
	EDITSEL;

	if (type == CORNER &&
	    (sel.xs != sel.ys || sel.xs == 3 || (sel.xs > 4 && sel.xs != 8) ||
	        sel.x & ~-sel.xs || sel.y & ~-sel.ys)) {
		conoutf(@"corner selection must be power of 2 aligned");
		return;
	}

	edittypexy(type, &sel);
	addmsg(1, 6, SV_EDITS, sel.x, sel.y, sel.xs, sel.ys, type);
}

void
heightfield(int t)
COMMAND(heightfield, ARG_1INT, ^(int t) {
{
	edittype(t == 0 ? FHF : CHF);
}
})
COMMAND(heightfield, ARG_1INT)

void
solid(int t)
COMMAND(solid, ARG_1INT, ^(int t) {
{
	edittype(t == 0 ? SPACE : SOLID);
}
})
COMMAND(solid, ARG_1INT)

void
corner()
COMMAND(corner, ARG_NONE, ^{
{
	edittype(CORNER);
}
})
COMMAND(corner, ARG_NONE)

void
editequalisexy(bool isfloor, const struct block *sel)
{
	int low = 127, hi = -128;
	loopselxy({
		if (s->floor < low)
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

547
548
549
550
551
552
553

554
555
556
557
558
559
560
561
562
563
564

565
566
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

589
590
591

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609

610
611
612

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633

634
635
636
637
638
639
640
641
642
643
644

645
646
647
648
649
650
651
652

653
654
655


656
657

658
659
660
661
662




663
664
665
666
667
668
669
670
671
672
673
674
675
676
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

565
566


567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

584
585


586

587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

606
607
608
609
610
611
612
613
614
615


616

617
618
619
620
621
622

623
624


625
626


627
628




629
630
631
632





















-
-
+
-






-
+
-








-
-
+
-




-
+





-
-
+
-









-
+

-
-
+
-



















-
+

-
-
+
-
















-
+

-
-
+
-



















-
+









-
-
+
-






-
+

-
-
+
+
-
-
+

-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
		else
			s->ceil = hi;
		if (s->floor >= s->ceil)
			s->floor = s->ceil - 1;
	});
}

void
equalize(int flr)
COMMAND(equalize, ARG_1INT, ^(int flr) {
{
	bool isfloor = (flr == 0);

	EDITSEL;

	editequalisexy(isfloor, &sel);
	addmsg(1, 6, SV_EDITE, sel.x, sel.y, sel.xs, sel.ys, isfloor);
}
})
COMMAND(equalize, ARG_1INT)

void
setvdeltaxy(int delta, const struct block *sel)
{
	loopselxy(s->vdelta = max(s->vdelta + delta, 0));
	remipmore(sel, 0);
}

void
setvdelta(int delta)
COMMAND(vdelta, ARG_1INT, ^(int delta) {
{
	EDITSEL;

	setvdeltaxy(delta, &sel);
	addmsg(1, 6, SV_EDITD, sel.x, sel.y, sel.xs, sel.ys, delta);
}
})

#define MAXARCHVERT 50
int archverts[MAXARCHVERT][MAXARCHVERT];
bool archvinit = false;

void
archvertex(int span, int vert, int delta)
COMMAND(archvertex, ARG_3INT, ^(int span, int vert, int delta) {
{
	if (!archvinit) {
		archvinit = true;
		for (int s = 0; s < MAXARCHVERT; s++)
			for (int v = 0; v < MAXARCHVERT; v++)
				archverts[s][v] = 0;
	}
	if (span >= MAXARCHVERT || vert >= MAXARCHVERT || span < 0 || vert < 0)
		return;
	archverts[span][vert] = delta;
}
})

void
arch(int sidedelta, int _a)
COMMAND(arch, ARG_2INT, ^(int sidedelta, int _a) {
{
	EDITSELMP;

	sel.xs++;
	sel.ys++;

	if (sel.xs > MAXARCHVERT)
		sel.xs = MAXARCHVERT;
	if (sel.ys > MAXARCHVERT)
		sel.ys = MAXARCHVERT;

	struct block *sel_ = &sel;
	// Ugly hack to make the macro work.
	struct block *sel = sel_;
	loopselxy(s->vdelta = sel->xs > sel->ys
	        ? (archverts[sel->xs - 1][x] +
	              (y == 0 || y == sel->ys - 1 ? sidedelta : 0))
	        : (archverts[sel->ys - 1][y] +
	              (x == 0 || x == sel->xs - 1 ? sidedelta : 0)));
	remipmore(sel, 0);
}
})

void
slope(int xd, int yd)
COMMAND(slope, ARG_2INT, ^(int xd, int yd) {
{
	EDITSELMP;

	int off = 0;
	if (xd < 0)
		off -= xd * sel.xs;
	if (yd < 0)
		off -= yd * sel.ys;

	sel.xs++;
	sel.ys++;

	struct block *sel_ = &sel;
	// Ugly hack to make the macro work.
	struct block *sel = sel_;
	loopselxy(s->vdelta = xd * x + yd * y + off);
	remipmore(sel, 0);
}
})

void
perlin(int scale, int seed, int psize)
COMMAND(perlin, ARG_3INT, ^(int scale, int seed, int psize) {
{
	EDITSELMP;

	sel.xs++;
	sel.ys++;

	makeundo();

	sel.xs--;
	sel.ys--;

	perlinarea(&sel, scale, seed, psize);

	sel.xs++;
	sel.ys++;

	remipmore(&sel, 0);

	sel.xs--;
	sel.ys--;
}
})

VARF(
    fullbright, 0, 0, 1, if (fullbright) {
	    if (noteditmode())
		    return;
	    for (int i = 0; i < mipsize; i++)
		    world[i].r = world[i].g = world[i].b = 176;
    });

void
edittag(int tag)
COMMAND(edittag, ARG_1INT, ^(int tag) {
{
	EDITSELMP;

	struct block *sel_ = &sel;
	// Ugly hack to make the macro work.
	struct block *sel = sel_;
	loopselxy(s->tag = tag);
}
})

void
newent(OFString *what, OFString *a1, OFString *a2, OFString *a3, OFString *a4)
COMMAND(newent, ARG_5STR,
    ^(OFString *what, OFString *a1, OFString *a2, OFString *a3, OFString *a4) {
{
	EDITSEL;
	    EDITSEL;

	newentity(sel.x, sel.y, (int)player1.origin.z, what,
	    [a1 cube_intValueWithBase:0], [a2 cube_intValueWithBase:0],
	    [a3 cube_intValueWithBase:0], [a4 cube_intValueWithBase:0]);
}
	    newentity(sel.x, sel.y, (int)player1.origin.z, what,
	        [a1 cube_intValueWithBase:0], [a2 cube_intValueWithBase:0],
	        [a3 cube_intValueWithBase:0], [a4 cube_intValueWithBase:0]);
    })

COMMANDN(select, selectpos, ARG_4INT)
COMMAND(edittag, ARG_1INT)
COMMAND(replace, ARG_NONE)
COMMAND(archvertex, ARG_3INT)
COMMAND(arch, ARG_2INT)
COMMAND(slope, ARG_2INT)
COMMANDN(vdelta, setvdelta, ARG_1INT)
COMMANDN(undo, editundo, ARG_NONE)
COMMAND(copy, ARG_NONE)
COMMAND(paste, ARG_NONE)
COMMAND(edittex, ARG_2INT)
COMMAND(newent, ARG_5STR)
COMMAND(perlin, ARG_3INT)