src/ai/trolly/shared.cpp
author rubidium
Thu, 27 Dec 2007 13:35:39 +0000
changeset 8640 1e93b81e96d2
parent 8635 3bbb6f87fced
child 8725 b769d0a63f06
permissions -rw-r--r--
(svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2096
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2096
diff changeset
     2
2381
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
     3
#include "../../stdafx.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
     4
#include "../../openttd.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
     5
#include "../../debug.h"
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 5835
diff changeset
     6
#include "../../map_func.h"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8635
diff changeset
     7
#include "../../vehicle_base.h"
2381
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2186
diff changeset
     8
#include "trolly.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     9
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    10
int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    11
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    12
	// 0 = vert
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    13
	// 1 = horz
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    14
	// 2 = dig up-left
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    15
	// 3 = dig down-right
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    16
	// 4 = dig down-left
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    17
	// 5 = dig up-right
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    18
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    19
	uint x1 = TileX(tile_a);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    20
	uint x2 = TileX(tile_b);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    21
	uint x3 = TileX(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    22
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    23
	uint y1 = TileY(tile_a);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    24
	uint y2 = TileY(tile_b);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    25
	uint y3 = TileY(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    26
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    27
	if (y1 == y2 && y2 == y3) return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    28
	if (x1 == x2 && x2 == x3) return 1;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    29
	if (y2 > y1) return x2 > x3 ? 2 : 4;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    30
	if (x2 > x1) return y2 > y3 ? 2 : 5;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    31
	if (y1 > y2) return x2 > x3 ? 5 : 3;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    32
	if (x1 > x2) return y2 > y3 ? 4 : 3;
110
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
	return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    35
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    36
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    37
int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    38
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    39
	int x1, x2, x3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    40
	int y1, y2, y3;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    41
	int r;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    42
926
bd4312619522 (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
    43
	x1 = TileX(tile_a);
bd4312619522 (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
    44
	x2 = TileX(tile_b);
bd4312619522 (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
    45
	x3 = TileX(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    46
926
bd4312619522 (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
    47
	y1 = TileY(tile_a);
bd4312619522 (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
    48
	y2 = TileY(tile_b);
bd4312619522 (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
    49
	y3 = TileY(tile_c);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    50
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    51
	r = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    52
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    53
	if (x1 < x2) r += 8;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    54
	if (y1 < y2) r += 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
	if (x1 > x2) r += 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    56
	if (y1 > y2) r += 4;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    57
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    58
	if (x2 < x3) r += 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    59
	if (y2 < y3) r += 4;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    60
	if (x2 > x3) r += 8;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    61
	if (y2 > y3) r += 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    62
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    63
	return r;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    64
}
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
// Get's the direction between 2 tiles seen from tile_a
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 2381
diff changeset
    67
DiagDirection AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    68
{
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 2381
diff changeset
    69
	if (TileY(tile_a) < TileY(tile_b)) return DIAGDIR_SE;
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 2381
diff changeset
    70
	if (TileY(tile_a) > TileY(tile_b)) return DIAGDIR_NW;
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 2381
diff changeset
    71
	if (TileX(tile_a) < TileX(tile_b)) return DIAGDIR_SW;
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 2381
diff changeset
    72
	return DIAGDIR_NE;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    73
}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    74
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    75
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    76
// 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
    77
//  and returns his flag
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    78
uint AiNew_GetSpecialVehicleFlag(Player* p, Vehicle* v)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    79
{
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    80
	uint i;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    81
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    82
	for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    83
		if (p->ainew.special_vehicles[i].veh_id == v->index) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    84
			return p->ainew.special_vehicles[i].flag;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    85
		}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    86
	}
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
	// Not found :(
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    89
	return 0;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    90
}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    91
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    92
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    93
bool AiNew_SetSpecialVehicleFlag(Player* p, Vehicle* v, uint flag)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    94
{
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    95
	int new_id = -1;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    96
	uint i;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    97
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
    98
	for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    99
		if (p->ainew.special_vehicles[i].veh_id == v->index) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   100
			p->ainew.special_vehicles[i].flag |= flag;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   101
			return true;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   102
		}
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
   103
		if (new_id == -1 &&
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
   104
				p->ainew.special_vehicles[i].veh_id == 0 &&
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
   105
				p->ainew.special_vehicles[i].flag == 0) {
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   106
			new_id = i;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3644
diff changeset
   107
		}
145
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
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   110
	// Out of special_vehicle spots :s
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   111
	if (new_id == -1) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4077
diff changeset
   112
		DEBUG(ai, 1, "special_vehicles list is too small");
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   113
		return false;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   114
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   115
	p->ainew.special_vehicles[new_id].veh_id = v->index;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   116
	p->ainew.special_vehicles[new_id].flag = flag;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   117
	return true;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   118
}