ai_shared.c
author tron
Fri, 24 Jun 2005 12:38:35 +0000
changeset 1977 37bbebf94434
parent 1891 862800791170
child 2096 32de4f127e79
permissions -rw-r--r--
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     1
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1299
diff changeset
     2
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 926
diff changeset
     3
#include "debug.h"
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 145
diff changeset
     4
#include "map.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     5
#include "ai.h"
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
     6
#include "vehicle.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     7
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
     8
int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
     9
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    10
	// 0 = vert
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    11
	// 1 = horz
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    12
	// 2 = dig up-left
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    13
	// 3 = dig down-right
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    14
	// 4 = dig down-left
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    15
	// 5 = dig up-right
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    16
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    17
	int x1, x2, x3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    18
	int y1, y2, y3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    19
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    20
	x1 = TileX(tile_a);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    21
	x2 = TileX(tile_b);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    22
	x3 = TileX(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    23
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    24
	y1 = TileY(tile_a);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    25
	y2 = TileY(tile_b);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    26
	y3 = TileY(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    27
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    28
	if (y1 == y2 && y2 == y3) return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    29
	if (x1 == x2 && x2 == x3) return 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    30
	if (y2 > y1) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    31
		if (x2 > x3) return 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    32
		else return 4;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    33
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    34
	if (x2 > x1) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    35
		if (y2 > y3) return 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    36
		else return 5;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    37
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    38
	if (y1 > y2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    39
		if (x2 > x3) return 5;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    40
		else return 3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    41
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    42
	if (x1 > x2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    43
		if (y2 > y3) return 4;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    44
		else return 3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    45
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    46
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    47
	return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    48
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    49
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    50
int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    51
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    52
	int x1, x2, x3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    53
	int y1, y2, y3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    54
	int r;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    56
	x1 = TileX(tile_a);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    57
	x2 = TileX(tile_b);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    58
	x3 = TileX(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    59
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    60
	y1 = TileY(tile_a);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    61
	y2 = TileY(tile_b);
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    62
	y3 = TileY(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    63
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    64
	r = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    65
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    66
	if (x1 < x2) r += 8;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    67
	if (y1 < y2) r += 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    68
	if (x1 > x2) r += 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    69
	if (y1 > y2) r += 4;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    70
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    71
	if (x2 < x3) r += 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
	if (y2 < y3) r += 4;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    73
	if (x2 > x3) r += 8;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    74
	if (y2 > y3) r += 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    75
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    76
	return r;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    77
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    78
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    79
// Get's the direction between 2 tiles seen from tile_a
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    80
int AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    81
{
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    82
	if (TileY(tile_a) < TileY(tile_b)) return 1;
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    83
	if (TileY(tile_a) > TileY(tile_b)) return 3;
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
    84
	if (TileX(tile_a) < TileX(tile_b)) return 2;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    85
	return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    86
}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    87
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    88
// This functions looks up if this vehicle is special for this AI
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    89
//  and returns his flag
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    90
uint AiNew_GetSpecialVehicleFlag(Player *p, Vehicle *v) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    91
	int i;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    92
	for (i=0;i<AI_MAX_SPECIAL_VEHICLES;i++) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    93
		if (p->ainew.special_vehicles[i].veh_id == v->index) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    94
			return p->ainew.special_vehicles[i].flag;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    95
		}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    96
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    97
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    98
	// Not found :(
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    99
	return 0;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   100
}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   101
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   102
bool AiNew_SetSpecialVehicleFlag(Player *p, Vehicle *v, uint flag) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   103
	int i, new_id = -1;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   104
	for (i=0;i<AI_MAX_SPECIAL_VEHICLES;i++) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   105
		if (p->ainew.special_vehicles[i].veh_id == v->index) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   106
			p->ainew.special_vehicles[i].flag |= flag;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   107
			return true;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   108
		}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   109
		if (new_id == -1 && p->ainew.special_vehicles[i].veh_id == 0 &&
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   110
			p->ainew.special_vehicles[i].flag == 0)
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   111
			new_id = i;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   112
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   113
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   114
	// Out of special_vehicle spots :s
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   115
	if (new_id == -1) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   116
		DEBUG(ai, 1)("special_vehicles list is too small :(");
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   117
		return false;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   118
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   119
	p->ainew.special_vehicles[new_id].veh_id = v->index;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   120
	p->ainew.special_vehicles[new_id].flag = flag;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   121
	return true;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   122
}