src/newgrf_commons.cpp
author rubidium
Fri, 18 Jul 2008 12:20:31 +0000
changeset 11171 04fc8e49df1b
parent 10775 7061477bfbcf
permissions -rw-r--r--
(svn r13729) -Fix: assumption that non-north tiles of a house do not have the 1x1 building bit set was flawed with some NewGRFs. This caused the amount of houses to differ, which causes the town radii to differ, which causes desyncs when towns are expanded.
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     1
/* $Id$ */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     2
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     3
/** @file newgrf_commons.cpp Implementation of the class OverrideManagerBase
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     4
 * and its descendance, present and futur
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     5
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     6
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     7
#include "stdafx.h"
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     8
#include "openttd.h"
7297
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
     9
#include "variables.h"
7345
6fab61079176 (svn r10089) -Fix (r10040): Use GetTileZ instead of GetClearGround in GetTerrainType as
maedhros
parents: 7331
diff changeset
    10
#include "landscape.h"
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    11
#include "town.h"
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    12
#include "industry.h"
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    13
#include "newgrf.h"
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    14
#include "newgrf_commons.h"
8604
8afdd9877afd (svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
rubidium
parents: 8371
diff changeset
    15
#include "tile_map.h"
8614
21126f54f5c8 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 8604
diff changeset
    16
#include "station_map.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8614
diff changeset
    17
#include "settings_type.h"
8955
8d6bb31af11b (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8954
diff changeset
    18
#include "tree_map.h"
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    19
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    20
/** Constructor of generic class
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    21
 * @param offset end of original data for this entity. i.e: houses = 110
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    22
 * @param maximum of entities this manager can deal with. i.e: houses = 512
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    23
 * @param invalid is the ID used to identify an invalid entity id
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    24
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    25
OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    26
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    27
	max_offset = offset;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    28
	max_new_entities = maximum;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    29
	invalid_ID = invalid;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    30
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    31
	mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    32
	entity_overrides = MallocT<uint16>(max_offset);
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    33
	memset(entity_overrides, invalid, sizeof(entity_overrides));
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    34
	grfid_overrides = CallocT<uint32>(max_offset);
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    35
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    36
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    37
/** Destructor of the generic class.
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    38
 * Frees allocated memory of constructor
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    39
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    40
OverrideManagerBase::~OverrideManagerBase()
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    41
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    42
	free(mapping_ID);
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    43
	free(entity_overrides);
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    44
	free(grfid_overrides);
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    45
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    46
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    47
/** Since the entity IDs defined by the GRF file does not necessarily correlate
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    48
 * to those used by the game, the IDs used for overriding old entities must be
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    49
 * translated when the entity spec is set.
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    50
 * @param local_id ID in grf file
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    51
 * @param grfid  ID of the grf file
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    52
 * @param entity_type original entity type
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    53
 */
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    54
void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    55
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    56
	assert(entity_type < max_offset);
8370
14da0d228a37 (svn r11424) -Fix: an override can be set only once
glx
parents: 8369
diff changeset
    57
	/* An override can be set only once */
14da0d228a37 (svn r11424) -Fix: an override can be set only once
glx
parents: 8369
diff changeset
    58
	if (entity_overrides[entity_type] != invalid_ID) return;
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    59
	entity_overrides[entity_type] = local_id;
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    60
	grfid_overrides[entity_type] = grfid;
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    61
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    62
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    63
/** Resets the mapping, which is used while initializing game */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    64
void OverrideManagerBase::ResetMapping()
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    65
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    66
	memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    67
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    68
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    69
/** Resets the override, which is used while initializing game */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    70
void OverrideManagerBase::ResetOverride()
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    71
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    72
	for (uint16 i = 0; i < max_offset; i++) {
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    73
		entity_overrides[i] = invalid_ID;
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
    74
		grfid_overrides[i] = 0;
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    75
	}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    76
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    77
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    78
/** Return the ID (if ever available) of a previously inserted entity.
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    79
 * @param grf_local_id ID of this enity withing the grfID
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    80
 * @param grfid ID of the grf file
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    81
 * @return the ID of the candidate, of the Invalid flag item ID
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    82
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    83
uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid)
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    84
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    85
	const EntityIDMapping *map;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    86
7731
ae4ec8ea5ac5 (svn r10516) -Fix: if doing a lookup for a ID, scan the whole range instead of only the "new" ones because the old ones could be overriden too.
rubidium
parents: 7496
diff changeset
    87
	for (uint16 id = 0; id < max_new_entities; id++) {
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    88
		map = &mapping_ID[id];
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    89
		if (map->entity_id == grf_local_id && map->grfid == grfid) {
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    90
			return id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    91
		}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    92
	}
8371
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
    93
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    94
	return invalid_ID;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    95
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    96
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    97
/** Reserves a place in the mapping array for an entity to be installed
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    98
 * @param grf_local_id is an arbitrary id given by the grf's author.  Also known as setid
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    99
 * @param grfid is the id of the grf file itself
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   100
 * @param substitute_id is the original entity from which data is copied for the new one
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   101
 * @return the proper usable slot id, or invalid marker if none is found
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   102
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   103
uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   104
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   105
	uint16 id = this->GetID(grf_local_id, grfid);
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   106
	EntityIDMapping *map;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   107
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   108
	/* Look to see if this entity has already been added. This is done
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   109
	 * separately from the loop below in case a GRF has been deleted, and there
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   110
	 * are any gaps in the array.
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   111
	 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   112
	if (id != invalid_ID) {
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   113
		return id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   114
	}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   115
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   116
	/* This entity hasn't been defined before, so give it an ID now. */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   117
	for (id = max_offset; id < max_new_entities; id++) {
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   118
		map = &mapping_ID[id];
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   119
8128
9fe288a99e99 (svn r11163) -Codechange: Verify that the IndustryTileOverrideManager skip the magic value of 0xFF when assigning a new tileID. This is really important, since the value is reserved for water checking.
belugas
parents: 8034
diff changeset
   120
		if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   121
			map->entity_id     = grf_local_id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   122
			map->grfid         = grfid;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   123
			map->substitute_id = substitute_id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   124
			return id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   125
		}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   126
	}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   127
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   128
	return invalid_ID;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   129
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   130
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   131
/** Gives the substitute of the entity, as specified by the grf file
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   132
 * @param entity_id of the entity being queried
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   133
 * @return mapped id
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   134
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   135
uint16 OverrideManagerBase::GetSubstituteID(byte entity_id)
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   136
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   137
	return mapping_ID[entity_id].substitute_id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   138
}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   139
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   140
/** Install the specs into the HouseSpecs array
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   141
 * It will find itself the proper slot onwhich it will go
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   142
 * @param hs HouseSpec read from the grf file, ready for inclusion
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   143
 */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   144
void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   145
{
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   146
	HouseID house_id = this->AddEntityID(hs->local_id, hs->grffile->grfid, hs->substitute_id);
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   147
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   148
	if (house_id == invalid_ID) {
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   149
		grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   150
		return;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   151
	}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   152
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   153
	memcpy(&_house_specs[house_id], hs, sizeof(*hs));
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   154
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   155
	/* Now add the overrides. */
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   156
	for (int i = 0; i != max_offset; i++) {
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   157
		HouseSpec *overridden_hs = GetHouseSpecs(i);
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   158
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
   159
		if (entity_overrides[i] != hs->local_id || grfid_overrides[i] != hs->grffile->grfid) continue;
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   160
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   161
		overridden_hs->override = house_id;
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   162
		entity_overrides[i] = invalid_ID;
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
   163
		grfid_overrides[i] = 0;
7125
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   164
	}
4ce0c7a12a3f (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   165
}
7297
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   166
9007
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   167
/** Return the ID (if ever available) of a previously inserted entity.
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   168
 * @param grf_local_id ID of this enity withing the grfID
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   169
 * @param grfid ID of the grf file
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   170
 * @return the ID of the candidate, of the Invalid flag item ID
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   171
 */
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   172
uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid)
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   173
{
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   174
	uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   175
	if (id != invalid_ID) return id;
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   176
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   177
	/* No mapping found, try the overrides */
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   178
	for (id = 0; id < max_offset; id++) {
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   179
		if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   180
	}
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   181
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   182
	return invalid_ID;
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   183
}
6fc05c8ecf87 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8955
diff changeset
   184
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   185
/** Method to find an entity ID and to mark it as reserved for the Industry to be included.
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   186
 * @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   187
 * @param grfid ID of the current grf file
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   188
 * @param substitute_id industry from which data has been copied
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   189
 * @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   190
 */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   191
uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   192
{
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   193
	/* This entity hasn't been defined before, so give it an ID now. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   194
	for (uint16 id = 0; id < max_new_entities; id++) {
8371
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   195
		/* Skip overriden industries */
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   196
		if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   197
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   198
		/* Get the real live industry */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   199
		const IndustrySpec *inds = GetIndustrySpec(id);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   200
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   201
		/* This industry must be one that is not available(enabled), mostly because of climate.
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   202
		 * And it must not already be used by a grf (grffile == NULL).
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   203
		 * So reseve this slot here, as it is the chosen one */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   204
		if (!inds->enabled && inds->grf_prop.grffile == NULL) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   205
			EntityIDMapping *map = &mapping_ID[id];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   206
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   207
			if (map->entity_id == 0 && map->grfid == 0) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   208
				/* winning slot, mark it as been used */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   209
				map->entity_id     = grf_local_id;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   210
				map->grfid         = grfid;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   211
				map->substitute_id = substitute_id;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   212
				return id;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   213
			}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   214
		}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   215
	}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   216
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   217
	return invalid_ID;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   218
}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   219
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   220
/** Method to install the new indistry data in its proper slot
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   221
 * The slot assigment is internal of this method, since it requires
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   222
 * checking what is available
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   223
 * @param inds Industryspec that comes from the grf decoding process
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   224
 */
8163
0a8d78012096 (svn r11198) -Fix: When industry override is not possible because it is already been overridden, mark the new candidate as not being an override
belugas
parents: 8128
diff changeset
   225
void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   226
{
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   227
	/* First step : We need to find if this industry is already specified in the savegame data */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   228
	IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   229
8371
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   230
	if (ind_id == invalid_ID) {
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   231
		/* Not found.
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   232
		 * Or it has already been overriden, so you've lost your place old boy.
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   233
		 * Or it is a simple substitute.
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   234
		 * We need to find a free available slot */
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   235
		ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
8c37a46425b3 (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 8370
diff changeset
   236
		inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   237
	}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   238
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   239
	if (ind_id == invalid_ID) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   240
		grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   241
		return;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   242
	}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   243
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   244
	/* Now that we know we can use the given id, copy the spech to its final destination*/
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   245
	memcpy(&_industry_specs[ind_id], inds, sizeof(*inds));
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   246
	/* and mark it as usable*/
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   247
	_industry_specs[ind_id].enabled = true;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   248
}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 7323
diff changeset
   249
7496
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   250
void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   251
{
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   252
	IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   253
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   254
	if (indt_id == invalid_ID) {
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   255
		grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   256
		return;
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   257
	}
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   258
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   259
	memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   260
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   261
	/* Now add the overrides. */
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   262
	for (int i = 0; i < max_offset; i++) {
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   263
		IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   264
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
   265
		if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
7496
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   266
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   267
		overridden_its->grf_prop.override = indt_id;
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   268
		overridden_its->enabled = false;
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   269
		entity_overrides[i] = invalid_ID;
8369
4decac386a96 (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 8210
diff changeset
   270
		grfid_overrides[i] = 0;
7496
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   271
	}
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   272
}
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7345
diff changeset
   273
7297
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   274
/** Function used by houses (and soon industries) to get information
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   275
 * on type of "terrain" the tile it is queries sits on.
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   276
 * @param tile TileIndex of the tile been queried
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   277
 * @return value corresponding to the grf expected format:
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   278
 *         Terrain type: 0 normal, 1 desert, 2 rainforest, 4 on or above snowline */
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   279
uint32 GetTerrainType(TileIndex tile)
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   280
{
10775
7061477bfbcf (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 10707
diff changeset
   281
	switch (_settings_game.game_creation.landscape) {
8890
7db39585f2b3 (svn r11964) -Fix [FS#1685]: Tropic zone data was returned incorrectly.
peter1138
parents: 8766
diff changeset
   282
		case LT_TROPIC: return GetTropicZone(tile);
8034
0f6a176ce1e9 (svn r11058) -Fix[FS#1216]: GetTerrainType was a bit too relax with snow line.
belugas
parents: 7734
diff changeset
   283
		case LT_ARCTIC: return GetTileZ(tile) > GetSnowLine() ? 4 : 0;
7297
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   284
		default:        return 0;
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   285
	}
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   286
}
1df77fb920bd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 7125
diff changeset
   287
7323
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   288
TileIndex GetNearbyTile(byte parameter, TileIndex tile)
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   289
{
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   290
	int8 x = GB(parameter, 0, 4);
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   291
	int8 y = GB(parameter, 4, 4);
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   292
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   293
	if (x >= 8) x -= 16;
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   294
	if (y >= 8) y -= 16;
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   295
8614
21126f54f5c8 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 8604
diff changeset
   296
	/* Swap width and height depending on axis for railway stations */
9029
aa985bbd4d1d (svn r12108) -Fix [FS#1753]: X/Y axis swap for station tiles in GetNearbyTile() was wrong way around.
peter1138
parents: 9007
diff changeset
   297
	if (IsRailwayStationTile(tile) && GetRailStationAxis(tile) == AXIS_Y) Swap(x, y);
8614
21126f54f5c8 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 8604
diff changeset
   298
7734
56228bfe84bc (svn r10519) -Fix: when getting a "nearby" tile, make sure you never roam outside of the map.
rubidium
parents: 7731
diff changeset
   299
	/* Make sure we never roam outside of the map */
56228bfe84bc (svn r10519) -Fix: when getting a "nearby" tile, make sure you never roam outside of the map.
rubidium
parents: 7731
diff changeset
   300
	return TILE_MASK(tile + TileDiffXY(x, y));
7323
b2871568db92 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 7297
diff changeset
   301
}
8954
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   302
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   303
/**
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   304
 * Common part of station var 0x67 , house var 0x62, indtile var 0x60, industry var 0x62.
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   305
 *
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   306
 * @param tile the tile of interest.
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   307
 * @return 0czzbbss: c = TileType; zz = TileZ; bb: 7-3 zero, 4-2 TerrainType, 1 water/shore, 0 zero; ss = TileSlope
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   308
 */
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   309
uint32 GetNearbyTileInformation(TileIndex tile)
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   310
{
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   311
	TileType tile_type = GetTileType(tile);
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   312
8955
8d6bb31af11b (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8954
diff changeset
   313
	/* Fake tile type for trees on shore */
8d6bb31af11b (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8954
diff changeset
   314
	if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
8d6bb31af11b (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8954
diff changeset
   315
8954
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   316
	uint z;
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   317
	Slope tileh = GetTileSlope(tile, &z);
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   318
	byte terrain_type = GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   319
	return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
ca1d17502817 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8890
diff changeset
   320
}