src/town.cpp
branchgamebalance
changeset 9899 cde52f745560
child 9900 750202458d8b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/town.cpp	Wed Mar 21 11:46:54 2007 +0000
@@ -0,0 +1,73 @@
+/* $Id */
+
+/** @file */
+
+#include "stdafx.h"
+#include "openttd.h"
+#include "road_map.h"
+#include "town.h"
+
+/* static */
+const Town *Town::GetRadiusGroupForTile(TileIndex tile, uint &group)
+{
+	const Town *t;
+	group = 0;
+	const Town *best_town;
+
+	DEBUG(eco, 7, "Obtaining highest town radius for tile 0x%x", tile);
+
+	/* We do have a tile that directly belongs to a town */
+	if (IsTileType(tile, MP_HOUSE) ||
+			(IsTileType(tile, MP_STREET) &&
+			(IsLevelCrossing(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile)) == OWNER_TOWN)) {
+		group = t->GetRadiusGroup(tile, true) + 1;
+		DEBUG(eco, 6, "Tile 0x%x belongs to town at 0x%x, level %d", tile, t->xy, group);
+		return t;
+	}
+
+	FOR_ALL_TOWNS(t) {
+		if (1 + t->GetRadiusGroup(tile, true) > group) {
+			group = t->GetRadiusGroup(tile, true) + 1;
+			DEBUG(eco, 7, "Higher group (%d) found for town at 0x%x", group, t->xy);
+			best_town = t;
+		}
+	}
+
+	return best_town;
+}
+
+uint Town::GetRadiusGroup(TileIndex tile, bool ignore_funding) const
+{
+	uint dist = DistanceSquare(tile, xy);
+	uint smallest = 0;
+
+	if (!ignore_funding && fund_buildings_months > 0 && dist < 25) return 4;
+
+	for (uint i = 0; i != lengthof(radius); i++) if (dist < radius[i]) smallest = i;
+
+	return smallest;
+}
+
+void Town::UpdateActivity()
+{
+	if (new_act_pass == 0 && act_pass == 0) {
+		DEBUG(eco, 7, "Town is not connected, bailing out");
+		return;
+	}
+
+	static const FixedT<int, 12> max_activity_change   (1, 400);
+	static const FixedT<int, 12> min_activity_change = -max_activity_change;
+	FixedT<int, 12>              activity_change       (1, 200);
+
+	activity_change *= act_pass;
+	activity_change /= max_pass;
+	activity_change -= max_activity_change;
+	DEBUG(eco, 6, "Raw EAL change for town at 0x%x is %f", xy, (double)activity_change);
+
+	if (activity_change > max_activity_change) activity_change = max_activity_change;
+	if (activity_change < min_activity_change) activity_change = min_activity_change;
+
+	this->SetActivity(this->GetActivity() + activity_change);
+
+	DEBUG(eco, 5, "Modifying EAL for town at 0x%x by %f to %f", xy, (double)activity_change, (double)this->GetActivity());
+}