ai_shared.c
changeset 2381 9a5ed6c0a2e6
parent 2380 392bba57462d
child 2382 a66af7211f95
equal deleted inserted replaced
2380:392bba57462d 2381:9a5ed6c0a2e6
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "debug.h"
       
     6 #include "map.h"
       
     7 #include "ai_new.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 	int x1, x2, x3;
       
    20 	int y1, y2, y3;
       
    21 
       
    22 	x1 = TileX(tile_a);
       
    23 	x2 = TileX(tile_b);
       
    24 	x3 = TileX(tile_c);
       
    25 
       
    26 	y1 = TileY(tile_a);
       
    27 	y2 = TileY(tile_b);
       
    28 	y3 = TileY(tile_c);
       
    29 
       
    30 	if (y1 == y2 && y2 == y3) return 0;
       
    31 	if (x1 == x2 && x2 == x3) return 1;
       
    32 	if (y2 > y1) {
       
    33 		if (x2 > x3) return 2;
       
    34 		else return 4;
       
    35 	}
       
    36 	if (x2 > x1) {
       
    37 		if (y2 > y3) return 2;
       
    38 		else return 5;
       
    39 	}
       
    40 	if (y1 > y2) {
       
    41 		if (x2 > x3) return 5;
       
    42 		else return 3;
       
    43 	}
       
    44 	if (x1 > x2) {
       
    45 		if (y2 > y3) return 4;
       
    46 		else return 3;
       
    47 	}
       
    48 
       
    49 	return 0;
       
    50 }
       
    51 
       
    52 int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
       
    53 {
       
    54 	int x1, x2, x3;
       
    55 	int y1, y2, y3;
       
    56 	int r;
       
    57 
       
    58 	x1 = TileX(tile_a);
       
    59 	x2 = TileX(tile_b);
       
    60 	x3 = TileX(tile_c);
       
    61 
       
    62 	y1 = TileY(tile_a);
       
    63 	y2 = TileY(tile_b);
       
    64 	y3 = TileY(tile_c);
       
    65 
       
    66 	r = 0;
       
    67 
       
    68 	if (x1 < x2) r += 8;
       
    69 	if (y1 < y2) r += 1;
       
    70 	if (x1 > x2) r += 2;
       
    71 	if (y1 > y2) r += 4;
       
    72 
       
    73 	if (x2 < x3) r += 2;
       
    74 	if (y2 < y3) r += 4;
       
    75 	if (x2 > x3) r += 8;
       
    76 	if (y2 > y3) r += 1;
       
    77 
       
    78 	return r;
       
    79 }
       
    80 
       
    81 // Get's the direction between 2 tiles seen from tile_a
       
    82 int AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
       
    83 {
       
    84 	if (TileY(tile_a) < TileY(tile_b)) return 1;
       
    85 	if (TileY(tile_a) > TileY(tile_b)) return 3;
       
    86 	if (TileX(tile_a) < TileX(tile_b)) return 2;
       
    87 	return 0;
       
    88 }
       
    89 
       
    90 // This functions looks up if this vehicle is special for this AI
       
    91 //  and returns his flag
       
    92 uint AiNew_GetSpecialVehicleFlag(Player *p, Vehicle *v) {
       
    93 	int i;
       
    94 	for (i=0;i<AI_MAX_SPECIAL_VEHICLES;i++) {
       
    95 		if (p->ainew.special_vehicles[i].veh_id == v->index) {
       
    96 			return p->ainew.special_vehicles[i].flag;
       
    97 		}
       
    98 	}
       
    99 
       
   100 	// Not found :(
       
   101 	return 0;
       
   102 }
       
   103 
       
   104 bool AiNew_SetSpecialVehicleFlag(Player *p, Vehicle *v, uint flag) {
       
   105 	int i, new_id = -1;
       
   106 	for (i=0;i<AI_MAX_SPECIAL_VEHICLES;i++) {
       
   107 		if (p->ainew.special_vehicles[i].veh_id == v->index) {
       
   108 			p->ainew.special_vehicles[i].flag |= flag;
       
   109 			return true;
       
   110 		}
       
   111 		if (new_id == -1 && p->ainew.special_vehicles[i].veh_id == 0 &&
       
   112 			p->ainew.special_vehicles[i].flag == 0)
       
   113 			new_id = i;
       
   114 	}
       
   115 
       
   116 	// Out of special_vehicle spots :s
       
   117 	if (new_id == -1) {
       
   118 		DEBUG(ai, 1)("special_vehicles list is too small :(");
       
   119 		return false;
       
   120 	}
       
   121 	p->ainew.special_vehicles[new_id].veh_id = v->index;
       
   122 	p->ainew.special_vehicles[new_id].flag = flag;
       
   123 	return true;
       
   124 }