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_class_func.h"
00017 #include "newgrf_object.h"
00018 #include "newgrf_sound.h"
00019 #include "newgrf_spritegroup.h"
00020 #include "newgrf_town.h"
00021 #include "object_base.h"
00022 #include "object_map.h"
00023 #include "tile_cmd.h"
00024 #include "town.h"
00025 #include "water.h"
00026 #include "newgrf_animation_base.h"
00027
00029 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
00030
00031 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
00033 ObjectSpec _object_specs[NUM_OBJECTS];
00034
00040 const ObjectSpec *ObjectSpec::Get(ObjectType index)
00041 {
00042 assert(index < NUM_OBJECTS);
00043 return &_object_specs[index];
00044 }
00045
00051 const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
00052 {
00053 return ObjectSpec::Get(GetObjectType(tile));
00054 }
00055
00060 bool ObjectSpec::IsEverAvailable() const
00061 {
00062 return this->enabled && HasBit(this->climate, _settings_game.game_creation.landscape) &&
00063 (this->flags & (_game_mode != GM_EDITOR ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
00064 }
00065
00070 bool ObjectSpec::IsAvailable() const
00071 {
00072 return this->IsEverAvailable() && _date > this->introduction_date &&
00073 (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365);
00074 }
00075
00080 uint ObjectSpec::Index() const
00081 {
00082 return this - _object_specs;
00083 }
00084
00086 void ResetObjects()
00087 {
00088
00089 MemSetT(_object_specs, 0, lengthof(_object_specs));
00090
00091
00092 MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
00093
00094 for (uint16 i = 0; i < lengthof(_original_objects); i++) {
00095 _object_specs[i].grf_prop.local_id = i;
00096 }
00097 }
00098
00099 template <typename Tspec, typename Tid, Tid Tmax>
00100 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00101 {
00102 ObjectClassID cls = ObjectClass::Allocate('LTHS');
00103 ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
00104 _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
00105 ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
00106
00107 cls = ObjectClass::Allocate('TRNS');
00108 ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
00109 _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
00110 ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
00111 }
00112
00113 template <typename Tspec, typename Tid, Tid Tmax>
00114 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
00115 {
00116 return this->GetSpec(index)->IsEverAvailable();
00117 }
00118
00119 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
00120
00121
00122 static uint32 ObjectGetRandomBits(const ResolverObject *object)
00123 {
00124 TileIndex t = object->u.object.tile;
00125 return IsValidTile(t) && IsTileType(t, MP_OBJECT) ? GetObjectRandomBits(t) : 0;
00126 }
00127
00128 static uint32 ObjectGetTriggers(const ResolverObject *object)
00129 {
00130 return 0;
00131 }
00132
00133 static void ObjectSetTriggers(const ResolverObject *object, int triggers)
00134 {
00135 }
00136
00137
00144 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
00145 {
00146 if (!IsTileType(tile, MP_OBJECT)) {
00147 return 0xFFFF;
00148 }
00149
00150 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00151
00152
00153 if (spec->grf_prop.grffile == NULL) {
00154 return 0xFFFE;
00155 }
00156
00157 if (spec->grf_prop.grffile->grfid == cur_grfid) {
00158 return spec->grf_prop.local_id;
00159 }
00160
00161 return 0xFFFE;
00162 }
00163
00172 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
00173 {
00174 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00175 bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00176
00177 return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
00178 }
00179
00187 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00188 {
00189 uint32 best_dist = UINT32_MAX;
00190 const Object *o;
00191 FOR_ALL_OBJECTS(o) {
00192 if (GetObjectType(o->location.tile) != type || o == current) continue;
00193
00194 best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00195 }
00196
00197 return best_dist;
00198 }
00199
00208 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00209 {
00210 uint32 grf_id = GetRegister(0x100);
00211 uint32 idx;
00212
00213
00214 switch (grf_id) {
00215 case 0:
00216 idx = local_id;
00217 break;
00218
00219 case 0xFFFFFFFF:
00220 grf_id = grfid;
00221
00222
00223 default:
00224 idx = _object_mngr.GetID(local_id, grf_id);
00225 break;
00226 }
00227
00228
00229 if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00230
00231 return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00232 }
00233
00235 static uint32 ObjectGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00236 {
00237 const Object *o = object->u.object.o;
00238 TileIndex tile = object->u.object.tile;
00239
00240 if (object->scope == VSG_SCOPE_PARENT) {
00241
00242 return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town, object->grffile);
00243 }
00244
00245
00246
00247 const Town *t = NULL;
00248
00249 if (o == NULL) {
00250 switch (variable) {
00251
00252 case 0x41:
00253 case 0x60:
00254 case 0x61:
00255 case 0x62:
00256 case 0x64:
00257 break;
00258
00259
00260 case 0x45:
00261 case 0x46:
00262 if (!IsValidTile(tile)) goto unhandled;
00263 t = ClosestTownFromTile(tile, UINT_MAX);
00264 break;
00265
00266
00267 case 0x42: return _date;
00268
00269
00270 case 0x44: return _current_company;
00271
00272
00273 case 0x48: return object->u.object.view;
00274
00275
00276
00277
00278
00279
00280
00281
00282 default:
00283 goto unhandled;
00284 }
00285
00286
00287 if (!IsValidTile(tile)) goto unhandled;
00288 } else {
00289 t = o->town;
00290 }
00291
00292 switch (variable) {
00293
00294 case 0x40: {
00295 uint offset = tile - o->location.tile;
00296 uint offset_x = TileX(offset);
00297 uint offset_y = TileY(offset);
00298 return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00299 }
00300
00301
00302 case 0x41: return GetTileSlope(tile) << 8 | GetTerrainType(tile);
00303
00304
00305 case 0x42: return o->build_date;
00306
00307
00308 case 0x43: return GetAnimationFrame(tile);
00309
00310
00311 case 0x44: return GetTileOwner(tile);
00312
00313
00314 case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00315
00316
00317 case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00318
00319
00320 case 0x47: return o->colour;
00321
00322
00323 case 0x48: return o->view;
00324
00325
00326 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00327
00328
00329 case 0x61:
00330 tile = GetNearbyTile(parameter, tile);
00331 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00332
00333
00334 case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index, object->grffile->grf_version >= 8);
00335
00336
00337 case 0x63:
00338 tile = GetNearbyTile(parameter, tile);
00339 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00340
00341
00342 case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, object->grffile->grfid, tile, o);
00343 }
00344
00345 unhandled:
00346 DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00347
00348 *available = false;
00349 return UINT_MAX;
00350 }
00351
00352 static const SpriteGroup *ObjectResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00353 {
00354
00355 return NULL;
00356 }
00357
00364 static const SpriteGroup *GetObjectSpriteGroup(const ObjectSpec *spec, const Object *o)
00365 {
00366 const SpriteGroup *group = NULL;
00367
00368 if (o == NULL) group = spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT];
00369 if (group != NULL) return group;
00370
00371
00372 return spec->grf_prop.spritegroup[0];
00373
00374 }
00375
00382 void ObjectStorePSA(ResolverObject *object, uint pos, int32 value)
00383 {
00384
00385 Object *o = object->u.object.o;
00386 if (object->scope != VSG_SCOPE_PARENT || o == NULL) return;
00387
00388
00389 TownStorePSA(o->town, object->grffile, pos, value);
00390 }
00391
00395 static void NewObjectResolver(ResolverObject *res, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0)
00396 {
00397 res->GetRandomBits = ObjectGetRandomBits;
00398 res->GetTriggers = ObjectGetTriggers;
00399 res->SetTriggers = ObjectSetTriggers;
00400 res->GetVariable = ObjectGetVariable;
00401 res->ResolveReal = ObjectResolveReal;
00402 res->StorePSA = ObjectStorePSA;
00403
00404 res->u.object.o = o;
00405 res->u.object.tile = tile;
00406 res->u.object.view = view;
00407
00408 res->callback = CBID_NO_CALLBACK;
00409 res->callback_param1 = 0;
00410 res->callback_param2 = 0;
00411 res->ResetState();
00412
00413 res->grffile = spec->grf_prop.grffile;
00414 }
00415
00427 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
00428 {
00429 ResolverObject object;
00430 NewObjectResolver(&object, spec, o, tile, view);
00431 object.callback = callback;
00432 object.callback_param1 = param1;
00433 object.callback_param2 = param2;
00434
00435 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00436 if (group == NULL) return CALLBACK_FAILED;
00437
00438 return group->GetCallbackResult();
00439 }
00440
00447 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00448 {
00449 const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00450 PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00451
00452 SpriteID image = dts->ground.sprite;
00453 PaletteID pal = dts->ground.pal;
00454
00455 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00456
00457
00458 if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00459 DrawWaterClassGround(ti);
00460 } else {
00461 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00462 }
00463 }
00464
00465 DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00466 }
00467
00473 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00474 {
00475 ResolverObject object;
00476 Object *o = Object::GetByTile(ti->tile);
00477 NewObjectResolver(&object, spec, o, ti->tile);
00478
00479 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, o), &object);
00480 if (group == NULL || group->type != SGT_TILELAYOUT) return;
00481
00482 DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00483 }
00484
00492 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00493 {
00494 ResolverObject object;
00495 NewObjectResolver(&object, spec, NULL, INVALID_TILE, view);
00496
00497 const SpriteGroup *group = SpriteGroup::Resolve(GetObjectSpriteGroup(spec, NULL), &object);
00498 if (group == NULL || group->type != SGT_TILELAYOUT) return;
00499
00500 const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
00501
00502 PaletteID palette;
00503 if (Company::IsValidID(_local_company)) {
00504
00505 if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00506 const Livery *l = Company::Get(_local_company)->livery;
00507 palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00508 } else {
00509 palette = COMPANY_SPRITE_COLOUR(_local_company);
00510 }
00511 } else {
00512
00513 palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00514 }
00515
00516 SpriteID image = dts->ground.sprite;
00517 PaletteID pal = dts->ground.pal;
00518
00519 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00520 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00521 }
00522
00523 DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00524 }
00525
00537 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
00538 {
00539 return GetObjectCallback(callback, param1, param2, spec, o, tile);
00540 }
00541
00543 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
00544 static const CallbackID cb_animation_speed = CBID_OBJECT_ANIMATION_SPEED;
00545 static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00546
00547 static const ObjectCallbackMask cbm_animation_speed = CBM_OBJ_ANIMATION_SPEED;
00548 static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00549 };
00550
00555 void AnimateNewObjectTile(TileIndex tile)
00556 {
00557 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00558 if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00559
00560 ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00561 }
00562
00570 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00571 {
00572 if (!HasBit(spec->animation.triggers, trigger)) return;
00573
00574 ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00575 }
00576
00583 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00584 {
00585 if (!HasBit(spec->animation.triggers, trigger)) return;
00586
00587 TILE_AREA_LOOP(tile, o->location) {
00588 TriggerObjectTileAnimation(o, tile, trigger, spec);
00589 }
00590 }
00591
00597 void GetObjectResolver(ResolverObject *ro, uint index)
00598 {
00599 NewObjectResolver(ro, ObjectSpec::GetByTile(index), Object::GetByTile(index), index);
00600 }