summaryrefslogtreecommitdiff
path: root/src/ai/trolly/shared.cpp
blob: 1183f0371f2208f79999f454b8fe72c29c5d10a7 (plain)
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
/* $Id$ */

/** @file shared.cpp Shared functions for the trolly AI. */

#include "../../stdafx.h"
#include "../../openttd.h"
#include "../../debug.h"
#include "../../map_func.h"
#include "../../vehicle_base.h"
#include "../../company_base.h"
#include "trolly.h"

int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
{
	// 0 = vert
	// 1 = horz
	// 2 = dig up-left
	// 3 = dig down-right
	// 4 = dig down-left
	// 5 = dig up-right

	uint x1 = TileX(tile_a);
	uint x2 = TileX(tile_b);
	uint x3 = TileX(tile_c);

	uint y1 = TileY(tile_a);
	uint y2 = TileY(tile_b);
	uint y3 = TileY(tile_c);

	if (y1 == y2 && y2 == y3) return 0;
	if (x1 == x2 && x2 == x3) return 1;
	if (y2 > y1) return x2 > x3 ? 2 : 4;
	if (x2 > x1) return y2 > y3 ? 2 : 5;
	if (y1 > y2) return x2 > x3 ? 5 : 3;
	if (x1 > x2) return y2 > y3 ? 4 : 3;

	return 0;
}

int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
{
	int x1, x2, x3;
	int y1, y2, y3;
	int r;

	x1 = TileX(tile_a);
	x2 = TileX(tile_b);
	x3 = TileX(tile_c);

	y1 = TileY(tile_a);
	y2 = TileY(tile_b);
	y3 = TileY(tile_c);

	r = 0;

	if (x1 < x2) r += 8;
	if (y1 < y2) r += 1;
	if (x1 > x2) r += 2;
	if (y1 > y2) r += 4;

	if (x2 < x3) r += 2;
	if (y2 < y3) r += 4;
	if (x2 > x3) r += 8;
	if (y2 > y3) r += 1;

	return r;
}

// Get's the direction between 2 tiles seen from tile_a
DiagDirection AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
{
	if (TileY(tile_a) < TileY(tile_b)) return DIAGDIR_SE;
	if (TileY(tile_a) > TileY(tile_b)) return DIAGDIR_NW;
	if (TileX(tile_a) < TileX(tile_b)) return DIAGDIR_SW;
	return DIAGDIR_NE;
}


// This functions looks up if this vehicle is special for this AI
//  and returns his flag
uint AiNew_GetSpecialVehicleFlag(Company *c, Vehicle *v)
{
	uint i;

	for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
		if (_companies_ainew[c->index].special_vehicles[i].veh_id == v->index) {
			return _companies_ainew[c->index].special_vehicles[i].flag;
		}
	}

	// Not found :(
	return 0;
}


bool AiNew_SetSpecialVehicleFlag(Company *c, Vehicle *v, uint flag)
{
	int new_id = -1;
	uint i;

	for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
		if (_companies_ainew[c->index].special_vehicles[i].veh_id == v->index) {
			_companies_ainew[c->index].special_vehicles[i].flag |= flag;
			return true;
		}
		if (new_id == -1 &&
				_companies_ainew[c->index].special_vehicles[i].veh_id == 0 &&
				_companies_ainew[c->index].special_vehicles[i].flag == 0) {
			new_id = i;
		}
	}

	// Out of special_vehicle spots :s
	if (new_id == -1) {
		DEBUG(ai, 1, "special_vehicles list is too small");
		return false;
	}
	_companies_ainew[c->index].special_vehicles[new_id].veh_id = v->index;
	_companies_ainew[c->index].special_vehicles[new_id].flag = flag;
	return true;
}