src/newgrf_house.cpp
author richk
Tue, 17 Jun 2008 13:22:13 +0000
branchNewGRF_ports
changeset 10994 cd9968b6f96b
parent 10724 68a692eacf22
permissions -rw-r--r--
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     1
/* $Id$ */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     2
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
     3
/** @file newgrf_house.cpp Implementation of NewGRF houses. */
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     4
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     5
#include "stdafx.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     6
#include "openttd.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     7
#include "variables.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
     8
#include "debug.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     9
#include "viewport_func.h"
6669
487ac09b95c4 (svn r9371) -Feature: Add support for variable snow lines in the arctic climate, supplied
maedhros
parents: 6658
diff changeset
    10
#include "landscape.h"
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    11
#include "town.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    12
#include "town_map.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    13
#include "sprite.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    14
#include "newgrf.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    15
#include "newgrf_house.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    16
#include "newgrf_spritegroup.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    17
#include "newgrf_callbacks.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    18
#include "newgrf_town.h"
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    19
#include "newgrf_sound.h"
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
    20
#include "newgrf_commons.h"
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
    21
#include "transparency.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    22
#include "functions.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    23
#include "player_func.h"
10274
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10184
diff changeset
    24
#include "animated_tile_func.h"
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10184
diff changeset
    25
#include "date_func.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    26
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    27
#include "table/strings.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    28
#include "table/sprites.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    29
#include "table/town_land.h"
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    30
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    31
static BuildingCounts    _building_counts;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    32
static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    33
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
    34
HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    35
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    36
/**
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    37
 * Check and update town and house values.
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    38
 *
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    39
 * Checked are the HouseIDs. Updated are the
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    40
 * town population the number of houses per
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    41
 * town, the town radius and the max passengers
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    42
 * of the town.
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    43
 */
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    44
void UpdateHousesAndTowns()
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    45
{
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    46
	Town *town;
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6743
diff changeset
    47
	InitializeBuildingCounts();
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6743
diff changeset
    48
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    49
	/* Reset town population and num_houses */
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    50
	FOR_ALL_TOWNS(town) {
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    51
		town->population = 0;
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    52
		town->num_houses = 0;
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    53
	}
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    54
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    55
	for (TileIndex t = 0; t < MapSize(); t++) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    56
		HouseID house_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    57
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    58
		if (!IsTileType(t, MP_HOUSE)) continue;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    59
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    60
		house_id = GetHouseType(t);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    61
		if (!GetHouseSpecs(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    62
			/* The specs for this type of house are not available any more, so
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    63
			 * replace it with the substitute original house type. */
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6743
diff changeset
    64
			house_id = _house_mngr.GetSubstituteID(house_id);
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6743
diff changeset
    65
			SetHouseType(t, house_id);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    66
		}
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    67
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    68
		town = GetTownByTile(t);
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    69
		IncreaseBuildingCount(town, house_id);
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    70
		if (IsHouseCompleted(t)) town->population += GetHouseSpecs(house_id)->population;
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    71
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    72
		/* Increase the number of houses for every house tile which
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    73
		 * has a size bit set. Multi tile buildings have got only
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    74
		 * one tile with such a bit set, so there is no problem. */
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    75
		if (GetHouseSpecs(GetHouseType(t))->building_flags & BUILDING_HAS_1_TILE) {
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    76
			town->num_houses++;
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    77
		}
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    78
	}
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    79
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    80
	/* Update the population and num_house dependant values */
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    81
	FOR_ALL_TOWNS(town) {
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    82
		UpdateTownRadius(town);
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    83
		UpdateTownMaxPass(town);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    84
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    85
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    86
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    87
HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    88
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    89
	/* Start from 1 because 0 means that no class has been assigned. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    90
	for (int i = 1; i != lengthof(_class_mapping); i++) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    91
		HouseClassMapping *map = &_class_mapping[i];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    92
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    93
		if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    94
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    95
		if (map->class_id == 0 && map->grfid == 0) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    96
			map->class_id = grf_class_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    97
			map->grfid    = grfid;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    98
			return (HouseClassID)i;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
    99
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   100
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   101
	return HOUSE_NO_CLASS;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   102
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   103
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   104
void InitializeBuildingCounts()
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   105
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   106
	memset(&_building_counts, 0, sizeof(_building_counts));
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   107
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   108
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   109
/**
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   110
 * IncreaseBuildingCount()
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   111
 * Increase the count of a building when it has been added by a town.
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   112
 * @param t The town that the building is being built in
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   113
 * @param house_id The id of the house being added
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   114
 */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   115
void IncreaseBuildingCount(Town *t, HouseID house_id)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   116
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   117
	HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   118
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   119
	if (!_loaded_newgrf_features.has_newhouses) return;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   120
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   121
	/* If there are 255 buildings of this type in this town, there are also
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   122
	 * at least that many houses of the same class in the town, and
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   123
	 * therefore on the map as well. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   124
	if (t->building_counts.id_count[house_id] == 255) return;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   125
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   126
	t->building_counts.id_count[house_id]++;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   127
	if (_building_counts.id_count[house_id] < 255) _building_counts.id_count[house_id]++;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   128
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   129
	/* Similarly, if there are 255 houses of this class in this town, there
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   130
	 * must be at least that number on the map too. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   131
	if (class_id == HOUSE_NO_CLASS || t->building_counts.class_count[class_id] == 255) return;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   132
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   133
	t->building_counts.class_count[class_id]++;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   134
	if (_building_counts.class_count[class_id] < 255) _building_counts.class_count[class_id]++;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   135
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   136
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   137
/**
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   138
 * DecreaseBuildingCount()
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   139
 * Decrease the number of a building when it is deleted.
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   140
 * @param t The town that the building was built in
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   141
 * @param house_id The id of the house being removed
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   142
 */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   143
void DecreaseBuildingCount(Town *t, HouseID house_id)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   144
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   145
	HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   146
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   147
	if (!_loaded_newgrf_features.has_newhouses) return;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   148
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   149
	if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   150
	if (_building_counts.id_count[house_id] > 0)   _building_counts.id_count[house_id]--;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   151
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   152
	if (class_id == HOUSE_NO_CLASS) return;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   153
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   154
	if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   155
	if (_building_counts.class_count[class_id] > 0)   _building_counts.class_count[class_id]--;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   156
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   157
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   158
static uint32 HouseGetRandomBits(const ResolverObject *object)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   159
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   160
	const TileIndex tile = object->u.house.tile;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   161
	return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseRandomBits(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   162
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   163
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   164
static uint32 HouseGetTriggers(const ResolverObject *object)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   165
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   166
	const TileIndex tile = object->u.house.tile;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   167
	return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseTriggers(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   168
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   169
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   170
static void HouseSetTriggers(const ResolverObject *object, int triggers)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   171
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   172
	const TileIndex tile = object->u.house.tile;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   173
	if (IsTileType(tile, MP_HOUSE)) SetHouseTriggers(tile, triggers);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   174
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   175
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   176
static uint32 GetNumHouses(HouseID house_id, const Town *town)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   177
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   178
	uint8 map_id_count, town_id_count, map_class_count, town_class_count;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   179
	HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   180
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   181
	map_id_count     = _building_counts.id_count[house_id];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   182
	map_class_count  = _building_counts.class_count[class_id];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   183
	town_id_count    = town->building_counts.id_count[house_id];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   184
	town_class_count = town->building_counts.class_count[class_id];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   185
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   186
	return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   187
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   188
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   189
static uint32 GetGRFParameter(HouseID house_id, byte parameter)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   190
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   191
	const HouseSpec *hs = GetHouseSpecs(house_id);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   192
	const GRFFile *file = hs->grffile;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   193
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   194
	if (parameter >= file->param_end) return 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   195
	return file->param[parameter];
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   196
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   197
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   198
uint32 GetNearbyTileInformation(byte parameter, TileIndex tile)
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   199
{
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   200
	tile = GetNearbyTile(parameter, tile);
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   201
	return GetNearbyTileInformation(tile);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   202
}
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   203
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   204
/**
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   205
 * HouseGetVariable():
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   206
 *
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   207
 * Used by the resolver to get values for feature 07 deterministic spritegroups.
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   208
 */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   209
static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   210
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   211
	const Town *town = object->u.house.town;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   212
	TileIndex tile   = object->u.house.tile;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   213
	HouseID house_id = object->u.house.house_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   214
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   215
	if (object->scope == VSG_SCOPE_PARENT) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   216
		return TownGetVariable(variable, parameter, available, town);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   217
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   218
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   219
	switch (variable) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   220
		/* Construction stage. */
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   221
		case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   222
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   223
		/* Building age. */
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   224
		case 0x41: return Clamp(_cur_year - GetHouseConstructionYear(tile), 0, 0xFF);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   225
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   226
		/* Town zone */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   227
		case 0x42: return GetTownRadiusGroup(town, tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   228
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   229
		/* Terrain type */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   230
		case 0x43: return GetTerrainType(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   231
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   232
		/* Number of this type of building on the map. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   233
		case 0x44: return GetNumHouses(house_id, town);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   234
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   235
		/* Whether the town is being created or just expanded. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   236
		case 0x45: return _generating_world ? 1 : 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   237
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   238
		/* Current animation frame. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   239
		case 0x46: return IsTileType(tile, MP_HOUSE) ? GetHouseAnimationFrame(tile) : 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   240
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   241
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   242
		/* Building counts for old houses with id = parameter. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   243
		case 0x60: return GetNumHouses(parameter, town);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   244
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   245
		/* Building counts for new houses with id = parameter. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   246
		case 0x61: {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   247
			const HouseSpec *hs = GetHouseSpecs(house_id);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   248
			if (hs->grffile == NULL) return 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   249
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   250
			HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   251
			return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   252
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   253
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   254
		/* Land info for nearby tiles. */
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   255
		case 0x62: return GetNearbyTileInformation(parameter, tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   256
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10724
diff changeset
   257
		/* Current animation frame of nearby house tiles */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10724
diff changeset
   258
		case 0x63: {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10724
diff changeset
   259
			TileIndex testtile = GetNearbyTile(parameter, tile);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10724
diff changeset
   260
			return IsTileType(testtile, MP_HOUSE) ? GetHouseAnimationFrame(testtile) : 0;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10724
diff changeset
   261
		}
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10724
diff changeset
   262
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   263
		/* Read GRF parameter */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   264
		case 0x7F: return GetGRFParameter(object->u.house.house_id, parameter);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   265
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   266
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   267
	DEBUG(grf, 1, "Unhandled house property 0x%X", variable);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   268
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   269
	*available = false;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   270
	return UINT_MAX;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   271
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   272
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   273
static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const SpriteGroup *group)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   274
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   275
	/* Houses do not have 'real' groups */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   276
	return NULL;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   277
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   278
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   279
/**
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   280
 * NewHouseResolver():
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   281
 *
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   282
 * Returns a resolver object to be used with feature 07 spritegroups.
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   283
 */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   284
static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   285
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   286
	res->GetRandomBits = HouseGetRandomBits;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   287
	res->GetTriggers   = HouseGetTriggers;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   288
	res->SetTriggers   = HouseSetTriggers;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   289
	res->GetVariable   = HouseGetVariable;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   290
	res->ResolveReal   = HouseResolveReal;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   291
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   292
	res->u.house.tile     = tile;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   293
	res->u.house.town     = town;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   294
	res->u.house.house_id = house_id;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   295
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   296
	res->callback        = CBID_NO_CALLBACK;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   297
	res->callback_param1 = 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   298
	res->callback_param2 = 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   299
	res->last_value      = 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   300
	res->trigger         = 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   301
	res->reseed          = 0;
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   302
	res->count           = 0;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   303
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   304
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   305
uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile)
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   306
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   307
	ResolverObject object;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   308
	const SpriteGroup *group;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   309
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   310
	NewHouseResolver(&object, house_id, tile, town);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   311
	object.callback = callback;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   312
	object.callback_param1 = param1;
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   313
	object.callback_param2 = param2;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   314
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   315
	group = Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   316
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   317
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   318
	return group->g.callback.result;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   319
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   320
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   321
void DrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte stage, HouseID house_id)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   322
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   323
	const DrawTileSprites *dts = group->g.layout.dts;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   324
	const DrawTileSeqStruct *dtss;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   325
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   326
	SpriteID image = dts->ground.sprite;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   327
	SpriteID pal   = dts->ground.pal;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   328
6870
ca3fd1fbe311 (svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents: 6868
diff changeset
   329
	if (IS_CUSTOM_SPRITE(image)) image += stage;
ca3fd1fbe311 (svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents: 6868
diff changeset
   330
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   331
	if (GB(image, 0, SPRITE_WIDTH) != 0) DrawGroundSprite(image, pal);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   332
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   333
	/* End now, if houses are invisible */
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   334
	if (IsInvisibilitySet(TO_HOUSES)) return;
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   335
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   336
	foreach_draw_tile_seq(dtss, dts->seq) {
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   337
		if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   338
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   339
		image = dtss->image.sprite;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   340
		pal   = dtss->image.pal;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   341
6870
ca3fd1fbe311 (svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents: 6868
diff changeset
   342
		if (IS_CUSTOM_SPRITE(image)) image += stage;
ca3fd1fbe311 (svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents: 6868
diff changeset
   343
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   344
		if ((HasBit(image, SPRITE_MODIFIER_OPAQUE) || !IsTransparencySet(TO_HOUSES)) && HasBit(image, PALETTE_MODIFIER_COLOR)) {
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   345
			if (pal == 0) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   346
				const HouseSpec *hs = GetHouseSpecs(house_id);
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   347
				if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
6870
ca3fd1fbe311 (svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents: 6868
diff changeset
   348
					uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   349
					if (callback != CALLBACK_FAILED) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   350
						/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   351
						pal = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   352
					}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   353
				} else {
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   354
					pal = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOR_START;
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   355
				}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   356
			}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   357
		} else {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   358
			pal = PAL_NONE;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   359
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   360
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   361
		if ((byte)dtss->delta_z != 0x80) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   362
			AddSortableSpriteToDraw(
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   363
				image, pal,
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   364
				ti->x + dtss->delta_x, ti->y + dtss->delta_y,
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   365
				dtss->size_x, dtss->size_y,
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   366
				dtss->size_z, ti->z + dtss->delta_z,
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   367
				IsTransparencySet(TO_HOUSES)
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   368
			);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   369
		} else {
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   370
			AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, IsTransparencySet(TO_HOUSES));
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   371
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   372
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   373
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   374
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   375
void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   376
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   377
	const HouseSpec *hs = GetHouseSpecs(house_id);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   378
	const SpriteGroup *group;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   379
	ResolverObject object;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   380
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   381
	if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   382
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   383
	NewHouseResolver(&object, house_id, ti->tile, GetTownByTile(ti->tile));
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   384
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   385
	group = Resolve(hs->spritegroup, &object);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   386
	if (group == NULL || group->type != SGT_TILELAYOUT) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   387
		/* XXX: This is for debugging purposes really, and shouldn't stay. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   388
		DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   389
	} else {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   390
		/* Limit the building stage to the number of stages supplied. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   391
		byte stage = GetHouseBuildingStage(ti->tile);
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   392
		stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   393
		DrawTileLayout(ti, group, stage, house_id);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   394
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   395
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   396
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   397
void AnimateNewHouseTile(TileIndex tile)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   398
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   399
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   400
	byte animation_speed = hs->animation_speed;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   401
	bool frame_set_by_callback = false;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   402
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   403
	if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) {
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   404
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   405
		if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   406
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   407
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   408
	/* An animation speed of 2 means the animation frame changes 4 ticks, and
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   409
	 * increasing this value by one doubles the wait. 2 is the minimum value
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   410
	 * allowed for animation_speed, which corresponds to 120ms, and 16 is the
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   411
	 * maximum, corresponding to around 33 minutes. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   412
	if (_tick_counter % (1 << animation_speed) != 0) return;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   413
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   414
	byte frame      = GetHouseAnimationFrame(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   415
	byte num_frames = GB(hs->animation_frames, 0, 7);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   416
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   417
	if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_NEXT_FRAME)) {
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   418
		uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0;
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   419
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   420
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   421
		if (callback_res != CALLBACK_FAILED) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   422
			frame_set_by_callback = true;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   423
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   424
			switch (callback_res & 0xFF) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   425
				case 0xFF:
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   426
					DeleteAnimatedTile(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   427
					break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   428
				case 0xFE:
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   429
					/* Carry on as normal. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   430
					frame_set_by_callback = false;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   431
					break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   432
				default:
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   433
					frame = callback_res & 0xFF;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   434
					break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   435
			}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   436
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   437
			/* If the lower 7 bits of the upper byte of the callback
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   438
			 * result are not empty, it is a sound effect. */
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   439
			if (GB(callback_res, 8, 7) != 0) PlayTileSound(hs->grffile, GB(callback_res, 8, 7), tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   440
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   441
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   442
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   443
	if (!frame_set_by_callback) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   444
		if (frame < num_frames) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   445
			frame++;
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   446
		} else if (frame == num_frames && HasBit(hs->animation_frames, 7)) {
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   447
			/* This animation loops, so start again from the beginning */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   448
			frame = 0;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   449
		} else {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   450
			/* This animation doesn't loop, so stay here */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   451
			DeleteAnimatedTile(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   452
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   453
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   454
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   455
	SetHouseAnimationFrame(tile, frame);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   456
	MarkTileDirtyByTile(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   457
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   458
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   459
void ChangeHouseAnimationFrame(const GRFFile *file, TileIndex tile, uint16 callback_result)
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   460
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   461
	switch (callback_result & 0xFF) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   462
		case 0xFD: /* Do nothing. */         break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   463
		case 0xFE: AddAnimatedTile(tile);    break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   464
		case 0xFF: DeleteAnimatedTile(tile); break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   465
		default:
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   466
			SetHouseAnimationFrame(tile, callback_result & 0xFF);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   467
			AddAnimatedTile(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   468
			break;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   469
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   470
	/* If the lower 7 bits of the upper byte of the callback
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   471
	 * result are not empty, it is a sound effect. */
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   472
	if (GB(callback_result, 8, 7) != 0) PlayTileSound(file, GB(callback_result, 8, 7), tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   473
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   474
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   475
bool CanDeleteHouse(TileIndex tile)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   476
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   477
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   478
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   479
	/* Human players are always allowed to remove buildings, as is water and
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   480
	 * anyone using the scenario editor. */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   481
	if ((IsValidPlayer(_current_player) && IsHumanPlayer(_current_player))
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   482
			|| _current_player == OWNER_WATER || _current_player == OWNER_NONE) return true;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   483
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   484
	if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   485
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   486
		return (callback_res == CALLBACK_FAILED || callback_res == 0);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   487
	} else {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   488
		return !(hs->extra_flags & BUILDING_IS_PROTECTED);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   489
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   490
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   491
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   492
static void AnimationControl(TileIndex tile, uint16 random_bits)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   493
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   494
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   495
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   496
	if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   497
		uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   498
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   499
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   500
		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   501
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   502
}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   503
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   504
bool NewHouseTileLoop(TileIndex tile)
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   505
{
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   506
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   507
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   508
	if (GetHouseProcessingTime(tile) > 0) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   509
		DecHouseProcessingTime(tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   510
		return true;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   511
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   512
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   513
	TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   514
	TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   515
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   516
	if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   517
		/* If this house is marked as having a synchronised callback, all the
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   518
		 * tiles will have the callback called at once, rather than when the
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   519
		 * tile loop reaches them. This should only be enabled for the northern
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   520
		 * tile, or strange things will happen (here, and in TTDPatch). */
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   521
		if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   522
			uint16 random = GB(Random(), 0, 16);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   523
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   524
			if (hs->building_flags & BUILDING_HAS_1_TILE)  AnimationControl(tile, random);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   525
			if (hs->building_flags & BUILDING_2_TILES_Y)   AnimationControl(TILE_ADDXY(tile, 0, 1), random);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   526
			if (hs->building_flags & BUILDING_2_TILES_X)   AnimationControl(TILE_ADDXY(tile, 1, 0), random);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   527
			if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   528
		} else {
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   529
			AnimationControl(tile, 0);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   530
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   531
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   532
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   533
	/* Check callback 21, which determines if a house should be destroyed. */
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   534
	if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   535
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   536
		if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) > 0) {
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   537
			ClearTownHouse(GetTownByTile(tile), tile);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   538
			return false;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   539
		}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   540
	}
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   541
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   542
	SetHouseProcessingTime(tile, hs->processing_time);
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   543
	return true;
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents:
diff changeset
   544
}
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   545
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   546
static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   547
{
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   548
	ResolverObject object;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   549
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   550
	/* We can't trigger a non-existent building... */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   551
	assert(IsTileType(tile, MP_HOUSE));
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   552
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   553
	HouseID hid = GetHouseType(tile);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   554
	HouseSpec *hs = GetHouseSpecs(hid);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   555
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   556
	if (hs->spritegroup == NULL) return;
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   557
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   558
	NewHouseResolver(&object, hid, tile, GetTownByTile(tile));
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   559
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   560
	object.callback = CBID_RANDOM_TRIGGER;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   561
	object.trigger = trigger;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   562
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   563
	const SpriteGroup *group = Resolve(hs->spritegroup, &object);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   564
	if (group == NULL) return;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   565
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   566
	byte new_random_bits = Random();
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   567
	byte random_bits = GetHouseRandomBits(tile);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   568
	random_bits &= ~object.reseed;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   569
	random_bits |= (first ? new_random_bits : base_random) & object.reseed;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   570
	SetHouseRandomBits(tile, random_bits);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   571
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   572
	switch (trigger) {
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   573
		case HOUSE_TRIGGER_TILE_LOOP:
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   574
			/* Random value already set. */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   575
			break;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   576
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   577
		case HOUSE_TRIGGER_TILE_LOOP_TOP:
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   578
			if (!first) break;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   579
			/* Random value of first tile already set. */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   580
			if (hs->building_flags & BUILDING_2_TILES_Y)   DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   581
			if (hs->building_flags & BUILDING_2_TILES_X)   DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   582
			if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   583
			break;
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   584
	}
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   585
}
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   586
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   587
void TriggerHouse(TileIndex t, HouseTrigger trigger)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   588
{
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   589
	DoTriggerHouse(t, trigger, 0, true);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   590
}