PDA

View Full Version : Using patterns



danrox2004
04-03-2007, 09:33 AM
Using patterns to save time and space
You can use patterns in scripts to save you time, and shorten your script. I wrote this because i saw a few scripts which had things like:

case n of
1: x:= 10; y:= 23;
2: x:= 20; y:= 46;
etc...


This can take a long time to type, especially if there are more then 20 or so possible places, such as in a world switcher.

First part - Creating a World switcher
Now, you could get the x and y coordinates of each individual world like this:
function GetXY(world: integer; var x, y: integer): boolean;
begin
Case world of
1: x := 137; y := 42
2: x := 137; y := 65
3: x := 137; y := 88
4: x := 137; y := 111
5: x := 137; y := 134
6: x := 137; y := 157
7: x := 137; y := 180
8: x := 137; y := 203
9: x := 137; y := 226
10: x := 137; y := 249
11: x := 137; y := 272
12: x := 137; y := 295
13: x := 137; y := 318
14: x := 137; y := 341
15: x := 137; y := 364
16: x := 137; y := 387
17: x := 137; y := 410
18: x := 137; y := 433
19: x := 137; y := 456
20: x := 137; y := 479
21: x := 230; y := 42
22: x := 230; y := 65
23: x := 230; y := 88
24: x := 230; y := 111
25: x := 230; y := 134
26: x := 230; y := 157
27: x := 230; y := 180
28: x := 230; y := 203
29: x := 230; y := 226
30: x := 230; y := 249
31: x := 230; y := 272
32: x := 230; y := 295
33: x := 230; y := 318
34: x := 230; y := 341
35: x := 230; y := 364
36: x := 230; y := 387
37: x := 230; y := 410
38: x := 230; y := 433
39: x := 230; y := 456
40: x := 230; y := 479
41: x := 323; y := 42
42: x := 323; y := 65
43: x := 323; y := 88
44: x := 323; y := 111
45: x := 323; y := 134
46: x := 323; y := 157
47: x := 323; y := 180
48: x := 323; y := 203
49: x := 323; y := 226
50: x := 323; y := 249
51: x := 323; y := 272
52: x := 323; y := 295
53: x := 323; y := 318
54: x := 323; y := 341
55: x := 323; y := 364
56: x := 323; y := 387
57: x := 323; y := 410
58: x := 323; y := 433
59: x := 323; y := 456
60: x := 323; y := 479
61: x := 416; y := 42
62: x := 416; y := 65
63: x := 416; y := 88
64: x := 416; y := 111
65: x := 416; y := 134
66: x := 416; y := 157
67: x := 416; y := 180
68: x := 416; y := 203
69: x := 416; y := 226
70: x := 416; y := 249
71: x := 416; y := 272
72: x := 416; y := 295
73: x := 416; y := 318
74: x := 416; y := 341
75: x := 416; y := 364
76: x := 416; y := 387
77: x := 416; y := 410
78: x := 416; y := 433
79: x := 416; y := 456
80: x := 416; y := 479
81: x := 509; y := 42
82: x := 509; y := 65
83: x := 509; y := 88
84: x := 509; y := 111
85: x := 509; y := 134
86: x := 509; y := 157
87: x := 509; y := 180
88: x := 509; y := 203
89: x := 509; y := 226
90: x := 509; y := 249
91: x := 509; y := 272
92: x := 509; y := 295
93: x := 509; y := 318
94: x := 509; y := 341
95: x := 509; y := 364
96: x := 509; y := 387
97: x := 509; y := 410
98: x := 509; y := 433
99: x := 509; y := 456
100: x := 509; y := 479
101: x := 602; y := 42
102: x := 602; y := 65
103: x := 602; y := 88
104: x := 602; y := 111
105: x := 602; y := 134
106: x := 602; y := 157
107: x := 602; y := 180
108: x := 602; y := 203
109: x := 602; y := 226
110: x := 602; y := 249
111: x := 602; y := 272
112: x := 602; y := 295
113: x := 602; y := 318
114: x := 602; y := 341
115: x := 602; y := 364
116: x := 602; y := 387
117: x := 602; y := 410
118: x := 602; y := 433
119: x := 602; y := 456
120: x := 602; y := 479
121: x := 695; y := 42
122: x := 695; y := 65
123: x := 695; y := 88
124: x := 695; y := 111
125: x := 695; y := 134
126: x := 695; y := 157
127: x := 695; y := 180
128: x := 695; y := 203
129: x := 695; y := 226
130: x := 695; y := 249
131: x := 695; y := 272
132: x := 695; y := 295
133: x := 695; y := 318
134: x := 695; y := 341
135: x := 695; y := 364
136: x := 695; y := 387
137: x := 695; y := 410
138: x := 695; y := 433
end;

note - no i did not write that all out

however that would take a long time to type and would be very ineffective. So we look for patterns. Notice how the first 20 all have 137 as there x value? Then the next 20 have 230, then 323 and so on. Once we notice that we can start to create our pattern.

the x value increases every 20 worlds
When x increases it goes up by 93 every time
The starting x value is 137

The x value increases every 20 worlds because that is when it goes onto the next column. Each column is 93 px apart.
So : x = 137 + Column * 93

Now for the y pattern. We notice that the y value loops back around every 20 worlds. This is because we start a new column. Each time the y value increases by 23. The starting y value is 42.
From this we can create a pattern:
y = 42 + Row * 23

Now we need to figure out what row and column each world is in. The modular operator comes into use here. If you haven't heard of it the modulator operator basically returns the remainder when a number is divided by another number. For example 10 mod 2 would return 0, as 2 fits into 10 perfectly, however 10 mod 3 returns 1 as 3 goes into 10 three times, with 1 remainder. This helps us when figuring out which row a world is in, we just divide it by 20 and find the remainder which is the row. So we do:
Row := World mod 20;

Now the column is just the world divided by 20, and then rounded down. For example 101 is in column 5. Trunc does the same thing as rounding number down, by removing all decimals. So we can do:
Column := trunc((World/20));

So to put all of this together, we can do:
function GetXY(World: integer; var x, y: Integer): Boolean;
var
column, row: integer;
begin
World := World - 1;
Row := World mod 20;
Column := trunc((World/20));
x := 137 + (Column * 93);
y := 42 + (Row * 23);
Result := True;
end;

If in a function, and we want to return more then one value we can put var in the function as above and it will store the value to the variable, instead if creating a temporary variable for the script.

And if anyone is interested, this is the code i wrote to get all the x and ys:
program New;
var
x, y: integer;
i: integer;

function GetXY(World: integer; var x, y: Integer): Boolean;
var
column, row: integer;
begin
World := World - 1;
Row := World mod 20;
Column := trunc((World/20));
x := 137 + (Column * 93);
y := 42 + (Row * 23);
Result := True;
end;

begin
Writeln('function GetXY(world: integer; var x, y: integer): boolean;');
Writeln('begin');
Writeln(' Case world of');
for i := 1 to 138 do
begin
GetXY(i, x, y)
Writeln(' ' + IntToStr(i) + ': x := ' + IntToStr(x) + '; y := ' + IntToStr(y));
end
Writeln('end;');
end.

My Third tutorial! If you like it check out my other ones

Janilabo
07-06-2007, 04:12 PM
Great tutorial on patters, thanks alot Dan!

Patterns are handy & very useful.