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 if (spec->grf_prop.grffile->grfid == cur_grfid) {
00144 return spec->grf_prop.local_id;
00145 }
00146
00147 return 0xFFFE;
00148 }
00149
00157 static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index)
00158 {
00159 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
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);
00196 uint32 idx;
00197
00198
00199 switch (grf_id) {
00200 case 0:
00201 idx = local_id;
00202 break;
00203
00204 case 0xFFFFFFFF:
00205 grf_id = grfid;
00206
00207
00208 default:
00209 idx = _object_mngr.GetID(local_id, grf_id);
00210 break;
00211 }
00212
00213
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
00227 return TownGetVariable(variable, parameter, available, (o == NULL) ? ClosestTownFromTile(tile, UINT_MAX) : o->town);
00228 }
00229
00230
00231
00232 const Town *t = NULL;
00233
00234 if (o == NULL) {
00235 switch (variable) {
00236
00237 case 0x41:
00238 case 0x60:
00239 case 0x61:
00240 case 0x62:
00241 case 0x64:
00242 break;
00243
00244
00245 case 0x45:
00246 case 0x46:
00247 if (!IsValidTile(tile)) goto unhandled;
00248 t = ClosestTownFromTile(tile, UINT_MAX);
00249 break;
00250
00251
00252 case 0x42: return _date;
00253
00254
00255 case 0x44: return _current_company;
00256
00257
00258 case 0x48: return object->u.object.view;
00259
00260
00261
00262
00263
00264
00265
00266
00267 default:
00268 goto unhandled;
00269 }
00270
00271
00272 if (!IsValidTile(tile)) goto unhandled;
00273 } else {
00274 t = o->town;
00275 }
00276
00277 switch (variable) {
00278
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
00287 case 0x41: return GetTileSlope(tile, NULL) << 8 | GetTerrainType(tile);
00288
00289
00290 case 0x42: return o->build_date;
00291
00292
00293 case 0x43: return GetAnimationFrame(tile);
00294
00295
00296 case 0x44: return GetTileOwner(tile);
00297
00298
00299 case 0x45: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceManhattan(tile, t->xy), 0xFFFF);
00300
00301
00302 case 0x46: return GetTownRadiusGroup(t, tile) << 16 | min(DistanceSquare(tile, t->xy), 0xFFFF);
00303
00304
00305 case 0x47: return o->colour;
00306
00307
00308 case 0x48: return o->view;
00309
00310
00311 case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, tile), object->grffile->grfid);
00312
00313
00314 case 0x61:
00315 tile = GetNearbyTile(parameter, tile);
00316 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetObjectRandomBits(tile) : 0;
00317
00318
00319 case 0x62: return GetNearbyObjectTileInformation(parameter, tile, o == NULL ? INVALID_OBJECT : o->index);
00320
00321
00322 case 0x63:
00323 tile = GetNearbyTile(parameter, tile);
00324 return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == o) ? GetAnimationFrame(tile) : 0;
00325
00326
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
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
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
00428
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
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
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 }