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 "genworld.h"
00017 #include "newgrf_class_func.h"
00018 #include "newgrf_object.h"
00019 #include "newgrf_sound.h"
00020 #include "object_base.h"
00021 #include "object_map.h"
00022 #include "tile_cmd.h"
00023 #include "town.h"
00024 #include "water.h"
00025 #include "newgrf_animation_base.h"
00026
00028 ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
00029
00030 extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
00032 ObjectSpec _object_specs[NUM_OBJECTS];
00033
00039 const ObjectSpec *ObjectSpec::Get(ObjectType index)
00040 {
00041 assert(index < NUM_OBJECTS);
00042 return &_object_specs[index];
00043 }
00044
00050 const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
00051 {
00052 return ObjectSpec::Get(GetObjectType(tile));
00053 }
00054
00059 bool ObjectSpec::IsEverAvailable() const
00060 {
00061 return this->enabled && HasBit(this->climate, _settings_game.game_creation.landscape) &&
00062 (this->flags & ((_game_mode != GM_EDITOR && !_generating_world) ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
00063 }
00064
00069 bool ObjectSpec::WasEverAvailable() const
00070 {
00071 return this->IsEverAvailable() && _date > this->introduction_date;
00072 }
00073
00078 bool ObjectSpec::IsAvailable() const
00079 {
00080 return this->WasEverAvailable() &&
00081 (_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365);
00082 }
00083
00088 uint ObjectSpec::Index() const
00089 {
00090 return this - _object_specs;
00091 }
00092
00094 void ResetObjects()
00095 {
00096
00097 MemSetT(_object_specs, 0, lengthof(_object_specs));
00098
00099
00100 MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
00101
00102 for (uint16 i = 0; i < lengthof(_original_objects); i++) {
00103 _object_specs[i].grf_prop.local_id = i;
00104 }
00105 }
00106
00107 template <typename Tspec, typename Tid, Tid Tmax>
00108 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00109 {
00110 ObjectClassID cls = ObjectClass::Allocate('LTHS');
00111 ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
00112 _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
00113 ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
00114
00115 cls = ObjectClass::Allocate('TRNS');
00116 ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
00117 _object_specs[OBJECT_TRANSMITTER].cls_id = cls;
00118 ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
00119 }
00120
00121 template <typename Tspec, typename Tid, Tid Tmax>
00122 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
00123 {
00124 return this->GetSpec(index)->IsEverAvailable();
00125 }
00126
00127 INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
00128
00129
00136 ObjectScopeResolver::ObjectScopeResolver(ResolverObject &ro, Object *obj, TileIndex tile, uint8 view)
00137 : ScopeResolver(ro)
00138 {
00139 this->obj = obj;
00140 this->tile = tile;
00141 this->view = view;
00142 }
00143
00144 uint32 ObjectScopeResolver::GetRandomBits() const
00145 {
00146 return IsValidTile(this->tile) && IsTileType(this->tile, MP_OBJECT) ? GetObjectRandomBits(this->tile) : 0;
00147 }
00148
00155 static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
00156 {
00157 if (!IsTileType(tile, MP_OBJECT)) {
00158 return 0xFFFF;
00159 }
00160
00161 const Object *o = Object::GetByTile(tile);
00162 const ObjectSpec *spec = ObjectSpec::Get(o->type);
00163
00164
00165 if (spec->grf_prop.grffile == NULL) {
00166 return 0xFFFE;
00167 }
00168
00169 if (spec->grf_prop.grffile->grfid == cur_grfid) {
00170 return spec->grf_prop.local_id | o->view << 16;
00171 }
00172
00173 return 0xFFFE;
00174 }
00175
00184 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
00185 {
00186 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00187 bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
00188
00189 return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
00190 }
00191
00199 static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
00200 {
00201 uint32 best_dist = UINT32_MAX;
00202 const Object *o;
00203 FOR_ALL_OBJECTS(o) {
00204 if (o->type != type || o == current) continue;
00205
00206 best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
00207 }
00208
00209 return best_dist;
00210 }
00211
00220 static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
00221 {
00222 uint32 grf_id = GetRegister(0x100);
00223 uint32 idx;
00224
00225
00226 switch (grf_id) {
00227 case 0:
00228 idx = local_id;
00229 break;
00230
00231 case 0xFFFFFFFF:
00232 grf_id = grfid;
00233
00234
00235 default:
00236 idx = _object_mngr.GetID(local_id, grf_id);
00237 break;
00238 }
00239
00240
00241 if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
00242
00243 return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
00244 }
00245
00247 uint32 ObjectScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00248 {
00249
00250
00251 const Town *t = NULL;
00252
00253 if (this->obj == NULL) {
00254 switch (variable) {
00255
00256 case 0x41:
00257 case 0x60:
00258 case 0x61:
00259 case 0x62:
00260 case 0x64:
00261 break;
00262
00263
00264 case 0x45:
00265 case 0x46:
00266 if (!IsValidTile(this->tile)) goto unhandled;
00267 t = ClosestTownFromTile(this->tile, UINT_MAX);
00268 break;
00269
00270
00271 case 0x42: return _date;
00272
00273
00274 case 0x44: return _current_company;
00275
00276
00277 case 0x48: return this->view;
00278
00279
00280
00281
00282
00283
00284
00285
00286 default:
00287 goto unhandled;
00288 }
00289
00290
00291 if (!IsValidTile(this->tile)) goto unhandled;
00292 } else {
00293 t = this->obj->town;
00294 }
00295
00296 switch (variable) {
00297
00298 case 0x40: {
00299 uint offset = this->tile - this->obj->location.tile;
00300 uint offset_x = TileX(offset);
00301 uint offset_y = TileY(offset);
00302 return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
00303 }
00304
00305
00306 case 0x41: return GetTileSlope(this->tile) << 8 | GetTerrainType(this->tile);
00307
00308
00309 case 0x42: return this->obj->build_date;
00310
00311
00312 case 0x43: return GetAnimationFrame(this->tile);
00313
00314
00315 case 0x44: return GetTileOwner(this->tile);
00316
00317
00318 case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceManhattan(this->tile, t->xy), 0xFFFF);
00319
00320
00321 case 0x46: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceSquare(this->tile, t->xy), 0xFFFF);
00322
00323
00324 case 0x47: return this->obj->colour;
00325
00326
00327 case 0x48: return this->obj->view;
00328
00329
00330 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, this->tile), this->ro.grffile->grfid);
00331
00332
00333 case 0x61: {
00334 TileIndex tile = GetNearbyTile(parameter, this->tile);
00335 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetObjectRandomBits(tile) : 0;
00336 }
00337
00338
00339 case 0x62: return GetNearbyObjectTileInformation(parameter, this->tile, this->obj == NULL ? INVALID_OBJECT : this->obj->index, this->ro.grffile->grf_version >= 8);
00340
00341
00342 case 0x63: {
00343 TileIndex tile = GetNearbyTile(parameter, this->tile);
00344 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetAnimationFrame(tile) : 0;
00345 }
00346
00347
00348 case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, this->ro.grffile->grfid, this->tile, this->obj);
00349 }
00350
00351 unhandled:
00352 DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
00353
00354 *available = false;
00355 return UINT_MAX;
00356 }
00357
00367 ObjectResolverObject::ObjectResolverObject(const ObjectSpec *spec, Object *obj, TileIndex tile, uint8 view,
00368 CallbackID callback, uint32 param1, uint32 param2)
00369 : ResolverObject(spec->grf_prop.grffile, callback, param1, param2), object_scope(*this, obj, tile, view)
00370 {
00371 this->town_scope = NULL;
00372 this->root_spritegroup = (obj == NULL && spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT] != NULL) ?
00373 spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT] : spec->grf_prop.spritegroup[0];
00374 }
00375
00376 ObjectResolverObject::~ObjectResolverObject()
00377 {
00378 delete this->town_scope;
00379 }
00380
00386 TownScopeResolver *ObjectResolverObject::GetTown()
00387 {
00388 if (this->town_scope == NULL) {
00389 Town *t;
00390 if (this->object_scope.obj != NULL) {
00391 t = this->object_scope.obj->town;
00392 } else {
00393 t = ClosestTownFromTile(this->object_scope.tile, UINT_MAX);
00394 }
00395 if (t == NULL) return NULL;
00396 this->town_scope = new TownScopeResolver(*this, t, this->object_scope.obj == NULL);
00397 }
00398 return this->town_scope;
00399 }
00400
00412 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
00413 {
00414 ObjectResolverObject object(spec, o, tile, view, callback, param1, param2);
00415 return object.ResolveCallback();
00416 }
00417
00424 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
00425 {
00426 const DrawTileSprites *dts = group->ProcessRegisters(NULL);
00427 PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
00428
00429 SpriteID image = dts->ground.sprite;
00430 PaletteID pal = dts->ground.pal;
00431
00432 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00433
00434
00435 if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
00436 DrawWaterClassGround(ti);
00437 } else {
00438 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
00439 }
00440 }
00441
00442 DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
00443 }
00444
00450 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
00451 {
00452 Object *o = Object::GetByTile(ti->tile);
00453 ObjectResolverObject object(spec, o, ti->tile);
00454
00455 const SpriteGroup *group = object.Resolve();
00456 if (group == NULL || group->type != SGT_TILELAYOUT) return;
00457
00458 DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
00459 }
00460
00468 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
00469 {
00470 ObjectResolverObject object(spec, NULL, INVALID_TILE, view);
00471 const SpriteGroup *group = object.Resolve();
00472 if (group == NULL || group->type != SGT_TILELAYOUT) return;
00473
00474 const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(NULL);
00475
00476 PaletteID palette;
00477 if (Company::IsValidID(_local_company)) {
00478
00479 if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
00480 const Livery *l = Company::Get(_local_company)->livery;
00481 palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
00482 } else {
00483 palette = COMPANY_SPRITE_COLOUR(_local_company);
00484 }
00485 } else {
00486
00487 palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
00488 }
00489
00490 SpriteID image = dts->ground.sprite;
00491 PaletteID pal = dts->ground.pal;
00492
00493 if (GB(image, 0, SPRITE_WIDTH) != 0) {
00494 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00495 }
00496
00497 DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
00498 }
00499
00511 uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
00512 {
00513 return GetObjectCallback(callback, param1, param2, spec, o, tile);
00514 }
00515
00517 struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
00518 static const CallbackID cb_animation_speed = CBID_OBJECT_ANIMATION_SPEED;
00519 static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
00520
00521 static const ObjectCallbackMask cbm_animation_speed = CBM_OBJ_ANIMATION_SPEED;
00522 static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
00523 };
00524
00529 void AnimateNewObjectTile(TileIndex tile)
00530 {
00531 const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
00532 if (spec == NULL || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
00533
00534 ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
00535 }
00536
00544 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00545 {
00546 if (!HasBit(spec->animation.triggers, trigger)) return;
00547
00548 ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
00549 }
00550
00557 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
00558 {
00559 if (!HasBit(spec->animation.triggers, trigger)) return;
00560
00561 TILE_AREA_LOOP(tile, o->location) {
00562 TriggerObjectTileAnimation(o, tile, trigger, spec);
00563 }
00564 }