newgrf_engine.c
changeset 3702 b4ba84e5beef
parent 3696 93002e004c57
child 3703 7a63c0e05948
equal deleted inserted replaced
3701:13911c5df3e7 3702:b4ba84e5beef
     6 #include "debug.h"
     6 #include "debug.h"
     7 #include "functions.h"
     7 #include "functions.h"
     8 #include "engine.h"
     8 #include "engine.h"
     9 #include "train.h"
     9 #include "train.h"
    10 #include "player.h"
    10 #include "player.h"
       
    11 #include "station.h"
       
    12 #include "airport.h"
    11 #include "newgrf_callbacks.h"
    13 #include "newgrf_callbacks.h"
    12 #include "newgrf_engine.h"
    14 #include "newgrf_engine.h"
    13 #include "newgrf_station.h"
    15 #include "newgrf_station.h"
    14 #include "newgrf_spritegroup.h"
    16 #include "newgrf_spritegroup.h"
    15 
    17 
   168 {
   170 {
   169 	if (v->type != VEH_Train) return v->subtype;
   171 	if (v->type != VEH_Train) return v->subtype;
   170 	if (IsTrainEngine(v)) return 0;
   172 	if (IsTrainEngine(v)) return 0;
   171 	if (IsFreeWagon(v)) return 4;
   173 	if (IsFreeWagon(v)) return 4;
   172 	return 2;
   174 	return 2;
       
   175 }
       
   176 
       
   177 
       
   178 /* TTDP style aircraft movement states for GRF Action 2 Var 0xE2 */
       
   179 enum {
       
   180 	AMS_TTDP_HANGER,
       
   181 	AMS_TTDP_TO_HANGER,
       
   182 	AMS_TTDP_TO_PAD1,
       
   183 	AMS_TTDP_TO_PAD2,
       
   184 	AMS_TTDP_TO_PAD3,
       
   185 	AMS_TTDP_TO_ENTRY_2_AND_3,
       
   186 	AMS_TTDP_TO_ENTRY_2_AND_3_AND_H,
       
   187 	AMS_TTDP_TO_JUNCTION,
       
   188 	AMS_TTDP_LEAVE_RUNWAY,
       
   189 	AMS_TTDP_TO_INWAY,
       
   190 	AMS_TTDP_TO_RUNWAY,
       
   191 	AMS_TTDP_TO_OUTWAY,
       
   192 	AMS_TTDP_WAITING,
       
   193 	AMS_TTDP_TAKEOFF,
       
   194 	AMS_TTDP_TO_TAKEOFF,
       
   195 	AMS_TTDP_CLIMBING,
       
   196 	AMS_TTDP_FLIGHT_APPROACH,
       
   197 	AMS_TTDP_UNUSED_0x11,
       
   198 	AMS_TTDP_FLIGHT_TO_TOWER,
       
   199 	AMS_TTDP_UNUSED_0x13,
       
   200 	AMS_TTDP_FLIGHT_FINAL,
       
   201 	AMS_TTDP_FLIGHT_DESCENT,
       
   202 	AMS_TTDP_BRAKING,
       
   203 	AMS_TTDP_HELI_TAKEOFF_AIRPOPT,
       
   204 	AMS_TTDP_HELI_TO_TAKEOFF_AIRPOPT,
       
   205 	AMS_TTDP_HELI_LAND_AIRPOPT,
       
   206 	AMS_TTDP_HELI_TAKEOFF_HELIPORT,
       
   207 	AMS_TTDP_HELI_TO_TAKEOFF_HELIPORT,
       
   208 	AMS_TTDP_HELI_LAND_HELIPORT,
       
   209 };
       
   210 
       
   211 
       
   212 /**
       
   213  * Map OTTD aircraft movement states to TTDPatch style movement states
       
   214  * (VarAction 2 Variable 0xE2)
       
   215  */
       
   216 static byte MapAircraftMovementState(const Vehicle *v)
       
   217 {
       
   218 	const Station *st = GetStation(v->u.air.targetairport);
       
   219 	byte amdflag = GetAirportMovingData(st->airport_type, v->u.air.pos)->flag;
       
   220 
       
   221 	switch (v->u.air.state) {
       
   222 		case HANGAR:
       
   223 			/* The international airport is a special case as helicopters can land in
       
   224 			 * front of the hanger. Helicopters also change their air.state to
       
   225 			 * AMED_HELI_LOWER some time before actually descending. */
       
   226 
       
   227 			/* This condition only occurs for helicopters, during descent,
       
   228 			 * to a landing by the hanger of an international airport. */
       
   229 			if (amdflag & AMED_HELI_LOWER) return AMS_TTDP_HELI_LAND_AIRPOPT;
       
   230 
       
   231 			/* This condition only occurs for helicopters, before starting descent,
       
   232 			 * to a landing by the hanger of an international airport. */
       
   233 			if (amdflag & AMED_SLOWTURN) return AMS_TTDP_FLIGHT_TO_TOWER;
       
   234 
       
   235 			// The final two conditions apply to helicopters or aircraft.
       
   236 			/* Has reached hanger? */
       
   237 			if (amdflag & AMED_EXACTPOS) return AMS_TTDP_HANGER;
       
   238 
       
   239 			// Still moving towards hanger.
       
   240 			return AMS_TTDP_TO_HANGER;
       
   241 
       
   242 		case TERM1:
       
   243 			if (amdflag & AMED_EXACTPOS) return AMS_TTDP_TO_PAD1;
       
   244 			return AMS_TTDP_TO_JUNCTION;
       
   245 
       
   246 		case TERM2:
       
   247 			if (amdflag & AMED_EXACTPOS) return AMS_TTDP_TO_PAD2;
       
   248 			return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H;
       
   249 
       
   250 		case TERM3:
       
   251 		case TERM4:
       
   252 		case TERM5:
       
   253 		case TERM6:
       
   254 			/* TTDPatch only has 3 terminals, so treat these states the same */
       
   255 			if (amdflag & AMED_EXACTPOS) return AMS_TTDP_TO_PAD3;
       
   256 			return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H;
       
   257 
       
   258 		case HELIPAD1:
       
   259 		case HELIPAD2: // Will only occur for helicopters.
       
   260 			if (amdflag & AMED_HELI_LOWER) return AMS_TTDP_HELI_LAND_AIRPOPT; // Descending.
       
   261 			if (amdflag & AMED_SLOWTURN)   return AMS_TTDP_FLIGHT_TO_TOWER;   // Still hasn't started descent.
       
   262 			return AMS_TTDP_TO_JUNCTION; // On the ground.
       
   263 
       
   264 		case TAKEOFF: // Moving to takeoff position.
       
   265 			return AMS_TTDP_TO_OUTWAY;
       
   266 
       
   267 		case STARTTAKEOFF: // Accelerating down runway.
       
   268 			return AMS_TTDP_TAKEOFF;
       
   269 
       
   270 		case ENDTAKEOFF: // Ascent
       
   271 			return AMS_TTDP_CLIMBING;
       
   272 
       
   273 		case HELITAKEOFF: // Helicopter is moving to take off position.
       
   274 			switch (st->airport_type) {
       
   275 				case AT_SMALL:
       
   276 				case AT_LARGE:
       
   277 				case AT_METROPOLITAN:
       
   278 				case AT_INTERNATIONAL:
       
   279 					if (amdflag & AMED_HELI_RAISE) return AMS_TTDP_HELI_TAKEOFF_AIRPOPT;
       
   280 					return AMS_TTDP_TO_JUNCTION;
       
   281 
       
   282 				case AT_HELIPORT:
       
   283 				case AT_OILRIG:
       
   284 					return AMS_TTDP_HELI_TAKEOFF_HELIPORT;
       
   285 
       
   286 				default:
       
   287 					return AMS_TTDP_HELI_TAKEOFF_AIRPOPT;
       
   288 			}
       
   289 
       
   290 		case FLYING:
       
   291 			return AMS_TTDP_FLIGHT_TO_TOWER;
       
   292 
       
   293 		case LANDING: // Descent
       
   294 			return AMS_TTDP_FLIGHT_DESCENT;
       
   295 
       
   296 		case ENDLANDING: // On the runway braking
       
   297 			if (amdflag & AMED_BRAKE) return AMS_TTDP_BRAKING;
       
   298 			// Landed - moving off runway
       
   299 			return AMS_TTDP_TO_INWAY;
       
   300 
       
   301 		case HELILANDING:
       
   302 		case HELIENDLANDING: // Helicoptor is decending.
       
   303 			if (amdflag & AMED_HELI_LOWER) {
       
   304 				switch (st->airport_type) {
       
   305 					case AT_HELIPORT:
       
   306 					case AT_OILRIG:
       
   307 						return AMS_TTDP_HELI_LAND_HELIPORT;
       
   308 
       
   309 					default:
       
   310 						return AMS_TTDP_HELI_LAND_AIRPOPT;
       
   311 				}
       
   312 			}
       
   313 			return AMS_TTDP_FLIGHT_TO_TOWER;
       
   314 
       
   315 		default:
       
   316 			return AMS_TTDP_HANGER;
       
   317 	}
       
   318 }
       
   319 
       
   320 
       
   321 /* TTDP style aircraft movement action for GRF Action 2 Var 0xE6 */
       
   322 enum {
       
   323 	AMA_TTDP_IN_HANGER,
       
   324 	AMA_TTDP_ON_PAD1,
       
   325 	AMA_TTDP_ON_PAD2,
       
   326 	AMA_TTDP_ON_PAD3,
       
   327 	AMA_TTDP_HANGER_TO_PAD1,
       
   328 	AMA_TTDP_HANGER_TO_PAD2,
       
   329 	AMA_TTDP_HANGER_TO_PAD3,
       
   330 	AMA_TTDP_LANDING_TO_PAD1,
       
   331 	AMA_TTDP_LANDING_TO_PAD2,
       
   332 	AMA_TTDP_LANDING_TO_PAD3,
       
   333 	AMA_TTDP_PAD1_TO_HANGER,
       
   334 	AMA_TTDP_PAD2_TO_HANGER,
       
   335 	AMA_TTDP_PAD3_TO_HANGER,
       
   336 	AMA_TTDP_PAD1_TO_TAKEOFF,
       
   337 	AMA_TTDP_PAD2_TO_TAKEOFF,
       
   338 	AMA_TTDP_PAD3_TO_TAKEOFF,
       
   339 	AMA_TTDP_HANGER_TO_TAKOFF,
       
   340 	AMA_TTDP_LANDING_TO_HANGER,
       
   341 	AMA_TTDP_IN_FLIGHT,
       
   342 };
       
   343 
       
   344 
       
   345 /**
       
   346  * Map OTTD aircraft movement states to TTDPatch style movement actions
       
   347  * (VarAction 2 Variable 0xE6)
       
   348  * This is not fully supported yet but it's enough for Planeset.
       
   349  */
       
   350 static byte MapAircraftMovementAction(const Vehicle *v)
       
   351 {
       
   352 	switch (v->u.air.state) {
       
   353 		case HANGAR:
       
   354 			return (v->cur_speed > 0) ? AMA_TTDP_LANDING_TO_HANGER : AMA_TTDP_IN_HANGER;
       
   355 
       
   356 		case TERM1:
       
   357 		case HELIPAD1:
       
   358 			return (v->current_order.type == OT_LOADING) ? AMA_TTDP_ON_PAD1 : AMA_TTDP_LANDING_TO_PAD1;
       
   359 
       
   360 		case TERM2:
       
   361 		case HELIPAD2:
       
   362 			return (v->current_order.type == OT_LOADING) ? AMA_TTDP_ON_PAD2 : AMA_TTDP_LANDING_TO_PAD2;
       
   363 
       
   364 		case TERM3:
       
   365 		case TERM4:
       
   366 		case TERM5:
       
   367 		case TERM6:
       
   368 			return (v->current_order.type == OT_LOADING) ? AMA_TTDP_ON_PAD3 : AMA_TTDP_LANDING_TO_PAD3;
       
   369 
       
   370 		case TAKEOFF:      // Moving to takeoff position
       
   371 		case STARTTAKEOFF: // Accelerating down runway
       
   372 		case ENDTAKEOFF:   // Ascent
       
   373 		case HELITAKEOFF:
       
   374 			// TODO Need to find which terminal (or hanger) we've come from. How?
       
   375 			return AMA_TTDP_PAD1_TO_TAKEOFF;
       
   376 
       
   377 		case FLYING:
       
   378 			return AMA_TTDP_IN_FLIGHT;
       
   379 
       
   380 		case LANDING:    // Descent
       
   381 		case ENDLANDING: // On the runway braking
       
   382 		case HELILANDING:
       
   383 		case HELIENDLANDING:
       
   384 			// TODO Need to check terminal we're landing to. Is it known yet?
       
   385 			return (v->current_order.type == OT_GOTO_DEPOT) ?
       
   386 				AMA_TTDP_LANDING_TO_HANGER : AMA_TTDP_LANDING_TO_PAD1;
       
   387 
       
   388 		default:
       
   389 			return AMA_TTDP_IN_HANGER;
       
   390 	}
   173 }
   391 }
   174 
   392 
   175 
   393 
   176 /* Vehicle Resolver Functions */
   394 /* Vehicle Resolver Functions */
   177 static inline const Vehicle *GRV(const ResolverObject *object)
   395 static inline const Vehicle *GRV(const ResolverObject *object)
   361 			}
   579 			}
   362 			break;
   580 			break;
   363 
   581 
   364 		case VEH_Aircraft:
   582 		case VEH_Aircraft:
   365 			switch (variable - 0x80) {
   583 			switch (variable - 0x80) {
   366 				// case 0x62: XXX Need to convert from ottd to ttdp state
   584 				case 0x62: return MapAircraftMovementState(v);  // Current movement state
   367 				case 0x63: return v->u.air.targetairport;
   585 				case 0x63: return v->u.air.targetairport;       // Airport to which the action refers
   368 				// case 0x66: XXX
   586 				case 0x66: return MapAircraftMovementAction(v); // Current movement action
   369 			}
   587 			}
   370 			break;
   588 			break;
   371 	}
   589 	}
   372 
   590 
   373 	DEBUG(grf, 1)("Unhandled vehicle property 0x%X, type 0x%X", variable, v->type);
   591 	DEBUG(grf, 1)("Unhandled vehicle property 0x%X, type 0x%X", variable, v->type);