# newgrf_object.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_object.cpp 26388 2014-03-03 20:02:31Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "company_base.h"
    00014 #include "company_func.h"
    00015 #include "debug.h"
    00016 #include "genworld.h"
    00017 #include "newgrf_class_func.h"
    00018 #include "newgrf_object.h"
    00019 #include "newgrf_sound.h"
    00020 #include "object_base.h"
    00021 #include "object_map.h"
    00022 #include "tile_cmd.h"
    00023 #include "town.h"
    00024 #include "water.h"
    00025 #include "newgrf_animation_base.h"
    00026
    00028 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
    00029
    00030 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
    00032 ObjectSpec _object_specs[NUM_OBJECTS];
    00033
    00039 /* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
    00040 {
    00041   assert(index < NUM_OBJECTS);
    00042   return &_object_specs[index];
    00043 }
    00044
    00050 /* static */ const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
    00051 {
    00052   return ObjectSpec::Get(GetObjectType(tile));
    00053 }
    00054
    00059 bool ObjectSpec::IsEverAvailable() const
    00060 {
    00061   return this->enabled && HasBit(this->climate, _settings_game.game_creation.landscape) &&
    00062       (this->flags & ((_game_mode != GM_EDITOR && !_generating_world) ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
    00063 }
    00064
    00069 bool ObjectSpec::WasEverAvailable() const
    00070 {
    00071   return this->IsEverAvailable() && _date > this->introduction_date;
    00072 }
    00073
    00078 bool ObjectSpec::IsAvailable() const
    00079 {
    00080   return this->WasEverAvailable() &&
    00081       (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365);
    00082 }
    00083
    00088 uint ObjectSpec::Index() const
    00089 {
    00090   return this - _object_specs;
    00091 }
    00092
    00094 void ResetObjects()
    00095 {
    00096   /* Clean the pool. */
    00097   MemSetT(_object_specs, 0, lengthof(_object_specs));
    00098
    00099   /* And add our originals. */
    00100   MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
    00101
    00102   for (uint16 i = 0; i < lengthof(_original_objects); i++) {
    00103     _object_specs[i].grf_prop.local_id = i;
    00104   }
    00105 }
    00106
    00107 template <typename Tspec, typename Tid, Tid Tmax>
    00108 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
    00109 {
    00110   ObjectClassID cls = ObjectClass::Allocate('LTHS');
    00111   ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
    00112   _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
    00113   ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
    00114
    00115   cls = ObjectClass::Allocate('TRNS');
    00116   ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
    00117   _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
    00118   ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
    00119 }
    00120
    00121 template <typename Tspec, typename Tid, Tid Tmax>
    00122 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
    00123 {
    00124   return this->GetSpec(index)->IsEverAvailable();
    00125 }
    00126
    00127 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
    00128
    00129
    00136 ObjectScopeResolver::ObjectScopeResolver(ResolverObject &ro, Object *obj, TileIndex tile, uint8 view)
    00137     : ScopeResolver(ro)
    00138 {
    00139   this->obj = obj;
    00140   this->tile = tile;
    00141   this->view = view;
    00142 }
    00143
    00144 /* virtual */ uint32 ObjectScopeResolver::GetRandomBits() const
    00145 {
    00146   return IsValidTile(this->tile) && IsTileType(this->tile, MP_OBJECT) ? GetObjectRandomBits(this->tile) : 0;
    00147 }
    00148
    00155 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
    00156 {
    00157   if (!IsTileType(tile, MP_OBJECT)) {
    00158     return 0xFFFF;
    00159   }
    00160
    00161   const Object *o = Object::GetByTile(tile);
    00162   const ObjectSpec *spec = ObjectSpec::Get(o->type);
    00163
    00164   /* Default objects have no associated NewGRF file */
    00165   if (spec->grf_prop.grffile == NULL) {
    00166     return 0xFFFE; // Defined in another grf file
    00167   }
    00168
    00169   if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
    00170     return spec->grf_prop.local_id | o->view << 16;
    00171   }
    00172
    00173   return 0xFFFE; // Defined in another grf file
    00174 }
    00175
    00184 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
    00185 {
    00186   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
    00187   bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
    00188
    00189   return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
    00190 }
    00191
    00199 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
    00200 {
    00201   uint32 best_dist = UINT32_MAX;
    00202   const Object *o;
    00203   FOR_ALL_OBJECTS(o) {
    00204     if (o->type != type || o == current) continue;
    00205
    00206     best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
    00207   }
    00208
    00209   return best_dist;
    00210 }
    00211
    00220 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
    00221 {
    00222   uint32 grf_id = GetRegister(0x100);  // Get the GRFID of the definition to look for in register 100h
    00223   uint32 idx;
    00224
    00225   /* Determine what will be the object type to look for */
    00226   switch (grf_id) {
    00227     case 0:  // this is a default object type
    00228       idx = local_id;
    00229       break;
    00230
    00231     case 0xFFFFFFFF: // current grf
    00232       grf_id = grfid;
    00233       /* FALL THROUGH */
    00234
    00235     default: // use the grfid specified in register 100h
    00236       idx = _object_mngr.GetID(local_id, grf_id);
    00237       break;
    00238   }
    00239
    00240   /* If the object type is invalid, there is none and the closest is far away. */
    00241   if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
    00242
    00243   return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
    00244 }
    00245
    00247 /* virtual */ uint32 ObjectScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
    00248 {
    00249   /* We get the town from the object, or we calculate the closest
    00250    * town if we need to when there's no object. */
    00251   const Town *t = NULL;
    00252
    00253   if (this->obj == NULL) {
    00254     switch (variable) {
    00255       /* Allow these when there's no object. */
    00256       case 0x41:
    00257       case 0x60:
    00258       case 0x61:
    00259       case 0x62:
    00260       case 0x64:
    00261         break;
    00262
    00263       /* Allow these, but find the closest town. */
    00264       case 0x45:
    00265       case 0x46:
    00266         if (!IsValidTile(this->tile)) goto unhandled;
    00267         t = ClosestTownFromTile(this->tile, UINT_MAX);
    00268         break;
    00269
    00270       /* Construction date */
    00271       case 0x42: return _date;
    00272
    00273       /* Object founder information */
    00274       case 0x44: return _current_company;
    00275
    00276       /* Object view */
    00277       case 0x48: return this->view;
    00278
    00279       /*
    00280        * Disallow the rest:
    00281        * 0x40: Relative position is passed as parameter during construction.
    00282        * 0x43: Animation counter is only for actual tiles.
    00283        * 0x47: Object colour is only valid when its built.
    00284        * 0x63: Animation counter of nearby tile, see above.
    00285        */
    00286       default:
    00287         goto unhandled;
    00288     }
    00289
    00290     /* If there's an invalid tile, then we don't have enough information at all. */
    00291     if (!IsValidTile(this->tile)) goto unhandled;
    00292   } else {
    00293     t = this->obj->town;
    00294   }
    00295
    00296   switch (variable) {
    00297     /* Relative position. */
    00298     case 0x40: {
    00299       uint offset = this->tile - this->obj->location.tile;
    00300       uint offset_x = TileX(offset);
    00301       uint offset_y = TileY(offset);
    00302       return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
    00303     }
    00304
    00305     /* Tile information. */
    00306     case 0x41: return GetTileSlope(this->tile) << 8 | GetTerrainType(this->tile);
    00307
    00308     /* Construction date */
    00309     case 0x42: return this->obj->build_date;
    00310
    00311     /* Animation counter */
    00312     case 0x43: return GetAnimationFrame(this->tile);
    00313
    00314     /* Object founder information */
    00315     case 0x44: return GetTileOwner(this->tile);
    00316
    00317     /* Get town zone and Manhattan distance of closest town */
    00318     case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceManhattan(this->tile, t->xy), 0xFFFF);
    00319
    00320     /* Get square of Euclidian distance of closes town */
    00321     case 0x46: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceSquare(this->tile, t->xy), 0xFFFF);
    00322
    00323     /* Object colour */
    00324     case 0x47: return this->obj->colour;
    00325
    00326     /* Object view */
    00327     case 0x48: return this->obj->view;
    00328
    00329     /* Get object ID at offset param */
    00330     case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, this->tile), this->ro.grffile->grfid);
    00331
    00332     /* Get random tile bits at offset param */
    00333     case 0x61: {
    00334       TileIndex tile = GetNearbyTile(parameter, this->tile);
    00335       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetObjectRandomBits(tile) : 0;
    00336     }
    00337
    00338     /* Land info of nearby tiles */
    00339     case 0x62: return GetNearbyObjectTileInformation(parameter, this->tile, this->obj == NULL ? INVALID_OBJECT : this->obj->index, this->ro.grffile->grf_version >= 8);
    00340
    00341     /* Animation counter of nearby tile */
    00342     case 0x63: {
    00343       TileIndex tile = GetNearbyTile(parameter, this->tile);
    00344       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetAnimationFrame(tile) : 0;
    00345     }
    00346
    00347     /* Count of object, distance of closest instance */
    00348     case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, this->ro.grffile->grfid, this->tile, this->obj);
    00349   }
    00350
    00351 unhandled:
    00352   DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
    00353
    00354   *available = false;
    00355   return UINT_MAX;
    00356 }
    00357
    00367 ObjectResolverObject::ObjectResolverObject(const ObjectSpec *spec, Object *obj, TileIndex tile, uint8 view,
    00368     CallbackID callback, uint32 param1, uint32 param2)
    00369   : ResolverObject(spec->grf_prop.grffile, callback, param1, param2), object_scope(*this, obj, tile, view)
    00370 {
    00371   this->town_scope = NULL;
    00372   this->root_spritegroup = (obj == NULL && spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT] != NULL) ?
    00373       spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT] : spec->grf_prop.spritegroup[0];
    00374 }
    00375
    00376 ObjectResolverObject::~ObjectResolverObject()
    00377 {
    00378   delete this->town_scope;
    00379 }
    00380
    00386 TownScopeResolver *ObjectResolverObject::GetTown()
    00387 {
    00388   if (this->town_scope == NULL) {
    00389     Town *t;
    00390     if (this->object_scope.obj != NULL) {
    00391       t = this->object_scope.obj->town;
    00392     } else {
    00393       t = ClosestTownFromTile(this->object_scope.tile, UINT_MAX);
    00394     }
    00395     if (t == NULL) return NULL;
    00396     this->town_scope = new TownScopeResolver(*this, t, this->object_scope.obj == NULL);
    00397   }
    00398   return this->town_scope;
    00399 }
    00400
    00412 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
    00413 {
    00414   ObjectResolverObject object(spec, o, tile, view, callback, param1, param2);
    00415   return object.ResolveCallback();
    00416 }
    00417
    00424 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
    00425 {
    00426   const DrawTileSprites *dts = group->ProcessRegisters(NULL);
    00427   PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
    00428
    00429   SpriteID image = dts->ground.sprite;
    00430   PaletteID pal  = dts->ground.pal;
    00431
    00432   if (GB(image, 0, SPRITE_WIDTH) != 0) {
    00433     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
    00434      * Do not do this if the tile's WaterClass is 'land'. */
    00435     if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
    00436       DrawWaterClassGround(ti);
    00437     } else {
    00438       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
    00439     }
    00440   }
    00441
    00442   DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
    00443 }
    00444
    00450 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
    00451 {
    00452   Object *o = Object::GetByTile(ti->tile);
    00453   ObjectResolverObject object(spec, o, ti->tile);
    00454
    00455   const SpriteGroup *group = object.Resolve();
    00456   if (group == NULL || group->type != SGT_TILELAYOUT) return;
    00457
    00458   DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
    00459 }
    00460
    00468 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
    00469 {
    00470   ObjectResolverObject object(spec, NULL, INVALID_TILE, view);
    00471   const SpriteGroup *group = object.Resolve();
    00472   if (group == NULL || group->type != SGT_TILELAYOUT) return;
    00473
    00474   const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
    00475
    00476   PaletteID palette;
    00477   if (Company::IsValidID(_local_company)) {
    00478     /* Get the colours of our company! */
    00479     if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
    00480       const Livery *l = Company::Get(_local_company)->livery;
    00481       palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
    00482     } else {
    00483       palette = COMPANY_SPRITE_COLOUR(_local_company);
    00484     }
    00485   } else {
    00486     /* There's no company, so just take the base palette. */
    00487     palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
    00488   }
    00489
    00490   SpriteID image = dts->ground.sprite;
    00491   PaletteID pal  = dts->ground.pal;
    00492
    00493   if (GB(image, 0, SPRITE_WIDTH) != 0) {
    00494     DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
    00495   }
    00496
    00497   DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
    00498 }
    00499
    00511 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
    00512 {
    00513   return GetObjectCallback(callback, param1, param2, spec, o, tile);
    00514 }
    00515
    00517 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
    00518   static const CallbackID cb_animation_speed      = CBID_OBJECT_ANIMATION_SPEED;
    00519   static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
    00520
    00521   static const ObjectCallbackMask cbm_animation_speed      = CBM_OBJ_ANIMATION_SPEED;
    00522   static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
    00523 };
    00524
    00529 void AnimateNewObjectTile(TileIndex tile)
    00530 {
    00531   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
    00532   if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
    00533
    00534   ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
    00535 }
    00536
    00544 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
    00545 {
    00546   if (!HasBit(spec->animation.triggers, trigger)) return;
    00547
    00548   ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
    00549 }
    00550
    00557 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
    00558 {
    00559   if (!HasBit(spec->animation.triggers, trigger)) return;
    00560
    00561   TILE_AREA_LOOP(tile, o->location) {
    00562     TriggerObjectTileAnimation(o, tile, trigger, spec);
    00563   }
    00564 }