newgrf_object.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "newgrf_class_func.h"
00017 #include "newgrf_object.h"
00018 #include "newgrf_sound.h"
00019 #include "newgrf_spritegroup.h"
00020 #include "newgrf_town.h"
00021 #include "object_base.h"
00022 #include "object_map.h"
00023 #include "tile_cmd.h"
00024 #include "town.h"
00025 #include "water.h"
00026 #include "newgrf_animation_base.h"
00027 
00029 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
00030 
00031 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
00033 ObjectSpec _object_specs[NUM_OBJECTS];
00034 
00040 /* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
00041 {
00042   assert(index < NUM_OBJECTS);
00043   return &_object_specs[index];
00044 }
00045 
00051 /* static */ const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
00052 {
00053   return ObjectSpec::Get(GetObjectType(tile));
00054 }
00055 
00060 bool ObjectSpec::IsEverAvailable() const
00061 {
00062   return this->enabled && HasBit(this->climate, _settings_game.game_creation.landscape) &&
00063       (this->flags & (_game_mode != GM_EDITOR ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
00064 }
00065 
00070 bool ObjectSpec::IsAvailable() const
00071 {
00072   return this->IsEverAvailable() && _date > this->introduction_date &&
00073       (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365);
00074 }
00075 
00080 uint ObjectSpec::Index() const
00081 {
00082   return this - _object_specs;
00083 }
00084 
00086 void ResetObjects()
00087 {
00088   /* Clean the pool. */
00089   MemSetT(_object_specs, 0, lengthof(_object_specs));
00090 
00091   /* And add our originals. */
00092   MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
00093 
00094   for (uint16 i = 0; i < lengthof(_original_objects); i++) {
00095     _object_specs[i].grf_prop.local_id = i;
00096   }
00097 }
00098 
00099 template <typename Tspec, typename Tid, Tid Tmax>
00100 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00101 {
00102   ObjectClassID cls = ObjectClass::Allocate('LTHS');
00103   ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
00104   _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
00105   ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
00106 
00107   cls = ObjectClass::Allocate('TRNS');
00108   ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
00109   _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
00110   ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
00111 }
00112 
00113 template <typename Tspec, typename Tid, Tid Tmax>
00114 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
00115 {
00116   return this->GetSpec(index)->IsEverAvailable();
00117 }
00118 
00119 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
00120 
00121 
00122 static uint32 ObjectGetRandomBits(const ResolverObject *object)
00123 {
00124   TileIndex t = object->u.object.tile;
00125   return IsValidTile(t) && IsTileType(t, MP_OBJECT) ? GetObjectRandomBits(t) : 0;
00126 }
00127 
00128 static uint32 ObjectGetTriggers(const ResolverObject *object)
00129 {
00130   return 0;
00131 }
00132 
00133 static void ObjectSetTriggers(const ResolverObject *object, int triggers)
00134 {
00135 }
00136 
00137 
00144 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
00145 {
00146   if (!IsTileType(tile, MP_OBJECT)) {
00147     return 0xFFFF;
00148   }
00149 
00150   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00151 
00152   /* Default objects have no associated NewGRF file */
00153   if (spec->grf_prop.grffile == NULL) {
00154     return 0xFFFE; // Defined in another grf file
00155   }
00156 
00157   if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
00158     return spec->grf_prop.local_id;
00159   }
00160 
00161   return 0xFFFE; // Defined in another grf file
00162 }
00163 
00172 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
00173 {
00174   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00175   bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00176 
00177   return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
00178 }
00179 
00187 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00188 {
00189   uint32 best_dist = UINT32_MAX;
00190   const Object *o;
00191   FOR_ALL_OBJECTS(o) {
00192     if (GetObjectType(o->location.tile) != type || o == current) continue;
00193 
00194     best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00195   }
00196 
00197   return best_dist;
00198 }
00199 
00208 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00209 {
00210   uint32 grf_id = GetRegister(0x100);  // Get the GRFID of the definition to look for in register 100h
00211   uint32 idx;
00212 
00213   /* Determine what will be the object type to look for */
00214   switch (grf_id) {
00215     case 0:  // this is a default object type
00216       idx = local_id;
00217       break;
00218 
00219     case 0xFFFFFFFF: // current grf
00220       grf_id = grfid;
00221       /* FALL THROUGH */
00222 
00223     default: // use the grfid specified in register 100h
00224       idx = _object_mngr.GetID(local_id, grf_id);
00225       break;
00226   }
00227 
00228   /* If the object type is invalid, there is none and the closest is far away. */
00229   if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00230 
00231   return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00232 }
00233 
00235 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00236 {
00237   const Object *o = object->u.object.o;
00238   TileIndex tile = object->u.object.tile;
00239 
00240   if (object->scope == VSG_SCOPE_PARENT) {
00241     /* Pass the request on to the town of the object */
00242     return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town, object->grffile);
00243   }
00244 
00245   /* We get the town from the object, or we calculate the closest
00246    * town if we need to when there's no object. */
00247   const Town *t = NULL;
00248 
00249   if (o == NULL) {
00250     switch (variable) {
00251       /* Allow these when there's no object. */
00252       case 0x41:
00253       case 0x60:
00254       case 0x61:
00255       case 0x62:
00256       case 0x64:
00257         break;
00258 
00259       /* Allow these, but find the closest town. */
00260       case 0x45:
00261       case 0x46:
00262         if (!IsValidTile(tile)) goto unhandled;
00263         t = ClosestTownFromTile(tile, UINT_MAX);
00264         break;
00265 
00266       /* Construction date */
00267       case 0x42: return _date;
00268 
00269       /* Object founder information */
00270       case 0x44: return _current_company;
00271 
00272       /* Object view */
00273       case 0x48: return object->u.object.view;
00274 
00275       /*
00276        * Disallow the rest:
00277        * 0x40: Relative position is passed as parameter during construction.
00278        * 0x43: Animation counter is only for actual tiles.
00279        * 0x47: Object colour is only valid when its built.
00280        * 0x63: Animation counter of nearby tile, see above.
00281        */
00282       default:
00283         goto unhandled;
00284     }
00285 
00286     /* If there's an invalid tile, then we don't have enough information at all. */
00287     if (!IsValidTile(tile)) goto unhandled;
00288   } else {
00289     t = o->town;
00290   }
00291 
00292   switch (variable) {
00293     /* Relative position. */
00294     case 0x40: {
00295       uint offset = tile - o->location.tile;
00296       uint offset_x = TileX(offset);
00297       uint offset_y = TileY(offset);
00298       return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00299     }
00300 
00301     /* Tile information. */
00302     case 0x41: return GetTileSlope(tile) << 8 | GetTerrainType(tile);
00303 
00304     /* Construction date */
00305     case 0x42: return o->build_date;
00306 
00307     /* Animation counter */
00308     case 0x43: return GetAnimationFrame(tile);
00309 
00310     /* Object founder information */
00311     case 0x44: return GetTileOwner(tile);
00312 
00313     /* Get town zone and Manhattan distance of closest town */
00314     case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00315 
00316     /* Get square of Euclidian distance of closes town */
00317     case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00318 
00319     /* Object colour */
00320     case 0x47: return o->colour;
00321 
00322     /* Object view */
00323     case 0x48: return o->view;
00324 
00325     /* Get object ID at offset param */
00326     case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00327 
00328     /* Get random tile bits at offset param */
00329     case 0x61:
00330       tile = GetNearbyTile(parameter, tile);
00331       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00332 
00333     /* Land info of nearby tiles */
00334     case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index, object->grffile->grf_version >= 8);
00335 
00336     /* Animation counter of nearby tile */
00337     case 0x63:
00338       tile = GetNearbyTile(parameter, tile);
00339       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00340 
00341     /* Count of object, distance of closest instance */
00342     case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
00343   }
00344 
00345 unhandled:
00346   DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00347 
00348   *available = false;
00349   return UINT_MAX;
00350 }
00351 
00352 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00353 {
00354   /* Objects do not have 'real' groups */
00355   return NULL;
00356 }
00357 
00364 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00365 {
00366   const SpriteGroup *group = NULL;
00367 
00368   if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00369   if (group != NULL) return group;
00370 
00371   /* Fall back to the default set if the selected cargo type is not defined */
00372   return spec->grf_prop.spritegroup[0];
00373 
00374 }
00375 
00382 void ObjectStorePSA(ResolverObject *object, uint pos, int32 value)
00383 {
00384   /* Objects have no persistent storage. */
00385   Object *o = object->u.object.o;
00386   if (object->scope != VSG_SCOPE_PARENT || o == NULL) return;
00387 
00388   /* Pass the request on to the town of the object */
00389   TownStorePSA(o->town, object->grffile, pos, value);
00390 }
00391 
00395 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0)
00396 {
00397   res->GetRandomBits = ObjectGetRandomBits;
00398   res->GetTriggers   = ObjectGetTriggers;
00399   res->SetTriggers   = ObjectSetTriggers;
00400   res->GetVariable   = ObjectGetVariable;
00401   res->ResolveReal   = ObjectResolveReal;
00402   res->StorePSA      = ObjectStorePSA;
00403 
00404   res->u.object.o    = o;
00405   res->u.object.tile = tile;
00406   res->u.object.view = view;
00407 
00408   res->callback        = CBID_NO_CALLBACK;
00409   res->callback_param1 = 0;
00410   res->callback_param2 = 0;
00411   res->ResetState();
00412 
00413   res->grffile = spec->grf_prop.grffile;
00414 }
00415 
00427 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
00428 {
00429   ResolverObject object;
00430   NewObjectResolver(&object, spec, o, tile, view);
00431   object.callback = callback;
00432   object.callback_param1 = param1;
00433   object.callback_param2 = param2;
00434 
00435   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00436   if (group == NULL) return CALLBACK_FAILED;
00437 
00438   return group->GetCallbackResult();
00439 }
00440 
00447 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00448 {
00449   const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00450   PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00451 
00452   SpriteID image = dts->ground.sprite;
00453   PaletteID pal  = dts->ground.pal;
00454 
00455   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00456     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00457      * Do not do this if the tile's WaterClass is 'land'. */
00458     if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00459       DrawWaterClassGround(ti);
00460     } else {
00461       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00462     }
00463   }
00464 
00465   DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00466 }
00467 
00473 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00474 {
00475   ResolverObject object;
00476   Object *o = Object::GetByTile(ti->tile);
00477   NewObjectResolver(&object, spec, o, ti->tile);
00478 
00479   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00480   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00481 
00482   DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00483 }
00484 
00492 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00493 {
00494   ResolverObject object;
00495   NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
00496 
00497   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
00498   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00499 
00500   const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
00501 
00502   PaletteID palette;
00503   if (Company::IsValidID(_local_company)) {
00504     /* Get the colours of our company! */
00505     if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00506       const Livery *l = Company::Get(_local_company)->livery;
00507       palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00508     } else {
00509       palette = COMPANY_SPRITE_COLOUR(_local_company);
00510     }
00511   } else {
00512     /* There's no company, so just take the base palette. */
00513     palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00514   }
00515 
00516   SpriteID image = dts->ground.sprite;
00517   PaletteID pal  = dts->ground.pal;
00518 
00519   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00520     DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00521   }
00522 
00523   DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00524 }
00525 
00537 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
00538 {
00539   return GetObjectCallback(callback, param1, param2, spec, o, tile);
00540 }
00541 
00543 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
00544   static const CallbackID cb_animation_speed      = CBID_OBJECT_ANIMATION_SPEED;
00545   static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00546 
00547   static const ObjectCallbackMask cbm_animation_speed      = CBM_OBJ_ANIMATION_SPEED;
00548   static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00549 };
00550 
00555 void AnimateNewObjectTile(TileIndex tile)
00556 {
00557   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00558   if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00559 
00560   ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00561 }
00562 
00570 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00571 {
00572   if (!HasBit(spec->animation.triggers, trigger)) return;
00573 
00574   ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00575 }
00576 
00583 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00584 {
00585   if (!HasBit(spec->animation.triggers, trigger)) return;
00586 
00587   TILE_AREA_LOOP(tile, o->location) {
00588     TriggerObjectTileAnimation(o, tile, trigger, spec);
00589   }
00590 }
00591 
00597 void GetObjectResolver(ResolverObject *ro, uint index)
00598 {
00599   NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);
00600 }