src/town.cpp
author celestar
Wed, 13 Jun 2007 11:45:14 +0000
branchgamebalance
changeset 9911 0b8b245a2391
parent 9903 dc85aaa556ae
permissions -rw-r--r--
(svn r10135) [gamebalance] -Sync: r9700:9900 from trunk
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     1
/* $Id */
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     2
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     3
/** @file */
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     4
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     5
#include "stdafx.h"
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     6
#include "openttd.h"
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     7
#include "road_map.h"
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
     8
#include "town.h"
9900
750202458d8b (svn r9389) [gamebalance] -Fix(r9332): I should not use uninitialized variables and shall also find out why my g++ didn't warn me in the first place
celestar
parents: 9899
diff changeset
     9
#include "town_map.h"
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    10
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    11
/* static */
9902
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    12
int64 Town::GetTilePrice(TileIndex tile)
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    13
{
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    14
	uint rad;
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    15
	const Town *t = GetRadiusGroupForTile(tile, rad);
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    16
	FixedT<int64, 16> price = t->GetActivity();
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    17
	DEBUG(eco, 6, "Getting price for tile 0x%x at %f (authority at 0x%x)", tile, (double)price, t->xy);
9903
dc85aaa556ae (svn r9534) [gamebalance] -Codechange: Made the prices a member of the Economy and removed all global variables concerning prices (INCOMPLETE).
celestar
parents: 9902
diff changeset
    18
	price *=  _eco->GetPrice(CEconomy::PURCHASE_LAND) * rad * rad * 10;
9902
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    19
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    20
	return price;
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    21
}
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    22
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    23
/* static */
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    24
const Town *Town::GetRadiusGroupForTile(TileIndex tile, uint &group)
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    25
{
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    26
	const Town *t;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    27
	group = 0;
9900
750202458d8b (svn r9389) [gamebalance] -Fix(r9332): I should not use uninitialized variables and shall also find out why my g++ didn't warn me in the first place
celestar
parents: 9899
diff changeset
    28
	const Town *best_town = NULL;
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    29
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    30
	DEBUG(eco, 7, "Obtaining highest town radius for tile 0x%x", tile);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    31
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    32
	/* We do have a tile that directly belongs to a town */
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    33
	if (IsTileType(tile, MP_HOUSE) ||
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    34
			(IsTileType(tile, MP_STREET) &&
9911
0b8b245a2391 (svn r10135) [gamebalance] -Sync: r9700:9900 from trunk
celestar
parents: 9903
diff changeset
    35
			(GetTileOwner(tile)) == OWNER_TOWN)) { /** @todo check this for level crossings, trams, etc */
9900
750202458d8b (svn r9389) [gamebalance] -Fix(r9332): I should not use uninitialized variables and shall also find out why my g++ didn't warn me in the first place
celestar
parents: 9899
diff changeset
    36
		t = GetTownByTile(tile);
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    37
		group = t->GetRadiusGroup(tile, true) + 1;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    38
		DEBUG(eco, 6, "Tile 0x%x belongs to town at 0x%x, level %d", tile, t->xy, group);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    39
		return t;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    40
	}
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    41
9902
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    42
	uint best_dist = UINT_MAX;
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    43
	FOR_ALL_TOWNS(t) {
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    44
		if (1 + t->GetRadiusGroup(tile, true) > group) {
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    45
			group = t->GetRadiusGroup(tile, true) + 1;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    46
			DEBUG(eco, 7, "Higher group (%d) found for town at 0x%x", group, t->xy);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    47
			best_town = t;
9902
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    48
			if (best_dist > DistanceSquare(tile, t->xy)) best_dist = DistanceSquare(tile, t->xy);
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    49
		} else if (1 + t->GetRadiusGroup(tile, true) == group) {
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    50
			DEBUG(eco, 7, "Same group found town at 0x%x is closer", t->xy);
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    51
			if (best_dist > DistanceSquare(tile, t->xy)) {
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    52
				best_dist = DistanceSquare(tile, t->xy);
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    53
				best_town = t;
ed8f92929297 (svn r9414) [gamebalance] -Feature: The cost of purchasing land off a town depends on the wealth level of a town.
celestar
parents: 9900
diff changeset
    54
			}
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    55
		}
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    56
	}
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    57
9900
750202458d8b (svn r9389) [gamebalance] -Fix(r9332): I should not use uninitialized variables and shall also find out why my g++ didn't warn me in the first place
celestar
parents: 9899
diff changeset
    58
	assert(best_town != NULL);
750202458d8b (svn r9389) [gamebalance] -Fix(r9332): I should not use uninitialized variables and shall also find out why my g++ didn't warn me in the first place
celestar
parents: 9899
diff changeset
    59
9899
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    60
	return best_town;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    61
}
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    62
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    63
uint Town::GetRadiusGroup(TileIndex tile, bool ignore_funding) const
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    64
{
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    65
	uint dist = DistanceSquare(tile, xy);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    66
	uint smallest = 0;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    67
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    68
	if (!ignore_funding && fund_buildings_months > 0 && dist < 25) return 4;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    69
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    70
	for (uint i = 0; i != lengthof(radius); i++) if (dist < radius[i]) smallest = i;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    71
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    72
	return smallest;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    73
}
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    74
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    75
void Town::UpdateActivity()
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    76
{
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    77
	if (new_act_pass == 0 && act_pass == 0) {
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    78
		DEBUG(eco, 7, "Town is not connected, bailing out");
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    79
		return;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    80
	}
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    81
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    82
	static const FixedT<int, 12> max_activity_change   (1, 400);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    83
	static const FixedT<int, 12> min_activity_change = -max_activity_change;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    84
	FixedT<int, 12>              activity_change       (1, 200);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    85
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    86
	activity_change *= act_pass;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    87
	activity_change /= max_pass;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    88
	activity_change -= max_activity_change;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    89
	DEBUG(eco, 6, "Raw EAL change for town at 0x%x is %f", xy, (double)activity_change);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    90
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    91
	if (activity_change > max_activity_change) activity_change = max_activity_change;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    92
	if (activity_change < min_activity_change) activity_change = min_activity_change;
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    93
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    94
	this->SetActivity(this->GetActivity() + activity_change);
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    95
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    96
	DEBUG(eco, 5, "Modifying EAL for town at 0x%x by %f to %f", xy, (double)activity_change, (double)this->GetActivity());
cde52f745560 (svn r9386) [gamebalance] -Feature: Selling and buying land costs now take proximity to town centers into account. Move some code around while I'm at it. Also selling land gives as much money as buying land costs.
celestar
parents:
diff changeset
    97
}