Changeset - r7806:a2c6f8da57ac
[Not reviewed]
master
0 2 0
glx - 17 years ago 2007-10-29 23:29:06
glx@openttd.org
(svn r11356) -Fix (r11305): funding industries in MP game was causing desync
2 files changed with 3 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -1525,145 +1525,145 @@ static void DoCreateNewIndustry(Industry
 

	
 
/** Helper function for Build/Fund an industry
 
 * @param tile tile where industry is built
 
 * @param type of industry to build
 
 * @param flags of operations to conduct
 
 * @param indspec pointer to industry specifications
 
 * @param itspec_index the index of the itsepc to build/fund
 
 * @return the pointer of the newly created industry, or NULL if it failed
 
 */
 
static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, uint32 flags, const IndustrySpec *indspec, uint itspec_index)
 
{
 
	const IndustryTileTable *it = indspec->table[itspec_index];
 
	bool custom_shape_check = false;
 

	
 
	if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
 

	
 
	if (HASBIT(GetIndustrySpec(type)->callback_flags, CBM_IND_LOCATION)) {
 
		if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index)) return NULL;
 
	} else {
 
		if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
 
	}
 

	
 
	if (!custom_shape_check && _patches.land_generator == LG_TERRAGENESIS && _generating_world && !CheckIfCanLevelIndustryPlatform(tile, 0, it, type)) return NULL;
 
	if (!CheckIfTooCloseToIndustry(tile, type)) return NULL;
 

	
 
	const Town *t = CheckMultipleIndustryInTown(tile, type);
 
	if (t == NULL) return NULL;
 

	
 
	if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
 
	if (!CheckSuitableIndustryPos(tile)) return NULL;
 

	
 
	Industry *i = new Industry(tile);
 
	if (i == NULL) return NULL;
 
	AutoPtrT<Industry> i_auto_delete = i;
 

	
 
	if (flags & DC_EXEC) {
 
		if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
 
		DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE);
 
		i_auto_delete.Detach();
 
	}
 

	
 
	return i;
 
}
 

	
 
/** Build/Fund an industry
 
 * @param tile tile where industry is built
 
 * @param flags of operations to conduct
 
 * @param p1 industry type see build_industry.h and see industry.h
 
 * @param p2 unused
 
 * @param p2 first layout to try
 
 * @return index of the newly create industry, or CMD_ERROR if it failed
 
 */
 
CommandCost CmdBuildIndustry(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	const IndustrySpec *indspec;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
 

	
 
	indspec = GetIndustrySpec(p1);
 

	
 
	/* Check if the to-be built/founded industry is available for this climate. */
 
	if (!indspec->enabled) {
 
		return CMD_ERROR;
 
	}
 

	
 
	/* If the patch for raw-material industries is not on, you cannot build raw-material industries.
 
	 * Raw material industries are industries that do not accept cargo (at least for now) */
 
	if (_game_mode != GM_EDITOR && _patches.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
 
		return CMD_ERROR;
 
	}
 

	
 
	if (_game_mode != GM_EDITOR && _patches.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
 
		if (flags & DC_EXEC) {
 
			/* Prospecting has a chance to fail, however we cannot guarantee that something can
 
			 * be built on the map, so the chance gets lower when the map is fuller, but there
 
			 * is nothing we can really do about that. */
 
			if (Random() <= indspec->prospecting_chance) {
 
				for (int i = 0; i < 5000; i++) {
 
					const Industry *ind = CreateNewIndustryHelper(RandomTile(), p1, flags, indspec, RandomRange(indspec->num_table));
 
					if (ind != NULL) {
 
						SetDParam(0, indspec->name);
 
						if (indspec->new_industry_text > STR_LAST_STRINGID) {
 
							SetDParam(1, STR_TOWN);
 
							SetDParam(2, ind->town->index);
 
						} else {
 
							SetDParam(1, ind->town->index);
 
						}
 
						AddNewsItem(indspec->new_industry_text,
 
								NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_TILE, NT_OPENCLOSE, 0), ind->xy, 0);
 
						break;
 
					}
 
				}
 
			}
 
		}
 
	} else {
 
		int count = indspec->num_table;
 
		const IndustryTileTable * const *itt = indspec->table;
 
		int num = RandomRange(count);
 
		int num = clamp(p2, 0, count - 1);
 

	
 
		_error_message = STR_0239_SITE_UNSUITABLE;
 
		do {
 
			if (--count < 0) return CMD_ERROR;
 
			if (--num < 0) num = indspec->num_table - 1;
 
		} while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, p1));
 

	
 
		if (CreateNewIndustryHelper(tile, p1, flags, indspec, num) == NULL) return CMD_ERROR;
 
	}
 

	
 
	return CommandCost(indspec->GetConstructionCost());
 
}
 

	
 

	
 
Industry *CreateNewIndustry(TileIndex tile, IndustryType type)
 
{
 
	const IndustrySpec *indspec = GetIndustrySpec(type);
 

	
 
	return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table));
 
}
 

	
 
enum {
 
	NB_NUMOFINDUSTRY = 11,
 
	NB_DIFFICULTY_LEVEL = 5,
 
};
 

	
 
static const byte _numof_industry_table[NB_DIFFICULTY_LEVEL][NB_NUMOFINDUSTRY] = {
 
	/* difficulty settings for number of industries */
 
	{0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   //none
 
	{0, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1},   //very low
 
	{0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   //low
 
	{0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   //normal
 
	{0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   //high
 
};
 

	
 
/** This function is the one who really do the creation work
 
 * of random industries during game creation
 
 * @param type IndustryType of the desired industry
 
 * @param amount of industries that need to be built */
 
static void PlaceInitialIndustry(IndustryType type, int amount)
 
{
 
	/* We need to bypass the amount given in parameter if it exceeds the maximum dimension of the
 
	 * _numof_industry_table.  newgrf can specify a big amount */
 
	int num = (amount > NB_NUMOFINDUSTRY) ? amount : _numof_industry_table[_opt.diff.number_industries][amount];
 
	const IndustrySpec *ind_spc = GetIndustrySpec(type);
 

	
 
	/* These are always placed next to the coastline, so we scale by the perimeter instead. */
 
	num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
src/industry_gui.cpp
Show inline comments
 
@@ -270,97 +270,97 @@ static void BuildDynamicIndustryWndProc(
 
							ShowErrorMessage(STR_0286_MUST_BUILD_TOWN_FIRST, STR_CAN_T_GENERATE_INDUSTRIES, 0, 0);
 
						} else {
 
							extern void GenerateIndustries();
 
							_generating_world = true;
 
							GenerateIndustries();
 
							_generating_world = false;
 
						}
 
					} else if (_game_mode != GM_EDITOR && _patches.raw_industry_construction == 2 && GetIndustrySpec(WP(w, fnd_d).select)->IsRawIndustry()) {
 
						DoCommandP(0, WP(w, fnd_d).select, 0, NULL, CMD_BUILD_INDUSTRY | CMD_MSG(STR_4830_CAN_T_CONSTRUCT_THIS_INDUSTRY));
 
						HandleButtonClick(w, DYNA_INDU_FUND_WIDGET);
 
					} else {
 
						HandlePlacePushButton(w, DYNA_INDU_FUND_WIDGET, SPR_CURSOR_INDUSTRY, 1, NULL);
 
					}
 
				} break;
 
			}
 
			break;
 

	
 
		case WE_RESIZE: {
 
			/* Adjust the number of items in the matrix depending of the rezise */
 
			w->vscroll.cap  += e->we.sizing.diff.y / (int)w->resize.step_height;
 
			w->widget[DYNA_INDU_MATRIX_WIDGET].data = (w->vscroll.cap << 8) + 1;
 
		} break;
 

	
 
		case WE_PLACE_OBJ: {
 
			bool success = true;
 
			/* We do not need to protect ourselves against "Random Many Industries" in this mode */
 
			const IndustrySpec *indsp = GetIndustrySpec(WP(w, fnd_d).select);
 

	
 
			if (_game_mode == GM_EDITOR) {
 
				/* Show error if no town exists at all */
 
				if (GetNumTowns() == 0) {
 
					SetDParam(0, indsp->name);
 
					ShowErrorMessage(STR_0286_MUST_BUILD_TOWN_FIRST, STR_0285_CAN_T_BUILD_HERE, e->we.place.pt.x, e->we.place.pt.y);
 
					return;
 
				}
 

	
 
				_current_player = OWNER_NONE;
 
				_generating_world = true;
 
				_ignore_restrictions = true;
 
				success = TryBuildIndustry(e->we.place.tile, WP(w, fnd_d).select);
 
				if (!success) {
 
					SetDParam(0, indsp->name);
 
					ShowErrorMessage(_error_message, STR_0285_CAN_T_BUILD_HERE, e->we.place.pt.x, e->we.place.pt.y);
 
				}
 

	
 
				_ignore_restrictions = false;
 
				_generating_world = false;
 
			} else {
 
				success = DoCommandP(e->we.place.tile, WP(w, fnd_d).select, 0, NULL, CMD_BUILD_INDUSTRY | CMD_MSG(STR_4830_CAN_T_CONSTRUCT_THIS_INDUSTRY));
 
				success = DoCommandP(e->we.place.tile, WP(w, fnd_d).select, InteractiveRandomRange(indsp->num_table), NULL, CMD_BUILD_INDUSTRY | CMD_MSG(STR_4830_CAN_T_CONSTRUCT_THIS_INDUSTRY));
 
			}
 

	
 
			/* If an industry has been built, just reset the cursor and the system */
 
			if (success) ResetObjectToPlace();
 
		} break;
 

	
 
		case WE_TICK:
 
			if (!WP(w, fnd_d).timer_enabled) break;
 
			if (--WP(w, fnd_d).callback_timer == 0) {
 
				/* We have just passed another day.
 
				 * See if we need to update availability of currently selected industry */
 
				WP(w, fnd_d).callback_timer = DAY_TICKS;  //restart counter
 

	
 
				const IndustrySpec *indsp = GetIndustrySpec(WP(w, fnd_d).select);
 

	
 
				if (indsp->enabled) {
 
					bool call_back_result = CheckIfCallBackAllowsAvailability(WP(w, fnd_d).select, IACT_USERCREATION);
 

	
 
					/* Only if result does match the previous state would it require a redraw. */
 
					if (call_back_result != _fund_gui.enabled[WP(w, fnd_d).index]) {
 
						_fund_gui.enabled[WP(w, fnd_d).index] = call_back_result;
 
						SetWindowDirty(w);
 
					}
 
				}
 
			}
 
			break;
 

	
 
		case WE_TIMEOUT:
 
		case WE_ABORT_PLACE_OBJ:
 
			RaiseWindowButtons(w);
 
			break;
 
	}
 
}
 

	
 
static const Widget _build_dynamic_industry_widgets[] = {
 
{   WWT_CLOSEBOX,    RESIZE_NONE,    7,     0,    10,     0,    13, STR_00C5,                       STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_RIGHT,    7,    11,   169,     0,    13, STR_0314_FUND_NEW_INDUSTRY,     STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{     WWT_MATRIX,      RESIZE_RB,    7,     0,   157,    14,   118, 0x801,                          STR_INDUSTRY_SELECTION_HINT},
 
{  WWT_SCROLLBAR,     RESIZE_LRB,    7,   158,   169,    14,   118, 0x0,                            STR_0190_SCROLL_BAR_SCROLLS_LIST},
 
{      WWT_PANEL,     RESIZE_RTB,    7,     0,   169,   119,   199, 0x0,                            STR_NULL},
 
{    WWT_TEXTBTN,     RESIZE_RTB,    7,     0,   157,   200,   211, STR_FUND_NEW_INDUSTRY,          STR_NULL},
 
{  WWT_RESIZEBOX,    RESIZE_LRTB,    7,   158,   169,   200,   211, 0x0,                            STR_RESIZE_BUTTON},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _build_industry_dynamic_desc = {
 
	WDP_AUTO, WDP_AUTO, 170, 212, 170, 212,
 
	WC_BUILD_INDUSTRY, WC_NONE,
0 comments (0 inline, 0 general)