(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / newgrf_object.cpp
blob41735b7fad0caaa4fc1b1efbefb512f38251d9bf
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 "newgrf.h"
17 #include "newgrf_class_func.h"
18 #include "newgrf_object.h"
19 #include "newgrf_sound.h"
20 #include "newgrf_spritegroup.h"
21 #include "newgrf_town.h"
22 #include "object_base.h"
23 #include "object_map.h"
24 #include "sprite.h"
25 #include "town.h"
26 #include "viewport_func.h"
27 #include "water.h"
28 #include "newgrf_animation_base.h"
30 /** The override manager for our objects. */
31 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
33 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
34 /** All the object specifications. */
35 ObjectSpec _object_specs[NUM_OBJECTS];
37 /**
38 * Get the specification associated with a specific ObjectType.
39 * @param index The object type to fetch.
40 * @return The specification.
42 /* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
44 assert(index < NUM_OBJECTS);
45 return &_object_specs[index];
48 /**
49 * Get the specification associated with a tile.
50 * @param tile The tile to fetch the data for.
51 * @return The specification.
53 /* static */ const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
55 return ObjectSpec::Get(GetObjectType(tile));
58 /**
59 * Check whether the object is available at this time.
60 * @return true if it is available.
62 bool ObjectSpec::IsAvailable() const
64 return this->enabled && _date > this->introduction_date &&
65 (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365) &&
66 HasBit(this->climate, _settings_game.game_creation.landscape) &&
67 (flags & (_game_mode != GM_EDITOR ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
70 /**
71 * Gets the index of this spec.
72 * @return The index.
74 uint ObjectSpec::Index() const
76 return this - _object_specs;
79 /** This function initialize the spec arrays of objects. */
80 void ResetObjects()
82 /* Clean the pool. */
83 MemSetT(_object_specs, 0, lengthof(_object_specs));
85 /* And add our originals. */
86 MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
88 for (uint16 i = 0; i < lengthof(_original_objects); i++) {
89 _object_specs[i].grf_prop.local_id = i;
93 template <typename Tspec, typename Tid, Tid Tmax>
94 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
96 /* We only add the transmitters in the scenario editor. */
97 if (_game_mode != GM_EDITOR) return;
99 ObjectClassID cls = ObjectClass::Allocate('LTHS');
100 ObjectClass::SetName(cls, STR_OBJECT_CLASS_LTHS);
101 _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
102 ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
104 cls = ObjectClass::Allocate('TRNS');
105 ObjectClass::SetName(cls, STR_OBJECT_CLASS_TRNS);
106 _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
107 ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
110 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
113 static uint32 ObjectGetRandomBits(const ResolverObject *object)
115 TileIndex t = object->u.object.tile;
116 return IsValidTile(t) && IsTileType(t, MP_OBJECT) ? GetObjectRandomBits(t) : 0;
119 static uint32 ObjectGetTriggers(const ResolverObject *object)
121 return 0;
124 static void ObjectSetTriggers(const ResolverObject *object, int triggers)
130 * Make an analysis of a tile and get the object type.
131 * @param tile TileIndex of the tile to query
132 * @param cur_grfid GRFID of the current callback chain
133 * @return value encoded as per NFO specs
135 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
137 if (!IsTileType(tile, MP_OBJECT)) {
138 return 0xFFFF;
141 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
143 /* Default objects have no associated NewGRF file */
144 if (spec->grf_prop.grffile == NULL) {
145 return 0xFFFE; // Defined in another grf file
148 if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
149 return spec->grf_prop.local_id;
152 return 0xFFFE; // Defined in another grf file
156 * Based on newhouses equivalent, but adapted for newobjects
157 * @param parameter from callback. It's in fact a pair of coordinates
158 * @param tile TileIndex from which the callback was initiated
159 * @param index of the object been queried for
160 * @return a construction of bits obeying the newgrf format
162 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index)
164 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
165 bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
167 return GetNearbyTileInformation(tile) | (is_same_object ? 1 : 0) << 8;
171 * Get the closest object of a given type.
172 * @param tile The tile to start searching from.
173 * @param type The type of the object to search for.
174 * @param current The current object (to ignore).
175 * @return The distance to the closest object.
177 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
179 uint32 best_dist = UINT32_MAX;
180 const Object *o;
181 FOR_ALL_OBJECTS(o) {
182 if (GetObjectType(o->location.tile) != type || o == current) continue;
184 best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
187 return best_dist;
191 * Implementation of var 65
192 * @param local_id Parameter given to the callback, which is the set id, or the local id, in our terminology.
193 * @param grfid The object's GRFID.
194 * @param tile The tile to look from.
195 * @param current Object for which the inquiry is made
196 * @return The formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
198 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
200 uint32 grf_id = GetRegister(0x100); // Get the GRFID of the definition to look for in register 100h
201 uint32 idx;
203 /* Determine what will be the object type to look for */
204 switch (grf_id) {
205 case 0: // this is a default object type
206 idx = local_id;
207 break;
209 case 0xFFFFFFFF: // current grf
210 grf_id = grfid;
211 /* FALL THROUGH */
213 default: // use the grfid specified in register 100h
214 idx = _object_mngr.GetID(local_id, grf_id);
215 break;
218 /* If the object type is invalid, there is none and the closest is far away. */
219 if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
221 return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
224 /** Used by the resolver to get values for feature 0F deterministic spritegroups. */
225 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
227 const Object *o = object->u.object.o;
228 TileIndex tile = object->u.object.tile;
230 if (object->scope == VSG_SCOPE_PARENT) {
231 /* Pass the request on to the town of the object */
232 return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town, object->grffile);
235 /* We get the town from the object, or we calculate the closest
236 * town if we need to when there's no object. */
237 const Town *t = NULL;
239 if (o == NULL) {
240 switch (variable) {
241 /* Allow these when there's no object. */
242 case 0x41:
243 case 0x60:
244 case 0x61:
245 case 0x62:
246 case 0x64:
247 break;
249 /* Allow these, but find the closest town. */
250 case 0x45:
251 case 0x46:
252 if (!IsValidTile(tile)) goto unhandled;
253 t = ClosestTownFromTile(tile, UINT_MAX);
254 break;
256 /* Construction date */
257 case 0x42: return _date;
259 /* Object founder information */
260 case 0x44: return _current_company;
262 /* Object view */
263 case 0x48: return object->u.object.view;
266 * Disallow the rest:
267 * 0x40: Relative position is passed as parameter during construction.
268 * 0x43: Animation counter is only for actual tiles.
269 * 0x47: Object colour is only valid when its built.
270 * 0x63: Animation counter of nearby tile, see above.
272 default:
273 goto unhandled;
276 /* If there's an invalid tile, then we don't have enough information at all. */
277 if (!IsValidTile(tile)) goto unhandled;
278 } else {
279 t = o->town;
282 switch (variable) {
283 /* Relative position. */
284 case 0x40: {
285 uint offset = tile - o->location.tile;
286 uint offset_x = TileX(offset);
287 uint offset_y = TileY(offset);
288 return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
291 /* Tile information. */
292 case 0x41: return GetTileSlope(tile, NULL) << 8 | GetTerrainType(tile);
294 /* Construction date */
295 case 0x42: return o->build_date;
297 /* Animation counter */
298 case 0x43: return GetAnimationFrame(tile);
300 /* Object founder information */
301 case 0x44: return GetTileOwner(tile);
303 /* Get town zone and Manhattan distance of closest town */
304 case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
306 /* Get square of Euclidian distance of closes town */
307 case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
309 /* Object colour */
310 case 0x47: return o->colour;
312 /* Object view */
313 case 0x48: return o->view;
315 /* Get object ID at offset param */
316 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
318 /* Get random tile bits at offset param */
319 case 0x61:
320 tile = GetNearbyTile(parameter, tile);
321 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
323 /* Land info of nearby tiles */
324 case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index);
326 /* Animation counter of nearby tile */
327 case 0x63:
328 tile = GetNearbyTile(parameter, tile);
329 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
331 /* Count of object, distance of closest instance */
332 case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
335 unhandled:
336 DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
338 *available = false;
339 return UINT_MAX;
342 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
344 /* Objects do not have 'real' groups */
345 return NULL;
349 * Get the object's sprite group.
350 * @param spec The specification to get the sprite group from.
351 * @param o The object to get he sprite group for.
352 * @return The resolved sprite group.
354 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
356 const SpriteGroup *group = NULL;
358 if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
359 if (group != NULL) return group;
361 /* Fall back to the default set if the selected cargo type is not defined */
362 return spec->grf_prop.spritegroup[0];
367 * Store a value into the persistent storage of the object's parent.
368 * @param object Object that we want to query.
369 * @param pos Position in the persistent storage to use.
370 * @param value Value to store.
372 void ObjectStorePSA(ResolverObject *object, uint pos, int32 value)
374 /* Objects have no persistent storage. */
375 Object *o = object->u.object.o;
376 if (object->scope != VSG_SCOPE_PARENT || o == NULL) return;
378 /* Pass the request on to the town of the object */
379 TownStorePSA(o->town, object->grffile, pos, value);
383 * Returns a resolver object to be used with feature 0F spritegroups.
385 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0)
387 res->GetRandomBits = ObjectGetRandomBits;
388 res->GetTriggers = ObjectGetTriggers;
389 res->SetTriggers = ObjectSetTriggers;
390 res->GetVariable = ObjectGetVariable;
391 res->ResolveReal = ObjectResolveReal;
392 res->StorePSA = ObjectStorePSA;
394 res->u.object.o = o;
395 res->u.object.tile = tile;
396 res->u.object.view = view;
398 res->callback = CBID_NO_CALLBACK;
399 res->callback_param1 = 0;
400 res->callback_param2 = 0;
401 res->ResetState();
403 res->grffile = spec->grf_prop.grffile;
407 * Perform a callback for an object.
408 * @param callback The callback to perform.
409 * @param param1 The first parameter to pass to the NewGRF.
410 * @param param2 The second parameter to pass to the NewGRF.
411 * @param spec The specification of the object / the entry point.
412 * @param o The object to call the callback for.
413 * @param tile The tile the callback is called for.
414 * @param view The view of the object (only used when o == NULL).
415 * @return The result of the callback.
417 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
419 ResolverObject object;
420 NewObjectResolver(&object, spec, o, tile, view);
421 object.callback = callback;
422 object.callback_param1 = param1;
423 object.callback_param2 = param2;
425 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
426 if (group == NULL) return CALLBACK_FAILED;
428 return group->GetCallbackResult();
432 * Draw an group of sprites on the map.
433 * @param ti Information about the tile to draw on.
434 * @param group The group of sprites to draw.
435 * @param spec Object spec to draw.
437 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
439 const DrawTileSprites *dts = group->ProcessRegisters(NULL);
440 PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
442 SpriteID image = dts->ground.sprite;
443 PaletteID pal = dts->ground.pal;
445 if (GB(image, 0, SPRITE_WIDTH) != 0) {
446 /* If the ground sprite is the default flat water sprite, draw also canal/river borders
447 * Do not do this if the tile's WaterClass is 'land'. */
448 if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
449 DrawWaterClassGround(ti);
450 } else {
451 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
455 DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
459 * Draw an object on the map.
460 * @param ti Information about the tile to draw on.
461 * @param spec Object spec to draw.
463 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
465 ResolverObject object;
466 Object *o = Object::GetByTile(ti->tile);
467 NewObjectResolver(&object, spec, o, ti->tile);
469 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
470 if (group == NULL || group->type != SGT_TILELAYOUT) return;
472 DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
476 * Draw representation of an object (tile) for GUI purposes.
477 * @param x Position x of image.
478 * @param y Position y of image.
479 * @param spec Object spec to draw.
480 * @param view The object's view.
482 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
484 ResolverObject object;
485 NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
487 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
488 if (group == NULL || group->type != SGT_TILELAYOUT) return;
490 const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
492 PaletteID palette;
493 if (Company::IsValidID(_local_company)) {
494 /* Get the colours of our company! */
495 if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
496 const Livery *l = Company::Get(_local_company)->livery;
497 palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
498 } else {
499 palette = COMPANY_SPRITE_COLOUR(_local_company);
501 } else {
502 /* There's no company, so just take the base palette. */
503 palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
506 SpriteID image = dts->ground.sprite;
507 PaletteID pal = dts->ground.pal;
509 if (GB(image, 0, SPRITE_WIDTH) != 0) {
510 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
513 DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
517 * Perform a callback for an object.
518 * @param callback The callback to perform.
519 * @param param1 The first parameter to pass to the NewGRF.
520 * @param param2 The second parameter to pass to the NewGRF.
521 * @param spec The specification of the object / the entry point.
522 * @param o The object to call the callback for.
523 * @param tile The tile the callback is called for.
524 * @return The result of the callback.
526 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile)
528 return GetObjectCallback(callback, param1, param2, spec, o, tile);
531 /** Helper class for animation control. */
532 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, StubGetObjectCallback> {
533 static const CallbackID cb_animation_speed = CBID_OBJECT_ANIMATION_SPEED;
534 static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
536 static const ObjectCallbackMask cbm_animation_speed = CBM_OBJ_ANIMATION_SPEED;
537 static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
541 * Handle the animation of the object tile.
542 * @param tile The tile to animate.
544 void AnimateNewObjectTile(TileIndex tile)
546 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
547 if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
549 ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
553 * Trigger the update of animation on a single tile.
554 * @param o The object that got triggered.
555 * @param tile The location of the triggered tile.
556 * @param trigger The trigger that is triggered.
557 * @param spec The spec associated with the object.
559 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
561 if (!HasBit(spec->animation.triggers, trigger)) return;
563 ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
567 * Trigger the update of animation on a whole object.
568 * @param o The object that got triggered.
569 * @param trigger The trigger that is triggered.
570 * @param spec The spec associated with the object.
572 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
574 if (!HasBit(spec->animation.triggers, trigger)) return;
576 TILE_AREA_LOOP(tile, o->location) {
577 TriggerObjectTileAnimation(o, tile, trigger, spec);
582 * Resolve an object's spec and such so we can get a variable.
583 * @param ro The resolver object to fill.
584 * @param index The object tile to get the data from.
586 void GetObjectResolver(ResolverObject *ro, uint index)
588 NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);