-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral.Walker.nut
210 lines (188 loc) · 5.39 KB
/
Spiral.Walker.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* SpiralWalker class v.3 r.223 [2012-01-28],
* part of Minchinweb's MetaLibrary v.4,
* originally part of WmDOT v.5
* Copyright © 2011-12 by W. Minchin. For more info,
* please visit https://github.com/MinchinWeb/openttd-metalibrary
*
* Permission is granted to you to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell this software, and provide these
* rights to others, provided:
*
* + The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the software.
* + Attribution is provided in the normal place for recognition of 3rd party
* contributions.
* + You accept that this software is provided to you "as is", without warranty.
*/
/** \brief Spiral Walker
* \version v.3 (2012-01-28)
* \author W. Minchin (%MinchinWeb)
* \since MetaLibrary v.4
*
* The SpiralWalker class allows you to define a starting point, and then
* 'walk' all the tiles in a spiral outward. It was originally used to
* find a build-able spot for my HQ in WmDOT, but is useful for many other
* things as well.
*
* \note `SpiralWalker` is designed to be a persistent class.
* \see \_MinchinWeb\_LW\_
* \todo add image showing the walk out pattern
*/
/* Functions provided:
* MetaLib.SpiralWalker()
* MetaLib.SpiralWalker.Start(Tile)
* .Reset()
* .Restart()
* .Walk()
* .GetStart()
* .GetStage()
* .GetStep()
*/
class _MinchinWeb_SW_ {
_start = null; ///< start tile
_startx = null; ///< x value of start tile
_starty = null; ///< y value of start tile
_x = null; ///< x value of current tile
_y = null; ///< y value of current tile
_current_tile = null; ///< current tile
_dx = null;
_dy = null;
_Steps = null; ///< see GetStep()
_Stage = null; ///< see GetStage()
_StageMax = null;
_StageSteps = null;
constructor() {
this._dx = -1;
this._dy = 0;
this._Steps = 0;
this._Stage = 1;
this._StageMax = 1;
this._StageSteps = 0;
}
/** \publicsection
* \brief Sets the starting tile for SpiralWalker
* \see Restart()
*/
function Start(Tile);
/** \brief Resets the variables for the SpiralWalker
* \see Restart()
*/
function Reset();
/** \brief Moves the SpiralWalker to the original starting position
* \see Reset()
*/
function Restart();
/** \brief 'Walks' the SpiralWalker one tile at a tile
* \return the tile that the SpiralWalker is now "standing on"
* \note This is where (most) of the action is!
* \note Before calling this function, you need to set the Start().
*/
function Walk();
/** \brief Returns the tile the SpiralWalker is starting on
* \return The tile the SpiralWalker is starting on
* \see Start()
*/
function GetStart() { return this._start; }
/** \brief Returns the Stage the SpiralWalker is on.
*
* Basically, the line segments its completed plus one; it takes four
* stages to complete a revolution.
* \return stage number
* \see GetStep()
* \todo Add an image showing how stages are counted
*/
function GetStage() { return this._Stage; }
/** \brief Returns the Tile the SpiralWalker is on.
* \return the Tile the SpiralWalker is on.
* \see GetStep()
* \see GetStage()
*/
function GetTile() { return this._current_tile; }
/** \brief Returns the number of steps the SpiralWalker has done.
* \return The number of steps the SpiralWalker has done.
* \see GetStage()
* \todo Add an image showing how steps are counted
*/
function GetStep() { return this._Steps; }
};
// == Function definition ==================================================
function _MinchinWeb_SW_::Start(Tile) {
this._start = Tile;
this._startx = AIMap.GetTileX(Tile);
this._starty = AIMap.GetTileY(Tile);
this._x = this._startx;
this._y = this._starty;
this._current_tile = this._start;
this._dx = -1;
this._dy = 0;
this._Steps = 0;
// this._Stage = 1;
this._Stage = 1;
this._StageMax = 1;
this._StageSteps = 0;
}
function _MinchinWeb_SW_::Reset() {
this._start = null;
this._startx = null;
this._starty = null;
this._x = null;
this._y = null;
this._current_tile = null;
}
function _MinchinWeb_SW_::Restart() {
this._x = this._startx;
this._y = this._starty;
this._current_tile = this._start;
this._dx = -1;
this._dy = 0;
this._Steps = 0;
this._Stage = 1;
this._StageMax = 1;
this._StageSteps = 0;
}
function _MinchinWeb_SW_::Walk() {
if (this._Steps == 0) {
this._Steps++;
} else {
this._x += this._dx;
this._y += this._dy;
this._StageSteps ++;
this._Steps ++;
// Check if it's time to turn
if (this._StageSteps == this._StageMax) {
this._StageSteps = 0;
if (this._Stage % 2 == 0) {
this._StageMax++;
}
this._Stage ++;
// Turn Clockwise
switch (this._dx) {
case 0:
switch (this._dy) {
case -1:
this._dx = -1;
this._dy = 0;
break;
case 1:
this._dx = 1;
this._dy = 0;
break;
}
break;
case -1:
this._dx = 0;
this._dy = 1;
break;
case 1:
this._dx = 0;
this._dy = -1;
break;
}
}
}
_MinchinWeb_Log_.Note(" SpiralWalker.Walk: " + this._dx + " " + this._dy + " : " + this._Steps + " " + this._Stage + " " + this._StageSteps + " " + this._StageMax + " :: " + this._x + ", " + this._y, 7);
this._current_tile = AIMap.GetTileIndex(this._x, this._y);
// AISign.BuildSign(this._current_tile, "" + this._Steps);
return this._current_tile;
}
// EOF