summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--projects/openttd.vcproj6
-rw-r--r--projects/openttd_vs80.vcproj8
-rw-r--r--source.list2
-rw-r--r--src/newgrf_commons.cpp69
-rw-r--r--src/newgrf_commons.h17
-rw-r--r--src/newgrf_industries.cpp225
-rw-r--r--src/newgrf_industries.h14
-rw-r--r--src/newgrf_spritegroup.h5
8 files changed, 344 insertions, 2 deletions
diff --git a/projects/openttd.vcproj b/projects/openttd.vcproj
index 2c3c68cf5..472860713 100644
--- a/projects/openttd.vcproj
+++ b/projects/openttd.vcproj
@@ -543,6 +543,9 @@
RelativePath=".\..\src\newgrf_house.h">
</File>
<File
+ RelativePath=".\..\src\newgrf_industries.h">
+ </File>
+ <File
RelativePath=".\..\src\newgrf_sound.h">
</File>
<File
@@ -980,6 +983,9 @@
RelativePath=".\..\src\newgrf_house.cpp">
</File>
<File
+ RelativePath=".\..\src\newgrf_industries.cpp">
+ </File>
+ <File
RelativePath=".\..\src\newgrf_sound.cpp">
</File>
<File
diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj
index c3e0a1adc..4845bd184 100644
--- a/projects/openttd_vs80.vcproj
+++ b/projects/openttd_vs80.vcproj
@@ -952,6 +952,10 @@
>
</File>
<File
+ RelativePath="..\src\newgrf_industries.h"
+ >
+ </File>
+ <File
RelativePath=".\..\src\newgrf_sound.h"
>
</File>
@@ -1528,6 +1532,10 @@
>
</File>
<File
+ RelativePath="..\src\newgrf_industries.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\newgrf_sound.cpp"
>
</File>
diff --git a/source.list b/source.list
index 5bc8880bd..4ba3a7b29 100644
--- a/source.list
+++ b/source.list
@@ -148,6 +148,7 @@ newgrf_commons.h
newgrf_config.h
newgrf_engine.h
newgrf_house.h
+newgrf_industries.h
newgrf_sound.h
newgrf_spritegroup.h
newgrf_station.h
@@ -297,6 +298,7 @@ newgrf_commons.cpp
newgrf_config.cpp
newgrf_engine.cpp
newgrf_house.cpp
+newgrf_industries.cpp
newgrf_sound.cpp
newgrf_spritegroup.cpp
newgrf_station.cpp
diff --git a/src/newgrf_commons.cpp b/src/newgrf_commons.cpp
index 9e796aa60..6c03de201 100644
--- a/src/newgrf_commons.cpp
+++ b/src/newgrf_commons.cpp
@@ -151,6 +151,75 @@ void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
}
}
+/** Method to find an entity ID and to mark it as reserved for the Industry to be included.
+ * @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid
+ * @param grfid ID of the current grf file
+ * @param substitute_id industry from which data has been copied
+ * @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise
+ */
+uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
+{
+ /* This entity hasn't been defined before, so give it an ID now. */
+ for (uint16 id = 0; id < max_new_entities; id++) {
+ /* Get the real live industry */
+ const IndustrySpec *inds = GetIndustrySpec(id);
+
+ /* This industry must be one that is not available(enabled), mostly because of climate.
+ * And it must not already be used by a grf (grffile == NULL).
+ * So reseve this slot here, as it is the chosen one */
+ if (!inds->enabled && inds->grf_prop.grffile == NULL) {
+ EntityIDMapping *map = &mapping_ID[id];
+
+ if (map->entity_id == 0 && map->grfid == 0) {
+ /* winning slot, mark it as been used */
+ map->entity_id = grf_local_id;
+ map->grfid = grfid;
+ map->substitute_id = substitute_id;
+ return id;
+ }
+ }
+ }
+
+ return invalid_ID;
+}
+
+/** Method to install the new indistry data in its proper slot
+ * The slot assigment is internal of this method, since it requires
+ * checking what is available
+ * @param inds Industryspec that comes from the grf decoding process
+ */
+void IndustryOverrideManager::SetEntitySpec(const IndustrySpec *inds)
+{
+ /* First step : We need to find if this industry is already specified in the savegame data */
+ IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
+
+ if (ind_id == invalid_ID) { // not found? So this is the introduction of a new industry
+ /* Second step is dealing with the override. */
+ if (inds->grf_prop.override != invalid_ID && _industry_specs[inds->grf_prop.override].grf_prop.override == invalid_ID) {
+ /* this is an override, which means it will take the place of the industry it is
+ * designed to replace. Before we conclude that the override is allowed,
+ * we first need to verify that the slot is not holding another override
+ * If it's the case,it will be considered as a normal substitute */
+ ind_id = inds->grf_prop.override;
+ } else {
+ /* It has already been overriden, so you've lost your place old boy.
+ * Or it is a simple substitute.
+ * In both case, we need to find a free available slot */
+ ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
+ }
+ }
+
+ if (ind_id == invalid_ID) {
+ grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
+ return;
+ }
+
+ /* Now that we know we can use the given id, copy the spech to its final destination*/
+ memcpy(&_industry_specs[ind_id], inds, sizeof(*inds));
+ /* and mark it as usable*/
+ _industry_specs[ind_id].enabled = true;
+}
+
/** Function used by houses (and soon industries) to get information
* on type of "terrain" the tile it is queries sits on.
* @param tile TileIndex of the tile been queried
diff --git a/src/newgrf_commons.h b/src/newgrf_commons.h
index a7c00e65c..f45594f14 100644
--- a/src/newgrf_commons.h
+++ b/src/newgrf_commons.h
@@ -35,7 +35,6 @@ protected:
uint16 invalid_ID; ///< ID used to dected invalid entities;
- virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
public:
EntityIDMapping *mapping_ID; ///< mapping of ids from grf files. Public out of convenience
@@ -46,6 +45,7 @@ public:
void ResetMapping();
void Add(uint8 local_id, uint entity_type);
+ virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
uint16 GetSubstituteID(byte entity_id);
uint16 GetID(uint8 grf_local_id, uint32 grfid);
@@ -59,12 +59,25 @@ struct HouseSpec;
class HouseOverrideManager : public OverrideManagerBase
{
public:
- HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : OverrideManagerBase(offset, maximum, invalid) {};
+ HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
+ OverrideManagerBase(offset, maximum, invalid) {};
void SetEntitySpec(const HouseSpec *hs);
};
+struct IndustrySpec;
+class IndustryOverrideManager : public OverrideManagerBase
+{
+ public:
+ IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
+ OverrideManagerBase(offset, maximum, invalid) {};
+
+ virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
+ void SetEntitySpec(const IndustrySpec *inds);
+};
+
extern HouseOverrideManager _house_mngr;
+extern IndustryOverrideManager _industry_mngr;
uint32 GetTerrainType(TileIndex tile);
TileIndex GetNearbyTile(byte parameter, TileIndex tile);
diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp
new file mode 100644
index 000000000..d07b6cf79
--- /dev/null
+++ b/src/newgrf_industries.cpp
@@ -0,0 +1,225 @@
+/* $Id$ */
+
+/** @file newgrf_industries.cpp */
+
+#include "stdafx.h"
+#include "openttd.h"
+#include "debug.h"
+#include "functions.h"
+#include "macros.h"
+#include "industry.h"
+#include "newgrf.h"
+#include "newgrf_callbacks.h"
+#include "newgrf_spritegroup.h"
+#include "newgrf_industries.h"
+#include "newgrf_commons.h"
+
+/* Since the industry IDs defined by the GRF file don't necessarily correlate
+ * to those used by the game, the IDs used for overriding old industries must be
+ * translated when the idustry spec is set. */
+IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
+
+/**
+ * Finds the distance for the closest tile with water/land given a tile
+ * @param tile the tile to find the distance too
+ * @param water whether to find water or land
+ * @note FAILS when an industry should be seen as water
+ */
+static uint GetClosestWaterDistance(TileIndex tile, bool water)
+{
+ TileIndex t;
+ uint best_dist;
+ for (t = 1; t < MapSize(); t++) {
+ if (IsTileType(t, MP_WATER) == water) break;
+ }
+ best_dist = DistanceManhattan(tile, t);
+
+ for (; t < MapSize(); t++) {
+ uint dist = DistanceManhattan(tile, t);
+ if (dist < best_dist) {
+ if (IsTileType(t, MP_WATER) == water) best_dist = dist;
+ } else {
+ /* When the Y distance between the current row and the 'source' tile
+ * is larger than the best distance, we've found the best distance */
+ if (TileY(t) - TileY(tile) > best_dist) return best_dist;
+ if (TileX(tile) > TileX(t)) {
+ /* We can safely skip this many tiles; from here all tiles have a
+ * higher or equal distance than the best distance */
+ t |= MapMaxX();
+ continue;
+ } else {
+ /* We can safely skip this many tiles; up to here all tiles have a
+ * higher or equal distance than the best distance */
+ t += best_dist - dist;
+ continue;
+ }
+ }
+ }
+
+ return best_dist;
+}
+
+/** This function implements the industries variables that newGRF defines.
+ * @param variable that is queried
+ * @param parameter unused
+ * @param available will return false if ever the variable asked for does not exist
+ * @param ind is of course the industry we are inquiring
+ * @return the value stored in the corresponding variable*/
+uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
+{
+ const Industry *industry = object->u.industry.ind;
+ TileIndex tile = object->u.industry.tile;
+ const IndustrySpec *indspec = GetIndustrySpec(industry->type);
+
+ switch (variable) {
+ case 0x40:
+ case 0x41:
+ case 0x42: { // waiting cargo, but only if those two callback flags are set
+ uint16 callback = indspec->callback_flags;
+ if (callback & (CBM_IND_PRODUCTION_CARGO_ARRIVAL | callback & CBM_IND_PRODUCTION_256_TICKS)) {
+ return max(industry->cargo_waiting[variable - 0x40], (uint16)0x7FFF);
+ } else {
+ return 0;
+ }
+ }
+ /* TODO: somehow determine whether we're in water or not */
+ case 0x43: return GetClosestWaterDistance(tile, true); // Manhattan distance of closes dry/water tile
+
+ case 0x60: { /* Get industry ID at offset param */
+ /*The parameter of this variable is an offset from the northernmost tile of the industry:
+ * the high nibble contains the Y offset, the low one the X offset; both are unsigned.
+ * The high word of the return value is currently reserved, and the low word can be:
+ * 00xxh if the tile is an industry tile and was defined in the current GRF with ID xx.
+ * FFxxh if the tile is an industry tile of an old type, and has the ID xx.
+ * FFFEh if the tile is an industry tile that was defined in another GRF file
+ * FFFFh if the tile isn't an industry tile, or doesn't belong to the current industry */
+ return GetIndustry(GetNearbyTile(parameter, tile))->type;
+ }
+
+ case 0x61: return 0; // Get random tile bits at offset param
+
+ case 0x62: // Land info of nearby tiles
+ case 0x63: // Animation stage of nerby tiles
+ case 0x64: break; // Distance of nearest industry of given type
+ /* Get town zone and Manhattan distance of closest town */
+ case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
+ /* Get square of Euclidian distance of closes town */
+ case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
+
+ /* Count of industry, distance of closest instance
+ * format is rr(reserved) cc(count) dddd(manhattan distance of closest sister)
+ * A lot more should be done, since it has to check for local id, grf id etc...
+ * let's just say it is a beginning ;) */
+ case 0x67: return GetIndustryTypeCount(industry->type) << 16 | 0;
+
+ /* Industry founder information.
+ * 0x10 if randomly created or from a map pre-newindustry.
+ * Else, the company who funded it */
+ case 0xA7: return 0x10;
+
+ case 0xB0: // Date when built since 1920 (in days)
+ case 0xB3: // Construction type
+ case 0xB4: break; // Date last cargo accepted since 1920 (in days)
+
+ /* Industry structure access*/
+ case 0x80: return industry->xy;
+ case 0x81: return GB(industry->xy, 8, 8);
+ /* Pointer to the town the industry is associated with */
+ case 0x82:
+ case 0x83:
+ case 0x84:
+ case 0x85: break; // not supported
+ case 0x86: return industry->width;
+ case 0x87: return industry->height;// xy dimensions
+ /* */
+ case 0x88:
+ case 0x89: return indspec->produced_cargo[variable - 0x88];
+ case 0x8A: return industry->cargo_waiting[0];
+ case 0x8B: return GB(industry->cargo_waiting[0], 8, 8);
+ case 0x8C: return industry->cargo_waiting[1];
+ case 0x8D: return GB(industry->cargo_waiting[1], 8, 8);
+ case 0x8E:
+ case 0x8F: return industry->production_rate[variable - 0x8E];
+ case 0x90:
+ case 0x91:
+ case 0x92: return indspec->accepts_cargo[variable - 0x90];
+ case 0x93: return industry->prod_level;
+ /* amount of cargo produced so far THIS month. */
+ case 0x94: return industry->this_month_production[0];
+ case 0x95: return GB(industry->this_month_production[0], 8, 8);
+ case 0x96: return industry->this_month_production[1];
+ case 0x97: return GB(industry->this_month_production[1], 8, 8);
+ /* amount of cargo transported so far THIS month. */
+ case 0x98: return industry->this_month_transported[0];
+ case 0x99: return GB(industry->this_month_transported[0], 8, 8);
+ case 0x9A: return industry->this_month_transported[1];
+ case 0x9B: return GB(industry->this_month_transported[0], 8, 8);
+ /* fraction of cargo transported LAST month. */
+ case 0x9C:
+ case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
+ /* amount of cargo produced LAST month. */
+ case 0x9E: return industry->last_month_production[0];
+ case 0x9F: return GB(industry->last_month_production[0], 8, 8);
+ case 0xA0: return industry->last_month_production[1];
+ case 0xA1: return GB(industry->last_month_production[1], 8, 8);
+ /* amount of cargo transported last month. */
+ case 0xA2: return industry->last_month_transported[0];
+ case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
+ case 0xA4: return industry->last_month_transported[1];
+ case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
+
+ case 0xA6: return industry->type;
+
+ case 0xA8: return industry->random_color;
+ case 0xA9: return industry->last_prod_year; // capped?
+ case 0xAA: return industry->counter;
+ case 0xAB: return GB(industry->counter, 8, 8);
+ case 0xAC: return industry->was_cargo_delivered;
+ }
+
+ DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
+
+ *available = false;
+ return (uint32)-1;
+}
+
+static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const SpriteGroup *group)
+{
+ /* IndustryTile do not have 'real' groups */
+ return NULL;
+}
+
+static void NewIndustryResolver(ResolverObject *res, IndustryType ind_id, TileIndex tile, Industry *indus)
+{
+ res->GetRandomBits = NULL;//IndustryTileGetRandomBits;
+ res->GetTriggers = NULL;//IndustryTileGetTriggers;
+ res->SetTriggers = NULL;//IndustryTileSetTriggers;
+ res->GetVariable = IndustryGetVariable;
+ res->ResolveReal = IndustryResolveReal;
+
+ res->u.industry.tile = tile;
+ res->u.industry.ind = indus;
+
+ res->callback = 0;
+ res->callback_param1 = 0;
+ res->callback_param2 = 0;
+ res->last_value = 0;
+ res->trigger = 0;
+ res->reseed = 0;
+}
+
+uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, TileIndex tile)
+{
+ ResolverObject object;
+ const SpriteGroup *group;
+
+ NewIndustryResolver(&object, industry->type, tile, industry);
+ object.callback = callback;
+ object.callback_param1 = param1;
+ object.callback_param2 = param2;
+
+ group = Resolve(GetIndustrySpec(industry->type)->grf_prop.spritegroup, &object);
+ if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
+
+ return group->g.callback.result;
+}
diff --git a/src/newgrf_industries.h b/src/newgrf_industries.h
new file mode 100644
index 000000000..9a31692cc
--- /dev/null
+++ b/src/newgrf_industries.h
@@ -0,0 +1,14 @@
+/* $Id$ */
+
+/** @file newgrf_industries.h */
+
+#ifndef NEWGRF_INDUSTRIES_H
+#define NEWGRF_INDUSTRIES_H
+
+#include "industry.h"
+#include "newgrf_spritegroup.h"
+
+uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available);
+uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, TileIndex tile);
+
+#endif /* NEWGRF_INDUSTRIES_H */
diff --git a/src/newgrf_spritegroup.h b/src/newgrf_spritegroup.h
index 83cead28a..2f15dbb31 100644
--- a/src/newgrf_spritegroup.h
+++ b/src/newgrf_spritegroup.h
@@ -6,6 +6,7 @@
#define NEWGRF_SPRITEGROUP_H
#include "town.h"
+#include "industry.h"
struct SpriteGroup;
@@ -197,6 +198,10 @@ struct ResolverObject {
HouseID house_id;
} house;
struct {
+ TileIndex tile;
+ Industry *ind;
+ } industry;
+ struct {
const struct CargoSpec *cs;
} cargo;
} u;