trolly.h

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

Generated on Fri Jan 9 19:01:46 2009 for openttd by  doxygen 1.5.6