00001
00002
00003
00004
00005
00006
00007
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 const ObjectSpec *ObjectSpec::Get(ObjectType index)
00039 {
00040 assert(index < NUM_OBJECTS);
00041 return &_object_specs[index];
00042 }
00043
00044 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
00066 MemSetT(_object_specs, 0, lengthof(_object_specs));
00067
00068
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 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00078 {
00079
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) {
00127 return spec->grf_prop.local_id;
00128 }
00129
00130 return 0xFFFE;
00131 }
00132
00140 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index)
00141 {
00142 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
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);
00179 uint32 idx;
00180
00181
00182 switch (grf_id) {
00183 case 0:
00184 idx = local_id;
00185 break;
00186
00187 case 0xFFFFFFFF:
00188 grf_id = grfid;
00189
00190
00191 default:
00192 idx = _object_mngr.GetID(local_id, grf_id);
00193 break;
00194 }
00195
00196
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
00210 return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town);
00211 }
00212
00213
00214
00215 const Town *t = NULL;
00216
00217 if (o == NULL) {
00218 switch (variable) {
00219
00220 case 0x41:
00221 case 0x60:
00222 case 0x61:
00223 case 0x62:
00224 case 0x64:
00225 break;
00226
00227
00228 case 0x45:
00229 case 0x46:
00230 if (!IsValidTile(tile)) goto unhandled;
00231 t = ClosestTownFromTile(tile, UINT_MAX);
00232 break;
00233
00234
00235 case 0x42: return _date;
00236
00237
00238 case 0x44: return _current_company;
00239
00240
00241 case 0x48: return object->u.object.view;
00242
00243
00244
00245
00246
00247
00248
00249
00250 default:
00251 goto unhandled;
00252 }
00253
00254
00255 if (!IsValidTile(tile)) goto unhandled;
00256 } else {
00257 t = o->town;
00258 }
00259
00260 switch (variable) {
00261
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
00270 case 0x41: return GetTileSlope(tile, NULL) << 8 | GetTerrainType(tile);
00271
00272
00273 case 0x42: return o->build_date;
00274
00275
00276 case 0x43: return GetAnimationFrame(tile);
00277
00278
00279 case 0x44: return GetTileOwner(tile);
00280
00281
00282 case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00283
00284
00285 case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00286
00287
00288 case 0x47: return o->colour;
00289
00290
00291 case 0x48: return o->view;
00292
00293
00294 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00295
00296
00297 case 0x61:
00298 tile = GetNearbyTile(parameter, tile);
00299 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00300
00301
00302 case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index);
00303
00304
00305 case 0x63:
00306 tile = GetNearbyTile(parameter, tile);
00307 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00308
00309
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
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
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
00411
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
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
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 }