src/newgrf_commons.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 9413 7042a8ec3fa8
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     1
/* $Id$ */
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     2
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     5
 */
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     6
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     7
#include "stdafx.h"
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
     8
#include "openttd.h"
6801
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
     9
#include "variables.h"
6849
e1b7913352f8 (svn r10089) -Fix (r10040): Use GetTileZ instead of GetClearGround in GetTerrainType as
maedhros
parents: 6835
diff changeset
    10
#include "landscape.h"
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    11
#include "town.h"
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    12
#include "industry.h"
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    13
#include "newgrf.h"
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    14
#include "newgrf_commons.h"
8108
b42a0e5c67ef (svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
rubidium
parents: 7875
diff changeset
    15
#include "tile_map.h"
8118
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 8108
diff changeset
    16
#include "station_map.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8118
diff changeset
    17
#include "settings_type.h"
8459
86e0352eb993 (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8458
diff changeset
    18
#include "tree_map.h"
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    19
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    20
/** Constructor of generic class
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    24
 */
eba0ac353e4d (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)
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    26
{
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    27
	max_offset = offset;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    28
	max_new_entities = maximum;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    29
	invalid_ID = invalid;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    30
eba0ac353e4d (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);
eba0ac353e4d (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);
eba0ac353e4d (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));
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    34
	grfid_overrides = CallocT<uint32>(max_offset);
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    35
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    36
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    37
/** Destructor of the generic class.
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    38
 * Frees allocated memory of constructor
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    39
 */
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    40
OverrideManagerBase::~OverrideManagerBase()
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    41
{
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    42
	free(mapping_ID);
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    43
	free(entity_overrides);
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    44
	free(grfid_overrides);
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    45
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    46
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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.
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    50
 * @param local_id ID in grf file
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    51
 * @param grfid  ID of the grf file
6629
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    53
 */
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    54
void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    55
{
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    56
	assert(entity_type < max_offset);
7874
ccad4f4f3c3e (svn r11424) -Fix: an override can be set only once
glx
parents: 7873
diff changeset
    57
	/* An override can be set only once */
ccad4f4f3c3e (svn r11424) -Fix: an override can be set only once
glx
parents: 7873
diff changeset
    58
	if (entity_overrides[entity_type] != invalid_ID) return;
6629
eba0ac353e4d (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;
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    60
	grfid_overrides[entity_type] = grfid;
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    61
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    62
eba0ac353e4d (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 */
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    64
void OverrideManagerBase::ResetMapping()
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    65
{
eba0ac353e4d (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));
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    67
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    68
eba0ac353e4d (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 */
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    70
void OverrideManagerBase::ResetOverride()
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    71
{
eba0ac353e4d (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++) {
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    73
		entity_overrides[i] = invalid_ID;
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
    74
		grfid_overrides[i] = 0;
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    75
	}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    76
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    77
eba0ac353e4d (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.
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    82
 */
eba0ac353e4d (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)
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    84
{
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    85
	const EntityIDMapping *map;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    86
7235
be9f15574c61 (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: 7000
diff changeset
    87
	for (uint16 id = 0; id < max_new_entities; id++) {
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    88
		map = &mapping_ID[id];
eba0ac353e4d (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) {
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    90
			return id;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    91
		}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    92
	}
7875
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
    93
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    94
	return invalid_ID;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    95
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
    96
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   102
 */
eba0ac353e4d (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)
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   104
{
eba0ac353e4d (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);
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   106
	EntityIDMapping *map;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   107
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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.
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   111
	 */
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   112
	if (id != invalid_ID) {
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   113
		return id;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   114
	}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   115
eba0ac353e4d (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. */
eba0ac353e4d (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++) {
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   118
		map = &mapping_ID[id];
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   119
7632
08e14d61a557 (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: 7538
diff changeset
   120
		if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
6629
eba0ac353e4d (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;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   122
			map->grfid         = grfid;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   123
			map->substitute_id = substitute_id;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   124
			return id;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   125
		}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   126
	}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   127
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   128
	return invalid_ID;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   129
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   130
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   133
 * @return mapped id
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   134
 */
eba0ac353e4d (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)
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   136
{
eba0ac353e4d (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;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   138
}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   139
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (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
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   143
 */
eba0ac353e4d (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)
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   145
{
eba0ac353e4d (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);
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   147
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   148
	if (house_id == invalid_ID) {
eba0ac353e4d (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.");
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   150
		return;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   151
	}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   152
eba0ac353e4d (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));
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   154
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   155
	/* Now add the overrides. */
eba0ac353e4d (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++) {
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   157
		HouseSpec *overridden_hs = GetHouseSpecs(i);
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   158
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
   159
		if (entity_overrides[i] != hs->local_id || grfid_overrides[i] != hs->grffile->grfid) continue;
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   160
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   161
		overridden_hs->override = house_id;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   162
		entity_overrides[i] = invalid_ID;
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
   163
		grfid_overrides[i] = 0;
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   164
	}
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents:
diff changeset
   165
}
6801
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   166
8511
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   167
/** Return the ID (if ever available) of a previously inserted entity.
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   168
 * @param grf_local_id ID of this enity withing the grfID
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   169
 * @param grfid ID of the grf file
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   170
 * @return the ID of the candidate, of the Invalid flag item ID
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   171
 */
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   172
uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid)
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   173
{
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   174
	uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   175
	if (id != invalid_ID) return id;
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   176
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   177
	/* No mapping found, try the overrides */
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   178
	for (id = 0; id < max_offset; id++) {
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   179
		if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   180
	}
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   181
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   182
	return invalid_ID;
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   183
}
abb116ef8cd8 (svn r12086) -Fix [FS#1747] (r11425): check overrides only for industries when mapping newgrf entities to 'real' entities
glx
parents: 8459
diff changeset
   184
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   185
/** Method to find an entity ID and to mark it as reserved for the Industry to be included.
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   186
 * @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   187
 * @param grfid ID of the current grf file
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   188
 * @param substitute_id industry from which data has been copied
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   189
 * @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   190
 */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   191
uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   192
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   193
	/* This entity hasn't been defined before, so give it an ID now. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   194
	for (uint16 id = 0; id < max_new_entities; id++) {
7875
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   195
		/* Skip overriden industries */
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   196
		if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   197
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   198
		/* Get the real live industry */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   199
		const IndustrySpec *inds = GetIndustrySpec(id);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   200
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   201
		/* This industry must be one that is not available(enabled), mostly because of climate.
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   202
		 * And it must not already be used by a grf (grffile == NULL).
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   203
		 * So reseve this slot here, as it is the chosen one */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   204
		if (!inds->enabled && inds->grf_prop.grffile == NULL) {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   205
			EntityIDMapping *map = &mapping_ID[id];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   206
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   207
			if (map->entity_id == 0 && map->grfid == 0) {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   208
				/* winning slot, mark it as been used */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   209
				map->entity_id     = grf_local_id;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   210
				map->grfid         = grfid;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   211
				map->substitute_id = substitute_id;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   212
				return id;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   213
			}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   214
		}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   215
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   216
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   217
	return invalid_ID;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   218
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   219
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   220
/** Method to install the new indistry data in its proper slot
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   221
 * The slot assigment is internal of this method, since it requires
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   222
 * checking what is available
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   223
 * @param inds Industryspec that comes from the grf decoding process
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   224
 */
7667
0355390b08d9 (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: 7632
diff changeset
   225
void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   226
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   227
	/* First step : We need to find if this industry is already specified in the savegame data */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   228
	IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   229
7875
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   230
	if (ind_id == invalid_ID) {
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   231
		/* Not found.
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   232
		 * Or it has already been overriden, so you've lost your place old boy.
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   233
		 * Or it is a simple substitute.
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   234
		 * We need to find a free available slot */
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   235
		ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
6e6e7606b77a (svn r11425) -Fix [FS#1424]: overriden industries were ignored when mapping newgrf industry type to 'real' industry type
glx
parents: 7874
diff changeset
   236
		inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   237
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   238
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   239
	if (ind_id == invalid_ID) {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   240
		grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   241
		return;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   242
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   243
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   244
	/* Now that we know we can use the given id, copy the spech to its final destination*/
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   245
	memcpy(&_industry_specs[ind_id], inds, sizeof(*inds));
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   246
	/* and mark it as usable*/
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   247
	_industry_specs[ind_id].enabled = true;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   248
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents: 6827
diff changeset
   249
7000
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   250
void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   251
{
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   252
	IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   253
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   254
	if (indt_id == invalid_ID) {
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   255
		grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   256
		return;
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   257
	}
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   258
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   259
	memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   260
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   261
	/* Now add the overrides. */
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   262
	for (int i = 0; i < max_offset; i++) {
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   263
		IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   264
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
   265
		if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
7000
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   266
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   267
		overridden_its->grf_prop.override = indt_id;
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   268
		overridden_its->enabled = false;
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   269
		entity_overrides[i] = invalid_ID;
7873
9bb5874c81df (svn r11423) -Codechange: store grfid when adding an override
glx
parents: 7714
diff changeset
   270
		grfid_overrides[i] = 0;
7000
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   271
	}
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   272
}
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6849
diff changeset
   273
6801
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   274
/** Function used by houses (and soon industries) to get information
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   275
 * on type of "terrain" the tile it is queries sits on.
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   276
 * @param tile TileIndex of the tile been queried
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   277
 * @return value corresponding to the grf expected format:
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   278
 *         Terrain type: 0 normal, 1 desert, 2 rainforest, 4 on or above snowline */
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   279
uint32 GetTerrainType(TileIndex tile)
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   280
{
9413
7042a8ec3fa8 (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: 9358
diff changeset
   281
	switch (_settings_game.game_creation.landscape) {
8394
8d765e1d2641 (svn r11964) -Fix [FS#1685]: Tropic zone data was returned incorrectly.
peter1138
parents: 8270
diff changeset
   282
		case LT_TROPIC: return GetTropicZone(tile);
7538
3741a86b9036 (svn r11058) -Fix[FS#1216]: GetTerrainType was a bit too relax with snow line.
belugas
parents: 7238
diff changeset
   283
		case LT_ARCTIC: return GetTileZ(tile) > GetSnowLine() ? 4 : 0;
6801
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   284
		default:        return 0;
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   285
	}
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   286
}
9d82611443fd (svn r10040) -Codechange: Make the function GetTerrainType public, as other functions require it
belugas
parents: 6629
diff changeset
   287
6827
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   288
TileIndex GetNearbyTile(byte parameter, TileIndex tile)
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   289
{
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   290
	int8 x = GB(parameter, 0, 4);
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   291
	int8 y = GB(parameter, 4, 4);
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   292
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   293
	if (x >= 8) x -= 16;
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   294
	if (y >= 8) y -= 16;
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   295
8118
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 8108
diff changeset
   296
	/* Swap width and height depending on axis for railway stations */
8533
921045950feb (svn r12108) -Fix [FS#1753]: X/Y axis swap for station tiles in GetNearbyTile() was wrong way around.
peter1138
parents: 8511
diff changeset
   297
	if (IsRailwayStationTile(tile) && GetRailStationAxis(tile) == AXIS_Y) Swap(x, y);
8118
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 8108
diff changeset
   298
7238
8986d0158eb9 (svn r10519) -Fix: when getting a "nearby" tile, make sure you never roam outside of the map.
rubidium
parents: 7235
diff changeset
   299
	/* Make sure we never roam outside of the map */
8986d0158eb9 (svn r10519) -Fix: when getting a "nearby" tile, make sure you never roam outside of the map.
rubidium
parents: 7235
diff changeset
   300
	return TILE_MASK(tile + TileDiffXY(x, y));
6827
e2450e25ad95 (svn r10066) -Codechange: Expose function GetNearbyTile by moving it to newgrf_commons.[cpp|h]. Will be used by industries in a few.
belugas
parents: 6801
diff changeset
   301
}
8458
38fe72ff1402 (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: 8394
diff changeset
   302
38fe72ff1402 (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: 8394
diff changeset
   303
/**
38fe72ff1402 (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: 8394
diff changeset
   304
 * Common part of station var 0x67 , house var 0x62, indtile var 0x60, industry var 0x62.
38fe72ff1402 (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: 8394
diff changeset
   305
 *
38fe72ff1402 (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: 8394
diff changeset
   306
 * @param tile the tile of interest.
38fe72ff1402 (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: 8394
diff changeset
   307
 * @return 0czzbbss: c = TileType; zz = TileZ; bb: 7-3 zero, 4-2 TerrainType, 1 water/shore, 0 zero; ss = TileSlope
38fe72ff1402 (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: 8394
diff changeset
   308
 */
38fe72ff1402 (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: 8394
diff changeset
   309
uint32 GetNearbyTileInformation(TileIndex tile)
38fe72ff1402 (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: 8394
diff changeset
   310
{
38fe72ff1402 (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: 8394
diff changeset
   311
	TileType tile_type = GetTileType(tile);
38fe72ff1402 (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: 8394
diff changeset
   312
8459
86e0352eb993 (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8458
diff changeset
   313
	/* Fake tile type for trees on shore */
86e0352eb993 (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8458
diff changeset
   314
	if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
86e0352eb993 (svn r12029) -Feature: Allow trees on shore.
frosch
parents: 8458
diff changeset
   315
8458
38fe72ff1402 (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: 8394
diff changeset
   316
	uint z;
38fe72ff1402 (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: 8394
diff changeset
   317
	Slope tileh = GetTileSlope(tile, &z);
38fe72ff1402 (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: 8394
diff changeset
   318
	byte terrain_type = GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
38fe72ff1402 (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: 8394
diff changeset
   319
	return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
38fe72ff1402 (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: 8394
diff changeset
   320
}