newgrf_object.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_object.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
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::IsAvailable() const
00061 {
00062   return this->enabled && _date > this->introduction_date &&
00063       (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365) &&
00064       HasBit(this->climate, _settings_game.game_creation.landscape) &&
00065       (flags & (_game_mode != GM_EDITOR ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
00066 }
00067 
00072 uint ObjectSpec::Index() const
00073 {
00074   return this - _object_specs;
00075 }
00076 
00078 void ResetObjects()
00079 {
00080   /* Clean the pool. */
00081   MemSetT(_object_specs, 0, lengthof(_object_specs));
00082 
00083   /* And add our originals. */
00084   MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
00085 
00086   for (uint16 i = 0; i < lengthof(_original_objects); i++) {
00087     _object_specs[i].grf_prop.local_id = i;
00088   }
00089 }
00090 
00091 template <typename Tspec, typename Tid, Tid Tmax>
00092 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00093 {
00094   /* We only add the transmitters in the scenario editor. */
00095   if (_game_mode != GM_EDITOR) return;
00096 
00097   ObjectClassID cls = ObjectClass::Allocate('LTHS');
00098   ObjectClass::SetName(cls, STR_OBJECT_CLASS_LTHS);
00099   _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
00100   ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
00101 
00102   cls = ObjectClass::Allocate('TRNS');
00103   ObjectClass::SetName(cls, STR_OBJECT_CLASS_TRNS);
00104   _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
00105   ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
00106 }
00107 
00108 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
00109 
00110 
00111 static uint32 ObjectGetRandomBits(const ResolverObject *object)
00112 {
00113   TileIndex t = object->u.object.tile;
00114   return IsValidTile(t) && IsTileType(t, MP_OBJECT) ? GetObjectRandomBits(t) : 0;
00115 }
00116 
00117 static uint32 ObjectGetTriggers(const ResolverObject *object)
00118 {
00119   return 0;
00120 }
00121 
00122 static void ObjectSetTriggers(const ResolverObject *object, int triggers)
00123 {
00124 }
00125 
00126 
00133 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
00134 {
00135   if (!IsTileType(tile, MP_OBJECT)) {
00136     return 0xFFFF;
00137   }
00138 
00139   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00140 
00141   /* Default objects have no associated NewGRF file */
00142   if (spec->grf_prop.grffile == NULL) {
00143     return 0xFFFE; // Defined in another grf file
00144   }
00145 
00146   if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
00147     return spec->grf_prop.local_id;
00148   }
00149 
00150   return 0xFFFE; // Defined in another grf file
00151 }
00152 
00161 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
00162 {
00163   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00164   bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00165 
00166   return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
00167 }
00168 
00176 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00177 {
00178   uint32 best_dist = UINT32_MAX;
00179   const Object *o;
00180   FOR_ALL_OBJECTS(o) {
00181     if (GetObjectType(o->location.tile) != type || o == current) continue;
00182 
00183     best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00184   }
00185 
00186   return best_dist;
00187 }
00188 
00197 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00198 {
00199   uint32 grf_id = GetRegister(0x100);  // Get the GRFID of the definition to look for in register 100h
00200   uint32 idx;
00201 
00202   /* Determine what will be the object type to look for */
00203   switch (grf_id) {
00204     case 0:  // this is a default object type
00205       idx = local_id;
00206       break;
00207 
00208     case 0xFFFFFFFF: // current grf
00209       grf_id = grfid;
00210       /* FALL THROUGH */
00211 
00212     default: // use the grfid specified in register 100h
00213       idx = _object_mngr.GetID(local_id, grf_id);
00214       break;
00215   }
00216 
00217   /* If the object type is invalid, there is none and the closest is far away. */
00218   if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00219 
00220   return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00221 }
00222 
00224 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00225 {
00226   const Object *o = object->u.object.o;
00227   TileIndex tile = object->u.object.tile;
00228 
00229   if (object->scope == VSG_SCOPE_PARENT) {
00230     /* Pass the request on to the town of the object */
00231     return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town, object->grffile);
00232   }
00233 
00234   /* We get the town from the object, or we calculate the closest
00235    * town if we need to when there's no object. */
00236   const Town *t = NULL;
00237 
00238   if (o == NULL) {
00239     switch (variable) {
00240       /* Allow these when there's no object. */
00241       case 0x41:
00242       case 0x60:
00243       case 0x61:
00244       case 0x62:
00245       case 0x64:
00246         break;
00247 
00248       /* Allow these, but find the closest town. */
00249       case 0x45:
00250       case 0x46:
00251         if (!IsValidTile(tile)) goto unhandled;
00252         t = ClosestTownFromTile(tile, UINT_MAX);
00253         break;
00254 
00255       /* Construction date */
00256       case 0x42: return _date;
00257 
00258       /* Object founder information */
00259       case 0x44: return _current_company;
00260 
00261       /* Object view */
00262       case 0x48: return object->u.object.view;
00263 
00264       /*
00265        * Disallow the rest:
00266        * 0x40: Relative position is passed as parameter during construction.
00267        * 0x43: Animation counter is only for actual tiles.
00268        * 0x47: Object colour is only valid when its built.
00269        * 0x63: Animation counter of nearby tile, see above.
00270        */
00271       default:
00272         goto unhandled;
00273     }
00274 
00275     /* If there's an invalid tile, then we don't have enough information at all. */
00276     if (!IsValidTile(tile)) goto unhandled;
00277   } else {
00278     t = o->town;
00279   }
00280 
00281   switch (variable) {
00282     /* Relative position. */
00283     case 0x40: {
00284       uint offset = tile - o->location.tile;
00285       uint offset_x = TileX(offset);
00286       uint offset_y = TileY(offset);
00287       return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00288     }
00289 
00290     /* Tile information. */
00291     case 0x41: return GetTileSlope(tile) << 8 | GetTerrainType(tile);
00292 
00293     /* Construction date */
00294     case 0x42: return o->build_date;
00295 
00296     /* Animation counter */
00297     case 0x43: return GetAnimationFrame(tile);
00298 
00299     /* Object founder information */
00300     case 0x44: return GetTileOwner(tile);
00301 
00302     /* Get town zone and Manhattan distance of closest town */
00303     case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00304 
00305     /* Get square of Euclidian distance of closes town */
00306     case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00307 
00308     /* Object colour */
00309     case 0x47: return o->colour;
00310 
00311     /* Object view */
00312     case 0x48: return o->view;
00313 
00314     /* Get object ID at offset param */
00315     case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00316 
00317     /* Get random tile bits at offset param */
00318     case 0x61:
00319       tile = GetNearbyTile(parameter, tile);
00320       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00321 
00322     /* Land info of nearby tiles */
00323     case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index, object->grffile->grf_version >= 8);
00324 
00325     /* Animation counter of nearby tile */
00326     case 0x63:
00327       tile = GetNearbyTile(parameter, tile);
00328       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00329 
00330     /* Count of object, distance of closest instance */
00331     case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
00332   }
00333 
00334 unhandled:
00335   DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00336 
00337   *available = false;
00338   return UINT_MAX;
00339 }
00340 
00341 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00342 {
00343   /* Objects do not have 'real' groups */
00344   return NULL;
00345 }
00346 
00353 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00354 {
00355   const SpriteGroup *group = NULL;
00356 
00357   if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00358   if (group != NULL) return group;
00359 
00360   /* Fall back to the default set if the selected cargo type is not defined */
00361   return spec->grf_prop.spritegroup[0];
00362 
00363 }
00364 
00371 void ObjectStorePSA(ResolverObject *object, uint pos, int32 value)
00372 {
00373   /* Objects have no persistent storage. */
00374   Object *o = object->u.object.o;
00375   if (object->scope != VSG_SCOPE_PARENT || o == NULL) return;
00376 
00377   /* Pass the request on to the town of the object */
00378   TownStorePSA(o->town, object->grffile, pos, value);
00379 }
00380 
00384 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0)
00385 {
00386   res->GetRandomBits = ObjectGetRandomBits;
00387   res->GetTriggers   = ObjectGetTriggers;
00388   res->SetTriggers   = ObjectSetTriggers;
00389   res->GetVariable   = ObjectGetVariable;
00390   res->ResolveReal   = ObjectResolveReal;
00391   res->StorePSA      = ObjectStorePSA;
00392 
00393   res->u.object.o    = o;
00394   res->u.object.tile = tile;
00395   res->u.object.view = view;
00396 
00397   res->callback        = CBID_NO_CALLBACK;
00398   res->callback_param1 = 0;
00399   res->callback_param2 = 0;
00400   res->ResetState();
00401 
00402   res->grffile = spec->grf_prop.grffile;
00403 }
00404 
00416 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
00417 {
00418   ResolverObject object;
00419   NewObjectResolver(&object, spec, o, tile, view);
00420   object.callback = callback;
00421   object.callback_param1 = param1;
00422   object.callback_param2 = param2;
00423 
00424   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00425   if (group == NULL) return CALLBACK_FAILED;
00426 
00427   return group->GetCallbackResult();
00428 }
00429 
00436 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00437 {
00438   const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00439   PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00440 
00441   SpriteID image = dts->ground.sprite;
00442   PaletteID pal  = dts->ground.pal;
00443 
00444   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00445     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00446      * Do not do this if the tile's WaterClass is 'land'. */
00447     if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00448       DrawWaterClassGround(ti);
00449     } else {
00450       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00451     }
00452   }
00453 
00454   DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00455 }
00456 
00462 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00463 {
00464   ResolverObject object;
00465   Object *o = Object::GetByTile(ti->tile);
00466   NewObjectResolver(&object, spec, o, ti->tile);
00467 
00468   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00469   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00470 
00471   DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00472 }
00473 
00481 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00482 {
00483   ResolverObject object;
00484   NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
00485 
00486   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
00487   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00488 
00489   const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
00490 
00491   PaletteID palette;
00492   if (Company::IsValidID(_local_company)) {
00493     /* Get the colours of our company! */
00494     if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00495       const Livery *l = Company::Get(_local_company)->livery;
00496       palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00497     } else {
00498       palette = COMPANY_SPRITE_COLOUR(_local_company);
00499     }
00500   } else {
00501     /* There's no company, so just take the base palette. */
00502     palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00503   }
00504 
00505   SpriteID image = dts->ground.sprite;
00506   PaletteID pal  = dts->ground.pal;
00507 
00508   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00509     DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00510   }
00511 
00512   DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00513 }
00514 
00526 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
00527 {
00528   return GetObjectCallback(callback, param1, param2, spec, o, tile);
00529 }
00530 
00532 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
00533   static const CallbackID cb_animation_speed      = CBID_OBJECT_ANIMATION_SPEED;
00534   static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00535 
00536   static const ObjectCallbackMask cbm_animation_speed      = CBM_OBJ_ANIMATION_SPEED;
00537   static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00538 };
00539 
00544 void AnimateNewObjectTile(TileIndex tile)
00545 {
00546   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00547   if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00548 
00549   ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00550 }
00551 
00559 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00560 {
00561   if (!HasBit(spec->animation.triggers, trigger)) return;
00562 
00563   ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00564 }
00565 
00572 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00573 {
00574   if (!HasBit(spec->animation.triggers, trigger)) return;
00575 
00576   TILE_AREA_LOOP(tile, o->location) {
00577     TriggerObjectTileAnimation(o, tile, trigger, spec);
00578   }
00579 }
00580 
00586 void GetObjectResolver(ResolverObject *ro, uint index)
00587 {
00588   NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);
00589 }