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 "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 const ObjectSpec *ObjectSpec::Get(ObjectType index)
00043 {
00044 assert(index < NUM_OBJECTS);
00045 return &_object_specs[index];
00046 }
00047
00053 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
00083 MemSetT(_object_specs, 0, lengthof(_object_specs));
00084
00085
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 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00095 {
00096
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
00144 if (spec->grf_prop.grffile == NULL) {
00145 return 0xFFFE;
00146 }
00147
00148 if (spec->grf_prop.grffile->grfid == cur_grfid) {
00149 return spec->grf_prop.local_id;
00150 }
00151
00152 return 0xFFFE;
00153 }
00154
00163 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
00164 {
00165 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00166 bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00167
00168 return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
00169 }
00170
00178 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00179 {
00180 uint32 best_dist = UINT32_MAX;
00181 const Object *o;
00182 FOR_ALL_OBJECTS(o) {
00183 if (GetObjectType(o->location.tile) != type || o == current) continue;
00184
00185 best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00186 }
00187
00188 return best_dist;
00189 }
00190
00199 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00200 {
00201 uint32 grf_id = GetRegister(0x100);
00202 uint32 idx;
00203
00204
00205 switch (grf_id) {
00206 case 0:
00207 idx = local_id;
00208 break;
00209
00210 case 0xFFFFFFFF:
00211 grf_id = grfid;
00212
00213
00214 default:
00215 idx = _object_mngr.GetID(local_id, grf_id);
00216 break;
00217 }
00218
00219
00220 if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00221
00222 return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00223 }
00224
00226 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00227 {
00228 const Object *o = object->u.object.o;
00229 TileIndex tile = object->u.object.tile;
00230
00231 if (object->scope == VSG_SCOPE_PARENT) {
00232
00233 return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town, object->grffile);
00234 }
00235
00236
00237
00238 const Town *t = NULL;
00239
00240 if (o == NULL) {
00241 switch (variable) {
00242
00243 case 0x41:
00244 case 0x60:
00245 case 0x61:
00246 case 0x62:
00247 case 0x64:
00248 break;
00249
00250
00251 case 0x45:
00252 case 0x46:
00253 if (!IsValidTile(tile)) goto unhandled;
00254 t = ClosestTownFromTile(tile, UINT_MAX);
00255 break;
00256
00257
00258 case 0x42: return _date;
00259
00260
00261 case 0x44: return _current_company;
00262
00263
00264 case 0x48: return object->u.object.view;
00265
00266
00267
00268
00269
00270
00271
00272
00273 default:
00274 goto unhandled;
00275 }
00276
00277
00278 if (!IsValidTile(tile)) goto unhandled;
00279 } else {
00280 t = o->town;
00281 }
00282
00283 switch (variable) {
00284
00285 case 0x40: {
00286 uint offset = tile - o->location.tile;
00287 uint offset_x = TileX(offset);
00288 uint offset_y = TileY(offset);
00289 return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00290 }
00291
00292
00293 case 0x41: return GetTileSlope(tile) << 8 | GetTerrainType(tile);
00294
00295
00296 case 0x42: return o->build_date;
00297
00298
00299 case 0x43: return GetAnimationFrame(tile);
00300
00301
00302 case 0x44: return GetTileOwner(tile);
00303
00304
00305 case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00306
00307
00308 case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00309
00310
00311 case 0x47: return o->colour;
00312
00313
00314 case 0x48: return o->view;
00315
00316
00317 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00318
00319
00320 case 0x61:
00321 tile = GetNearbyTile(parameter, tile);
00322 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00323
00324
00325 case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index, object->grffile->grf_version >= 8);
00326
00327
00328 case 0x63:
00329 tile = GetNearbyTile(parameter, tile);
00330 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00331
00332
00333 case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
00334 }
00335
00336 unhandled:
00337 DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00338
00339 *available = false;
00340 return UINT_MAX;
00341 }
00342
00343 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00344 {
00345
00346 return NULL;
00347 }
00348
00355 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00356 {
00357 const SpriteGroup *group = NULL;
00358
00359 if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00360 if (group != NULL) return group;
00361
00362
00363 return spec->grf_prop.spritegroup[0];
00364
00365 }
00366
00373 void ObjectStorePSA(ResolverObject *object, uint pos, int32 value)
00374 {
00375
00376 Object *o = object->u.object.o;
00377 if (object->scope != VSG_SCOPE_PARENT || o == NULL) return;
00378
00379
00380 TownStorePSA(o->town, object->grffile, pos, value);
00381 }
00382
00386 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0)
00387 {
00388 res->GetRandomBits = ObjectGetRandomBits;
00389 res->GetTriggers = ObjectGetTriggers;
00390 res->SetTriggers = ObjectSetTriggers;
00391 res->GetVariable = ObjectGetVariable;
00392 res->ResolveReal = ObjectResolveReal;
00393 res->StorePSA = ObjectStorePSA;
00394
00395 res->u.object.o = o;
00396 res->u.object.tile = tile;
00397 res->u.object.view = view;
00398
00399 res->callback = CBID_NO_CALLBACK;
00400 res->callback_param1 = 0;
00401 res->callback_param2 = 0;
00402 res->ResetState();
00403
00404 res->grffile = spec->grf_prop.grffile;
00405 }
00406
00418 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
00419 {
00420 ResolverObject object;
00421 NewObjectResolver(&object, spec, o, tile, view);
00422 object.callback = callback;
00423 object.callback_param1 = param1;
00424 object.callback_param2 = param2;
00425
00426 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00427 if (group == NULL) return CALLBACK_FAILED;
00428
00429 return group->GetCallbackResult();
00430 }
00431
00438 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00439 {
00440 const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00441 PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00442
00443 SpriteID image = dts->ground.sprite;
00444 PaletteID pal = dts->ground.pal;
00445
00446 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00447
00448
00449 if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00450 DrawWaterClassGround(ti);
00451 } else {
00452 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00453 }
00454 }
00455
00456 DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00457 }
00458
00464 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00465 {
00466 ResolverObject object;
00467 Object *o = Object::GetByTile(ti->tile);
00468 NewObjectResolver(&object, spec, o, ti->tile);
00469
00470 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00471 if (group == NULL || group->type != SGT_TILELAYOUT) return;
00472
00473 DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00474 }
00475
00483 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00484 {
00485 ResolverObject object;
00486 NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
00487
00488 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
00489 if (group == NULL || group->type != SGT_TILELAYOUT) return;
00490
00491 const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
00492
00493 PaletteID palette;
00494 if (Company::IsValidID(_local_company)) {
00495
00496 if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00497 const Livery *l = Company::Get(_local_company)->livery;
00498 palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00499 } else {
00500 palette = COMPANY_SPRITE_COLOUR(_local_company);
00501 }
00502 } else {
00503
00504 palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00505 }
00506
00507 SpriteID image = dts->ground.sprite;
00508 PaletteID pal = dts->ground.pal;
00509
00510 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00511 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00512 }
00513
00514 DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00515 }
00516
00528 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
00529 {
00530 return GetObjectCallback(callback, param1, param2, spec, o, tile);
00531 }
00532
00534 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
00535 static const CallbackID cb_animation_speed = CBID_OBJECT_ANIMATION_SPEED;
00536 static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00537
00538 static const ObjectCallbackMask cbm_animation_speed = CBM_OBJ_ANIMATION_SPEED;
00539 static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00540 };
00541
00546 void AnimateNewObjectTile(TileIndex tile)
00547 {
00548 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00549 if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00550
00551 ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00552 }
00553
00561 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00562 {
00563 if (!HasBit(spec->animation.triggers, trigger)) return;
00564
00565 ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00566 }
00567
00574 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00575 {
00576 if (!HasBit(spec->animation.triggers, trigger)) return;
00577
00578 TILE_AREA_LOOP(tile, o->location) {
00579 TriggerObjectTileAnimation(o, tile, trigger, spec);
00580 }
00581 }
00582
00588 void GetObjectResolver(ResolverObject *ro, uint index)
00589 {
00590 NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);
00591 }