Loading

Paste #p7dw4cffm

  1. Index: src/core/math_func.hpp
  2. ===================================================================
  3. --- src/core/math_func.hpp  (Revision 23438)
  4. +++ src/core/math_func.hpp  (Arbeitskopie)
  5. @@ -348,4 +348,18 @@
  6.  
  7.  uint32 IntSqrt(uint32 num);
  8.  
  9. +/**
  10. + * Computes the smallest nonnegative number r with a = q*b + r.
  11. + * (remainder of integer division with rounding towards -infinity)
  12. + * We can't rely on % giving sane results for negative a.
  13. + * @param a Numerator
  14. + * @param b Denominator
  15. + * @return Remainder
  16. + */
  17. +static FORCEINLINE uint Mod(int a, uint b)
  18. +{
  19. +   if (a >= 0) return ((uint)a)%b;
  20. +   return b-(((uint)(-a-1))%b)-1;
  21. +}
  22. +
  23.  #endif /* MATH_FUNC_HPP */
  24. Index: src/lang/english.txt
  25. ===================================================================
  26. --- src/lang/english.txt    (Revision 23438)
  27. +++ src/lang/english.txt    (Arbeitskopie)
  28. @@ -1298,11 +1298,11 @@
  29.  STR_CONFIG_SETTING_CYCLE_SIGNAL_ALL                             :All
  30.  
  31.  STR_CONFIG_SETTING_TOWN_LAYOUT                                  :{LTBLUE}Road layout for new towns: {ORANGE}{STRING1}
  32. -STR_CONFIG_SETTING_TOWN_LAYOUT_DEFAULT                          :original
  33. -STR_CONFIG_SETTING_TOWN_LAYOUT_BETTER_ROADS                     :better roads
  34. -STR_CONFIG_SETTING_TOWN_LAYOUT_2X2_GRID                         :2x2 grid
  35. -STR_CONFIG_SETTING_TOWN_LAYOUT_3X3_GRID                         :3x3 grid
  36. +STR_CONFIG_SETTING_TOWN_LAYOUT_DEFAULT                          :natural
  37. +STR_CONFIG_SETTING_TOWN_LAYOUT_GRID                             :grid
  38.  STR_CONFIG_SETTING_TOWN_LAYOUT_RANDOM                           :random
  39. +STR_CONFIG_SETTING_MIN_TOWN_SPACING                             :{LTBLUE}Smallest minimum distance beetween parallel roads: {ORANGE}{STRING1}
  40. +STR_CONFIG_SETTING_MAX_TOWN_SPACING                             :{LTBLUE}Largest minimum distance beetween parallel roads: {ORANGE}{STRING1}
  41.  STR_CONFIG_SETTING_ALLOW_TOWN_ROADS                             :{LTBLUE}Towns are allowed to build roads: {ORANGE}{STRING1}
  42.  STR_CONFIG_SETTING_ALLOW_TOWN_LEVEL_CROSSINGS                   :{LTBLUE}Towns are allowed to build level crossings: {ORANGE}{STRING1}
  43.  STR_CONFIG_SETTING_NOISE_LEVEL                                  :{LTBLUE}Allow town controlled noise level for airports: {ORANGE}{STRING}
  44. Index: src/table/settings.ini
  45. ===================================================================
  46. --- src/table/settings.ini  (Revision 23438)
  47. +++ src/table/settings.ini  (Arbeitskopie)
  48. @@ -475,7 +475,7 @@
  49.  type     = SLE_UINT8
  50.  from     = 59
  51.  guiflags = SGF_MULTISTRING
  52. -def      = TL_ORIGINAL
  53. +def      = TL_NATURAL
  54.  min      = TL_BEGIN
  55.  max      = NUM_TLS - 1
  56.  interval = 1
  57. @@ -483,6 +483,29 @@
  58.  strval   = STR_CONFIG_SETTING_TOWN_LAYOUT_DEFAULT
  59.  proc     = TownFoundingChanged
  60.  
  61. +[SDT_VAR]
  62. +base     = GameSettings
  63. +var      = economy.town_min_spacing
  64. +type     = SLE_UINT8
  65. +from     = 167
  66. +def      = 2
  67. +min      = 1
  68. +max      = MAX_TOWN_SPACING
  69. +interval = 1
  70. +str      = STR_CONFIG_SETTING_MIN_TOWN_SPACING
  71. +
  72. +[SDT_VAR]
  73. +base     = GameSettings
  74. +var      = economy.town_max_spacing
  75. +type     = SLE_UINT8
  76. +from     = 167
  77. +def      = 2
  78. +min      = 1
  79. +max      = MAX_TOWN_SPACING
  80. +interval = 1
  81. +str      = STR_CONFIG_SETTING_MAX_TOWN_SPACING
  82. +#proc = check max >= min
  83. +
  84.  [SDT_BOOL]
  85.  base     = GameSettings
  86.  var      = economy.allow_town_roads
  87. Index: src/genworld.cpp
  88. ===================================================================
  89. --- src/genworld.cpp    (Revision 23438)
  90. +++ src/genworld.cpp    (Arbeitskopie)
  91. @@ -129,7 +129,7 @@
  92.  
  93.             /* only generate towns, tree and industries in newgame mode. */
  94.             if (_game_mode != GM_EDITOR) {
  95. -               if (!GenerateTowns(_settings_game.economy.town_layout)) {
  96. +               if (!GenerateTowns(_settings_game.economy.town_layout, TS_RANDOM)) {
  97.                     _cur_company.Restore();
  98.                     HandleGeneratingWorldAbortion();
  99.                     return;
  100. Index: src/settings_gui.cpp
  101. ===================================================================
  102. --- src/settings_gui.cpp    (Revision 23438)
  103. +++ src/settings_gui.cpp    (Arbeitskopie)
  104. @@ -1459,6 +1459,8 @@
  105.     SettingEntry("economy.fund_roads"),
  106.     SettingEntry("economy.fund_buildings"),
  107.     SettingEntry("economy.town_layout"),
  108. +   SettingEntry("economy.town_min_spacing"),
  109. +   SettingEntry("economy.town_max_spacing"),
  110.     SettingEntry("economy.allow_town_roads"),
  111.     SettingEntry("economy.allow_town_level_crossings"),
  112.     SettingEntry("economy.found_town"),
  113. Index: src/settings_type.h
  114. ===================================================================
  115. --- src/settings_type.h (Revision 23438)
  116. +++ src/settings_type.h (Arbeitskopie)
  117. @@ -411,6 +411,8 @@
  118.     uint8  larger_towns;                     ///< the number of cities to build. These start off larger and grow twice as fast
  119.     uint8  initial_city_size;                ///< multiplier for the initial size of the cities compared to towns
  120.     TownLayoutByte town_layout;              ///< select town layout, @see TownLayout
  121. +   TownSpacing town_min_spacing;            ///< smallest minimal distance between parallel roads for towns
  122. +   TownSpacing town_max_spacing;            ///< largest minimal distance between parallel roads for towns
  123.     bool   allow_town_roads;                 ///< towns are allowed to build roads (always allowed when generating world / in SE)
  124.     TownFoundingByte found_town;             ///< town founding, @see TownFounding
  125.     bool   station_noise_level;              ///< build new airports when the town noise level is still within accepted limits
  126. Index: src/town_cmd.cpp
  127. ===================================================================
  128. --- src/town_cmd.cpp    (Revision 23438)
  129. +++ src/town_cmd.cpp    (Arbeitskopie)
  130. @@ -126,8 +126,16 @@
  131.  /**
  132.   * Assigns town layout. If Random, generates one based on TileHash.
  133.   */
  134. -void Town::InitializeLayout(TownLayout layout)
  135. +void Town::InitializeLayout(TownLayout layout, TownSpacing spacing)
  136.  {
  137. +   if (spacing == TS_RANDOM) {
  138. +       TownSpacing min_spacing = _settings_game.economy.town_min_spacing;
  139. +       TownSpacing max_spacing = max(_settings_game.economy.town_max_spacing, _settings_game.economy.town_min_spacing);
  140. +       this->spacing = (TileHash(TileX(this->xy), TileY(this->xy)) / (NUM_TLS - 1)) % (max_spacing - min_spacing + 1) + min_spacing;
  141. +   } else {
  142. +       this->spacing = spacing;
  143. +   }
  144. +
  145.     if (layout != TL_RANDOM) {
  146.         this->layout = layout;
  147.         return;
  148. @@ -180,7 +188,7 @@
  149.  };
  150.  
  151.  static bool BuildTownHouse(Town *t, TileIndex tile);
  152. -static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout);
  153. +static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout, TownSpacing spacing);
  154.  
  155.  static void TownDrawHouseLift(const TileInfo *ti)
  156.  {
  157. @@ -861,7 +869,7 @@
  158.     }
  159.  
  160.     Slope cur_slope = _settings_game.construction.build_on_slopes ? GetFoundationSlope(tile) : GetTileSlope(tile);
  161. -   bool ret = !IsNeighborRoadTile(tile, dir, t->layout == TL_ORIGINAL ? 1 : 2);
  162. +   bool ret = !IsNeighborRoadTile(tile, dir, t->spacing);
  163.     if (cur_slope == SLOPE_FLAT) return ret;
  164.  
  165.     /* If the tile is not a slope in the right direction, then
  166. @@ -928,15 +936,10 @@
  167.     switch (t->layout) {
  168.         default: NOT_REACHED();
  169.  
  170. -       case TL_2X2_GRID:
  171. -           if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y;
  172. -           if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X;
  173. +       case TL_GRID:
  174. +           if ((grid_pos.x % (t->spacing + 1)) == 0) rcmd |= ROAD_Y;
  175. +           if ((grid_pos.y % (t->spacing + 1)) == 0) rcmd |= ROAD_X;
  176.             break;
  177. -
  178. -       case TL_3X3_GRID:
  179. -           if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
  180. -           if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
  181. -           break;
  182.     }
  183.  
  184.     /* Optimise only X-junctions */
  185. @@ -1134,14 +1137,12 @@
  186.         switch (t1->layout) {
  187.             default: NOT_REACHED();
  188.  
  189. -           case TL_3X3_GRID:
  190. -           case TL_2X2_GRID:
  191. +           case TL_GRID:
  192.                 rcmd = GetTownRoadGridElement(t1, tile, target_dir);
  193.                 if (rcmd == ROAD_NONE) return;
  194.                 break;
  195.  
  196. -           case TL_BETTER_ROADS:
  197. -           case TL_ORIGINAL:
  198. +           case TL_NATURAL:
  199.                 if (!IsRoadAllowedHere(t1, tile, target_dir)) return;
  200.  
  201.                 DiagDirection source_dir = ReverseDiagDir(target_dir);
  202. @@ -1181,13 +1182,11 @@
  203.         switch (t1->layout) {
  204.             default: NOT_REACHED();
  205.  
  206. -           case TL_3X3_GRID:
  207. -           case TL_2X2_GRID:
  208. +           case TL_GRID:
  209.                 rcmd = GetTownRoadGridElement(t1, tile, target_dir);
  210.                 break;
  211.  
  212. -           case TL_BETTER_ROADS:
  213. -           case TL_ORIGINAL:
  214. +           case TL_NATURAL:
  215.                 rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
  216.                 break;
  217.         }
  218. @@ -1217,23 +1216,16 @@
  219.         if (!IsValidTile(house_tile)) return;
  220.  
  221.         if (_settings_game.economy.allow_town_roads || _generating_world) {
  222. +           if (t1->spacing > 2) GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
  223.             switch (t1->layout) {
  224.                 default: NOT_REACHED();
  225.  
  226. -               case TL_3X3_GRID: // Use 2x2 grid afterwards!
  227. -                   GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
  228. -                   /* FALL THROUGH */
  229. -
  230. -               case TL_2X2_GRID:
  231. +               case TL_GRID:
  232.                     rcmd = GetTownRoadGridElement(t1, house_tile, target_dir);
  233.                     allow_house = (rcmd == ROAD_NONE);
  234.                     break;
  235.  
  236. -               case TL_BETTER_ROADS: // Use original afterwards!
  237. -                   GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
  238. -                   /* FALL THROUGH */
  239. -
  240. -               case TL_ORIGINAL:
  241. +               case TL_NATURAL:
  242.                     /* Allow a house at the edge. 60% chance or
  243.                      * always ok if no road allowed. */
  244.                     rcmd = DiagDirToRoadBits(target_dir);
  245. @@ -1291,21 +1283,19 @@
  246.     assert(tile < MapSize());
  247.  
  248.     /* Number of times to search.
  249. -    * Better roads, 2X2 and 3X3 grid grow quite fast so we give
  250. +    * spaced out and grid towns grow quite fast so we give
  251.      * them a little handicap. */
  252.     switch (t->layout) {
  253. -       case TL_BETTER_ROADS:
  254. -           _grow_town_result = 10 + t->num_houses * 2 / 9;
  255. +       case TL_NATURAL:
  256. +           _grow_town_result = 10 + t->num_houses * ((t->spacing > 1)?2:4) / 9;
  257.             break;
  258.  
  259. -       case TL_3X3_GRID:
  260. -       case TL_2X2_GRID:
  261. +       case TL_GRID:
  262.             _grow_town_result = 10 + t->num_houses * 1 / 9;
  263.             break;
  264.  
  265.         default:
  266. -           _grow_town_result = 10 + t->num_houses * 4 / 9;
  267. -           break;
  268. +           NOT_REACHED();
  269.     }
  270.  
  271.     do {
  272. @@ -1485,7 +1475,7 @@
  273.   * @param layout the (road) layout of the town
  274.   * @param manual was the town placed manually?
  275.   */
  276. -static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize size, bool city, TownLayout layout, bool manual)
  277. +static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize size, bool city, TownLayout layout, TownSpacing spacing, bool manual)
  278.  {
  279.     t->xy = tile;
  280.     t->num_houses = 0;
  281. @@ -1532,7 +1522,7 @@
  282.     t->UpdateVirtCoord();
  283.     InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0);
  284.  
  285. -   t->InitializeLayout(layout);
  286. +   t->InitializeLayout(layout, spacing);
  287.  
  288.     t->larger_town = city;
  289.  
  290. @@ -1615,17 +1605,23 @@
  291.     TownLayout layout = Extract<TownLayout, 3, 3>(p1);
  292.     TownNameParams par(_settings_game.game_creation.town_name);
  293.     bool random = HasBit(p1, 6);
  294. +   TownSpacing spacing = Extract<TownLayout, 7, 3>(p1);
  295.     uint32 townnameparts = p2;
  296.  
  297.     if (size >= TSZ_END) return CMD_ERROR;
  298.     if (layout >= NUM_TLS) return CMD_ERROR;
  299. +   if (spacing > MAX_TOWN_SPACING) return CMD_ERROR;
  300.  
  301.     /* Some things are allowed only in the scenario editor */
  302.     if (_game_mode != GM_EDITOR) {
  303.         if (_settings_game.economy.found_town == TF_FORBIDDEN) return CMD_ERROR;
  304.         if (size == TSZ_LARGE) return CMD_ERROR;
  305.         if (random) return CMD_ERROR;
  306. -       if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT && layout != _settings_game.economy.town_layout) {
  307. +       if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT &&
  308. +               (layout != _settings_game.economy.town_layout ||
  309. +               spacing < _settings_game.economy.town_min_spacing ||
  310. +               spacing > _settings_game.economy.town_max_spacing
  311. +               )) {
  312.             return CMD_ERROR;
  313.         }
  314.     }
  315. @@ -1667,7 +1663,7 @@
  316.         UpdateNearestTownForRoadTiles(true);
  317.         Town *t;
  318.         if (random) {
  319. -           t = CreateRandomTown(20, townnameparts, size, city, layout);
  320. +           t = CreateRandomTown(20, townnameparts, size, city, layout, spacing);
  321.             if (t == NULL) {
  322.                 cost = CommandCost(STR_ERROR_NO_SPACE_FOR_TOWN);
  323.             } else {
  324. @@ -1675,7 +1671,7 @@
  325.             }
  326.         } else {
  327.             t = new Town(tile);
  328. -           DoCreateTown(t, tile, townnameparts, size, city, layout, true);
  329. +           DoCreateTown(t, tile, townnameparts, size, city, layout, spacing, true);
  330.         }
  331.         UpdateNearestTownForRoadTiles(false);
  332.         _generating_world = false;
  333. @@ -1712,11 +1708,10 @@
  334.   * @param layout which town layout algo is in effect
  335.   * @return the adjusted tile
  336.   */
  337. -static TileIndex AlignTileToGrid(TileIndex tile, TownLayout layout)
  338. +static TileIndex AlignTileToGrid(TileIndex tile, TownLayout layout, TownSpacing spacing)
  339.  {
  340.     switch (layout) {
  341. -       case TL_2X2_GRID: return TileXY(TileX(tile) - TileX(tile) % 3, TileY(tile) - TileY(tile) % 3);
  342. -       case TL_3X3_GRID: return TileXY(TileX(tile) & ~3, TileY(tile) & ~3);
  343. +       case TL_GRID: return TileXY(TileX(tile) - TileX(tile) % (spacing + 1), TileY(tile) - TileY(tile) % (spacing + 1));
  344.         default:          return tile;
  345.     }
  346.  }
  347. @@ -1730,11 +1725,10 @@
  348.   * @param layout which town layout algo is in effect
  349.   * @return true if the tile is in the correct location
  350.   */
  351. -static bool IsTileAlignedToGrid(TileIndex tile, TownLayout layout)
  352. +static bool IsTileAlignedToGrid(TileIndex tile, TownLayout layout, TownSpacing spacing)
  353.  {
  354.     switch (layout) {
  355. -       case TL_2X2_GRID: return TileX(tile) % 3 == 0 && TileY(tile) % 3 == 0;
  356. -       case TL_3X3_GRID: return TileX(tile) % 4 == 0 && TileY(tile) % 4 == 0;
  357. +       case TL_GRID: return TileX(tile) % (spacing + 1) == 0 && TileY(tile) % (spacing + 1) == 0;
  358.         default:          return true;
  359.     }
  360.  }
  361. @@ -1746,6 +1740,7 @@
  362.     TileIndex tile; ///< holds the tile that was found
  363.     uint max_dist;  ///< holds the distance that tile is from the water
  364.     TownLayout layout; ///< tells us what kind of town we're building
  365. +   TownSpacing spacing; ///<tells us what size of grid the town uses
  366.  };
  367.  
  368.  /**
  369. @@ -1771,7 +1766,7 @@
  370.  
  371.     if (IsTileType(tile, MP_CLEAR) &&
  372.             GetTileSlope(tile) == SLOPE_FLAT &&
  373. -           IsTileAlignedToGrid(tile, sp->layout) &&
  374. +           IsTileAlignedToGrid(tile, sp->layout, sp->spacing) &&
  375.             dist > sp->max_dist) {
  376.         sp->tile = tile;
  377.         sp->max_dist = dist;
  378. @@ -1803,9 +1798,9 @@
  379.   * @param layout the road layout to search for
  380.   * @return tile that was found
  381.   */
  382. -static TileIndex FindNearestGoodCoastalTownSpot(TileIndex tile, TownLayout layout)
  383. +static TileIndex FindNearestGoodCoastalTownSpot(TileIndex tile, TownLayout layout, TownSpacing spacing)
  384.  {
  385. -   SpotData sp = { INVALID_TILE, 0, layout };
  386. +   SpotData sp = { INVALID_TILE, 0, layout, spacing };
  387.  
  388.     TileIndex coast = tile;
  389.     if (CircularTileSearch(&coast, 40, FindNearestEmptyLand, NULL)) {
  390. @@ -1817,18 +1812,18 @@
  391.     return INVALID_TILE;
  392.  }
  393.  
  394. -static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout)
  395. +static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout, TownSpacing spacing)
  396.  {
  397.     if (!Town::CanAllocateItem()) return NULL;
  398.  
  399.     do {
  400.         /* Generate a tile index not too close from the edge */
  401. -       TileIndex tile = AlignTileToGrid(RandomTile(), layout);
  402. +       TileIndex tile = AlignTileToGrid(RandomTile(), layout, spacing);
  403.  
  404.         /* if we tried to place the town on water, slide it over onto
  405.          * the nearest likely-looking spot */
  406.         if (IsTileType(tile, MP_WATER)) {
  407. -           tile = FindNearestGoodCoastalTownSpot(tile, layout);
  408. +           tile = FindNearestGoodCoastalTownSpot(tile, layout, spacing);
  409.             if (tile == INVALID_TILE) continue;
  410.         }
  411.  
  412. @@ -1838,7 +1833,7 @@
  413.         /* Allocate a town struct */
  414.         Town *t = new Town(tile);
  415.  
  416. -       DoCreateTown(t, tile, townnameparts, size, city, layout, false);
  417. +       DoCreateTown(t, tile, townnameparts, size, city, layout, spacing, false);
  418.  
  419.         /* if the population is still 0 at the point, then the
  420.          * placement is so bad it couldn't grow at all */
  421. @@ -1864,7 +1859,7 @@
  422.   * @param layout which towns will be set to, when created
  423.   * @return true if towns have been successfully created
  424.   */
  425. -bool GenerateTowns(TownLayout layout)
  426. +bool GenerateTowns(TownLayout layout, TownSpacing spacing)
  427.  {
  428.     uint current_number = 0;
  429.     uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.number_towns : 0;
  430. @@ -1882,7 +1877,7 @@
  431.         /* Get a unique name for the town. */
  432.         if (!GenerateTownName(&townnameparts)) continue;
  433.         /* try 20 times to create a random-sized town for the first loop. */
  434. -       if (CreateRandomTown(20, townnameparts, TSZ_RANDOM, city, layout) != NULL) current_number++; // If creation was successful, raise a flag.
  435. +       if (CreateRandomTown(20, townnameparts, TSZ_RANDOM, city, layout, spacing) != NULL) current_number++; // If creation was successful, raise a flag.
  436.     } while (--total);
  437.  
  438.     if (current_number != 0) return true;
  439. @@ -1890,7 +1885,7 @@
  440.     /* If current_number is still zero at this point, it means that not a single town has been created.
  441.      * So give it a last try, but now more aggressive */
  442.     if (GenerateTownName(&townnameparts) &&
  443. -           CreateRandomTown(10000, townnameparts, TSZ_RANDOM, _settings_game.economy.larger_towns != 0, layout) != NULL) {
  444. +           CreateRandomTown(10000, townnameparts, TSZ_RANDOM, _settings_game.economy.larger_towns != 0, layout, spacing) != NULL) {
  445.         return true;
  446.     }
  447.  
  448. @@ -2052,14 +2047,10 @@
  449.     TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
  450.  
  451.     switch (t->layout) {
  452. -       case TL_2X2_GRID:
  453. -           if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false;
  454. +       case TL_GRID:
  455. +           if ((grid_pos.x % (t->spacing + 1)) == 0 || (grid_pos.y % (t->spacing + 1)) == 0) return false;
  456.             break;
  457.  
  458. -       case TL_3X3_GRID:
  459. -           if ((grid_pos.x % 4) == 0 || (grid_pos.y % 4) == 0) return false;
  460. -           break;
  461. -
  462.         default:
  463.             break;
  464.     }
  465. @@ -2084,17 +2075,12 @@
  466.     TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
  467.  
  468.     switch (t->layout) {
  469. -       case TL_2X2_GRID:
  470. -           grid_pos.x %= 3;
  471. -           grid_pos.y %= 3;
  472. -           if ((grid_pos.x != 2 && grid_pos.x != -1) ||
  473. -               (grid_pos.y != 2 && grid_pos.y != -1)) return false;
  474. +       case TL_GRID:
  475. +           grid_pos.x = Mod(grid_pos.x, t->spacing + 1);
  476. +           grid_pos.y = Mod(grid_pos.y, t->spacing + 1);
  477. +           if ((grid_pos.x <= 2) || (grid_pos.y < 2)) return false;
  478.             break;
  479.  
  480. -       case TL_3X3_GRID:
  481. -           if ((grid_pos.x & 3) < 2 || (grid_pos.y & 3) < 2) return false;
  482. -           break;
  483. -
  484.         default:
  485.             break;
  486.     }
  487. Index: src/town_gui.cpp
  488. ===================================================================
  489. --- src/town_gui.cpp    (Revision 23438)
  490. +++ src/town_gui.cpp    (Arbeitskopie)
  491. @@ -1110,7 +1110,7 @@
  492.  
  493.                 _generating_world = true;
  494.                 UpdateNearestTownForRoadTiles(true);
  495. -               if (!GenerateTowns(this->town_layout)) {
  496. +               if (!GenerateTowns(this->town_layout, TS_RANDOM)) { //TODO: allow selecting spacing
  497.                     ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
  498.                 }
  499.                 UpdateNearestTownForRoadTiles(false);
  500. Index: src/town.h
  501. ===================================================================
  502. --- src/town.h  (Revision 23438)
  503. +++ src/town.h  (Arbeitskopie)
  504. @@ -94,6 +94,7 @@
  505.  
  506.     bool larger_town;              ///< if this is a larger town and should grow more quickly
  507.     TownLayoutByte layout;         ///< town specific road layout
  508. +   TownSpacing spacing;           ///< minimum distance between parallel roads
  509.  
  510.     std::list<PersistentStorage *> psa_list;
  511.  
  512. @@ -112,7 +113,7 @@
  513.     /** Destroy the town. */
  514.     ~Town();
  515.  
  516. -   void InitializeLayout(TownLayout layout);
  517. +   void InitializeLayout(TownLayout layout, TownSpacing spacing);
  518.  
  519.     /**
  520.      * Calculate the max town noise.
  521. @@ -192,7 +193,7 @@
  522.  HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
  523.  void SetTownRatingTestMode(bool mode);
  524.  uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
  525. -bool GenerateTowns(TownLayout layout);
  526. +bool GenerateTowns(TownLayout layout, TownSpacing spacing);
  527.  const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect);
  528.  
  529.  
  530. Index: src/town_type.h
  531. ===================================================================
  532. --- src/town_type.h (Revision 23438)
  533. +++ src/town_type.h (Arbeitskopie)
  534. @@ -81,15 +81,17 @@
  535.   */
  536.  enum TownLayout {
  537.     TL_BEGIN = 0,
  538. -   TL_ORIGINAL = 0,     ///< Original algorithm (min. 1 distance between roads)
  539. -   TL_BETTER_ROADS,     ///< Extended original algorithm (min. 2 distance between roads)
  540. -   TL_2X2_GRID,         ///< Geometric 2x2 grid algorithm
  541. -   TL_3X3_GRID,         ///< Geometric 3x3 grid algorithm
  542. +   TL_NATURAL = 0,      ///< Original algorithm
  543. +   TL_GRID,             ///< Geometric grid algorithm
  544.  
  545.     TL_RANDOM,           ///< Random town layout
  546.  
  547.     NUM_TLS,             ///< Number of town layouts
  548.  };
  549. +typedef byte TownSpacing;
  550. +static const TownSpacing TS_RANDOM = 0;
  551. +static const TownSpacing MAX_TOWN_SPACING = 6;
  552. +
  553.  template <> struct EnumPropsT<TownLayout> : MakeEnumPropsT<TownLayout, byte, TL_BEGIN, NUM_TLS, NUM_TLS, 3> {};
  554.  /** It needs to be 8bits, because we save and load it as such */
  555.  typedef SimpleTinyEnumT<TownLayout, byte> TownLayoutByte; // typedefing-enumification of TownLayout
  556. Index: src/saveload/afterload.cpp
  557. ===================================================================
  558. --- src/saveload/afterload.cpp  (Revision 23438)
  559. +++ src/saveload/afterload.cpp  (Arbeitskopie)
  560. @@ -1960,7 +1960,7 @@
  561.         /* allow_town_roads is added, set it if town_layout wasn't TL_NO_ROADS */
  562.         if (_settings_game.economy.town_layout == 0) { // was TL_NO_ROADS
  563.             _settings_game.economy.allow_town_roads = false;
  564. -           _settings_game.economy.town_layout = TL_BETTER_ROADS;
  565. +           _settings_game.economy.town_layout = TL_NATURAL;
  566.         } else {
  567.             _settings_game.economy.allow_town_roads = true;
  568.             _settings_game.economy.town_layout = _settings_game.economy.town_layout - 1;
  569. @@ -2713,6 +2713,32 @@
  570.         }
  571.     }
  572.  
  573. +   if (IsSavegameVersionBefore(167)) {
  574. +       /* Town spacing parameter has been added */
  575. +       Town *t;
  576. +       FOR_ALL_TOWNS(t) {
  577. +           switch (t->layout) {
  578. +               default: NOT_REACHED();
  579. +               case 0: /* TL_ORIGINAL */
  580. +                   t->layout = TL_NATURAL;
  581. +                   t->spacing = 1;
  582. +                   break;
  583. +               case 1: /* TL_BETTER_ROADS */
  584. +                   t->layout = TL_NATURAL;
  585. +                   t->spacing = 2;
  586. +                   break;
  587. +               case 2: /* TL_2x2_GRID */
  588. +                   t->layout = TL_GRID;
  589. +                   t->spacing = 2;
  590. +                   break;
  591. +               case 3: /* TL_3x3_GRID */
  592. +                   t->layout = TL_GRID;
  593. +                   t->spacing = 3;
  594. +                   break;
  595. +           }
  596. +       }
  597. +   }
  598. +
  599.     /* Road stops is 'only' updating some caches */
  600.     AfterLoadRoadStops();
  601.     AfterLoadLabelMaps();
  602. Index: src/saveload/saveload.cpp
  603. ===================================================================
  604. --- src/saveload/saveload.cpp   (Revision 23438)
  605. +++ src/saveload/saveload.cpp   (Arbeitskopie)
  606. @@ -231,8 +231,9 @@
  607.   *  164   23290
  608.   *  165   23304
  609.   *  166   23415
  610. + *  167   town road speard
  611.   */
  612. -extern const uint16 SAVEGAME_VERSION = 166; ///< Current savegame version of OpenTTD.
  613. +extern const uint16 SAVEGAME_VERSION = 167; ///< Current savegame version of OpenTTD.
  614.  
  615.  SavegameType _savegame_type; ///< type of savegame we are loading

Comments