Loading

HORRIBLE

  1. Index: src/industry_cmd.cpp
  2. ===================================================================
  3. --- src/industry_cmd.cpp    (revision 27177)
  4. +++ src/industry_cmd.cpp    (working copy)
  5. @@ -1756,8 +1756,13 @@
  6.  
  7.             WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
  8.  
  9. +           /* While it's true Industry Tiles don't have any owner, the following
  10. +           DoCommand below is inadvertently setting bit 4 of m1 to 1. To preserve its
  11. +           original value, store it first before executing DoCommand, then restore it
  12. +           before MakeIndustry takes action. */
  13. +           Owner old_owner = GetTileOwner(cur_tile);
  14.             DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
  15. -
  16. +           SetTileOwner(cur_tile, old_owner);
  17.             MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
  18.  
  19.             if (_generating_world) {
  20. Index: src/industry_map.h
  21. ===================================================================
  22. --- src/industry_map.h  (revision 27177)
  23. +++ src/industry_map.h  (working copy)
  24. @@ -280,7 +280,7 @@
  25.  static inline void MakeIndustry(TileIndex t, IndustryID index, IndustryGfx gfx, uint8 random, WaterClass wc)
  26.  {
  27.     SetTileType(t, MP_INDUSTRY);
  28. -   _m[t].m1 = 0;
  29. +   _m[t].m1 &= 1<<4; // Make sure bit 4 is unaffected.
  30.     _m[t].m2 = index;
  31.     SetIndustryRandomBits(t, random); // m3
  32.     _m[t].m4 = 0;
  33. Index: src/saveload/afterload.cpp
  34. ===================================================================
  35. --- src/saveload/afterload.cpp  (revision 27177)
  36. +++ src/saveload/afterload.cpp  (working copy)
  37. @@ -430,7 +430,7 @@
  38.   */
  39.  static void FixOwnerOfRailTrack(TileIndex t)
  40.  {
  41. -   assert(!Company::IsValidID(GetTileOwner(t)) && (IsLevelCrossingTile(t) || IsPlainRailTile(t)));
  42. +   assert(!Company::IsValidID(GetTileOwnerOld(t)) && (IsLevelCrossingTile(t) || IsPlainRailTile(t)));
  43.  
  44.     /* remove leftover rail piece from crossing (from very old savegames) */
  45.     Train *v = NULL, *w;
  46. @@ -452,8 +452,8 @@
  47.         TileIndex tt = t + TileOffsByDiagDir(dd);
  48.         if (GetTileTrackStatus(t, TRANSPORT_RAIL, 0, dd) != 0 &&
  49.                 GetTileTrackStatus(tt, TRANSPORT_RAIL, 0, ReverseDiagDir(dd)) != 0 &&
  50. -               Company::IsValidID(GetTileOwner(tt))) {
  51. -           SetTileOwner(t, GetTileOwner(tt));
  52. +               Company::IsValidID(GetTileOwnerOld(tt))) {
  53. +           SetTileOwner(t, GetTileOwnerOld(tt));
  54.             return;
  55.         }
  56.     }
  57. @@ -614,7 +614,7 @@
  58.      * walk through the whole map.. */
  59.     if (IsSavegameVersionBefore(4, 3)) {
  60.         for (TileIndex t = 0; t < map_size; t++) {
  61. -           if (IsTileType(t, MP_WATER) && GetTileOwner(t) >= MAX_COMPANIES) {
  62. +           if (IsTileType(t, MP_WATER) && GetTileOwnerOld(t) >= MAX_COMPANIES) {
  63.                 SetTileOwner(t, OWNER_WATER);
  64.             }
  65.         }
  66. @@ -820,7 +820,7 @@
  67.                 default: break;
  68.  
  69.                 case MP_WATER:
  70. -                   if (GetWaterTileType(t) == WATER_TILE_LOCK && GetTileOwner(t) == OWNER_WATER) SetTileOwner(t, OWNER_NONE);
  71. +                   if (GetWaterTileType(t) == WATER_TILE_LOCK && GetTileOwnerOld(t) == OWNER_WATER) SetTileOwner(t, OWNER_NONE);
  72.                     break;
  73.  
  74.                 case MP_STATION: {
  75. @@ -949,7 +949,7 @@
  76.  
  77.                 case MP_ROAD:
  78.                     _m[t].m4 |= (_m[t].m2 << 4);
  79. -                   if ((GB(_m[t].m5, 4, 2) == ROAD_TILE_CROSSING ? (Owner)_m[t].m3 : GetTileOwner(t)) == OWNER_TOWN) {
  80. +                   if ((GB(_m[t].m5, 4, 2) == ROAD_TILE_CROSSING ? (Owner)_m[t].m3 : GetTileOwnerOld(t)) == OWNER_TOWN) {
  81.                         SetTownIndex(t, CalcClosestTownFromTile(t)->index);
  82.                     } else {
  83.                         SetTownIndex(t, 0);
  84. @@ -1104,7 +1104,7 @@
  85.                     if (!IsRoadStop(t)) break;
  86.  
  87.                     if (fix_roadtypes) SetRoadTypes(t, (RoadTypes)GB(_m[t].m3, 0, 3));
  88. -                   SB(_me[t].m7, 0, 5, HasBit(_me[t].m6, 2) ? OWNER_TOWN : GetTileOwner(t));
  89. +                   SB(_me[t].m7, 0, 5, HasBit(_me[t].m6, 2) ? OWNER_TOWN : GetTileOwnerOld(t));
  90.                     SB(_m[t].m3, 4, 4, _m[t].m1);
  91.                     _m[t].m4 = 0;
  92.                     break;
  93. @@ -1114,7 +1114,7 @@
  94.                     if (((old_bridge && IsBridge(t)) ? (TransportType)GB(_m[t].m5, 1, 2) : GetTunnelBridgeTransportType(t)) == TRANSPORT_ROAD) {
  95.                         if (fix_roadtypes) SetRoadTypes(t, (RoadTypes)GB(_m[t].m3, 0, 3));
  96.  
  97. -                       Owner o = GetTileOwner(t);
  98. +                       Owner o = GetTileOwnerOld(t);
  99.                         SB(_me[t].m7, 0, 5, o); // road owner
  100.                         SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); // tram owner
  101.                     }
  102. @@ -1143,7 +1143,7 @@
  103.                         if (GB(_m[t].m5, 3, 2) == TRANSPORT_RAIL) {
  104.                             MakeRailNormal(
  105.                                 t,
  106. -                               GetTileOwner(t),
  107. +                               GetTileOwnerOld(t),
  108.                                 axis == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X,
  109.                                 GetRailType(t)
  110.                             );
  111. @@ -1155,7 +1155,7 @@
  112.                                 axis == AXIS_X ? ROAD_Y : ROAD_X,
  113.                                 ROADTYPES_ROAD,
  114.                                 town,
  115. -                               GetTileOwner(t), OWNER_NONE
  116. +                               GetTileOwnerOld(t), OWNER_NONE
  117.                             );
  118.                         }
  119.                     } else {
  120. @@ -1165,10 +1165,10 @@
  121.                             if (!IsTileFlat(t)) {
  122.                                 MakeShore(t);
  123.                             } else {
  124. -                               if (GetTileOwner(t) == OWNER_WATER) {
  125. +                               if (GetTileOwnerOld(t) == OWNER_WATER) {
  126.                                     MakeSea(t);
  127.                                 } else {
  128. -                                   MakeCanal(t, GetTileOwner(t), Random());
  129. +                                   MakeCanal(t, GetTileOwnerOld(t), Random());
  130.                                 }
  131.                             }
  132.                         }
  133. @@ -1604,7 +1604,7 @@
  134.         for (TileIndex t = 0; t < map_size; t++) {
  135.             if (IsTileType(t, MP_WATER) &&
  136.                     GetWaterTileType(t) == WATER_TILE_CLEAR &&
  137. -                   GetTileOwner(t) == OWNER_WATER &&
  138. +                   GetTileOwnerOld(t) == OWNER_WATER &&
  139.                     TileHeight(t) != 0) {
  140.                 SetTileOwner(t, OWNER_NONE);
  141.             }
  142. @@ -1756,7 +1756,7 @@
  143.             if (IsTileType(t, MP_WATER)) {
  144.                 if (GetWaterClass(t) != WATER_CLASS_RIVER) {
  145.                     if (IsWater(t)) {
  146. -                       Owner o = GetTileOwner(t);
  147. +                       Owner o = GetTileOwnerOld(t);
  148.                         if (o == OWNER_WATER) {
  149.                             MakeSea(t);
  150.                         } else {
  151. @@ -1792,7 +1792,7 @@
  152.             }
  153.  
  154.             if (IsBuoyTile(t) || IsDriveThroughStopTile(t) || IsTileType(t, MP_WATER)) {
  155. -               Owner o = GetTileOwner(t);
  156. +               Owner o = GetTileOwnerOld(t);
  157.                 if (o < MAX_COMPANIES && !Company::IsValidID(o)) {
  158.                     Backup<CompanyByte> cur_company(_current_company, o, FILE_LINE);
  159.                     ChangeTileOwner(t, o, INVALID_OWNER);
  160. @@ -1811,10 +1811,10 @@
  161.                     if (o < MAX_COMPANIES && !Company::IsValidID(o)) SetRoadOwner(t, rt, OWNER_NONE);
  162.                 }
  163.                 if (IsLevelCrossing(t)) {
  164. -                   if (!Company::IsValidID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
  165. +                   if (!Company::IsValidID(GetTileOwnerOld(t))) FixOwnerOfRailTrack(t);
  166.                 }
  167.             } else if (IsPlainRailTile(t)) {
  168. -               if (!Company::IsValidID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
  169. +               if (!Company::IsValidID(GetTileOwnerOld(t))) FixOwnerOfRailTrack(t);
  170.             }
  171.         }
  172.  
  173. @@ -2447,7 +2447,7 @@
  174.     if (IsSavegameVersionBefore(148)) {
  175.         Object *o;
  176.         FOR_ALL_OBJECTS(o) {
  177. -           Owner owner = GetTileOwner(o->location.tile);
  178. +           Owner owner = GetTileOwnerOld(o->location.tile);
  179.             o->colour = (owner == OWNER_NONE) ? Random() & 0xF : Company::Get(owner)->livery->colour1;
  180.         }
  181.     }
  182. @@ -2821,7 +2821,7 @@
  183.     if (IsSavegameVersionBefore(172)) {
  184.         for (TileIndex t = 0; t < map_size; t++) {
  185.             if (!IsStandardRoadStopTile(t)) continue;
  186. -           Owner o = GetTileOwner(t);
  187. +           Owner o = GetTileOwnerOld(t);
  188.             SetRoadOwner(t, ROADTYPE_ROAD, o);
  189.             SetRoadOwner(t, ROADTYPE_TRAM, o);
  190.         }
  191. @@ -2985,6 +2985,18 @@
  192.     ResetSignalHandlers();
  193.  
  194.     AfterLoadLinkGraphs();
  195. +
  196. +   /* Convert m1 bit 4 value for all industry tiles */
  197. +   if (IsSavegameVersionBefore(195)) {
  198. +       for (TileIndex t = 0; t < map_size; t++) {
  199. +           /* It's not possible to know who was the original owner of a canal tile built under
  200. +           an Oil Rig tile from an old save game. To preserve savegame backward compatibility,
  201. +           don't do anything about them. Old and new values for a given industry tile can co-exist. */
  202. +           if (IsTileType(t, MP_INDUSTRY) && (GetWaterClass(t) != WATER_CLASS_CANAL)) {
  203. +               SB(_m[t].m1, 4, 1, 1);
  204. +           }
  205. +       }
  206. +   }
  207.     return true;
  208.  }
  209.  
  210. Index: src/saveload/saveload.cpp
  211. ===================================================================
  212. --- src/saveload/saveload.cpp   (revision 27177)
  213. +++ src/saveload/saveload.cpp   (working copy)
  214. @@ -262,8 +262,9 @@
  215.   *  192   26700
  216.   *  193   26802
  217.   *  194   26881
  218. + *  195   CanalOwner testings
  219.   */
  220. -extern const uint16 SAVEGAME_VERSION = 194; ///< Current savegame version of OpenTTD.
  221. +extern const uint16 SAVEGAME_VERSION = 195; ///< Current savegame version of OpenTTD.
  222.  
  223.  SavegameType _savegame_type; ///< type of savegame we are loading
  224.  
  225. Index: src/tile_map.h
  226. ===================================================================
  227. --- src/tile_map.h  (revision 27177)
  228. +++ src/tile_map.h  (working copy)
  229. @@ -168,14 +168,27 @@
  230.   * @pre IsValidTile(tile)
  231.   * @pre The type of the tile must not be MP_HOUSE and MP_INDUSTRY
  232.   */
  233. -static inline Owner GetTileOwner(TileIndex tile)
  234. +
  235. +static inline Owner GetTileOwnerOld(TileIndex tile)
  236.  {
  237.     assert(IsValidTile(tile));
  238.     assert(!IsTileType(tile, MP_HOUSE));
  239.     assert(!IsTileType(tile, MP_INDUSTRY));
  240. -
  241.     return (Owner)GB(_m[tile].m1, 0, 5);
  242.  }
  243. +static inline Owner GetTileOwner(TileIndex tile)
  244. +{
  245. +   assert(IsValidTile(tile));
  246. +   assert(!IsTileType(tile, MP_HOUSE));
  247. +   assert(!IsTileType(tile, MP_INDUSTRY));
  248. +   if ((IsTileType(tile, MP_STATION) || IsTileType(tile, MP_WATER) || IsTileType(tile, MP_OBJECT))) {
  249. +       Owner co = (Owner)GB(_m[tile].m1, 0, 4);
  250. +       return co == OWNER_TOWN ? OWNER_NONE : co;
  251. +      
  252. +   } else {
  253. +       return (Owner)GB(_m[tile].m1, 0, 5);
  254. +   }
  255. +}
  256.  
  257.  /**
  258.   * Sets the owner of a tile
  259. @@ -193,8 +206,11 @@
  260.     assert(IsValidTile(tile));
  261.     assert(!IsTileType(tile, MP_HOUSE));
  262.     assert(!IsTileType(tile, MP_INDUSTRY));
  263. -
  264. -   SB(_m[tile].m1, 0, 5, owner);
  265. +   if ((IsTileType(tile, MP_STATION) || IsTileType(tile, MP_WATER) || IsTileType(tile, MP_OBJECT)) && owner == OWNER_NONE) {
  266. +       SB(_m[tile].m1, 0, 4, OWNER_TOWN);
  267. +   } else {
  268. +       SB(_m[tile].m1, 0, 5, owner);
  269. +   }
  270.  }
  271.  
  272.  /**
  273. Index: src/water_cmd.cpp
  274. ===================================================================
  275. --- src/water_cmd.cpp   (revision 27177)
  276. +++ src/water_cmd.cpp   (working copy)
  277. @@ -419,7 +419,9 @@
  278.         if (IsTileType(tile, MP_WATER) && (!IsTileOwner(tile, OWNER_WATER) || wc == WATER_CLASS_SEA)) continue;
  279.  
  280.         bool water = IsWaterTile(tile);
  281. +
  282.         ret = DoCommand(tile, 0, 0, flags | DC_FORCE_CLEAR_TILE, CMD_LANDSCAPE_CLEAR);
  283. +
  284.         if (ret.Failed()) return ret;
  285.  
  286.         if (!water) cost.AddCost(ret);
  287. Index: src/water_map.h
  288. ===================================================================
  289. --- src/water_map.h (revision 27177)
  290. +++ src/water_map.h (working copy)
  291. @@ -176,6 +176,12 @@
  292.     return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
  293.  }
  294.  
  295. +static inline bool IsCanalOnRiver(TileIndex t)
  296. +{
  297. +   assert(HasTileWaterClass(t));
  298. +   return HasBit(_m[t].m1, 4);
  299. +}
  300. +
  301.  /**
  302.   * Is it a water tile with plain water?
  303.   * @param t Tile to query.
  304. @@ -346,8 +352,33 @@
  305.     return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
  306.  }
  307.  
  308. +/**
  309. + * Comment about getting owner of canal
  310. + */
  311. +static inline Owner GetCanalOwner(TileIndex t)
  312. +{
  313. +   assert(HasTileWaterGround(t));
  314.  
  315. +   if (GetWaterClass(t) != WATER_CLASS_CANAL) {
  316. +       return GetTileOwner(t);
  317. +       } else {
  318. +       Owner co = (Owner)(GB(_me[t].m6, 0, 2) && GB(_me[t].m6, 6, 2));
  319. +       /* Canals don't have OWNER_TOWN, and remapping OWNER_NONE
  320. +       * to OWNER_TOWN makes it use one bit less */
  321. +       return co == OWNER_TOWN ? OWNER_NONE : co;
  322. +   }
  323. +}
  324.  /**
  325. + * Comment about setting owner of canal
  326. + */
  327. +static inline void SetCanalOwner(TileIndex t, Owner co)
  328. +{
  329. +   if (co == OWNER_NONE) co = OWNER_TOWN;
  330. +   SB(_me[t].m6, 0, 2, GB(co, 0, 2));
  331. +   SB(_me[t].m6, 6, 2, GB(co, 2, 2));
  332. +}
  333. +
  334. +/**
  335.   * Helper function to make a coast tile.
  336.   * @param t The tile to change into water
  337.   */
  338. @@ -413,6 +444,7 @@
  339.  {
  340.     assert(o != OWNER_WATER);
  341.     MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
  342. +   SetCanalOwner(t, o);
  343.  }
  344.  
  345.  /**

Comments