Remove SIGTYPE_LAST_NOPBS
[openttd/fttd.git] / src / newgrf_object.cpp
blobf52f7f23399ebf952715e2cdc0990c2a88c159af
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file newgrf_object.cpp Handling of object NewGRFs. */
12 #include "stdafx.h"
13 #include "company_base.h"
14 #include "company_func.h"
15 #include "debug.h"
16 #include "genworld.h"
17 #include "newgrf_class_func.h"
18 #include "newgrf_object.h"
19 #include "newgrf_sound.h"
20 #include "object_base.h"
21 #include "object.h"
22 #include "map/object.h"
23 #include "map/slope.h"
24 #include "tile_cmd.h"
25 #include "town.h"
26 #include "water.h"
27 #include "newgrf_animation_base.h"
29 /** The override manager for our objects. */
30 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
32 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
33 /** All the object specifications. */
34 ObjectSpec _object_specs[NUM_OBJECTS];
36 /**
37 * Get the specification associated with a specific ObjectType.
38 * @param index The object type to fetch.
39 * @return The specification.
41 /* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
43 assert(index < NUM_OBJECTS);
44 return &_object_specs[index];
47 /**
48 * Get the specification associated with a tile.
49 * @param tile The tile to fetch the data for.
50 * @return The specification.
52 /* static */ const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
54 return ObjectSpec::Get(GetObjectType(tile));
57 /**
58 * Check whether the object might be available at some point in this game with the current game mode.
59 * @return true if it might be available.
61 bool ObjectSpec::IsEverAvailable() const
63 return this->enabled && HasBit(this->climate, _settings_game.game_creation.landscape) &&
64 (this->flags & ((_game_mode != GM_EDITOR && !_generating_world) ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
67 /**
68 * Check whether the object was available at some point in the past or present in this game with the current game mode.
69 * @return true if it was ever or is available.
71 bool ObjectSpec::WasEverAvailable() const
73 return this->IsEverAvailable() && _date > this->introduction_date;
76 /**
77 * Check whether the object is available at this time.
78 * @return true if it is available.
80 bool ObjectSpec::IsAvailable() const
82 return this->WasEverAvailable() &&
83 (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365);
86 /**
87 * Gets the index of this spec.
88 * @return The index.
90 uint ObjectSpec::Index() const
92 return this - _object_specs;
95 /** This function initialize the spec arrays of objects. */
96 void ResetObjects()
98 /* Clean the pool. */
99 MemSetT(_object_specs, 0, lengthof(_object_specs));
101 /* And add our originals. */
102 MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
104 for (uint16 i = 0; i < lengthof(_original_objects); i++) {
105 _object_specs[i].grf_prop.local_id = i;
109 template <typename Tspec, typename Tid, Tid Tmax>
110 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
112 ObjectClassID cls = ObjectClass::Allocate('LTHS');
113 ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
114 _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
115 ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
117 cls = ObjectClass::Allocate('TRNS');
118 ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
119 _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
120 ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
123 template <typename Tspec, typename Tid, Tid Tmax>
124 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
126 return this->GetSpec(index)->IsEverAvailable();
129 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
132 * Constructor of an object scope resolver.
133 * @param ro Surrounding resolver.
134 * @param obj Object being resolved.
135 * @param tile %Tile of the object.
136 * @param view View of the object.
138 ObjectScopeResolver::ObjectScopeResolver(ResolverObject &ro, Object *obj, TileIndex tile, uint8 view)
139 : ScopeResolver(ro)
141 this->obj = obj;
142 this->tile = tile;
143 this->view = view;
146 /* virtual */ uint32 ObjectScopeResolver::GetRandomBits() const
148 return IsValidTile(this->tile) && IsObjectTile(this->tile) ? GetObjectRandomBits(this->tile) : 0;
152 * Make an analysis of a tile and get the object type.
153 * @param tile TileIndex of the tile to query
154 * @param cur_grfid GRFID of the current callback chain
155 * @return value encoded as per NFO specs
157 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
159 if (!IsObjectTile(tile)) {
160 return 0xFFFF;
163 const Object *o = Object::GetByTile(tile);
164 const ObjectSpec *spec = ObjectSpec::Get(o->type);
166 /* Default objects have no associated NewGRF file */
167 if (spec->grf_prop.grffile == NULL) {
168 return 0xFFFE; // Defined in another grf file
171 if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
172 return spec->grf_prop.local_id | o->view << 16;
175 return 0xFFFE; // Defined in another grf file
179 * Based on newhouses equivalent, but adapted for newobjects
180 * @param parameter from callback. It's in fact a pair of coordinates
181 * @param tile TileIndex from which the callback was initiated
182 * @param index of the object been queried for
183 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
184 * @return a construction of bits obeying the newgrf format
186 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
188 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
189 bool is_same_object = (IsObjectTile(tile) && GetObjectIndex(tile) == index);
191 return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
195 * Get the closest object of a given type.
196 * @param tile The tile to start searching from.
197 * @param type The type of the object to search for.
198 * @param current The current object (to ignore).
199 * @return The distance to the closest object.
201 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
203 uint32 best_dist = UINT32_MAX;
204 const Object *o;
205 FOR_ALL_OBJECTS(o) {
206 if (o->type != type || o == current) continue;
208 best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
211 return best_dist;
215 * Implementation of var 65
216 * @param local_id Parameter given to the callback, which is the set id, or the local id, in our terminology.
217 * @param grfid The object's GRFID.
218 * @param tile The tile to look from.
219 * @param current Object for which the inquiry is made
220 * @return The formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
222 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
224 uint32 grf_id = GetRegister(0x100); // Get the GRFID of the definition to look for in register 100h
225 uint32 idx;
227 /* Determine what will be the object type to look for */
228 switch (grf_id) {
229 case 0: // this is a default object type
230 idx = local_id;
231 break;
233 case 0xFFFFFFFF: // current grf
234 grf_id = grfid;
235 /* FALL THROUGH */
237 default: // use the grfid specified in register 100h
238 idx = _object_mngr.GetID(local_id, grf_id);
239 break;
242 /* If the object type is invalid, there is none and the closest is far away. */
243 if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
245 return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
248 /** Used by the resolver to get values for feature 0F deterministic spritegroups. */
249 /* virtual */ uint32 ObjectScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
251 /* We get the town from the object, or we calculate the closest
252 * town if we need to when there's no object. */
253 const Town *t = NULL;
255 if (this->obj == NULL) {
256 switch (variable) {
257 /* Allow these when there's no object. */
258 case 0x41:
259 case 0x60:
260 case 0x61:
261 case 0x62:
262 case 0x64:
263 break;
265 /* Allow these, but find the closest town. */
266 case 0x45:
267 case 0x46:
268 if (!IsValidTile(this->tile)) goto unhandled;
269 t = ClosestTownFromTile(this->tile);
270 break;
272 /* Construction date */
273 case 0x42: return _date;
275 /* Object founder information */
276 case 0x44: return _current_company;
278 /* Object view */
279 case 0x48: return this->view;
282 * Disallow the rest:
283 * 0x40: Relative position is passed as parameter during construction.
284 * 0x43: Animation counter is only for actual tiles.
285 * 0x47: Object colour is only valid when its built.
286 * 0x63: Animation counter of nearby tile, see above.
288 default:
289 goto unhandled;
292 /* If there's an invalid tile, then we don't have enough information at all. */
293 if (!IsValidTile(this->tile)) goto unhandled;
294 } else {
295 t = this->obj->town;
298 switch (variable) {
299 /* Relative position. */
300 case 0x40: {
301 uint offset = this->tile - this->obj->location.tile;
302 uint offset_x = TileX(offset);
303 uint offset_y = TileY(offset);
304 return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
307 /* Tile information. */
308 case 0x41: return GetTileSlope(this->tile) << 8 | GetTerrainType(this->tile);
310 /* Construction date */
311 case 0x42: return this->obj->build_date;
313 /* Animation counter */
314 case 0x43: return GetAnimationFrame(this->tile);
316 /* Object founder information */
317 case 0x44: return GetTileOwner(this->tile);
319 /* Get town zone and Manhattan distance of closest town */
320 case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceManhattan(this->tile, t->xy), 0xFFFF);
322 /* Get square of Euclidian distance of closes town */
323 case 0x46: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceSquare(this->tile, t->xy), 0xFFFF);
325 /* Object colour */
326 case 0x47: return this->obj->colour;
328 /* Object view */
329 case 0x48: return this->obj->view;
331 /* Get object ID at offset param */
332 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, this->tile), this->ro.grffile->grfid);
334 /* Get random tile bits at offset param */
335 case 0x61: {
336 TileIndex tile = GetNearbyTile(parameter, this->tile);
337 return (IsObjectTile(tile) && Object::GetByTile(tile) == this->obj) ? GetObjectRandomBits(tile) : 0;
340 /* Land info of nearby tiles */
341 case 0x62: return GetNearbyObjectTileInformation(parameter, this->tile, this->obj == NULL ? INVALID_OBJECT : this->obj->index, this->ro.grffile->grf_version >= 8);
343 /* Animation counter of nearby tile */
344 case 0x63: {
345 TileIndex tile = GetNearbyTile(parameter, this->tile);
346 return (IsObjectTile(tile) && Object::GetByTile(tile) == this->obj) ? GetAnimationFrame(tile) : 0;
349 /* Count of object, distance of closest instance */
350 case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, this->ro.grffile->grfid, this->tile, this->obj);
353 unhandled:
354 DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
356 *available = false;
357 return UINT_MAX;
361 * Get the object's sprite group.
362 * @param spec The specification to get the sprite group from.
363 * @param o The object to get he sprite group for.
364 * @return The resolved sprite group.
366 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
368 const SpriteGroup *group = NULL;
370 if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
371 if (group != NULL) return group;
373 /* Fall back to the default set if the selected cargo type is not defined */
374 return spec->grf_prop.spritegroup[0];
379 * Constructor of the object resolver.
380 * @param obj Object being resolved.
381 * @param tile %Tile of the object.
382 * @param view View of the object.
383 * @param callback Callback ID.
384 * @param callback_param1 First parameter (var 10) of the callback.
385 * @param callback_param2 Second parameter (var 18) of the callback.
387 ObjectResolverObject::ObjectResolverObject(const ObjectSpec *spec, Object *obj, TileIndex tile, uint8 view,
388 CallbackID callback, uint32 param1, uint32 param2)
389 : ResolverObject(spec->grf_prop.grffile, callback, param1, param2), object_scope(*this, obj, tile, view)
391 this->town_scope = NULL;
394 ObjectResolverObject::~ObjectResolverObject()
396 delete this->town_scope;
400 * Get the town resolver scope that belongs to this object resolver.
401 * On the first call, the town scope is created (if possible).
402 * @return Town scope, if available.
404 TownScopeResolver *ObjectResolverObject::GetTown()
406 if (this->town_scope == NULL) {
407 Town *t;
408 if (this->object_scope.obj != NULL) {
409 t = this->object_scope.obj->town;
410 } else {
411 t = ClosestTownFromTile(this->object_scope.tile);
413 if (t == NULL) return NULL;
414 this->town_scope = new TownScopeResolver(*this, t, this->object_scope.obj == NULL);
416 return this->town_scope;
420 * Perform a callback for an object.
421 * @param callback The callback to perform.
422 * @param param1 The first parameter to pass to the NewGRF.
423 * @param param2 The second parameter to pass to the NewGRF.
424 * @param spec The specification of the object / the entry point.
425 * @param o The object to call the callback for.
426 * @param tile The tile the callback is called for.
427 * @param view The view of the object (only used when o == NULL).
428 * @return The result of the callback.
430 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
432 ObjectResolverObject object(spec, o, tile, view, callback, param1, param2);
433 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), object);
434 if (group == NULL) return CALLBACK_FAILED;
436 return group->GetCallbackResult();
440 * Draw an group of sprites on the map.
441 * @param ti Information about the tile to draw on.
442 * @param group The group of sprites to draw.
443 * @param spec Object spec to draw.
445 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
447 const DrawTileSprites *dts = group->ProcessRegisters(NULL);
448 PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
450 SpriteID image = dts->ground.sprite;
451 PaletteID pal = dts->ground.pal;
453 if (GB(image, 0, SPRITE_WIDTH) != 0) {
454 /* If the ground sprite is the default flat water sprite, draw also canal/river borders
455 * Do not do this if the tile's WaterClass is 'land'. */
456 if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
457 DrawWaterClassGround(ti);
458 } else {
459 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
463 DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
467 * Draw an object on the map.
468 * @param ti Information about the tile to draw on.
469 * @param spec Object spec to draw.
471 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
473 Object *o = Object::GetByTile(ti->tile);
474 ObjectResolverObject object(spec, o, ti->tile);
476 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), object);
477 if (group == NULL || group->type != SGT_TILELAYOUT) return;
479 DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
483 * Draw representation of an object (tile) for GUI purposes.
484 * @param x Position x of image.
485 * @param y Position y of image.
486 * @param spec Object spec to draw.
487 * @param view The object's view.
489 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
491 ObjectResolverObject object(spec, NULL, INVALID_TILE, view);
492 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), object);
493 if (group == NULL || group->type != SGT_TILELAYOUT) return;
495 const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
497 PaletteID palette;
498 if (Company::IsValidID(_local_company)) {
499 /* Get the colours of our company! */
500 if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
501 const Livery *l = Company::Get(_local_company)->livery;
502 palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
503 } else {
504 palette = COMPANY_SPRITE_COLOUR(_local_company);
506 } else {
507 /* There's no company, so just take the base palette. */
508 palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
511 SpriteID image = dts->ground.sprite;
512 PaletteID pal = dts->ground.pal;
514 if (GB(image, 0, SPRITE_WIDTH) != 0) {
515 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
518 DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
522 * Perform a callback for an object.
523 * @param callback The callback to perform.
524 * @param param1 The first parameter to pass to the NewGRF.
525 * @param param2 The second parameter to pass to the NewGRF.
526 * @param spec The specification of the object / the entry point.
527 * @param o The object to call the callback for.
528 * @param tile The tile the callback is called for.
529 * @param extra_data Ignored.
530 * @return The result of the callback.
532 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
534 return GetObjectCallback(callback, param1, param2, spec, o, tile);
537 /** Helper class for animation control. */
538 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
539 static const CallbackID cb_animation_speed = CBID_OBJECT_ANIMATION_SPEED;
540 static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
542 static const ObjectCallbackMask cbm_animation_speed = CBM_OBJ_ANIMATION_SPEED;
543 static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
547 * Handle the animation of the object tile.
548 * @param tile The tile to animate.
550 void AnimateNewObjectTile(TileIndex tile)
552 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
553 if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
555 ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
559 * Trigger the update of animation on a single tile.
560 * @param o The object that got triggered.
561 * @param tile The location of the triggered tile.
562 * @param trigger The trigger that is triggered.
563 * @param spec The spec associated with the object.
565 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
567 if (!HasBit(spec->animation.triggers, trigger)) return;
569 ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
573 * Trigger the update of animation on a whole object.
574 * @param o The object that got triggered.
575 * @param trigger The trigger that is triggered.
576 * @param spec The spec associated with the object.
578 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
580 if (!HasBit(spec->animation.triggers, trigger)) return;
582 TILE_AREA_LOOP(tile, o->location) {
583 TriggerObjectTileAnimation(o, tile, trigger, spec);