newgrf_object.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_object.cpp 21890 2011-01-22 14:52:20Z 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   if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
00144     return spec->grf_prop.local_id;
00145   }
00146 
00147   return 0xFFFE; // Defined in another grf file
00148 }
00149 
00157 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index)
00158 {
00159   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00160   bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00161 
00162   return GetNearbyTileInformation(tile) | (is_same_object ? 1 : 0) << 8;
00163 }
00164 
00172 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00173 {
00174   uint32 best_dist = UINT32_MAX;
00175   const Object *o;
00176   FOR_ALL_OBJECTS(o) {
00177     if (GetObjectType(o->location.tile) != type || o == current) continue;
00178 
00179     best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00180   }
00181 
00182   return best_dist;
00183 }
00184 
00193 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00194 {
00195   uint32 grf_id = GetRegister(0x100);  // Get the GRFID of the definition to look for in register 100h
00196   uint32 idx;
00197 
00198   /* Determine what will be the object type to look for */
00199   switch (grf_id) {
00200     case 0:  // this is a default object type
00201       idx = local_id;
00202       break;
00203 
00204     case 0xFFFFFFFF: // current grf
00205       grf_id = grfid;
00206       /* FALL THROUGH */
00207 
00208     default: // use the grfid specified in register 100h
00209       idx = _object_mngr.GetID(local_id, grf_id);
00210       break;
00211   }
00212 
00213   /* If the object type is invalid, there is none and the closest is far away. */
00214   if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00215 
00216   return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00217 }
00218 
00220 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00221 {
00222   const Object *o = object->u.object.o;
00223   TileIndex tile = object->u.object.tile;
00224 
00225   if (object->scope == VSG_SCOPE_PARENT) {
00226     /* Pass the request on to the town of the object */
00227     return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town);
00228   }
00229 
00230   /* We get the town from the object, or we calculate the closest
00231    * town if we need to when there's no object. */
00232   const Town *t = NULL;
00233 
00234   if (o == NULL) {
00235     switch (variable) {
00236       /* Allow these when there's no object. */
00237       case 0x41:
00238       case 0x60:
00239       case 0x61:
00240       case 0x62:
00241       case 0x64:
00242         break;
00243 
00244       /* Allow these, but find the closest town. */
00245       case 0x45:
00246       case 0x46:
00247         if (!IsValidTile(tile)) goto unhandled;
00248         t = ClosestTownFromTile(tile, UINT_MAX);
00249         break;
00250 
00251       /* Construction date */
00252       case 0x42: return _date;
00253 
00254       /* Object founder information */
00255       case 0x44: return _current_company;
00256 
00257       /* Object view */
00258       case 0x48: return object->u.object.view;
00259 
00260       /*
00261        * Disallow the rest:
00262        * 0x40: Relative position is passed as parameter during construction.
00263        * 0x43: Animation counter is only for actual tiles.
00264        * 0x47: Object colour is only valid when its built.
00265        * 0x63: Animation counter of nearby tile, see above.
00266        */
00267       default:
00268         goto unhandled;
00269     }
00270 
00271     /* If there's an invalid tile, then we don't have enough information at all. */
00272     if (!IsValidTile(tile)) goto unhandled;
00273   } else {
00274     t = o->town;
00275   }
00276 
00277   switch (variable) {
00278     /* Relative position. */
00279     case 0x40: {
00280       uint offset = tile - o->location.tile;
00281       uint offset_x = TileX(offset);
00282       uint offset_y = TileY(offset);
00283       return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00284     }
00285 
00286     /* Tile information. */
00287     case 0x41: return GetTileSlope(tile, NULL) << 8 | GetTerrainType(tile);
00288 
00289     /* Construction date */
00290     case 0x42: return o->build_date;
00291 
00292     /* Animation counter */
00293     case 0x43: return GetAnimationFrame(tile);
00294 
00295     /* Object founder information */
00296     case 0x44: return GetTileOwner(tile);
00297 
00298     /* Get town zone and Manhattan distance of closest town */
00299     case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00300 
00301     /* Get square of Euclidian distance of closes town */
00302     case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00303 
00304     /* Object colour */
00305     case 0x47: return o->colour;
00306 
00307     /* Object view */
00308     case 0x48: return o->view;
00309 
00310     /* Get object ID at offset param */
00311     case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00312 
00313     /* Get random tile bits at offset param */
00314     case 0x61:
00315       tile = GetNearbyTile(parameter, tile);
00316       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00317 
00318     /* Land info of nearby tiles */
00319     case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index);
00320 
00321     /* Animation counter of nearby tile */
00322     case 0x63:
00323       tile = GetNearbyTile(parameter, tile);
00324       return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00325 
00326     /* Count of object, distance of closest instance */
00327     case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
00328   }
00329 
00330 unhandled:
00331   DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00332 
00333   *available = false;
00334   return UINT_MAX;
00335 }
00336 
00337 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00338 {
00339   /* Objects do not have 'real' groups */
00340   return NULL;
00341 }
00342 
00349 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00350 {
00351   const SpriteGroup *group = NULL;
00352 
00353   if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00354   if (group != NULL) return group;
00355 
00356   /* Fall back to the default set if the selected cargo type is not defined */
00357   return spec->grf_prop.spritegroup[0];
00358 
00359 }
00360 
00364 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, const Object *o, TileIndex tile, uint8 view = 0)
00365 {
00366   res->GetRandomBits = ObjectGetRandomBits;
00367   res->GetTriggers   = ObjectGetTriggers;
00368   res->SetTriggers   = ObjectSetTriggers;
00369   res->GetVariable   = ObjectGetVariable;
00370   res->ResolveReal   = ObjectResolveReal;
00371 
00372   res->u.object.o    = o;
00373   res->u.object.tile = tile;
00374   res->u.object.view = view;
00375 
00376   res->callback        = CBID_NO_CALLBACK;
00377   res->callback_param1 = 0;
00378   res->callback_param2 = 0;
00379   res->last_value      = 0;
00380   res->trigger         = 0;
00381   res->reseed          = 0;
00382   res->count           = 0;
00383 
00384   res->grffile = spec->grf_prop.grffile;
00385 }
00386 
00398 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, const Object *o, TileIndex tile, uint8 view)
00399 {
00400   ResolverObject object;
00401   NewObjectResolver(&object, spec, o, tile, view);
00402   object.callback = callback;
00403   object.callback_param1 = param1;
00404   object.callback_param2 = param2;
00405 
00406   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00407   if (group == NULL) return CALLBACK_FAILED;
00408 
00409   return group->GetCallbackResult();
00410 }
00411 
00418 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00419 {
00420   const DrawTileSprites *dts = group->dts;
00421   PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00422 
00423   SpriteID image = dts->ground.sprite;
00424   PaletteID pal  = dts->ground.pal;
00425 
00426   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00427     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00428      * Do not do this if the tile's WaterClass is 'land'. */
00429     if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00430       DrawWaterClassGround(ti);
00431     } else {
00432       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00433     }
00434   }
00435 
00436   DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00437 }
00438 
00444 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00445 {
00446   ResolverObject object;
00447   const Object *o = Object::GetByTile(ti->tile);
00448   NewObjectResolver(&object, spec, o, ti->tile);
00449 
00450   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00451   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00452 
00453   DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00454 }
00455 
00463 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00464 {
00465   ResolverObject object;
00466   NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
00467 
00468   const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
00469   if (group == NULL || group->type != SGT_TILELAYOUT) return;
00470 
00471   const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->dts;
00472 
00473   PaletteID palette;
00474   if (Company::IsValidID(_local_company)) {
00475     /* Get the colours of our company! */
00476     if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00477       const Livery *l = Company::Get(_local_company)->livery;
00478       palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00479     } else {
00480       palette = COMPANY_SPRITE_COLOUR(_local_company);
00481     }
00482   } else {
00483     /* There's no company, so just take the base palette. */
00484     palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00485   }
00486 
00487   SpriteID image = dts->ground.sprite;
00488   PaletteID pal  = dts->ground.pal;
00489 
00490   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00491     DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00492   }
00493 
00494   DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00495 }
00496 
00507 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, const Object *o, TileIndex tile)
00508 {
00509   return GetObjectCallback(callback, param1, param2, spec, o, tile);
00510 }
00511 
00513 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, StubGetObjectCallback> {
00514   static const CallbackID cb_animation_speed      = CBID_OBJECT_ANIMATION_SPEED;
00515   static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00516 
00517   static const ObjectCallbackMask cbm_animation_speed      = CBM_OBJ_ANIMATION_SPEED;
00518   static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00519 };
00520 
00525 void AnimateNewObjectTile(TileIndex tile)
00526 {
00527   const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00528   if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00529 
00530   ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00531 }
00532 
00540 void TriggerObjectTileAnimation(const Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00541 {
00542   if (!HasBit(spec->animation.triggers, trigger)) return;
00543 
00544   ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00545 }
00546 
00553 void TriggerObjectAnimation(const Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00554 {
00555   if (!HasBit(spec->animation.triggers, trigger)) return;
00556 
00557   TILE_AREA_LOOP(tile, o->location) {
00558     TriggerObjectTileAnimation(o, tile, trigger, spec);
00559   }
00560 }
00561 
00567 void GetObjectResolver(ResolverObject *ro, uint index)
00568 {
00569   NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);
00570 }

Generated on Sun May 15 19:20:11 2011 for OpenTTD by  doxygen 1.6.1