newgrf_object.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_object.cpp 21454 2010-12-10 21:32:52Z 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 "core/mem_func.hpp"
00016 #include "debug.h"
00017 #include "newgrf.h"
00018 #include "newgrf_class_func.h"
00019 #include "newgrf_object.h"
00020 #include "newgrf_sound.h"
00021 #include "newgrf_spritegroup.h"
00022 #include "newgrf_town.h"
00023 #include "object_base.h"
00024 #include "object_map.h"
00025 #include "sprite.h"
00026 #include "town.h"
00027 #include "viewport_func.h"
00028 #include "water.h"
00029 #include "newgrf_animation_base.h"
00030 
00032 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
00033 
00034 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
00036 ObjectSpec _object_specs[NUM_OBJECTS];
00037 
00038 /* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
00039 {
00040   assert(index < NUM_OBJECTS);
00041   return &_object_specs[index];
00042 }
00043 
00044 /* static */ const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
00045 {
00046   return ObjectSpec::Get(GetObjectType(tile));
00047 }
00048 
00049 bool ObjectSpec::IsAvailable() const
00050 {
00051   return this->enabled && _date > this->introduction_date &&
00052       (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365) &&
00053       HasBit(this->climate, _settings_game.game_creation.landscape) &&
00054       (flags & (_game_mode != GM_EDITOR ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
00055 }
00056 
00057 uint ObjectSpec::Index() const
00058 {
00059   return this - _object_specs;
00060 }
00061 
00063 void ResetObjects()
00064 {
00065   /* Clean the pool. */
00066   MemSetT(_object_specs, 0, lengthof(_object_specs));
00067 
00068   /* And add our originals. */
00069   MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
00070 
00071   for (uint16 i = 0; i < lengthof(_original_objects); i++) {
00072     _object_specs[i].grf_prop.local_id = i;
00073   }
00074 }
00075 
00076 template <typename Tspec, typename Tid, Tid Tmax>
00077 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00078 {
00079   /* We only add the transmitters in the scenario editor. */
00080   if (_game_mode != GM_EDITOR) return;
00081 
00082   ObjectClassID cls = ObjectClass::Allocate('LTHS');
00083   ObjectClass::SetName(cls, STR_OBJECT_CLASS_LTHS);
00084   _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
00085   ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
00086 
00087   cls = ObjectClass::Allocate('TRNS');
00088   ObjectClass::SetName(cls, STR_OBJECT_CLASS_TRNS);
00089   _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
00090   ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
00091 }
00092 
00093 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
00094 
00095 
00096 static uint32 ObjectGetRandomBits(const ResolverObject *object)
00097 {
00098   TileIndex t = object->u.object.tile;
00099   return IsValidTile(t) && IsTileType(t, MP_OBJECT) ? GetObjectRandomBits(t) : 0;
00100 }
00101 
00102 static uint32 ObjectGetTriggers(const ResolverObject *object)
00103 {
00104   return 0;
00105 }
00106 
00107 static void ObjectSetTriggers(const ResolverObject *object, int triggers)
00108 {
00109 }
00110 
00111 
00118 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
00119 {
00120   if (!IsTileType(tile, MP_OBJECT)) {
00121     return 0xFFFF;
00122   }
00123 
00124   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00125 
00126   if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
00127     return spec->grf_prop.local_id;
00128   }
00129 
00130   return 0xFFFE; // Defined in another grf file
00131 }
00132 
00140 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index)
00141 {
00142   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00143   bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00144 
00145   return GetNearbyTileInformation(tile) | (is_same_object ? 1 : 0) << 8;
00146 }
00147 
00155 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00156 {
00157   uint32 best_dist = UINT32_MAX;
00158   const Object *o;
00159   FOR_ALL_OBJECTS(o) {
00160     if (GetObjectType(o->location.tile) != type || o == current) continue;
00161 
00162     best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00163   }
00164 
00165   return best_dist;
00166 }
00167 
00176 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00177 {
00178   uint32 grf_id = GetRegister(0x100);  // Get the GRFID of the definition to look for in register 100h
00179   uint32 idx;
00180 
00181   /* Determine what will be the object type to look for */
00182   switch (grf_id) {
00183     case 0:  // this is a default object type
00184       idx = local_id;
00185       break;
00186 
00187     case 0xFFFFFFFF: // current grf
00188       grf_id = grfid;
00189       /* FALL THROUGH */
00190 
00191     default: // use the grfid specified in register 100h
00192       idx = _object_mngr.GetID(local_id, grf_id);
00193       break;
00194   }
00195 
00196   /* If the object type is invalid, there is none and the closest is far away. */
00197   if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00198 
00199   return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00200 }
00201 
00203 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00204 {
00205   const Object *o = object->u.object.o;
00206   TileIndex tile = object->u.object.tile;
00207 
00208   if (object->scope == VSG_SCOPE_PARENT) {
00209     /* Pass the request on to the town of the object */
00210     return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town);
00211   }
00212 
00213   /* We get the town from the object, or we calculate the closest
00214    * town if we need to when there's no object. */
00215   const Town *t = NULL;
00216 
00217   if (o == NULL) {
00218     switch (variable) {
00219       /* Allow these when there's no object. */
00220       case 0x41:
00221       case 0x60:
00222       case 0x61:
00223       case 0x62:
00224       case 0x64:
00225         break;
00226 
00227       /* Allow these, but find the closest town. */
00228       case 0x45:
00229       case 0x46:
00230         if (!IsValidTile(tile)) goto unhandled;
00231         t = ClosestTownFromTile(tile, UINT_MAX);
00232         break;
00233 
00234       /* Construction date */
00235       case 0x42: return _date;
00236 
00237       /* Object founder information */
00238       case 0x44: return _current_company;
00239 
00240       /* Object view */
00241       case 0x48: return object->u.object.view;
00242 
00243       /*
00244        * Disallow the rest:
00245        * 0x40: Relative position is passed as parameter during construction.
00246        * 0x43: Animation counter is only for actual tiles.
00247        * 0x47: Object colour is only valid when its built.
00248        * 0x63: Animation counter of nearby tile, see above.
00249        */
00250       default:
00251         goto unhandled;
00252     }
00253 
00254     /* If there's an invalid tile, then we don't have enough information at all. */
00255     if (!IsValidTile(tile)) goto unhandled;
00256   } else {
00257     t = o->town;
00258   }
00259 
00260   switch (variable) {
00261     /* Relative position. */
00262     case 0x40: {
00263       uint offset = tile - o->location.tile;
00264       uint offset_x = TileX(offset);
00265       uint offset_y = TileY(offset);
00266       return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00267     }
00268 
00269     /* Tile information. */
00270     case 0x41: return GetTileSlope(tile, NULL) << 8 | GetTerrainType(tile);
00271 
00272     /* Construction date */
00273     case 0x42: return o->build_date;
00274 
00275     /* Animation counter */
00276     case 0x43: return GetAnimationFrame(tile);
00277 
00278     /* Object founder information */
00279     case 0x44: return GetTileOwner(tile);
00280 
00281     /* Get town zone and Manhattan distance of closest town */
00282     case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00283 
00284     /* Get square of Euclidian distance of closes town */
00285     case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00286 
00287     /* Object colour */
00288     case 0x47: return o->colour;
00289 
00290     /* Object view */
00291     case 0x48: return o->view;
00292 
00293     /* Get object ID at offset param */
00294     case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00295 
00296     /* Get random tile bits at offset param */
00297     case 0x61:
00298       tile = GetNearbyTile(parameter, tile);
00299       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00300 
00301     /* Land info of nearby tiles */
00302     case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index);
00303 
00304     /* Animation counter of nearby tile */
00305     case 0x63:
00306       tile = GetNearbyTile(parameter, tile);
00307       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00308 
00309     /* Count of object, distance of closest instance */
00310     case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
00311   }
00312 
00313 unhandled:
00314   DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00315 
00316   *available = false;
00317   return UINT_MAX;
00318 }
00319 
00320 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00321 {
00322   /* Objects do not have 'real' groups */
00323   return NULL;
00324 }
00325 
00332 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00333 {
00334   const SpriteGroup *group = NULL;
00335 
00336   if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00337   if (group != NULL) return group;
00338 
00339   /* Fall back to the default set if the selected cargo type is not defined */
00340   return spec->grf_prop.spritegroup[0];
00341 
00342 }
00343 
00347 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, const Object *o, TileIndex tile, uint8 view = 0)
00348 {
00349   res->GetRandomBits = ObjectGetRandomBits;
00350   res->GetTriggers   = ObjectGetTriggers;
00351   res->SetTriggers   = ObjectSetTriggers;
00352   res->GetVariable   = ObjectGetVariable;
00353   res->ResolveReal   = ObjectResolveReal;
00354 
00355   res->u.object.o    = o;
00356   res->u.object.tile = tile;
00357   res->u.object.view = view;
00358 
00359   res->callback        = CBID_NO_CALLBACK;
00360   res->callback_param1 = 0;
00361   res->callback_param2 = 0;
00362   res->last_value      = 0;
00363   res->trigger         = 0;
00364   res->reseed          = 0;
00365   res->count           = 0;
00366 
00367   res->grffile = spec->grf_prop.grffile;
00368 }
00369 
00381 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, const Object *o, TileIndex tile, uint8 view)
00382 {
00383   ResolverObject object;
00384   NewObjectResolver(&object, spec, o, tile, view);
00385   object.callback = callback;
00386   object.callback_param1 = param1;
00387   object.callback_param2 = param2;
00388 
00389   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00390   if (group == NULL) return CALLBACK_FAILED;
00391 
00392   return group->GetCallbackResult();
00393 }
00394 
00401 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00402 {
00403   const DrawTileSprites *dts = group->dts;
00404   PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00405 
00406   SpriteID image = dts->ground.sprite;
00407   PaletteID pal  = dts->ground.pal;
00408 
00409   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00410     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00411      * Do not do this if the tile's WaterClass is 'land'. */
00412     if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00413       DrawWaterClassGround(ti);
00414     } else {
00415       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00416     }
00417   }
00418 
00419   DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00420 }
00421 
00427 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00428 {
00429   ResolverObject object;
00430   const Object *o = Object::GetByTile(ti->tile);
00431   NewObjectResolver(&object, spec, o, ti->tile);
00432 
00433   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00434   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00435 
00436   DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00437 }
00438 
00446 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00447 {
00448   ResolverObject object;
00449   NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
00450 
00451   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
00452   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00453 
00454   const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->dts;
00455 
00456   PaletteID palette;
00457   if (Company::IsValidID(_local_company)) {
00458     /* Get the colours of our company! */
00459     if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00460       const Livery *l = Company::Get(_local_company)->livery;
00461       palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00462     } else {
00463       palette = COMPANY_SPRITE_COLOUR(_local_company);
00464     }
00465   } else {
00466     /* There's no company, so just take the base palette. */
00467     palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00468   }
00469 
00470   SpriteID image = dts->ground.sprite;
00471   PaletteID pal  = dts->ground.pal;
00472 
00473   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00474     DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00475   }
00476 
00477   DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00478 }
00479 
00490 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, const Object *o, TileIndex tile)
00491 {
00492   return GetObjectCallback(callback, param1, param2, spec, o, tile);
00493 }
00494 
00496 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, StubGetObjectCallback> {
00497   static const CallbackID cb_animation_speed      = CBID_OBJECT_ANIMATION_SPEED;
00498   static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00499 
00500   static const ObjectCallbackMask cbm_animation_speed      = CBM_OBJ_ANIMATION_SPEED;
00501   static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00502 };
00503 
00508 void AnimateNewObjectTile(TileIndex tile)
00509 {
00510   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00511   if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00512 
00513   ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00514 }
00515 
00523 void TriggerObjectTileAnimation(const Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00524 {
00525   if (!HasBit(spec->animation.triggers, trigger)) return;
00526 
00527   ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00528 }
00529 
00536 void TriggerObjectAnimation(const Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00537 {
00538   if (!HasBit(spec->animation.triggers, trigger)) return;
00539 
00540   TILE_AREA_LOOP(tile, o->location) {
00541     TriggerObjectTileAnimation(o, tile, trigger, spec);
00542   }
00543 }
00544 
00550 void GetObjectResolver(ResolverObject *ro, uint index)
00551 {
00552   NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);
00553 }

Generated on Sun Jan 9 16:01:58 2011 for OpenTTD by  doxygen 1.6.1