Index: grfspecial.c
===================================================================
--- grfspecial.c	(revision 757)
+++ grfspecial.c	(working copy)
@@ -79,22 +79,18 @@
 
 #define foreach_engine for (i = 0; i < numinfo; i++)
 
-static void dewagonize(int condition, int engine, int numinfo)
+static void dewagonize(int condition, int engine)
 {
 	EngineInfo *ei = &_engine_info[engine];
 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
 	int i;
 
 	if (condition) {
-		foreach_engine {
-			ei[i].unk2 &= ~0x80;
-			rvi[i].flags &= ~2;
-		}
+		ei->unk2 &= ~0x80;
+		rvi->flags &= ~2;
 	} else {
-		foreach_engine {
-			ei[i].unk2 |= 0x80;
-			rvi[i].flags |= 2;
-		}
+		ei->unk2 |= 0x80;
+		rvi->flags |= 2;
 	}
 }
 
@@ -107,16 +103,18 @@
 	int ret = 0;
 
 	switch (prop) {
-		case 0x05: {	/* Track type */
-			uint8 tracktype = grf_load_byte(&buf);
+		case 0x05:
+		{	/* Track type */
+			foreach_engine {
+				uint8 tracktype = grf_load_byte(&buf);
 
-			foreach_engine {
 				ei[i].railtype_climates &= 0xf;
 				ei[i].railtype_climates |= tracktype << 4;
 			}
 			break;
 		}
-		case 0x08: {	/* AI passenger service */
+		case 0x08:
+		{	/* AI passenger service */
 			/* TODO */
 			foreach_engine {
 				grf_load_byte(&buf);
@@ -124,37 +122,41 @@
 			ret = 1;
 			break;
 		}
-		case 0x09: {	/* Speed */
-			uint16 speed = grf_load_word(&buf);
+		case 0x09:
+		{	/* Speed */
+			foreach_engine {
+				uint16 speed = grf_load_word(&buf);
 
-			foreach_engine {
 				rvi[i].max_speed = speed;
+				dewagonize(speed, engine + i);
 			}
-			dewagonize(speed, engine + i, numinfo);
 			break;
 		}
-		case 0x0b: {	/* Power */
-			uint16 power = grf_load_word(&buf);
+		case 0x0b:
+		{	/* Power */
+			foreach_engine {
+				uint16 power = grf_load_word(&buf);
 
-			foreach_engine {
 				rvi[i].power = power;
+				dewagonize(power, engine + i);
 			}
-			dewagonize(power, engine + i, numinfo);
 			break;
 		}
-		case 0x0d: {	/* Running cost factor */
-			uint8 runcostfact = grf_load_byte(&buf);
+		case 0x0d:
+		{	/* Running cost factor */
+			foreach_engine {
+				uint8 runcostfact = grf_load_byte(&buf);
 
-			foreach_engine {
 				rvi[i].running_cost_base = runcostfact;
+				dewagonize(runcostfact, engine + i);
 			}
-			dewagonize(runcostfact, engine + i, numinfo);
 			break;
 		}
-		case 0x0e: {	/* Running cost base */
-			uint32 base = grf_load_dword(&buf);
+		case 0x0e:
+		{	/* Running cost base */
+			foreach_engine {
+				uint32 base = grf_load_dword(&buf);
 
-			foreach_engine {
 				switch (base) {
 				case 0x4c30:
 					rvi[i].engclass = 0;
@@ -166,24 +168,26 @@
 					rvi[i].engclass = 2;
 					break;
 				}
+				dewagonize(base, engine + i);
 			}
-			dewagonize(base, engine + i, numinfo);
 			break;
 		}
-		case 0x12: {	/* Sprite ID */
-			uint8 spriteid = grf_load_byte(&buf);
+		case 0x12:
+		{	/* Sprite ID */
+			foreach_engine {
+				uint8 spriteid = grf_load_byte(&buf);
 
-			foreach_engine {
 				if (spriteid == 0xfd && rvi[i].image_index != 0xfd)
 					_engine_original_sprites[engine + i] = rvi[i].image_index;
 				rvi[i].image_index = spriteid;
 			}
 			break;
 		}
-		case 0x13:{	/* Dual-headed */
-			uint8 dual = grf_load_byte(&buf);
+		case 0x13:
+		{	/* Dual-headed */
+			foreach_engine {
+				uint8 dual = grf_load_byte(&buf);
 
-			foreach_engine {
 				if (dual) {
 					rvi[i].flags |= 1;
 				} else {
@@ -192,40 +196,44 @@
 			}
 			break;
 		}
-		case 0x14: {	/* Cargo capacity */
-			uint8 capacity = grf_load_byte(&buf);
+		case 0x14:
+		{	/* Cargo capacity */
+			foreach_engine {
+				uint8 capacity = grf_load_byte(&buf);
 
-			foreach_engine {
 				rvi[i].capacity = capacity;
 			}
 			break;
 		}
-		case 0x15: {	/* Cargo type */
-			uint8 ctype = grf_load_byte(&buf);
+		case 0x15:
+		{	/* Cargo type */
+			foreach_engine {
+				uint8 ctype = grf_load_byte(&buf);
 
-			foreach_engine {
 				rvi[i].cargo_type = ctype;
 			}
 			break;
 		}
-		case 0x16: {	/* Weight */
-			uint8 weight = grf_load_byte(&buf);
+		case 0x16:
+		{	/* Weight */
+			foreach_engine {
+				uint8 weight = grf_load_byte(&buf);
 
-			foreach_engine {
 				rvi[i].weight = weight;
 			}
 			break;
 		}
-		case 0x17: {	/* Cost factor */
-			uint8 cfactor = grf_load_byte(&buf);
+		case 0x17:
+		{	/* Cost factor */
+			foreach_engine {
+				uint8 cfactor = grf_load_byte(&buf);
 
-			foreach_engine {
 				rvi[i].base_cost = cfactor;
 			}
 			break;
 		}
-		case 0x18: {	
-			/* AI rank */
+		case 0x18:
+		{	/* AI rank */
 			/* TODO: _railveh_score should be merged to _rail_vehicle_info. */
 			foreach_engine {
 				grf_load_byte(&buf);
@@ -233,32 +241,52 @@
 			ret = 1;
 			break;
 		}
-		case 0x19: {	
-			/* Engine traction type */
+		case 0x19:
+		{	/* Engine traction type */
 			/* TODO: What do the individual numbers mean?
 			 * XXX: And in what base are they, in fact? --pasky */
-			uint8 traction = grf_load_byte(&buf);
-			int engclass;
+			foreach_engine {
+				uint8 traction = grf_load_byte(&buf);
+				int engclass;
 
-			if (traction <= 0x07)
-				engclass = 0;
-			else if (traction <= 0x27)
-				engclass = 1;
-			else if (traction <= 0x31)
-				engclass = 2;
-			else
-				break;
+				if (traction <= 0x07)
+					engclass = 0;
+				else if (traction <= 0x27)
+					engclass = 1;
+				else if (traction <= 0x31)
+					engclass = 2;
+				else
+					break;
 
-			foreach_engine {
 				rvi[i].engclass = engclass;
 			}
 			break;
 		}
-		case 0x21: {	/* Shorter tenders */
+		/* TODO */
+		/* Fall-through for unimplemented four bytes long properties. */
+		case 0x1d:	/* Refit cargo */
+			foreach_engine {
+				grf_load_word(&buf);
+			}
+		/* Fall-through for unimplemented two bytes long properties. */
+		case 0x1b:	/* Powered wagons power bonus */
+			foreach_engine {
+				grf_load_byte(&buf);
+			}
+		/* Fall-through for unimplemented one byte long properties. */
+		case 0x1a:	/* Sort order */
+		case 0x1c:	/* Refit cost */
+		case 0x1e:	/* Callback */
+		case 0x1f:	/* Tractive effort */
+		case 0x21:	/* Shorter tenders */
+		case 0x22:	/* Visual */
+		case 0x23:	/* Powered wagons weight bonus */
 			/* TODO */
+			foreach_engine {
+				grf_load_byte(&buf);
+			}
 			ret = 1;
 			break;
-		}
 		default:
 			ret = 1;
 			break;
@@ -278,11 +306,12 @@
 	switch (prop) {
 		case 0x08:
 		{	/* Sprite ID */
-			uint8 spriteid = grf_load_byte(&buf);
+			foreach_engine {
+				uint8 spriteid = grf_load_byte(&buf);
 
-			if (spriteid == 0xff)
-				spriteid = 0xfd; // ships have different custom id
-			foreach_engine {
+				if (spriteid == 0xff)
+					spriteid = 0xfd; // ships have different custom id in the GRF file
+
 				// This is currently not used but there's no reason
 				// in not having it here for the future.
 				if (spriteid == 0xfd
@@ -296,80 +325,80 @@
 		}
 		case 0x09:
 		{	/* Refittable */
-			uint8 refittable = grf_load_byte(&buf);
+			foreach_engine {
+				uint8 refittable = grf_load_byte(&buf);
 
-			foreach_engine {
 				svi[i].refittable = refittable;
 			}
 			break;
 		}
 		case 0x0a:
 		{	/* Cost factor */
-			uint8 cost_factor = grf_load_byte(&buf);
+			foreach_engine {
+				uint8 cost_factor = grf_load_byte(&buf);
 
-			foreach_engine {
 				svi[i].base_cost = cost_factor; // ?? is it base_cost?
 			}
 			break;
 		}
 		case 0x0b:
 		{	/* Speed */
-			uint8 speed = grf_load_byte(&buf);
+			foreach_engine {
+				uint8 speed = grf_load_byte(&buf);
 
-			foreach_engine {
 				svi[i].max_speed = speed; // ?? units
 			}
 			break;
 		}
 		case 0x0c:
 		{	/* Cargo type */
-			uint8 cargo = grf_load_byte(&buf);
 
-			// XXX: Need to consult this with patchman yet.
+			foreach_engine {
+				uint8 cargo = grf_load_byte(&buf);
+
+				// XXX: Need to consult this with patchman yet.
 #if 0
-			// Documentation claims this is already the
-			// per-landscape cargo type id, but newships.grf
-			// assume otherwise.
-			cargo = local_cargo_id_ctype[cargo];
+				// Documentation claims this is already the
+				// per-landscape cargo type id, but newships.grf
+				// assume otherwise.
+				cargo = local_cargo_id_ctype[cargo];
 #endif
-
-			foreach_engine {
 				svi[i].cargo_type = cargo;
 			}
 			break;
 		}
 		case 0x0d:
 		{	/* Cargo capacity */
-			uint16 capacity = grf_load_word(&buf);
+			foreach_engine {
+				uint16 capacity = grf_load_word(&buf);
 
-			foreach_engine {
 				svi[i].capacity = capacity;
 			}
 			break;
 		}
 		case 0x0f:
 		{	/* Running cost factor */
-			uint8 runcost = grf_load_byte(&buf);
+			foreach_engine {
+				uint8 runcost = grf_load_byte(&buf);
 
-			foreach_engine {
 				svi[i].running_cost = runcost;
 			}
 			break;
 		}
 		case 0x10:
 		{	/* SFX */
-			uint8 sfx = grf_load_byte(&buf);
+			foreach_engine {
+				uint8 sfx = grf_load_byte(&buf);
 
-			foreach_engine {
 				svi[i].sfx = sfx;
 			}
 			break;
 		}
 		case 0x11:
 		{	/* Cargos available for refitting */
-			uint32 refit_mask = grf_load_dword(&buf);
+			foreach_engine {
+				uint32 refit_mask = grf_load_dword(&buf);
 
-			foreach_engine {
 				_engine_refit_masks[SHIP_ENGINES_INDEX + engine + i] = refit_mask;
 			}
 			break;
@@ -439,17 +468,18 @@
 			switch (prop) {
 			case 0x00: {	
 				/* Introduction date */
-				uint16 date = grf_load_word(&buf);
-				
-				foreach_engine
+				foreach_engine {
+					uint16 date = grf_load_word(&buf);
+					
 					ei[i].base_intro = date;
+				}
 				break;
 			}
 			case 0x02: {	
 				/* Decay speed */
-				uint8 decay = grf_load_byte(&buf);
-								
 				foreach_engine {
+					uint8 decay = grf_load_byte(&buf);
+									
 					ei[i].unk2 &= 0x80;
 					ei[i].unk2 |= decay & 0x7f;
 				}
@@ -457,25 +487,27 @@
 			}
 			case 0x03: {
 				/* Vehicle life */
-				uint8 life = grf_load_byte(&buf);
+				foreach_engine {
+					uint8 life = grf_load_byte(&buf);
 
-				foreach_engine
 					ei[i].lifelength = life;
+				}
 				break;
 			}
 			case 0x04: {	
 				/* Model life */
-				uint8 life = grf_load_byte(&buf);
+				foreach_engine {
+					uint8 life = grf_load_byte(&buf);
 
-				foreach_engine
 					ei[i].base_life = life;
+				}
 				break;
 			}
 			case 0x06: {
 				/* Climates available */
-				uint8 climates = grf_load_byte(&buf);
+				foreach_engine {
+					uint8 climates = grf_load_byte(&buf);
 
-				foreach_engine {
 					ei[i].railtype_climates &= 0xf0;
 					ei[i].railtype_climates |= climates;
 				}
@@ -488,6 +520,9 @@
 				 * credit ;-). --pasky */
 				/* TODO: This needs to be supported by
 				 * LoadUnloadVehicle() first. */
+				foreach_engine {
+					grf_load_byte(&buf);
+				}
 				goto ignoring;
 			}
 			default:
