# shared.cpp

    00001 /* $Id: shared.cpp 11818 2008-01-12 14:10:35Z rubidium $ */
    00002
    00003 #include "../../stdafx.h"
    00004 #include "../../openttd.h"
    00005 #include "../../debug.h"
    00006 #include "../../map_func.h"
    00007 #include "../../vehicle_base.h"
    00008 #include "../../player_base.h"
    00009 #include "trolly.h"
    00010
    00011 int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
    00012 {
    00013   // 0 = vert
    00014   // 1 = horz
    00015   // 2 = dig up-left
    00016   // 3 = dig down-right
    00017   // 4 = dig down-left
    00018   // 5 = dig up-right
    00019
    00020   uint x1 = TileX(tile_a);
    00021   uint x2 = TileX(tile_b);
    00022   uint x3 = TileX(tile_c);
    00023
    00024   uint y1 = TileY(tile_a);
    00025   uint y2 = TileY(tile_b);
    00026   uint y3 = TileY(tile_c);
    00027
    00028   if (y1 == y2 && y2 == y3) return 0;
    00029   if (x1 == x2 && x2 == x3) return 1;
    00030   if (y2 > y1) return x2 > x3 ? 2 : 4;
    00031   if (x2 > x1) return y2 > y3 ? 2 : 5;
    00032   if (y1 > y2) return x2 > x3 ? 5 : 3;
    00033   if (x1 > x2) return y2 > y3 ? 4 : 3;
    00034
    00035   return 0;
    00036 }
    00037
    00038 int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
    00039 {
    00040   int x1, x2, x3;
    00041   int y1, y2, y3;
    00042   int r;
    00043
    00044   x1 = TileX(tile_a);
    00045   x2 = TileX(tile_b);
    00046   x3 = TileX(tile_c);
    00047
    00048   y1 = TileY(tile_a);
    00049   y2 = TileY(tile_b);
    00050   y3 = TileY(tile_c);
    00051
    00052   r = 0;
    00053
    00054   if (x1 < x2) r += 8;
    00055   if (y1 < y2) r += 1;
    00056   if (x1 > x2) r += 2;
    00057   if (y1 > y2) r += 4;
    00058
    00059   if (x2 < x3) r += 2;
    00060   if (y2 < y3) r += 4;
    00061   if (x2 > x3) r += 8;
    00062   if (y2 > y3) r += 1;
    00063
    00064   return r;
    00065 }
    00066
    00067 // Get's the direction between 2 tiles seen from tile_a
    00068 DiagDirection AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
    00069 {
    00070   if (TileY(tile_a) < TileY(tile_b)) return DIAGDIR_SE;
    00071   if (TileY(tile_a) > TileY(tile_b)) return DIAGDIR_NW;
    00072   if (TileX(tile_a) < TileX(tile_b)) return DIAGDIR_SW;
    00073   return DIAGDIR_NE;
    00074 }
    00075
    00076
    00077 // This functions looks up if this vehicle is special for this AI
    00078 //  and returns his flag
    00079 uint AiNew_GetSpecialVehicleFlag(Player* p, Vehicle* v)
    00080 {
    00081   uint i;
    00082
    00083   for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
    00084     if (_players_ainew[p->index].special_vehicles[i].veh_id == v->index) {
    00085       return _players_ainew[p->index].special_vehicles[i].flag;
    00086     }
    00087   }
    00088
    00089   // Not found :(
    00090   return 0;
    00091 }
    00092
    00093
    00094 bool AiNew_SetSpecialVehicleFlag(Player* p, Vehicle* v, uint flag)
    00095 {
    00096   int new_id = -1;
    00097   uint i;
    00098
    00099   for (i = 0; i < AI_MAX_SPECIAL_VEHICLES; i++) {
    00100     if (_players_ainew[p->index].special_vehicles[i].veh_id == v->index) {
    00101       _players_ainew[p->index].special_vehicles[i].flag |= flag;
    00102       return true;
    00103     }
    00104     if (new_id == -1 &&
    00105         _players_ainew[p->index].special_vehicles[i].veh_id == 0 &&
    00106         _players_ainew[p->index].special_vehicles[i].flag == 0) {
    00107       new_id = i;
    00108     }
    00109   }
    00110
    00111   // Out of special_vehicle spots :s
    00112   if (new_id == -1) {
    00113     DEBUG(ai, 1, "special_vehicles list is too small");
    00114     return false;
    00115   }
    00116   _players_ainew[p->index].special_vehicles[new_id].veh_id = v->index;
    00117   _players_ainew[p->index].special_vehicles[new_id].flag = flag;
    00118   return true;
    00119 }