newgrf_object.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_object.cpp 26085 2013-11-24 14:41:19Z 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 ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00162 
00163   /* Default objects have no associated NewGRF file */
00164   if (spec->grf_prop.grffile == NULL) {
00165     return 0xFFFE; // Defined in another grf file
00166   }
00167 
00168   if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
00169     return spec->grf_prop.local_id;
00170   }
00171 
00172   return 0xFFFE; // Defined in another grf file
00173 }
00174 
00183 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
00184 {
00185   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00186   bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00187 
00188   return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
00189 }
00190 
00198 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00199 {
00200   uint32 best_dist = UINT32_MAX;
00201   const Object *o;
00202   FOR_ALL_OBJECTS(o) {
00203     if (o->type != type || o == current) continue;
00204 
00205     best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00206   }
00207 
00208   return best_dist;
00209 }
00210 
00219 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00220 {
00221   uint32 grf_id = GetRegister(0x100);  // Get the GRFID of the definition to look for in register 100h
00222   uint32 idx;
00223 
00224   /* Determine what will be the object type to look for */
00225   switch (grf_id) {
00226     case 0:  // this is a default object type
00227       idx = local_id;
00228       break;
00229 
00230     case 0xFFFFFFFF: // current grf
00231       grf_id = grfid;
00232       /* FALL THROUGH */
00233 
00234     default: // use the grfid specified in register 100h
00235       idx = _object_mngr.GetID(local_id, grf_id);
00236       break;
00237   }
00238 
00239   /* If the object type is invalid, there is none and the closest is far away. */
00240   if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00241 
00242   return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00243 }
00244 
00246 /* virtual */ uint32 ObjectScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00247 {
00248   /* We get the town from the object, or we calculate the closest
00249    * town if we need to when there's no object. */
00250   const Town *t = NULL;
00251 
00252   if (this->obj == NULL) {
00253     switch (variable) {
00254       /* Allow these when there's no object. */
00255       case 0x41:
00256       case 0x60:
00257       case 0x61:
00258       case 0x62:
00259       case 0x64:
00260         break;
00261 
00262       /* Allow these, but find the closest town. */
00263       case 0x45:
00264       case 0x46:
00265         if (!IsValidTile(this->tile)) goto unhandled;
00266         t = ClosestTownFromTile(this->tile, UINT_MAX);
00267         break;
00268 
00269       /* Construction date */
00270       case 0x42: return _date;
00271 
00272       /* Object founder information */
00273       case 0x44: return _current_company;
00274 
00275       /* Object view */
00276       case 0x48: return this->view;
00277 
00278       /*
00279        * Disallow the rest:
00280        * 0x40: Relative position is passed as parameter during construction.
00281        * 0x43: Animation counter is only for actual tiles.
00282        * 0x47: Object colour is only valid when its built.
00283        * 0x63: Animation counter of nearby tile, see above.
00284        */
00285       default:
00286         goto unhandled;
00287     }
00288 
00289     /* If there's an invalid tile, then we don't have enough information at all. */
00290     if (!IsValidTile(this->tile)) goto unhandled;
00291   } else {
00292     t = this->obj->town;
00293   }
00294 
00295   switch (variable) {
00296     /* Relative position. */
00297     case 0x40: {
00298       uint offset = this->tile - this->obj->location.tile;
00299       uint offset_x = TileX(offset);
00300       uint offset_y = TileY(offset);
00301       return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00302     }
00303 
00304     /* Tile information. */
00305     case 0x41: return GetTileSlope(this->tile) << 8 | GetTerrainType(this->tile);
00306 
00307     /* Construction date */
00308     case 0x42: return this->obj->build_date;
00309 
00310     /* Animation counter */
00311     case 0x43: return GetAnimationFrame(this->tile);
00312 
00313     /* Object founder information */
00314     case 0x44: return GetTileOwner(this->tile);
00315 
00316     /* Get town zone and Manhattan distance of closest town */
00317     case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceManhattan(this->tile, t->xy), 0xFFFF);
00318 
00319     /* Get square of Euclidian distance of closes town */
00320     case 0x46: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceSquare(this->tile, t->xy), 0xFFFF);
00321 
00322     /* Object colour */
00323     case 0x47: return this->obj->colour;
00324 
00325     /* Object view */
00326     case 0x48: return this->obj->view;
00327 
00328     /* Get object ID at offset param */
00329     case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, this->tile), this->ro.grffile->grfid);
00330 
00331     /* Get random tile bits at offset param */
00332     case 0x61: {
00333       TileIndex tile = GetNearbyTile(parameter, this->tile);
00334       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetObjectRandomBits(tile) : 0;
00335     }
00336 
00337     /* Land info of nearby tiles */
00338     case 0x62: return GetNearbyObjectTileInformation(parameter, this->tile, this->obj == NULL ? INVALID_OBJECT : this->obj->index, this->ro.grffile->grf_version >= 8);
00339 
00340     /* Animation counter of nearby tile */
00341     case 0x63: {
00342       TileIndex tile = GetNearbyTile(parameter, this->tile);
00343       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetAnimationFrame(tile) : 0;
00344     }
00345 
00346     /* Count of object, distance of closest instance */
00347     case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, this->ro.grffile->grfid, this->tile, this->obj);
00348   }
00349 
00350 unhandled:
00351   DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00352 
00353   *available = false;
00354   return UINT_MAX;
00355 }
00356 
00363 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00364 {
00365   const SpriteGroup *group = NULL;
00366 
00367   if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00368   if (group != NULL) return group;
00369 
00370   /* Fall back to the default set if the selected cargo type is not defined */
00371   return spec->grf_prop.spritegroup[0];
00372 
00373 }
00374 
00384 ObjectResolverObject::ObjectResolverObject(const ObjectSpec *spec, Object *obj, TileIndex tile, uint8 view,
00385     CallbackID callback, uint32 param1, uint32 param2)
00386   : ResolverObject(spec->grf_prop.grffile, callback, param1, param2), object_scope(*this, obj, tile, view)
00387 {
00388   this->town_scope = NULL;
00389 }
00390 
00391 ObjectResolverObject::~ObjectResolverObject()
00392 {
00393   delete this->town_scope;
00394 }
00395 
00401 TownScopeResolver *ObjectResolverObject::GetTown()
00402 {
00403   if (this->town_scope == NULL) {
00404     Town *t;
00405     if (this->object_scope.obj != NULL) {
00406       t = this->object_scope.obj->town;
00407     } else {
00408       t = ClosestTownFromTile(this->object_scope.tile, UINT_MAX);
00409     }
00410     if (t == NULL) return NULL;
00411     this->town_scope = new TownScopeResolver(*this, t, this->object_scope.obj == NULL);
00412   }
00413   return this->town_scope;
00414 }
00415 
00427 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
00428 {
00429   ObjectResolverObject object(spec, o, tile, view, callback, param1, param2);
00430   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), object);
00431   if (group == NULL) return CALLBACK_FAILED;
00432 
00433   return group->GetCallbackResult();
00434 }
00435 
00442 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00443 {
00444   const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00445   PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00446 
00447   SpriteID image = dts->ground.sprite;
00448   PaletteID pal  = dts->ground.pal;
00449 
00450   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00451     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00452      * Do not do this if the tile's WaterClass is 'land'. */
00453     if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00454       DrawWaterClassGround(ti);
00455     } else {
00456       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00457     }
00458   }
00459 
00460   DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00461 }
00462 
00468 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00469 {
00470   Object *o = Object::GetByTile(ti->tile);
00471   ObjectResolverObject object(spec, o, ti->tile);
00472 
00473   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), object);
00474   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00475 
00476   DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00477 }
00478 
00486 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00487 {
00488   ObjectResolverObject object(spec, NULL, INVALID_TILE, view);
00489   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), object);
00490   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00491 
00492   const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
00493 
00494   PaletteID palette;
00495   if (Company::IsValidID(_local_company)) {
00496     /* Get the colours of our company! */
00497     if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00498       const Livery *l = Company::Get(_local_company)->livery;
00499       palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00500     } else {
00501       palette = COMPANY_SPRITE_COLOUR(_local_company);
00502     }
00503   } else {
00504     /* There's no company, so just take the base palette. */
00505     palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00506   }
00507 
00508   SpriteID image = dts->ground.sprite;
00509   PaletteID pal  = dts->ground.pal;
00510 
00511   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00512     DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00513   }
00514 
00515   DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00516 }
00517 
00529 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
00530 {
00531   return GetObjectCallback(callback, param1, param2, spec, o, tile);
00532 }
00533 
00535 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
00536   static const CallbackID cb_animation_speed      = CBID_OBJECT_ANIMATION_SPEED;
00537   static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00538 
00539   static const ObjectCallbackMask cbm_animation_speed      = CBM_OBJ_ANIMATION_SPEED;
00540   static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00541 };
00542 
00547 void AnimateNewObjectTile(TileIndex tile)
00548 {
00549   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00550   if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00551 
00552   ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00553 }
00554 
00562 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00563 {
00564   if (!HasBit(spec->animation.triggers, trigger)) return;
00565 
00566   ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00567 }
00568 
00575 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00576 {
00577   if (!HasBit(spec->animation.triggers, trigger)) return;
00578 
00579   TILE_AREA_LOOP(tile, o->location) {
00580     TriggerObjectTileAnimation(o, tile, trigger, spec);
00581   }
00582 }