9628
|
1 |
/* $Id$ */
|
|
2 |
|
|
3 |
/** @file newgrf_industrytiles.cpp */
|
|
4 |
|
|
5 |
#include "stdafx.h"
|
|
6 |
#include "openttd.h"
|
|
7 |
#include "functions.h"
|
|
8 |
#include "variables.h"
|
|
9 |
#include "debug.h"
|
|
10 |
#include "viewport.h"
|
|
11 |
#include "landscape.h"
|
|
12 |
#include "newgrf.h"
|
|
13 |
#include "industry.h"
|
|
14 |
#include "newgrf_commons.h"
|
|
15 |
#include "newgrf_spritegroup.h"
|
|
16 |
#include "newgrf_callbacks.h"
|
|
17 |
#include "newgrf_industries.h"
|
|
18 |
#include "industry_map.h"
|
|
19 |
#include "clear_map.h"
|
|
20 |
#include "table/sprites.h"
|
|
21 |
#include "sprite.h"
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Based on newhouses equivalent, but adapted for newindustries
|
|
25 |
* @param parameter from callback. It's in fact a pair of coordinates
|
|
26 |
* @param tile TileIndex from which the callback was initiated
|
|
27 |
* @param index of the industry been queried for
|
|
28 |
* @return a construction of bits obeying the newgrf format
|
|
29 |
*/
|
|
30 |
static uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index)
|
|
31 |
{
|
|
32 |
byte tile_type;
|
|
33 |
bool is_same_industry;
|
|
34 |
|
|
35 |
tile = GetNearbyTile(parameter, tile);
|
|
36 |
is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
|
|
37 |
tile_type = GetTerrainType(tile) << 2 | (IsTileType(tile, MP_WATER) ? 1 : 0) << 1 | (is_same_industry ? 1 : 0);
|
|
38 |
|
|
39 |
return GetTileType(tile) << 24 | (TileHeight(tile) * 8) << 16 | tile_type << 8 | GetTileSlope(tile, NULL);
|
|
40 |
}
|
|
41 |
|
|
42 |
/** This is the position of the tile relative to the northernmost tile of the industry.
|
|
43 |
* Format: 00yxYYXX
|
|
44 |
* Variable Content
|
|
45 |
* x the x offset from the northernmost tile
|
|
46 |
* XX same, but stored in a byte instead of a nibble
|
|
47 |
* y the y offset from the northernmost tile
|
|
48 |
* YY same, but stored in a byte instead of a nibble
|
|
49 |
* @param tile TileIndex of the tile to evaluate
|
|
50 |
* @param ind_tile northernmost tile of the industry
|
|
51 |
*/
|
|
52 |
static uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
|
|
53 |
{
|
|
54 |
byte x = TileX(tile) - TileX(ind_tile);
|
|
55 |
byte y = TileY(tile) - TileY(ind_tile);
|
|
56 |
|
|
57 |
return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
|
|
58 |
}
|
|
59 |
|
|
60 |
static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
|
|
61 |
{
|
|
62 |
const Industry *inds = object->u.industry.ind;
|
|
63 |
TileIndex tile = object->u.industry.tile;
|
|
64 |
|
|
65 |
if (object->scope == VSG_SCOPE_PARENT) {
|
|
66 |
return IndustryGetVariable(object, variable, parameter, available);
|
|
67 |
}
|
|
68 |
|
|
69 |
switch (variable) {
|
|
70 |
/* Construction state of the tile: a value between 0 and 3 */
|
|
71 |
case 0x40 : return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
|
|
72 |
|
|
73 |
case 0x41 : return GetTerrainType(tile);
|
|
74 |
|
|
75 |
/* Current town zone of the tile in the nearest town */
|
|
76 |
case 0x42 : return GetTownRadiusGroup(ClosestTownFromTile(tile, (uint)-1), tile);
|
|
77 |
|
|
78 |
/* Relative position */
|
|
79 |
case 0x43 : return GetRelativePosition(tile, inds->xy);
|
|
80 |
|
|
81 |
/* Animation frame. Like house variable 46 but can contain anything 0..FF. */
|
|
82 |
case 0x44 : return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryAnimationState(tile) : 0;
|
|
83 |
|
|
84 |
/* Land info of nearby tiles */
|
|
85 |
case 0x60 : return GetNearbyIndustryTileInformation(parameter, tile, inds->index);
|
|
86 |
|
|
87 |
case 0x61 : {/* Animation stage of nearby tiles */
|
|
88 |
tile = GetNearbyTile(parameter, tile);
|
|
89 |
if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == inds) {
|
|
90 |
return GetIndustryAnimationState(tile);
|
|
91 |
}
|
|
92 |
return 0xFFFFFFFF;
|
|
93 |
}
|
|
94 |
|
|
95 |
/* Get industry tile ID at offset */
|
|
96 |
case 0x62 : return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), tile, inds);
|
|
97 |
}
|
|
98 |
|
|
99 |
return 0;
|
|
100 |
}
|
|
101 |
|
|
102 |
static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const SpriteGroup *group)
|
|
103 |
{
|
|
104 |
/* IndustryTile do not have 'real' groups. Or do they?? */
|
|
105 |
return NULL;
|
|
106 |
}
|
|
107 |
|
|
108 |
uint32 IndustryTileGetRandomBits(const ResolverObject *object)
|
|
109 |
{
|
|
110 |
const TileIndex tile = object->u.industry.tile;
|
|
111 |
return (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) ? 0 : GetIndustryRandomBits(tile);
|
|
112 |
}
|
|
113 |
|
|
114 |
uint32 IndustryTileGetTriggers(const ResolverObject *object)
|
|
115 |
{
|
|
116 |
const TileIndex tile = object->u.industry.tile;
|
|
117 |
return (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) ? 0 : GetIndustryTriggers(tile);
|
|
118 |
}
|
|
119 |
|
|
120 |
void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
|
|
121 |
{
|
|
122 |
const TileIndex tile = object->u.industry.tile;
|
|
123 |
if (IsTileType(tile, MP_INDUSTRY)) SetIndustryTriggers(tile, triggers);
|
|
124 |
}
|
|
125 |
|
|
126 |
static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
|
|
127 |
{
|
|
128 |
res->GetRandomBits = IndustryTileGetRandomBits;
|
|
129 |
res->GetTriggers = IndustryTileGetTriggers;
|
|
130 |
res->SetTriggers = IndustryTileSetTriggers;
|
|
131 |
res->GetVariable = IndustryTileGetVariable;
|
|
132 |
res->ResolveReal = IndustryTileResolveReal;
|
|
133 |
|
|
134 |
res->u.industry.tile = tile;
|
|
135 |
res->u.industry.ind = indus;
|
|
136 |
res->u.industry.gfx = gfx;
|
|
137 |
|
|
138 |
res->callback = 0;
|
|
139 |
res->callback_param1 = 0;
|
|
140 |
res->callback_param2 = 0;
|
|
141 |
res->last_value = 0;
|
|
142 |
res->trigger = 0;
|
|
143 |
res->reseed = 0;
|
|
144 |
}
|
|
145 |
|
9629
|
146 |
void IndustryDrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte rnd_color, byte stage, IndustryGfx gfx)
|
|
147 |
{
|
|
148 |
const DrawTileSprites *dts = group->g.layout.dts;
|
|
149 |
const DrawTileSeqStruct *dtss;
|
|
150 |
|
|
151 |
SpriteID image = dts->ground_sprite;
|
|
152 |
SpriteID pal = dts->ground_pal;
|
|
153 |
|
|
154 |
if (GB(image, 0, SPRITE_WIDTH) != 0) DrawGroundSprite(image, pal);
|
|
155 |
|
|
156 |
foreach_draw_tile_seq(dtss, dts->seq) {
|
|
157 |
if (GB(dtss->image, 0, SPRITE_WIDTH) == 0) continue;
|
|
158 |
|
|
159 |
image = dtss->image + stage;
|
|
160 |
pal = dtss->pal;
|
|
161 |
|
|
162 |
if (!HASBIT(image, SPRITE_MODIFIER_OPAQUE) && HASBIT(_transparent_opt, TO_INDUSTRIES)) {
|
|
163 |
SETBIT(image, PALETTE_MODIFIER_TRANSPARENT);
|
|
164 |
pal = PALETTE_TO_TRANSPARENT;
|
|
165 |
} else if (HASBIT(image, PALETTE_MODIFIER_COLOR)) {
|
|
166 |
pal = GENERAL_SPRITE_COLOR(rnd_color);
|
|
167 |
} else {
|
|
168 |
pal = PAL_NONE;
|
|
169 |
}
|
|
170 |
|
|
171 |
if ((byte)dtss->delta_z != 0x80) {
|
|
172 |
AddSortableSpriteToDraw(
|
|
173 |
image, pal,
|
|
174 |
ti->x + dtss->delta_x, ti->y + dtss->delta_y,
|
|
175 |
dtss->size_x, dtss->size_y,
|
|
176 |
dtss->size_z, ti->z + dtss->delta_z
|
|
177 |
);
|
|
178 |
} else {
|
|
179 |
AddChildSpriteScreen(image, pal, dtss->delta_x, dtss->delta_y);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
9628
|
184 |
uint16 GetIndustryTileCallback(uint16 callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
|
|
185 |
{
|
|
186 |
ResolverObject object;
|
|
187 |
const SpriteGroup *group;
|
|
188 |
|
|
189 |
NewIndustryTileResolver(&object, gfx_id, tile, industry);
|
|
190 |
object.callback = callback;
|
|
191 |
object.callback_param1 = param1;
|
|
192 |
object.callback_param2 = param2;
|
|
193 |
|
|
194 |
group = Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
|
|
195 |
if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
|
|
196 |
|
|
197 |
return group->g.callback.result;
|
|
198 |
}
|
9629
|
199 |
|
|
200 |
bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
|
|
201 |
{
|
|
202 |
const SpriteGroup *group;
|
|
203 |
ResolverObject object;
|
|
204 |
|
|
205 |
if (ti->tileh != SLOPE_FLAT) {
|
|
206 |
bool draw_old_one = true;
|
|
207 |
if (HASBIT(inds->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
|
|
208 |
/* Called to determine the type (if any) of foundation to draw for industry tile */
|
|
209 |
uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
|
|
210 |
draw_old_one = callback_res == 0 || callback_res == CALLBACK_FAILED;
|
|
211 |
}
|
|
212 |
|
|
213 |
if (draw_old_one) DrawFoundation(ti, ti->tileh);
|
|
214 |
}
|
|
215 |
|
|
216 |
NewIndustryTileResolver(&object, gfx, ti->tile, i);
|
|
217 |
|
|
218 |
group = Resolve(inds->grf_prop.spritegroup, &object);
|
|
219 |
if (group == NULL || group->type != SGT_TILELAYOUT) {
|
|
220 |
return false;
|
|
221 |
} else {
|
|
222 |
/* Limit the building stage to the number of stages supplied. */
|
|
223 |
byte stage = GetIndustryConstructionStage(ti->tile);
|
|
224 |
stage = clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
|
|
225 |
IndustryDrawTileLayout(ti, group, i->random_color, stage, gfx);
|
|
226 |
return true;
|
|
227 |
}
|
|
228 |
}
|