newgrf_object.cpp

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