town_cmd.c
changeset 3674 d6c991bdcbe1
parent 3657 eb28d97d3d18
child 3877 0a298def0202
equal deleted inserted replaced
3673:00ba3772ca20 3674:d6c991bdcbe1
   885 {
   885 {
   886 	t->max_pass = t->population >> 3;
   886 	t->max_pass = t->population >> 3;
   887 	t->max_mail = t->population >> 4;
   887 	t->max_mail = t->population >> 4;
   888 }
   888 }
   889 
   889 
   890 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts)
   890 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, uint size_mode)
   891 {
   891 {
   892 	int x, i;
   892 	int x, i;
   893 
   893 
   894 	// clear the town struct
   894 	// clear the town struct
   895 	i = t->index;
   895 	i = t->index;
   933 	t->townnameparts = townnameparts;
   933 	t->townnameparts = townnameparts;
   934 
   934 
   935 	UpdateTownVirtCoord(t);
   935 	UpdateTownVirtCoord(t);
   936 	_town_sort_dirty = true;
   936 	_town_sort_dirty = true;
   937 
   937 
   938 	x = (Random() & 0xF) + 8;
   938 	if (size_mode == 0) {
   939 	if (_game_mode == GM_EDITOR)
   939 		x = (Random() & 0xF) + 8;
   940 		x = _new_town_size * 16 + 3;
   940 	} else {
       
   941 		x = (size_mode - 1) * 16 + 3;
       
   942 	}
   941 
   943 
   942 	t->num_houses += x;
   944 	t->num_houses += x;
   943 	UpdateTownRadius(t);
   945 	UpdateTownRadius(t);
   944 
   946 
   945 	i = x * 4;
   947 	i = x * 4;
   978 
   980 
   979 /** Create a new town.
   981 /** Create a new town.
   980  * This obviously only works in the scenario editor. Function not removed
   982  * This obviously only works in the scenario editor. Function not removed
   981  * as it might be possible in the future to fund your own town :)
   983  * as it might be possible in the future to fund your own town :)
   982  * @param tile coordinates where town is built
   984  * @param tile coordinates where town is built
   983  * @param p1 unused
   985  * @param p1 size of the town (1 = small, 2 = medium, 3 = large)
   984  * @param p2 unused
   986  * @param p2 unused
   985  */
   987  */
   986 int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   988 int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   987 {
   989 {
   988 	Town *t;
   990 	Town *t;
  1015 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
  1017 	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
  1016 
  1018 
  1017 	// Create the town
  1019 	// Create the town
  1018 	if (flags & DC_EXEC) {
  1020 	if (flags & DC_EXEC) {
  1019 		_generating_world = true;
  1021 		_generating_world = true;
  1020 		DoCreateTown(t, tile, townnameparts);
  1022 		DoCreateTown(t, tile, townnameparts, p1);
  1021 		_generating_world = false;
  1023 		_generating_world = false;
  1022 	}
  1024 	}
  1023 	return 0;
  1025 	return 0;
  1024 }
  1026 }
  1025 
  1027 
  1026 Town *CreateRandomTown(uint attempts)
  1028 Town *CreateRandomTown(uint attempts, uint size_mode)
  1027 {
  1029 {
  1028 	TileIndex tile;
  1030 	TileIndex tile;
  1029 	Town *t;
  1031 	Town *t;
  1030 	uint32 townnameparts;
  1032 	uint32 townnameparts;
  1031 
  1033 
  1045 
  1047 
  1046 		// Allocate a town struct
  1048 		// Allocate a town struct
  1047 		t = AllocateTown();
  1049 		t = AllocateTown();
  1048 		if (t == NULL) break;
  1050 		if (t == NULL) break;
  1049 
  1051 
  1050 		DoCreateTown(t, tile, townnameparts);
  1052 		DoCreateTown(t, tile, townnameparts, size_mode);
  1051 		return t;
  1053 		return t;
  1052 	} while (--attempts);
  1054 	} while (--attempts);
  1053 	return NULL;
  1055 	return NULL;
  1054 }
  1056 }
  1055 
  1057 
  1059 {
  1061 {
  1060 	uint num = 0;
  1062 	uint num = 0;
  1061 	uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
  1063 	uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
  1062 
  1064 
  1063 	do {
  1065 	do {
  1064 		if (CreateRandomTown(20) != NULL) 	//try 20 times for the first loop
  1066 		if (CreateRandomTown(20, 0) != NULL) 	//try 20 times to create a random-sized town for the first loop.
  1065 			num++;
  1067 			num++;
  1066 	} while (--n);
  1068 	} while (--n);
  1067 
  1069 
  1068 	// give it a last try, but now more aggressive
  1070 	// give it a last try, but now more aggressive
  1069 	if (num == 0 && CreateRandomTown(10000) == NULL) {
  1071 	if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
  1070 		Town *t;
  1072 		Town *t;
  1071 		FOR_ALL_TOWNS(t) { if (IsValidTown(t)) {num = 1; break;}}
  1073 		FOR_ALL_TOWNS(t) { if (IsValidTown(t)) {num = 1; break;}}
  1072 
  1074 
  1073 		//XXX can we handle that more gracefully?
  1075 		//XXX can we handle that more gracefully?
  1074 		if (num == 0) error("Could not generate any town");
  1076 		if (num == 0 && _game_mode != GM_EDITOR) error("Could not generate any town");
  1075 		return false;
  1077 		return false;
  1076 	}
  1078 	}
  1077 
  1079 
  1078 	return true;
  1080 	return true;
  1079 }
  1081 }