src/ai/trolly/shared.cpp
changeset 5584 1111b4d36e35
parent 5475 2e6990a8c7c4
child 8139 4e91c448c409
equal deleted inserted replaced
5583:136d8764c7e6 5584:1111b4d36e35
       
     1 /* $Id$ */
       
     2 
       
     3 #include "../../stdafx.h"
       
     4 #include "../../openttd.h"
       
     5 #include "../../debug.h"
       
     6 #include "../../map.h"
       
     7 #include "trolly.h"
       
     8 #include "../../vehicle.h"
       
     9 
       
    10 int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
       
    11 {
       
    12 	// 0 = vert
       
    13 	// 1 = horz
       
    14 	// 2 = dig up-left
       
    15 	// 3 = dig down-right
       
    16 	// 4 = dig down-left
       
    17 	// 5 = dig up-right
       
    18 
       
    19 	uint x1 = TileX(tile_a);
       
    20 	uint x2 = TileX(tile_b);
       
    21 	uint x3 = TileX(tile_c);
       
    22 
       
    23 	uint y1 = TileY(tile_a);
       
    24 	uint y2 = TileY(tile_b);
       
    25 	uint y3 = TileY(tile_c);
       
    26 
       
    27 	if (y1 == y2 && y2 == y3) return 0;
       
    28 	if (x1 == x2 && x2 == x3) return 1;
       
    29 	if (y2 > y1) return x2 > x3 ? 2 : 4;
       
    30 	if (x2 > x1) return y2 > y3 ? 2 : 5;
       
    31 	if (y1 > y2) return x2 > x3 ? 5 : 3;
       
    32 	if (x1 > x2) return y2 > y3 ? 4 : 3;
       
    33 
       
    34 	return 0;
       
    35 }
       
    36 
       
    37 int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
       
    38 {
       
    39 	int x1, x2, x3;
       
    40 	int y1, y2, y3;
       
    41 	int r;
       
    42 
       
    43 	x1 = TileX(tile_a);
       
    44 	x2 = TileX(tile_b);
       
    45 	x3 = TileX(tile_c);
       
    46 
       
    47 	y1 = TileY(tile_a);
       
    48 	y2 = TileY(tile_b);
       
    49 	y3 = TileY(tile_c);
       
    50 
       
    51 	r = 0;
       
    52 
       
    53 	if (x1 < x2) r += 8;
       
    54 	if (y1 < y2) r += 1;
       
    55 	if (x1 > x2) r += 2;
       
    56 	if (y1 > y2) r += 4;
       
    57 
       
    58 	if (x2 < x3) r += 2;
       
    59 	if (y2 < y3) r += 4;
       
    60 	if (x2 > x3) r += 8;
       
    61 	if (y2 > y3) r += 1;
       
    62 
       
    63 	return r;
       
    64 }
       
    65 
       
    66 // Get's the direction between 2 tiles seen from tile_a
       
    67 DiagDirection AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
       
    68 {
       
    69 	if (TileY(tile_a) < TileY(tile_b)) return DIAGDIR_SE;
       
    70 	if (TileY(tile_a) > TileY(tile_b)) return DIAGDIR_NW;
       
    71 	if (TileX(tile_a) < TileX(tile_b)) return DIAGDIR_SW;
       
    72 	return DIAGDIR_NE;
       
    73 }
       
    74 
       
    75 
       
    76 // This functions looks up if this vehicle is special for this AI
       
    77 //  and returns his flag
       
    78 uint AiNew_GetSpecialVehicleFlag(Player* p, Vehicle* v)
       
    79 {
       
    80 	uint i;
       
    81 
       
    82 	for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
       
    83 		if (p->ainew.special_vehicles[i].veh_id == v->index) {
       
    84 			return p->ainew.special_vehicles[i].flag;
       
    85 		}
       
    86 	}
       
    87 
       
    88 	// Not found :(
       
    89 	return 0;
       
    90 }
       
    91 
       
    92 
       
    93 bool AiNew_SetSpecialVehicleFlag(Player* p, Vehicle* v, uint flag)
       
    94 {
       
    95 	int new_id = -1;
       
    96 	uint i;
       
    97 
       
    98 	for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
       
    99 		if (p->ainew.special_vehicles[i].veh_id == v->index) {
       
   100 			p->ainew.special_vehicles[i].flag |= flag;
       
   101 			return true;
       
   102 		}
       
   103 		if (new_id == -1 &&
       
   104 				p->ainew.special_vehicles[i].veh_id == 0 &&
       
   105 				p->ainew.special_vehicles[i].flag == 0) {
       
   106 			new_id = i;
       
   107 		}
       
   108 	}
       
   109 
       
   110 	// Out of special_vehicle spots :s
       
   111 	if (new_id == -1) {
       
   112 		DEBUG(ai, 1, "special_vehicles list is too small");
       
   113 		return false;
       
   114 	}
       
   115 	p->ainew.special_vehicles[new_id].veh_id = v->index;
       
   116 	p->ainew.special_vehicles[new_id].flag = flag;
       
   117 	return true;
       
   118 }