Tutorial 4.4 (Utilities) - Aerll/rpp GitHub Wiki

About utils

In r++ there are plenty of functions, which help us simplify our code. We will be mostly using them in combination with other functions. All of them are easy to distinguish, since their names start with util:. Let's have a look at some of them.

util:RemoveRotation

Removes rotation from given indices.

Example:

int tile = util:RemoveRotation(1.VHR);
// same as
int tile = 1;

util:Rotations

Returns given indices with all non-mirrored rotations: N, R, VH, VHR.

Example:

Insert(0).If(
    IndexAt([0, 0]).Is(util:Rotations(1))
);
// same as
Insert(0).If(
    IndexAt([0, 0]).Is(1.N, 1.R, 1.VH, 1.VHR)
);

util:Mirrors

Returns given indices with all mirrored rotations: V, H, VR, HR.

Example:

Insert(0).If(
    IndexAt([0, 0]).Is(util:Mirrors(1))
);
// same as
Insert(0).If(
    IndexAt([0, 0]).Is(1.V, 1.H, 1.VR, 1.HR)
);

util:All

Returns given indices with all rotations: N, V, H, R, VH, VR, HR, VHR.

Example:

Insert(0).If(
    IndexAt([0, 0]).Is(util:All(1))
);
// same as
Insert(0).If(
    IndexAt([0, 0]).Is(1.N, 1.V, 1.H, 1.R, 1.VH, 1.VR, 1.HR, 1.VHR)
);

util:Negate

Negates given value or coordinate.

Example:

int v   = util:Negate(5);
coord c = util:Negate([v, 5]);
// same as
int v   = -5;
coord c = [5, -5];

util:CopiesOf

Generates N copies of the given value.

Example:

Insert(1, 1.V, 3).If(
    IndexAt([0, 0]).Is(g:mask)
).Chance(util:CopiesOf(2, 5), 10);
// same as
Insert(1, 1.V, 3).If(
    IndexAt([0, 0]).Is(g:mask)
).Chance(5, 5, 10);

Example

In this example, we will slightly improve our automapper, which randomized silver 1x1 unhooks. Our goal is to add rotations to it, as well as some custom probabilities. As a reminder, here is our old rule:

Insert(silver1, silver2, silver3, silver4, silver5, silver6, silver7, silver8).If(
    IndexAt([0, 0]).Is(g:mask)
).Roll();

Firstly let's think about the rotations. If we look at the tiles, we will see, that all of them, except for silver8, look different with every rotation. We can therefore generate all rotations for these tiles, by using one of the utility functions. We can simply write:

Insert(util:All(silver1, silver2, silver3, silver4, silver5, silver6, silver7), silver8).If(
    IndexAt([0, 0]).Is(g:mask)
).Roll();

Notice that silver8 is not inside of util:All, so rotations won't be generated for it.

Next step will be to nicely distribute all of our tiles. Instead of using Roll, we will make use of Chance. Let's say, that we want to have 3 different chances for silver1 to silver6, silver7 and silver8. We need to remember, that util:All generates 8 tiles from each tile, therefore Chance will need 8 values per tile. Normally we would need to write 57 values, which is simply ridiculous. Fortunately we can make use of another utility function and simply write:

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

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;

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

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);
⚠️ **GitHub.com Fallback** ⚠️