# trolly.h

    00001 /* $Id: trolly.h 11818 2008-01-12 14:10:35Z rubidium $ */
    00002
    00003 #ifndef AI_TROLLY_H
    00004 #define AI_TROLLY_H
    00005
    00006 #include "../../aystar.h"
    00007 #include "../../player_type.h"
    00008 #include "../../vehicle_type.h"
    00009 #include "../../date_type.h"
    00010
    00011 /*
    00012  * These defines can be altered to change the behavoir of the AI
    00013  *
    00014  * WARNING:
    00015  *   This can also alter the AI in a negative way. I will never claim these settings
    00016  *   are perfect, but don't change them if you don't know what the effect is.
    00017  */
    00018
    00019 // How many times it the H multiplied. The higher, the more it will go straight to the
    00020 //   end point. The lower, how more it will find the route with the lowest cost.
    00021 //   also: the lower, the longer it takes before route is calculated..
    00022 #define AI_PATHFINDER_H_MULTIPLER 100
    00023
    00024 // How many loops may AyStar do before it stops
    00025 //   0 = infinite
    00026 #define AI_PATHFINDER_LOOPS_PER_TICK 5
    00027
    00028 // How long may the AI search for one route?
    00029 //   0 = infinite
    00030 // This number is the number of tiles tested.
    00031 //  It takes (AI_PATHFINDER_MAX_SEARCH_NODES / AI_PATHFINDER_LOOPS_PER_TICK) ticks
    00032 //  to get here.. with 5000 / 10 = 500. 500 / 74 (one day) = 8 days till it aborts
    00033 //   (that is: if the AI is on VERY FAST! :p
    00034 #define AI_PATHFINDER_MAX_SEARCH_NODES 5000
    00035
    00036 // If you enable this, the AI is not allowed to make 90degree turns
    00037 #define AI_PATHFINDER_NO_90DEGREES_TURN
    00038
    00039 // Below are defines for the g-calculation
    00040
    00041 // Standard penalty given to a tile
    00042 #define AI_PATHFINDER_PENALTY 150
    00043 // The penalty given to a tile that is going up
    00044 #define AI_PATHFINDER_TILE_GOES_UP_PENALTY 450
    00045 // The penalty given to a tile which would have to use fundation
    00046 #define AI_PATHFINDER_FOUNDATION_PENALTY 100
    00047 // Changing direction is a penalty, to prevent curved ways (with that: slow ways)
    00048 #define AI_PATHFINDER_DIRECTION_CHANGE_PENALTY 200
    00049 // Same penalty, only for when road already exists
    00050 #define AI_PATHFINDER_DIRECTION_CHANGE_ON_EXISTING_ROAD_PENALTY 50
    00051 // A diagonal track cost the same as a straigh, but a diagonal is faster... so give
    00052 //  a bonus for using diagonal track
    00053 #ifdef AI_PATHFINDER_NO_90DEGREES_TURN
    00054 #define AI_PATHFINDER_DIAGONAL_BONUS 95
    00055 #else
    00056 #define AI_PATHFINDER_DIAGONAL_BONUS 75
    00057 #endif
    00058 // If a roadblock already exists, it gets a bonus
    00059 #define AI_PATHFINDER_ROAD_ALREADY_EXISTS_BONUS 140
    00060 // To prevent 3 direction changes in 3 tiles, this penalty is given in such situation
    00061 #define AI_PATHFINDER_CURVE_PENALTY 200
    00062
    00063 // Penalty a bridge gets per length
    00064 #define AI_PATHFINDER_BRIDGE_PENALTY 180
    00065 // The penalty for a bridge going up
    00066 #define AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY 1000
    00067
    00068 // Tunnels are expensive...
    00069 //  Because of that, every tile the cost is increased with 1/8th of his value
    00070 //  This is also true if you are building a tunnel yourself
    00071 #define AI_PATHFINDER_TUNNEL_PENALTY 350
    00072
    00073 /*
    00074  * Ai_New defines
    00075  */
    00076
    00077 // How long may we search cities and industry for a new route?
    00078 #define AI_LOCATE_ROUTE_MAX_COUNTER 200
    00079
    00080 // How many days must there be between building the first station and the second station
    00081 //  within one city. This number is in days and should be more than 4 months.
    00082 #define AI_CHECKCITY_DATE_BETWEEN 180
    00083
    00084 // How many cargo is needed for one station in a city?
    00085 #define AI_CHECKCITY_CARGO_PER_STATION 60
    00086 // How much cargo must there not be used in a city before we can build a new station?
    00087 #define AI_CHECKCITY_NEEDED_CARGO 50
    00088 // When there is already a station which takes the same good and the rating of that
    00089 //  city is higher then this numer, we are not going to attempt to build anything
    00090 //  there
    00091 #define AI_CHECKCITY_CARGO_RATING 50
    00092 // But, there is a chance of 1 out of this number, that we do ;)
    00093 #define AI_CHECKCITY_CARGO_RATING_CHANCE 5
    00094 // If a city is too small to contain a station, there is a small chance
    00095 //  that we still do so.. just to make the city bigger!
    00096 #define AI_CHECKCITY_CITY_CHANCE 5
    00097
    00098 // This number indicates for every unit of cargo, how many tiles two stations maybe be away
    00099 //  from eachother. In other words: if we have 120 units of cargo in one station, and 120 units
    00100 //  of the cargo in the other station, both stations can be 96 units away from eachother, if the
    00101 //  next number is 0.4.
    00102 #define AI_LOCATEROUTE_BUS_CARGO_DISTANCE 0.4
    00103 #define AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE 0.7
    00104 // In whole tiles, the minimum distance for a truck route
    00105 #define AI_LOCATEROUTE_TRUCK_MIN_DISTANCE 30
    00106
    00107 // The amount of tiles in a square from -X to +X that is scanned for a station spot
    00108 //  (so if this number is 10, 20x20 = 400 tiles are scanned for _the_ perfect spot
    00109 // Safe values are between 15 and 5
    00110 #define AI_FINDSTATION_TILE_RANGE 10
    00111
    00112 // Building on normal speed goes very fast. Idle this amount of ticks between every
    00113 //  building part. It is calculated like this: (4 - competitor_speed) * num + 1
    00114 //  where competitor_speed is between 0 (very slow) to 4 (very fast)
    00115 #define AI_BUILDPATH_PAUSE 10
    00116
    00117 // Minimum % of reliabilty a vehicle has to have before the AI buys it
    00118 #define AI_VEHICLE_MIN_RELIABILTY 60
    00119
    00120 // The minimum amount of money a player should always have
    00121 #define AI_MINIMUM_MONEY 15000
    00122
    00123 // If the most cheap route is build, how much is it going to cost..
    00124 // This is to prevent the AI from trying to build a route which can not be paid for
    00125 #define AI_MINIMUM_BUS_ROUTE_MONEY 25000
    00126 #define AI_MINIMUM_TRUCK_ROUTE_MONEY 35000
    00127
    00128 // The minimum amount of money before we are going to repay any money
    00129 #define AI_MINIMUM_LOAN_REPAY_MONEY 40000
    00130 // How many repays do we do if we have enough money to do so?
    00131 //  Every repay is 10000
    00132 #define AI_LOAN_REPAY 2
    00133 // How much income must we have before paying back a loan? Month-based (and looked at the last month)
    00134 #define AI_MINIMUM_INCOME_FOR_LOAN 7000
    00135
    00136 // If there is <num> time as much cargo in the station then the vehicle can handle
    00137 //  reuse the station instead of building a new one!
    00138 #define AI_STATION_REUSE_MULTIPLER 2
    00139
    00140 // No more than this amount of vehicles per station..
    00141 #define AI_CHECK_MAX_VEHICLE_PER_STATION 10
    00142
    00143 // How many thick between building 2 vehicles
    00144 #define AI_BUILD_VEHICLE_TIME_BETWEEN DAY_TICKS
    00145
    00146 // How many days must there between vehicle checks
    00147 //  The more often, the less non-money-making lines there will be
    00148 //   but the unfair it may seem to a human player
    00149 #define AI_DAYS_BETWEEN_VEHICLE_CHECKS 30
    00150
    00151 // How money profit does a vehicle needs to make to stay in order
    00152 //  This is the profit of this year + profit of last year
    00153 //  But also for vehicles that are just one year old. In other words:
    00154 //   Vehicles of 2 years do easier meet this setting then vehicles
    00155 //   of one year. This is a very good thing. New vehicles are filtered,
    00156 //   while old vehicles stay longer, because we do get less in return.
    00157 #define AI_MINIMUM_ROUTE_PROFIT 1000
    00158
    00159 // A vehicle is considered lost when he his cargo is more than 180 days old
    00160 #define AI_VEHICLE_LOST_DAYS 180
    00161
    00162 // How many times may the AI try to find a route before it gives up
    00163 #define AI_MAX_TRIES_FOR_SAME_ROUTE 8
    00164
    00165 /*
    00166  * End of defines
    00167  */
    00168
    00169 // This stops 90degrees curves
    00170 static const byte _illegal_curves[6] = {
    00171   255, 255, // Horz and vert, don't have the effect
    00172   5, // upleft and upright are not valid
    00173   4, // downright and downleft are not valid
    00174   2, // downleft and upleft are not valid
    00175   3, // upright and downright are not valid
    00176 };
    00177
    00178 enum {
    00179   AI_STATE_STARTUP = 0,
    00180   AI_STATE_FIRST_TIME,
    00181   AI_STATE_NOTHING,
    00182   AI_STATE_WAKE_UP,
    00183   AI_STATE_LOCATE_ROUTE,
    00184   AI_STATE_FIND_STATION,
    00185   AI_STATE_FIND_PATH,
    00186   AI_STATE_FIND_DEPOT,
    00187   AI_STATE_VERIFY_ROUTE,
    00188   AI_STATE_BUILD_STATION,
    00189   AI_STATE_BUILD_PATH,
    00190   AI_STATE_BUILD_DEPOT,
    00191   AI_STATE_BUILD_VEHICLE,
    00192   AI_STATE_WAIT_FOR_BUILD,
    00193   AI_STATE_GIVE_ORDERS,
    00194   AI_STATE_START_VEHICLE,
    00195   AI_STATE_REPAY_MONEY,
    00196   AI_STATE_CHECK_ALL_VEHICLES,
    00197   AI_STATE_ACTION_DONE,
    00198   AI_STATE_STOP, // Temporary function to stop the AI
    00199 };
    00200
    00201 // Used for tbt (train/bus/truck)
    00202 enum {
    00203   AI_TRAIN = 0,
    00204   AI_BUS,
    00205   AI_TRUCK,
    00206 };
    00207
    00208 enum {
    00209   AI_ACTION_NONE = 0,
    00210   AI_ACTION_BUS_ROUTE,
    00211   AI_ACTION_TRUCK_ROUTE,
    00212   AI_ACTION_REPAY_LOAN,
    00213   AI_ACTION_CHECK_ALL_VEHICLES,
    00214 };
    00215
    00216 // Used for from_type/to_type
    00217 enum {
    00218   AI_NO_TYPE = 0,
    00219   AI_CITY,
    00220   AI_INDUSTRY,
    00221 };
    00222
    00223 // Flags for in the vehicle
    00224 enum {
    00225   AI_VEHICLEFLAG_SELL = 1,
    00226   // Remember, flags must be in power of 2
    00227 };
    00228
    00229 #define AI_NO_CARGO 0xFF // Means that there is no cargo defined yet (used for industry)
    00230 #define AI_NEED_CARGO 0xFE // Used when the AI needs to find out a cargo for the route
    00231 #define AI_STATION_RANGE TileXY(MapMaxX(), MapMaxY())
    00232
    00233 #define AI_PATHFINDER_NO_DIRECTION (byte)-1
    00234
    00235 // Flags used in user_data
    00236 #define AI_PATHFINDER_FLAG_BRIDGE 1
    00237 #define AI_PATHFINDER_FLAG_TUNNEL 2
    00238
    00239 typedef void AiNew_StateFunction(Player *p);
    00240
    00241 // ai_new.c
    00242 void AiNewDoGameLoop(Player *p);
    00243
    00244 struct Ai_PathFinderInfo {
    00245   TileIndex start_tile_tl;
    00246   TileIndex start_tile_br;
    00247   TileIndex end_tile_tl;
    00248   TileIndex end_tile_br;
    00249   DiagDirection start_direction;
    00250   DiagDirection end_direction;
    00251
    00252   TileIndex route[500];
    00253   byte route_extra[500];
    00254   int route_length;
    00255   int position;
    00256
    00257   bool rail_or_road;
    00258 };
    00259
    00260 // ai_pathfinder.c
    00261 AyStar *new_AyStar_AiPathFinder(int max_tiles_around, Ai_PathFinderInfo *PathFinderInfo);
    00262 void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo);
    00263
    00264 // ai_shared.c
    00265 int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c);
    00266 int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c);
    00267 DiagDirection AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b);
    00268 bool AiNew_SetSpecialVehicleFlag(Player *p, Vehicle *v, uint flag);
    00269 uint AiNew_GetSpecialVehicleFlag(Player *p, Vehicle *v);
    00270
    00271 // ai_build.c
    00272 bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile);
    00273 CommandCost AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag);
    00274 CommandCost AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag);
    00275 CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag);
    00276 EngineID AiNew_PickVehicle(Player *p);
    00277 CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag);
    00278 CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag);
    00279
    00280 /* The amount of memory reserved for the AI-special-vehicles */
    00281 #define AI_MAX_SPECIAL_VEHICLES 100
    00282
    00283 struct Ai_SpecialVehicle {
    00284   VehicleID veh_id;
    00285   uint32 flag;
    00286 };
    00287
    00288 struct PlayerAiNew {
    00289   uint8 state;
    00290   uint tick;
    00291   uint idle;
    00292
    00293   int temp;
    00294
    00295
    00296
    00297   int counter;
    00298
    00299
    00300   /* Pathfinder stuff */
    00301   Ai_PathFinderInfo path_info;
    00302   AyStar *pathfinder;
    00303
    00304   /* Route stuff */
    00305
    00306   CargoID cargo;
    00307   byte tbt;
    00308   Money new_cost;
    00309
    00310   byte action;
    00311
    00312   int last_id;
    00313   Date last_vehiclecheck_date; // Used in CheckVehicle
    00314   Ai_SpecialVehicle special_vehicles[AI_MAX_SPECIAL_VEHICLES];
    00315
    00316   TileIndex from_tile;
    00317   TileIndex to_tile;
    00318
    00319   DiagDirectionByte from_direction;
    00320   DiagDirectionByte to_direction;
    00321
    00322   bool from_deliver;
    00323   bool to_deliver;
    00324
    00325   TileIndex depot_tile;
    00326   DiagDirectionByte depot_direction;
    00327
    00328   byte amount_veh;
    00329   byte cur_veh;
    00330   VehicleID veh_id;
    00331   VehicleID veh_main_id;
    00332
    00333   int from_ic;
    00334   byte from_type;
    00335   int to_ic;
    00336   byte to_type;
    00337 };
    00338 extern PlayerAiNew _players_ainew[MAX_PLAYERS];
    00339
    00340 #endif /* AI_TROLLY_H */