-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtiles.nut
147 lines (134 loc) · 4.56 KB
/
tiles.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
/**
* This file is part of WormAI: An OpenTTD AI.
*
* @file tiles.nut Class containing tile related functions for WormAI.
*
* License: GNU GPL - version 2 (see license.txt)
* Author: Wormnest (Jacob Boerema)
* Copyright: Jacob Boerema, 2013-2016.
*
*/
/**
* Define the WormTiles class containing tile related functions.
*/
class WormTiles
{
static DIR_NE = 2;
static DIR_NW = 0;
static DIR_SE = 1;
static DIR_SW = 3;
/**
* Add a square area to an AITileList containing tiles that are within radius
* tiles from the center tile, taking the edges of the map into account.
* @note This function was taken from Rondje. Name was changed from SafeAddRectangle to SafeAddSquare.
* @param list The AITileList in which the valid tiles will be returned.
* @param tile The center tile.
* @param radius The radius of tiles.
*/
static function SafeAddSquare(list, tile, radius);
/**
* A safe implementation of AITileList.AddRectangle. Only valid tiles are
* added to the tile list.
* @note Taken from AdmiralAI.
* @param tile_list The AITileList to add the tiles to.
* @param center_tile The center of the rectangle.
* @param x_min The amount of tiles to the north-east, relative to center_tile.
* @param y_min The amount of tiles to the north-west, relative to center_tile.
* @param x_plus The amount of tiles to the south-west, relative to center_tile.
* @param y_plus The amount of tiles to the south-east, relative to center_tile.
*/
static function SafeAddRectangle(tile_list, center_tile, x_min, y_min, x_plus, y_plus);
/**
* Get the direction from one tile to another.
* @note Taken from SimpleAI.
* @param tilefrom The first tile.
* @param tileto The second tile
* @return The direction from the first tile to the second tile.
*/
static function GetDirection(tilefrom, tileto);
/**
* Get the next direction clockwise or counterclockwise relative to the specified direction.
* @param direction The current direction
* @param clockwise Boolean: true go clockwise, false go counter clockwise.
* @return The next direction.
*/
static function GetNextDirection(direction, clockwise);
/**
* Convert our tile direction to AIRail RailTrack direction.
* @param direction The tile direction.
* @return The RailTrack direction.
*/
static function DirectionToRailTrackDirection(direction);
}
function WormTiles::SafeAddSquare(list, tile, radius)
{
local x1 = max(1, AIMap.GetTileX(tile) - radius);
local y1 = max(1, AIMap.GetTileY(tile) - radius);
local x2 = min(AIMap.GetMapSizeX() - 2, AIMap.GetTileX(tile) + radius);
local y2 = min(AIMap.GetMapSizeY() - 2, AIMap.GetTileY(tile) + radius);
list.AddRectangle(AIMap.GetTileIndex(x1, y1),AIMap.GetTileIndex(x2, y2));
}
function WormTiles::SafeAddRectangle(tile_list, center_tile, x_min, y_min, x_plus, y_plus)
{
local tile_x = AIMap.GetTileX(center_tile);
local tile_y = AIMap.GetTileY(center_tile);
local tile_from = AIMap.GetTileIndex(max(1, tile_x - x_min), max(1, tile_y - y_min));
local tile_to = AIMap.GetTileIndex(min(AIMap.GetMapSizeX() - 2, tile_x + x_plus), min(AIMap.GetMapSizeY() - 2, tile_y + y_plus));
tile_list.AddRectangle(tile_from, tile_to);
}
function WormTiles::GetDirection(tilefrom, tileto)
{
local distx = AIMap.GetTileX(tileto) - AIMap.GetTileX(tilefrom);
local disty = AIMap.GetTileY(tileto) - AIMap.GetTileY(tilefrom);
local ret = 0;
if (abs(distx) > abs(disty)) {
ret = 2;
disty = distx;
}
if (disty > 0) {
ret = ret + 1;
}
return ret;
}
function WormTiles::GetNextDirection(direction, clockwise)
{
switch (direction) {
case WormTiles.DIR_NW:
return (clockwise ? WormTiles.DIR_NE : WormTiles.DIR_SW);
break;
case WormTiles.DIR_NE:
return (clockwise ? WormTiles.DIR_SE : WormTiles.DIR_NW);
break;
case WormTiles.DIR_SE:
return (clockwise ? WormTiles.DIR_SW : WormTiles.DIR_NE);
break;
case WormTiles.DIR_SW:
return (clockwise ? WormTiles.DIR_NW : WormTiles.DIR_SE);
break;
default:
AILog.Error("Illegal direction!");
return direction;
break;
}
}
function WormTiles::DirectionToRailTrackDirection(direction)
{
switch (direction) {
case WormTiles.DIR_NW:
return AIRail.RAILTRACK_NW_SE;
break;
case WormTiles.DIR_NE:
return AIRail.RAILTRACK_NE_SW;
break;
case WormTiles.DIR_SE:
return AIRail.RAILTRACK_NW_SE;
break;
case WormTiles.DIR_SW:
return AIRail.RAILTRACK_NE_SW;
break;
default:
AILog.Error("Illegal direction!");
return null;
break;
}
}