town_cmd.c
changeset 1260 632f63689178
parent 1252 fa73bce0f2e0
child 1261 914ecc20a2df
equal deleted inserted replaced
1259:8d8515e3da29 1260:632f63689178
    16 #include "economy.h"
    16 #include "economy.h"
    17 #include "gui.h"
    17 #include "gui.h"
    18 #include "network.h"
    18 #include "network.h"
    19 
    19 
    20 enum {
    20 enum {
       
    21 	/* Max towns: 64000 (8 * 8000) */
       
    22 	TOWN_POOL_BLOCK_SIZE_BITS = 3,       /* In bits, so (1 << 3) == 8 */
       
    23 	TOWN_POOL_MAX_BLOCKS      = 8000,
       
    24 };
       
    25 
       
    26 /**
       
    27  * Called if a new block is added to the town-pool
       
    28  */
       
    29 static void TownPoolNewBlock(uint start_item)
       
    30 {
       
    31 	Town *t;
       
    32 
       
    33 	FOR_ALL_TOWNS_FROM(t, start_item)
       
    34 			t->index = start_item++;
       
    35 }
       
    36 
       
    37 /* Initialize the town-pool */
       
    38 MemoryPool _town_pool = { "Towns", TOWN_POOL_MAX_BLOCKS, TOWN_POOL_BLOCK_SIZE_BITS, sizeof(Town), &TownPoolNewBlock, 0, 0, NULL };
       
    39 
       
    40 
       
    41 enum {
    21 	TOWN_HAS_CHURCH     = 0x02,
    42 	TOWN_HAS_CHURCH     = 0x02,
    22 	TOWN_HAS_STADIUM    = 0x04
    43 	TOWN_HAS_STADIUM    = 0x04
    23 };
    44 };
    24 
    45 
    25 // Local
    46 // Local
   411 
   432 
   412 	if (_game_mode == GM_EDITOR)
   433 	if (_game_mode == GM_EDITOR)
   413 		return;
   434 		return;
   414 
   435 
   415 	i = _cur_town_ctr;
   436 	i = _cur_town_ctr;
       
   437 	if (++_cur_town_ctr >= GetTownPoolSize())
       
   438 		_cur_town_ctr = 0;
       
   439 
   416 	t = GetTown(i);
   440 	t = GetTown(i);
   417 	if (++i == _towns_size) i = 0;
       
   418 	_cur_town_ctr = i;
       
   419 
   441 
   420 	if (t->xy != 0)
   442 	if (t->xy != 0)
   421 		TownTickHandler(t);
   443 		TownTickHandler(t);
   422 
       
   423 }
   444 }
   424 
   445 
   425 static byte GetTownRoadMask(TileIndex tile)
   446 static byte GetTownRoadMask(TileIndex tile)
   426 {
   447 {
   427 	byte b = GetRoadBitsByTile(tile);
   448 	byte b = GetRoadBitsByTile(tile);
   952 static Town *AllocateTown(void)
   973 static Town *AllocateTown(void)
   953 {
   974 {
   954 	Town *t;
   975 	Town *t;
   955 	FOR_ALL_TOWNS(t) {
   976 	FOR_ALL_TOWNS(t) {
   956 		if (t->xy == 0) {
   977 		if (t->xy == 0) {
   957 			if (t->index > _total_towns) _total_towns = t->index;
   978 			uint index = t->index;
       
   979 
       
   980 			if (t->index > _total_towns)
       
   981 				_total_towns = t->index;
       
   982 
       
   983 			memset(t, 0, sizeof(Town));
       
   984 			t->index = index;
       
   985 
   958 			return t;
   986 			return t;
   959 		}
   987 		}
   960 	}
   988 	}
       
   989 
       
   990 	/* Check if we can add a block to the pool */
       
   991 	if (AddBlockToPool(&_town_pool))
       
   992 		return AllocateTown();
       
   993 
   961 	return NULL;
   994 	return NULL;
   962 }
   995 }
   963 
   996 
   964 int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   997 int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2)
   965 {
   998 {
  1833 }
  1866 }
  1834 
  1867 
  1835 void InitializeTowns(void)
  1868 void InitializeTowns(void)
  1836 {
  1869 {
  1837 	Subsidy *s;
  1870 	Subsidy *s;
  1838 	Town *t;
  1871 
  1839 	int i;
  1872 	/* Clean the town pool and create 1 block in it */
  1840 
  1873 	CleanPool(&_town_pool);
  1841 	memset(_towns, 0, sizeof(_towns[0]) * _towns_size);
  1874 	AddBlockToPool(&_town_pool);
  1842 
       
  1843 	i = 0;
       
  1844 	FOR_ALL_TOWNS(t)
       
  1845 		t->index = i++;
       
  1846 
  1875 
  1847 	memset(_subsidies, 0, sizeof(_subsidies));
  1876 	memset(_subsidies, 0, sizeof(_subsidies));
  1848 	for (s=_subsidies; s != endof(_subsidies); s++)
  1877 	for (s=_subsidies; s != endof(_subsidies); s++)
  1849 		s->cargo_type = 0xFF;
  1878 		s->cargo_type = 0xFF;
  1850 
  1879 
  1851 	_cur_town_ctr = 0;
  1880 	_cur_town_ctr = 0;
       
  1881 	_total_towns = 0;
  1852 	_town_sort_dirty = true;
  1882 	_town_sort_dirty = true;
  1853 	_total_towns = 0;
       
  1854 }
  1883 }
  1855 
  1884 
  1856 const TileTypeProcs _tile_type_town_procs = {
  1885 const TileTypeProcs _tile_type_town_procs = {
  1857 	DrawTile_Town,						/* draw_tile_proc */
  1886 	DrawTile_Town,						/* draw_tile_proc */
  1858 	GetSlopeZ_Town,						/* get_slope_z_proc */
  1887 	GetSlopeZ_Town,						/* get_slope_z_proc */
  1939 }
  1968 }
  1940 
  1969 
  1941 static void Load_TOWN(void)
  1970 static void Load_TOWN(void)
  1942 {
  1971 {
  1943 	int index;
  1972 	int index;
       
  1973 
  1944 	while ((index = SlIterateArray()) != -1) {
  1974 	while ((index = SlIterateArray()) != -1) {
  1945 		Town *t = GetTown(index);
  1975 		Town *t;
  1946 
  1976 
       
  1977 		if (!AddBlockIfNeeded(&_town_pool, index))
       
  1978 			error("Towns: failed loading savegame: too many towns");
       
  1979 
       
  1980 		t = GetTown(index);
  1947 		SlObject(t, _town_desc);
  1981 		SlObject(t, _town_desc);
  1948 		if (index > _total_towns) _total_towns = index;
  1982 
  1949 	}
  1983 		if ((uint)index > _total_towns)
       
  1984 			_total_towns = index;
       
  1985 	}
       
  1986 
       
  1987 	/* This is to ensure all pointers are within the limits of
       
  1988 	 *  the size of the TownPool */
       
  1989 	if (_cur_town_ctr >= GetTownPoolSize())
       
  1990 		_cur_town_ctr = 0;
  1950 }
  1991 }
  1951 
  1992 
  1952 void AfterLoadTown(void)
  1993 void AfterLoadTown(void)
  1953 {
  1994 {
  1954 	Town *t;
  1995 	Town *t;