Skip to content

Tutorial 5.2 (Tests involving objects)

Aerll edited this page Sep 18, 2023 · 13 revisions

HasSpaceFor

Tests whether there is enough space to place a given object.

Example:

Insert(green).If(
    IndexAt([0, 0]).HasSpaceFor(:2x2)
);

IsOverlapping

Tests whether the tile is overlapping one of the given objects.

Example:

Insert(green).If(
    IndexAt([0, 0]).IsOverlapping(red:1x2, blue:2x1)
);

IsNotOverlapping

Tests whether the tile is not overlapping any of the given objects.

Example:

Insert(green).If(
    IndexAt([0, 0]).IsNotOverlapping(red:1x2, blue:2x1)
);

Example

In this example we will yet again improve our automapper, which randomized 1x1 silver unhooks. We will make sure, that it doesn't place them on top of big unhooks.

We will start by creating all of the objects, representing each big unhook:

object silver2x2       = Rect(3, 20);
object silver2x2:bolts = Rect(5, 22);
object silver3x2       = Rect(67, 85);
object silver3x3       = Rect(80, 114);

object silver2x2:1 = Indices(48_49, 64_66);
object silver3x2:1 = Indices(35_37, 50_53);

As a reminder, here is our old code:

Insert(g:mask);
Insert(util:All(silver1, silver2, silver3, silver4, silver5, silver6, silver7), silver8).If(
    IndexAt([0, 0]).Is(g:mask)
).Chance(util:CopiesOf(48, 5), util:CopiesOf(8, 6), 15);

We can quickly notice, that our first rule is responsible for replacing every tile on the map. Whatever turns into a mask will be randomized. We need to change it, so that the mask isn't placed on top of our newly created objects. For that we will use IsNotOverlapping:

Insert(g:mask).If(
    IndexAt([0, 0]).IsNotOverlapping(util:All(silver2x2, silver2x2:bolts, silver3x2, silver3x3, silver2x2:1, silver3x2:1))
);

Here we're using util:All to generate all rotations for each object. It's one of the utility functions, which also works with objects.

Full code

#include "base.r"
#output "generic_unhookable.rules"

int silver1 = 1;
int silver2 = 2;
int silver3 = 16;
int silver4 = 17;
int silver5 = 18;
int silver6 = 32;
int silver7 = 33;
int silver8 = 34;

object silver2x2       = Rect(3, 20);
object silver2x2:bolts = Rect(5, 22);
object silver3x2       = Rect(67, 85);
object silver3x3       = Rect(80, 114);

object silver2x2:1 = Indices(48_49, 64_66);
object silver3x2:1 = Indices(35_37, 50_53);

AutoMapper("Rolling Stones");
NewRun();

Insert(g:mask).If(
    IndexAt([0, 0]).IsNotOverlapping(util:All(silver2x2, silver2x2:bolts, silver3x2, silver3x3, silver2x2:1, silver3x2:1))
);
Insert(util:All(silver1, silver2, silver3, silver4, silver5, silver6, silver7), silver8).If(
    IndexAt([0, 0]).Is(g:mask)
).Chance(util:CopiesOf(48, 5), util:CopiesOf(8, 6), 15);