00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "variables.h"
00014 #include "landscape.h"
00015 #include "debug.h"
00016 #include "station_base.h"
00017 #include "waypoint_base.h"
00018 #include "roadstop_base.h"
00019 #include "newgrf_cargo.h"
00020 #include "newgrf_commons.h"
00021 #include "newgrf_station.h"
00022 #include "newgrf_spritegroup.h"
00023 #include "newgrf_sound.h"
00024 #include "town.h"
00025 #include "newgrf_town.h"
00026 #include "gfx_func.h"
00027 #include "date_func.h"
00028 #include "company_func.h"
00029 #include "animated_tile_func.h"
00030 #include "functions.h"
00031 #include "tunnelbridge_map.h"
00032 #include "spritecache.h"
00033
00034 #include "table/strings.h"
00035
00036 static StationClass _station_classes[STAT_CLASS_MAX];
00037
00038 enum {
00039 MAX_SPECLIST = 255,
00040 };
00041
00042 enum TriggerArea {
00043 TA_TILE,
00044 TA_PLATFORM,
00045 TA_WHOLE,
00046 };
00047
00048 struct ETileArea : TileArea {
00049 ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
00050 {
00051 switch (ta) {
00052 default: NOT_REACHED();
00053
00054 case TA_TILE:
00055 this->tile = tile;
00056 this->w = 1;
00057 this->h = 1;
00058 break;
00059
00060 case TA_PLATFORM: {
00061 TileIndex start, end;
00062 Axis axis = GetRailStationAxis(tile);
00063 TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
00064
00065 for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(tile, end + delta); end += delta) { }
00066 for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(tile, start - delta); start -= delta) { }
00067
00068 this->tile = start;
00069 this->w = TileX(end) - TileX(start) + 1;
00070 this->h = TileY(end) - TileY(start) + 1;
00071 break;
00072 }
00073
00074 case TA_WHOLE:
00075 st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT);
00076 this->w++;
00077 this->h++;
00078 break;
00079 }
00080 }
00081 };
00082
00083
00089 void ResetStationClasses()
00090 {
00091 for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
00092 _station_classes[i].id = 0;
00093 _station_classes[i].name = STR_EMPTY;
00094 _station_classes[i].stations = 0;
00095
00096 free(_station_classes[i].spec);
00097 _station_classes[i].spec = NULL;
00098 }
00099
00100
00101 _station_classes[0].id = 'DFLT';
00102 _station_classes[0].name = STR_STATION_CLASS_DFLT;
00103 _station_classes[0].stations = 1;
00104 _station_classes[0].spec = MallocT<StationSpec*>(1);
00105 _station_classes[0].spec[0] = NULL;
00106
00107 _station_classes[1].id = 'WAYP';
00108 _station_classes[1].name = STR_STATION_CLASS_WAYP;
00109 _station_classes[1].stations = 1;
00110 _station_classes[1].spec = MallocT<StationSpec*>(1);
00111 _station_classes[1].spec[0] = NULL;
00112 }
00113
00119 StationClassID AllocateStationClass(uint32 cls)
00120 {
00121 for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
00122 if (_station_classes[i].id == cls) {
00123
00124 return i;
00125 } else if (_station_classes[i].id == 0) {
00126
00127 _station_classes[i].id = cls;
00128 return i;
00129 }
00130 }
00131
00132 grfmsg(2, "StationClassAllocate: already allocated %d classes, using default", STAT_CLASS_MAX);
00133 return STAT_CLASS_DFLT;
00134 }
00135
00137 void SetStationClassName(StationClassID sclass, StringID name)
00138 {
00139 assert(sclass < STAT_CLASS_MAX);
00140 _station_classes[sclass].name = name;
00141 }
00142
00144 StringID GetStationClassName(StationClassID sclass)
00145 {
00146 assert(sclass < STAT_CLASS_MAX);
00147 return _station_classes[sclass].name;
00148 }
00149
00154 uint GetNumStationClasses()
00155 {
00156 uint i;
00157 for (i = 0; i < STAT_CLASS_MAX && _station_classes[i].id != 0; i++) {}
00158 return i;
00159 }
00160
00166 uint GetNumCustomStations(StationClassID sclass)
00167 {
00168 assert(sclass < STAT_CLASS_MAX);
00169 return _station_classes[sclass].stations;
00170 }
00171
00176 void SetCustomStationSpec(StationSpec *statspec)
00177 {
00178 StationClass *station_class;
00179 int i;
00180
00181
00182 if (statspec->allocated) return;
00183
00184 assert(statspec->sclass < STAT_CLASS_MAX);
00185 station_class = &_station_classes[statspec->sclass];
00186
00187 i = station_class->stations++;
00188 station_class->spec = ReallocT(station_class->spec, station_class->stations);
00189
00190 station_class->spec[i] = statspec;
00191 statspec->allocated = true;
00192 }
00193
00200 const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
00201 {
00202 assert(sclass < STAT_CLASS_MAX);
00203 if (station < _station_classes[sclass].stations)
00204 return _station_classes[sclass].spec[station];
00205
00206
00207
00208 return NULL;
00209 }
00210
00218 const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx, int *index)
00219 {
00220 uint j;
00221
00222 for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
00223 for (j = 0; j < _station_classes[i].stations; j++) {
00224 const StationSpec *statspec = _station_classes[i].spec[j];
00225 if (statspec == NULL) continue;
00226 if (statspec->grffile->grfid == grfid && statspec->localidx == localidx) {
00227 if (index != NULL) *index = j;
00228 return statspec;
00229 }
00230 }
00231 }
00232
00233 return NULL;
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
00245 {
00246 uint32 retval = 0;
00247
00248 if (axis == AXIS_X) {
00249 Swap(platforms, length);
00250 Swap(x, y);
00251 }
00252
00253
00254 platforms = min(15, platforms);
00255 length = min(15, length);
00256 x = min(15, x);
00257 y = min(15, y);
00258 if (centred) {
00259 x -= platforms / 2;
00260 y -= length / 2;
00261 SB(retval, 0, 4, y & 0xF);
00262 SB(retval, 4, 4, x & 0xF);
00263 } else {
00264 SB(retval, 0, 4, y);
00265 SB(retval, 4, 4, length - y - 1);
00266 SB(retval, 8, 4, x);
00267 SB(retval, 12, 4, platforms - x - 1);
00268 }
00269 SB(retval, 16, 4, length);
00270 SB(retval, 20, 4, platforms);
00271 SB(retval, 24, 4, tile);
00272
00273 return retval;
00274 }
00275
00276
00277
00278
00279
00280
00281 static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
00282 {
00283 byte orig_type = 0;
00284 Axis orig_axis = AXIS_X;
00285 StationID sid = GetStationIndex(tile);
00286
00287 if (check_type) orig_type = GetCustomStationSpecIndex(tile);
00288 if (check_axis) orig_axis = GetRailStationAxis(tile);
00289
00290 while (true) {
00291 TileIndex new_tile = TILE_ADD(tile, delta);
00292
00293 if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
00294 if (!HasStationRail(new_tile)) break;
00295 if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
00296 if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
00297
00298 tile = new_tile;
00299 }
00300 return tile;
00301 }
00302
00303
00304 static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
00305 {
00306 int tx = TileX(tile);
00307 int ty = TileY(tile);
00308 int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1, 0), check_type, check_axis));
00309 int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
00310 int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1, 0), check_type, check_axis)) + 1;
00311 int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0, 1), check_type, check_axis)) + 1;
00312
00313 tx -= sx; ex -= sx;
00314 ty -= sy; ey -= sy;
00315
00316 return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
00317 }
00318
00319
00320 static uint32 GetRailContinuationInfo(TileIndex tile)
00321 {
00322
00323 static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
00324 static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
00325
00326
00327 static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
00328 static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
00329
00330 Axis axis = GetRailStationAxis(tile);
00331
00332
00333 const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
00334 const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
00335
00336 uint32 res = 0;
00337 uint i;
00338
00339 for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
00340 TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
00341 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
00342 if (trackbits != TRACK_BIT_NONE) {
00343
00344 SetBit(res, i + 8);
00345
00346
00347
00348 if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
00349 continue;
00350 }
00351
00352
00353 if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
00354 }
00355 }
00356
00357 return res;
00358 }
00359
00360
00361
00362 static uint32 StationGetRandomBits(const ResolverObject *object)
00363 {
00364 const BaseStation *st = object->u.station.st;
00365 const TileIndex tile = object->u.station.tile;
00366 return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
00367 }
00368
00369
00370 static uint32 StationGetTriggers(const ResolverObject *object)
00371 {
00372 const BaseStation *st = object->u.station.st;
00373 return st == NULL ? 0 : st->waiting_triggers;
00374 }
00375
00376
00377 static void StationSetTriggers(const ResolverObject *object, int triggers)
00378 {
00379 BaseStation *st = const_cast<BaseStation *>(object->u.station.st);
00380 assert(st != NULL);
00381 st->waiting_triggers = triggers;
00382 }
00383
00389 static struct {
00390 uint32 v40;
00391 uint32 v41;
00392 uint32 v45;
00393 uint32 v46;
00394 uint32 v47;
00395 uint32 v49;
00396 uint8 valid;
00397 } _svc;
00398
00399 static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00400 {
00401 const BaseStation *st = object->u.station.st;
00402 TileIndex tile = object->u.station.tile;
00403
00404 if (object->scope == VSG_SCOPE_PARENT) {
00405
00406 const Town *t;
00407
00408 if (st != NULL) {
00409 t = st->town;
00410 } else if (tile != INVALID_TILE) {
00411 t = ClosestTownFromTile(tile, UINT_MAX);
00412 } else {
00413 *available = false;
00414 return UINT_MAX;
00415 }
00416
00417 return TownGetVariable(variable, parameter, available, t);
00418 }
00419
00420 if (st == NULL) {
00421
00422 switch (variable) {
00423 case 0x40:
00424 case 0x41:
00425 case 0x46:
00426 case 0x47:
00427 case 0x49: return 0x2110000;
00428 case 0x42: return 0;
00429 case 0x43: return _current_company;
00430 case 0x44: return 2;
00431 case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00432 }
00433
00434 *available = false;
00435 return UINT_MAX;
00436 }
00437
00438 switch (variable) {
00439
00440 case 0x40:
00441 if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); }
00442 return _svc.v40;
00443
00444 case 0x41:
00445 if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true, false, false); SetBit(_svc.valid, 1); }
00446 return _svc.v41;
00447
00448 case 0x42: return GetTerrainType(tile) | (GetRailType(tile) << 8);
00449 case 0x43: return st->owner;
00450 case 0x44: return HasStationReservation(tile) ? 7 : 4;
00451 case 0x45:
00452 if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); }
00453 return _svc.v45;
00454
00455 case 0x46:
00456 if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); }
00457 return _svc.v46;
00458
00459 case 0x47:
00460 if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true, false, true); SetBit(_svc.valid, 4); }
00461 return _svc.v47;
00462
00463 case 0x49:
00464 if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(tile, false, true, false); SetBit(_svc.valid, 5); }
00465 return _svc.v49;
00466
00467 case 0x4A:
00468 return GetStationAnimationFrame(tile);
00469
00470
00471
00472 case 0x66:
00473 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00474 return st->TileBelongsToRailStation(tile) ? GetStationAnimationFrame(tile) : UINT_MAX;
00475
00476 case 0x67: {
00477 Axis axis = GetRailStationAxis(tile);
00478
00479 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00480
00481 Slope tileh = GetTileSlope(tile, NULL);
00482 bool swap = (axis == AXIS_Y && HasBit(tileh, SLOPE_W) != HasBit(tileh, SLOPE_E));
00483
00484 return GetNearbyTileInformation(tile) ^ (swap ? SLOPE_EW : 0);
00485 }
00486
00487 case 0x68: {
00488 TileIndex nearby_tile = GetNearbyTile(parameter, tile);
00489
00490 if (!HasStationTileRail(nearby_tile)) return 0xFFFFFFFF;
00491
00492 uint32 grfid = st->speclist[GetCustomStationSpecIndex(tile)].grfid;
00493 bool perpendicular = GetRailStationAxis(tile) != GetRailStationAxis(nearby_tile);
00494 bool same_station = st->TileBelongsToRailStation(nearby_tile);
00495 uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
00496
00497 if (IsCustomStationSpecIndex(nearby_tile)) {
00498 const StationSpecList ssl = BaseStation::GetByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
00499 res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
00500 }
00501 return res;
00502 }
00503
00504
00505 case 0x82: return 50;
00506 case 0x84: return st->string_id;
00507 case 0x86: return 0;
00508 case 0xF0: return st->facilities;
00509 case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00510 }
00511
00512 return st->GetNewGRFVariable(object, variable, parameter, available);
00513 }
00514
00515 uint32 Station::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
00516 {
00517 switch (variable) {
00518 case 0x48: {
00519 CargoID cargo_type;
00520 uint32 value = 0;
00521
00522 for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00523 if (HasBit(this->goods[cargo_type].acceptance_pickup, GoodsEntry::PICKUP)) SetBit(value, cargo_type);
00524 }
00525 return value;
00526 }
00527
00528 case 0x8A: return this->had_vehicle_of_type;
00529 case 0xF1: return this->airport_type;
00530 case 0xF2: return this->truck_stops->status;
00531 case 0xF3: return this->bus_stops->status;
00532 case 0xF6: return this->airport_flags;
00533 case 0xF7: return GB(this->airport_flags, 8, 8);
00534 }
00535
00536
00537 if (variable >= 0x60 && variable <= 0x65) {
00538 CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grffile);
00539
00540 if (c == CT_INVALID) return 0;
00541 const GoodsEntry *ge = &this->goods[c];
00542
00543 switch (variable) {
00544 case 0x60: return min(ge->cargo.Count(), 4095);
00545 case 0x61: return ge->days_since_pickup;
00546 case 0x62: return ge->rating;
00547 case 0x63: return ge->cargo.DaysInTransit();
00548 case 0x64: return ge->last_speed | (ge->last_age << 8);
00549 case 0x65: return GB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 3;
00550 }
00551 }
00552
00553
00554 if (variable >= 0x8C && variable <= 0xEC) {
00555 const GoodsEntry *g = &this->goods[GB(variable - 0x8C, 3, 4)];
00556 switch (GB(variable - 0x8C, 0, 3)) {
00557 case 0: return g->cargo.Count();
00558 case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (GB(g->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 7);
00559 case 2: return g->days_since_pickup;
00560 case 3: return g->rating;
00561 case 4: return g->cargo.Source();
00562 case 5: return g->cargo.DaysInTransit();
00563 case 6: return g->last_speed;
00564 case 7: return g->last_age;
00565 }
00566 }
00567
00568 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
00569
00570 *available = false;
00571 return UINT_MAX;
00572 }
00573
00574 uint32 Waypoint::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
00575 {
00576 switch (variable) {
00577 case 0x48: return 0;
00578 case 0x8A: return HVOT_WAYPOINT;
00579 case 0xF1: return 0;
00580 case 0xF2: return 0;
00581 case 0xF3: return 0;
00582 case 0xF6: return 0;
00583 case 0xF7: return 0;
00584 }
00585
00586
00587 if (variable >= 0x60 && variable <= 0x65) {
00588 return 0;
00589 }
00590
00591
00592 if (variable >= 0x8C && variable <= 0xEC) {
00593 switch (GB(variable - 0x8C, 0, 3)) {
00594 case 3: return INITIAL_STATION_RATING;
00595 case 4: return INVALID_STATION;
00596 default: return 0;
00597 }
00598 }
00599
00600 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
00601
00602 *available = false;
00603 return UINT_MAX;
00604 }
00605
00606 static const SpriteGroup *StationResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00607 {
00608 const BaseStation *bst = object->u.station.st;
00609 const StationSpec *statspec = object->u.station.statspec;
00610 uint set;
00611
00612 uint cargo = 0;
00613 CargoID cargo_type = object->u.station.cargo_type;
00614
00615 if (bst == NULL || statspec->sclass == STAT_CLASS_WAYP) {
00616 return group->loading[0];
00617 }
00618
00619 const Station *st = Station::From(bst);
00620
00621 switch (cargo_type) {
00622 case CT_INVALID:
00623 case CT_DEFAULT_NA:
00624 case CT_PURCHASE:
00625 cargo = 0;
00626 break;
00627
00628 case CT_DEFAULT:
00629 for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00630 cargo += st->goods[cargo_type].cargo.Count();
00631 }
00632 break;
00633
00634 default:
00635 cargo = st->goods[cargo_type].cargo.Count();
00636 break;
00637 }
00638
00639 if (HasBit(statspec->flags, SSF_DIV_BY_STATION_SIZE)) cargo /= (st->train_station.w + st->train_station.h);
00640 cargo = min(0xfff, cargo);
00641
00642 if (cargo > statspec->cargo_threshold) {
00643 if (group->num_loading > 0) {
00644 set = ((cargo - statspec->cargo_threshold) * group->num_loading) / (4096 - statspec->cargo_threshold);
00645 return group->loading[set];
00646 }
00647 } else {
00648 if (group->num_loaded > 0) {
00649 set = (cargo * group->num_loaded) / (statspec->cargo_threshold + 1);
00650 return group->loaded[set];
00651 }
00652 }
00653
00654 return group->loading[0];
00655 }
00656
00657
00658 static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00659 {
00660 res->GetRandomBits = StationGetRandomBits;
00661 res->GetTriggers = StationGetTriggers;
00662 res->SetTriggers = StationSetTriggers;
00663 res->GetVariable = StationGetVariable;
00664 res->ResolveReal = StationResolveReal;
00665
00666 res->u.station.st = st;
00667 res->u.station.statspec = statspec;
00668 res->u.station.tile = tile;
00669
00670 res->callback = CBID_NO_CALLBACK;
00671 res->callback_param1 = 0;
00672 res->callback_param2 = 0;
00673 res->last_value = 0;
00674 res->trigger = 0;
00675 res->reseed = 0;
00676 res->count = 0;
00677 res->grffile = (statspec != NULL ? statspec->grffile : NULL);
00678 }
00679
00680 static const SpriteGroup *ResolveStation(ResolverObject *object)
00681 {
00682 const SpriteGroup *group;
00683 CargoID ctype = CT_DEFAULT_NA;
00684
00685 if (object->u.station.st == NULL) {
00686
00687 ctype = CT_PURCHASE;
00688 } else if (Station::IsExpected(object->u.station.st)) {
00689 const Station *st = Station::From(object->u.station.st);
00690
00691 const CargoSpec *cs;
00692 FOR_ALL_CARGOSPECS(cs) {
00693 if (object->u.station.statspec->spritegroup[cs->Index()] != NULL &&
00694 !st->goods[cs->Index()].cargo.Empty()) {
00695 ctype = cs->Index();
00696 break;
00697 }
00698 }
00699 }
00700
00701 group = object->u.station.statspec->spritegroup[ctype];
00702 if (group == NULL) {
00703 ctype = CT_DEFAULT;
00704 group = object->u.station.statspec->spritegroup[ctype];
00705 }
00706
00707 if (group == NULL) return NULL;
00708
00709
00710 object->u.station.cargo_type = ctype;
00711
00712
00713 _svc.valid = 0;
00714
00715 return SpriteGroup::Resolve(group, object);
00716 }
00717
00718 SpriteID GetCustomStationRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00719 {
00720 const SpriteGroup *group;
00721 ResolverObject object;
00722
00723 NewStationResolver(&object, statspec, st, tile);
00724
00725 group = ResolveStation(&object);
00726 if (group == NULL || group->type != SGT_RESULT) return 0;
00727 return group->GetResult() - 0x42D;
00728 }
00729
00730
00731 SpriteID GetCustomStationGroundRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00732 {
00733 const SpriteGroup *group;
00734 ResolverObject object;
00735
00736 NewStationResolver(&object, statspec, st, tile);
00737 if (HasBit(statspec->flags, SSF_SEPARATE_GROUND)) {
00738 object.callback_param1 = 1;
00739 }
00740
00741 group = ResolveStation(&object);
00742 if (group == NULL || group->type != SGT_RESULT) return 0;
00743 return group->GetResult() - 0x42D;
00744 }
00745
00746
00747 uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00748 {
00749 const SpriteGroup *group;
00750 ResolverObject object;
00751
00752 NewStationResolver(&object, statspec, st, tile);
00753
00754 object.callback = callback;
00755 object.callback_param1 = param1;
00756 object.callback_param2 = param2;
00757
00758 group = ResolveStation(&object);
00759 if (group == NULL) return CALLBACK_FAILED;
00760 return group->GetCallbackResult();
00761 }
00762
00763
00771 int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
00772 {
00773 uint i;
00774
00775 if (statspec == NULL || st == NULL) return 0;
00776
00777 for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
00778 if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
00779 }
00780
00781 if (i == MAX_SPECLIST) {
00782
00783
00784
00785
00786
00787 for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
00788 if (st->speclist[i].spec == statspec) return i;
00789 }
00790
00791 return -1;
00792 }
00793
00794 if (exec) {
00795 if (i >= st->num_specs) {
00796 st->num_specs = i + 1;
00797 st->speclist = ReallocT(st->speclist, st->num_specs);
00798
00799 if (st->num_specs == 2) {
00800
00801 st->speclist[0].spec = NULL;
00802 st->speclist[0].grfid = 0;
00803 st->speclist[0].localidx = 0;
00804 }
00805 }
00806
00807 st->speclist[i].spec = statspec;
00808 st->speclist[i].grfid = statspec->grffile->grfid;
00809 st->speclist[i].localidx = statspec->localidx;
00810 }
00811
00812 return i;
00813 }
00814
00815
00821 void DeallocateSpecFromStation(BaseStation *st, byte specindex)
00822 {
00823
00824 if (specindex == 0) return;
00825
00826 ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE);
00827
00828 TILE_LOOP(tile, area.w, area.h, area.tile) {
00829 if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
00830 return;
00831 }
00832 }
00833
00834
00835 st->speclist[specindex].spec = NULL;
00836 st->speclist[specindex].grfid = 0;
00837 st->speclist[specindex].localidx = 0;
00838
00839
00840 if (specindex == st->num_specs - 1) {
00841 for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--) {}
00842
00843 if (st->num_specs > 1) {
00844 st->speclist = ReallocT(st->speclist, st->num_specs);
00845 } else {
00846 free(st->speclist);
00847 st->num_specs = 0;
00848 st->speclist = NULL;
00849 st->cached_anim_triggers = 0;
00850 return;
00851 }
00852 }
00853
00854 StationUpdateAnimTriggers(st);
00855 }
00856
00866 bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
00867 {
00868 const StationSpec *statspec;
00869 const DrawTileSprites *sprites;
00870 const DrawTileSeqStruct *seq;
00871 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
00872 SpriteID relocation;
00873 SpriteID image;
00874 SpriteID palette = COMPANY_SPRITE_COLOUR(_local_company);
00875 uint tile = 2;
00876
00877 statspec = GetCustomStationSpec(sclass, station);
00878 if (statspec == NULL) return false;
00879
00880 relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE);
00881
00882 if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
00883 uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
00884 if (callback != CALLBACK_FAILED) tile = callback;
00885 }
00886
00887 if (statspec->renderdata == NULL) {
00888 sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
00889 } else {
00890 sprites = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
00891 }
00892
00893 image = sprites->ground.sprite;
00894 SpriteID pal = sprites->ground.pal;
00895 if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) {
00896 image += GetCustomStationGroundRelocation(statspec, NULL, INVALID_TILE);
00897 image += rti->custom_ground_offset;
00898 } else {
00899 image += rti->total_offset;
00900 }
00901
00902 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00903
00904 Point child_offset = {0, 0};
00905
00906 foreach_draw_tile_seq(seq, sprites->seq) {
00907 image = seq->image.sprite;
00908 if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) {
00909 image += rti->total_offset;
00910 } else {
00911 image += relocation;
00912 }
00913
00914 pal = SpriteLayoutPaletteTransform(image, seq->image.pal, palette);
00915
00916 if ((byte)seq->delta_z != 0x80) {
00917 Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
00918 DrawSprite(image, pal, x + pt.x, y + pt.y);
00919
00920 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
00921 child_offset.x = pt.x + spr->x_offs;
00922 child_offset.y = pt.y + spr->y_offs;
00923 } else {
00924
00925 DrawSprite(image, pal, x + child_offset.x + seq->delta_x, y + child_offset.y + seq->delta_y);
00926 }
00927 }
00928
00929 return true;
00930 }
00931
00932
00933 const StationSpec *GetStationSpec(TileIndex t)
00934 {
00935 if (!IsCustomStationSpecIndex(t)) return NULL;
00936
00937 const BaseStation *st = BaseStation::GetByTile(t);
00938 uint specindex = GetCustomStationSpecIndex(t);
00939 return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
00940 }
00941
00942
00943
00944
00945 bool IsStationTileBlocked(TileIndex tile)
00946 {
00947 const StationSpec *statspec = GetStationSpec(tile);
00948
00949 return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile));
00950 }
00951
00952
00953
00954 bool IsStationTileElectrifiable(TileIndex tile)
00955 {
00956 const StationSpec *statspec = GetStationSpec(tile);
00957
00958 return
00959 statspec == NULL ||
00960 HasBit(statspec->pylons, GetStationGfx(tile)) ||
00961 !HasBit(statspec->wires, GetStationGfx(tile));
00962 }
00963
00964 void AnimateStationTile(TileIndex tile)
00965 {
00966 const StationSpec *ss = GetStationSpec(tile);
00967 if (ss == NULL) return;
00968
00969 const BaseStation *st = BaseStation::GetByTile(tile);
00970
00971 uint8 animation_speed = ss->anim_speed;
00972
00973 if (HasBit(ss->callback_mask, CBM_STATION_ANIMATION_SPEED)) {
00974 uint16 callback = GetStationCallback(CBID_STATION_ANIMATION_SPEED, 0, 0, ss, st, tile);
00975 if (callback != CALLBACK_FAILED) animation_speed = Clamp(callback & 0xFF, 0, 16);
00976 }
00977
00978 if (_tick_counter % (1 << animation_speed) != 0) return;
00979
00980 uint8 frame = GetStationAnimationFrame(tile);
00981 uint8 num_frames = ss->anim_frames;
00982
00983 bool frame_set_by_callback = false;
00984
00985 if (HasBit(ss->callback_mask, CBM_STATION_ANIMATION_NEXT_FRAME)) {
00986 uint32 param = HasBit(ss->flags, SSF_CB141_RANDOM_BITS) ? Random() : 0;
00987 uint16 callback = GetStationCallback(CBID_STATION_ANIM_NEXT_FRAME, param, 0, ss, st, tile);
00988
00989 if (callback != CALLBACK_FAILED) {
00990 frame_set_by_callback = true;
00991
00992 switch (callback & 0xFF) {
00993 case 0xFF:
00994 DeleteAnimatedTile(tile);
00995 break;
00996
00997 case 0xFE:
00998 frame_set_by_callback = false;
00999 break;
01000
01001 default:
01002 frame = callback & 0xFF;
01003 break;
01004 }
01005
01006
01007
01008 if (GB(callback, 8, 7) != 0) PlayTileSound(ss->grffile, GB(callback, 8, 7), tile);
01009 }
01010 }
01011
01012 if (!frame_set_by_callback) {
01013 if (frame < num_frames) {
01014 frame++;
01015 } else if (frame == num_frames && HasBit(ss->anim_status, 0)) {
01016
01017 frame = 0;
01018 } else {
01019
01020 DeleteAnimatedTile(tile);
01021 }
01022 }
01023
01024 SetStationAnimationFrame(tile, frame);
01025 MarkTileDirtyByTile(tile);
01026 }
01027
01028
01029 static void ChangeStationAnimationFrame(const StationSpec *ss, const BaseStation *st, TileIndex tile, uint16 random_bits, StatAnimTrigger trigger, CargoID cargo_type)
01030 {
01031 uint16 callback = GetStationCallback(CBID_STATION_ANIM_START_STOP, (random_bits << 16) | Random(), (uint8)trigger | (cargo_type << 8), ss, st, tile);
01032 if (callback == CALLBACK_FAILED) return;
01033
01034 switch (callback & 0xFF) {
01035 case 0xFD: break;
01036 case 0xFE: AddAnimatedTile(tile); break;
01037 case 0xFF: DeleteAnimatedTile(tile); break;
01038 default:
01039 SetStationAnimationFrame(tile, callback);
01040 AddAnimatedTile(tile);
01041 break;
01042 }
01043
01044
01045
01046 if (GB(callback, 8, 7) != 0) PlayTileSound(ss->grffile, GB(callback, 8, 7), tile);
01047 }
01048
01049 void StationAnimationTrigger(const BaseStation *st, TileIndex tile, StatAnimTrigger trigger, CargoID cargo_type)
01050 {
01051
01052 static const TriggerArea tas[] = {
01053 TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
01054 };
01055
01056
01057 if (st == NULL) st = BaseStation::GetByTile(tile);
01058
01059
01060
01061 if (!HasBit(st->cached_anim_triggers, trigger)) return;
01062
01063 uint16 random_bits = Random();
01064 ETileArea area = ETileArea(st, tile, tas[trigger]);
01065
01066
01067 for (uint y = 0; y < area.h; y++) {
01068 for (uint x = 0; x < area.w; x++) {
01069 if (st->TileBelongsToRailStation(area.tile)) {
01070 const StationSpec *ss = GetStationSpec(area.tile);
01071 if (ss != NULL && HasBit(ss->anim_triggers, trigger)) {
01072 CargoID cargo;
01073 if (cargo_type == CT_INVALID) {
01074 cargo = CT_INVALID;
01075 } else {
01076 cargo = GetReverseCargoTranslation(cargo_type, ss->grffile);
01077 }
01078 ChangeStationAnimationFrame(ss, st, area.tile, random_bits, trigger, cargo);
01079 }
01080 }
01081 area.tile += TileDiffXY(1, 0);
01082 }
01083 area.tile += TileDiffXY(-area.w, 1);
01084 }
01085 }
01086
01091 void StationUpdateAnimTriggers(BaseStation *st)
01092 {
01093 st->cached_anim_triggers = 0;
01094
01095
01096
01097 for (uint i = 0; i < st->num_specs; i++) {
01098 const StationSpec *ss = st->speclist[i].spec;
01099 if (ss != NULL) st->cached_anim_triggers |= ss->anim_triggers;
01100 }
01101 }