(svn r10586) [NewGRF_ports] -Feature: Load an airport via a newgrf file. Currently, only graphics support provided.
Try loading rb_airports.grf as a newgrf. Airport gui should list one Commuter airport in the Small Airports category.
DO NOT TRY TO USE NORMAL AIRPORTS. All normal stuff is disabled, to validate that airport is coming from newgrf file.
Work in progress... dont expect miracles!
--- a/docs/landscape.html Mon Jul 16 00:31:25 2007 +0000
+++ b/docs/landscape.html Mon Jul 16 00:39:06 2007 +0000
@@ -961,6 +961,7 @@
</tr>
</table>
</li>
+ <li>m6 bit 7: 1 when newstation is an fsmport, otherwise 0</li>
<li>m6 bit 3: 1 when a drive through road stop is built over a town owned road, otherwise 0</li>
<li>m6 bits 1..0 : <a href="#tropic_zone">Tropic zone definition</a></li>
</ul>
--- a/docs/landscape_grid.html Mon Jul 16 00:31:25 2007 +0000
+++ b/docs/landscape_grid.html Mon Jul 16 00:39:06 2007 +0000
@@ -184,7 +184,7 @@
<td class="bits">XXXX XXXX</td>
<td class="bits">XXXX XXXX</td>
<td class="bits">XXXX XXXX</td>
- <td class="bits"><span class="free">OOOO O</span>XXX</td>
+ <td class="bits">X<span class="free">OOO O</span>XXX</td>
<td class="bits"><span class="free">OOOO OOOO</span></td>
</tr>
<tr>
--- a/projects/openttd_vs80.vcproj Mon Jul 16 00:31:25 2007 +0000
+++ b/projects/openttd_vs80.vcproj Mon Jul 16 00:39:06 2007 +0000
@@ -468,11 +468,11 @@
>
</File>
<File
- RelativePath=".\..\src\cargotype.cpp"
+ RelativePath=".\..\src\cargopacket.cpp"
>
</File>
<File
- RelativePath=".\..\src\cargopacket.cpp"
+ RelativePath=".\..\src\cargotype.cpp"
>
</File>
<File
@@ -752,11 +752,11 @@
>
</File>
<File
- RelativePath=".\..\src\articulated_vehicles.h"
+ RelativePath=".\..\src\airport_states.h"
>
</File>
<File
- RelativePath=".\..\src\airport_states.h"
+ RelativePath=".\..\src\articulated_vehicles.h"
>
</File>
<File
@@ -768,11 +768,11 @@
>
</File>
<File
- RelativePath=".\..\src\cargotype.h"
+ RelativePath=".\..\src\cargopacket.h"
>
</File>
<File
- RelativePath=".\..\src\cargopacket.h"
+ RelativePath=".\..\src\cargotype.h"
>
</File>
<File
@@ -956,6 +956,10 @@
>
</File>
<File
+ RelativePath="..\src\newgrf_fsmports.h"
+ >
+ </File>
+ <File
RelativePath=".\..\src\newgrf_house.h"
>
</File>
@@ -1676,6 +1680,10 @@
>
</File>
<File
+ RelativePath="..\src\newgrf_fsmports.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\newgrf_house.cpp"
>
</File>
--- a/source.list Mon Jul 16 00:31:25 2007 +0000
+++ b/source.list Mon Jul 16 00:39:06 2007 +0000
@@ -149,6 +149,7 @@
newgrf_commons.h
newgrf_config.h
newgrf_engine.h
+newgrf_fsmports.h
newgrf_house.h
newgrf_industries.h
newgrf_industrytiles.h
@@ -339,6 +340,7 @@
newgrf_commons.cpp
newgrf_config.cpp
newgrf_engine.cpp
+newgrf_fsmports.cpp
newgrf_house.cpp
newgrf_industries.cpp
newgrf_industrytiles.cpp
--- a/src/airport_gui.cpp Mon Jul 16 00:31:25 2007 +0000
+++ b/src/airport_gui.cpp Mon Jul 16 00:39:06 2007 +0000
@@ -18,6 +18,25 @@
#include "station.h"
#include "airport.h"
#include "depot.h"
+#include "newgrf_callbacks.h"
+
+//TODO: remove station.h once converted to newgrf airports. stations just used as temporary loader
+#include "station.h"
+static struct {
+ byte orientation;
+ byte numtracks;
+ byte platlength;
+ bool dragdrop;
+
+ bool newstations;
+ bool newstation_selected;
+ FSMportsClassIDByte fsmports_class;
+ byte station_type;
+ byte station_count;
+} _railstation;
+//TODO: remove above after conversion
+
+
static byte _selected_airport_type;
@@ -34,7 +53,11 @@
static void PlaceAirport(TileIndex tile)
{
- DoCommandP(tile, _selected_airport_type, _ctrl_pressed, CcBuildAirport, CMD_BUILD_AIRPORT | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_A001_CAN_T_BUILD_AIRPORT_HERE));
+ //DoCommandP(tile, _selected_airport_type, _ctrl_pressed, CcBuildAirport, CMD_BUILD_AIRPORT | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_A001_CAN_T_BUILD_AIRPORT_HERE));
+ DoCommandP(tile,
+ _railstation.orientation | (_railstation.numtracks << 8) | (_railstation.platlength << 16) | (_ctrl_pressed << 24),
+ _selected_airport_type | (_railstation.fsmports_class << 8) | (_railstation.station_type << 16), CcBuildAirport,
+ CMD_BUILD_AIRPORT | CMD_NO_WATER | CMD_AUTO | CMD_MSG(STR_A001_CAN_T_BUILD_AIRPORT_HERE));
}
static void PlaceAir_DemolishArea(TileIndex tile)
@@ -151,6 +174,10 @@
break;
case WE_PAINT: {
+ bool newstations = (GetNumFSMportsClasses() != 0);
+ int y_offset;
+ const FSMportsSpec *fsmportspec = newstations ? GetCustomFSMportsSpec(_railstation.fsmports_class, _railstation.station_type) : NULL;
+
int i; // airport enabling loop
uint32 avail_airports;
const AirportFTAClass *airport;
@@ -172,9 +199,19 @@
*/
for (i = 0; i < 9; i++) SetWindowWidgetDisabledState(w, i + 7, !HASBIT(avail_airports, i));
- // select default the coverage area to 'Off' (16)
airport = GetAirport(_selected_airport_type);
- SetTileSelectSize(airport->size_x, airport->size_y);
+
+ if (_railstation.newstation_selected) {
+ int x = _railstation.platlength;
+ int y = _railstation.numtracks;
+ if (_railstation.orientation == 0) Swap(x, y);
+ SetTileSelectSize(x, y);
+ } else {
+
+ // select default the coverage area to 'Off' (16)
+ SetTileSelectSize(airport->size_x, airport->size_y);
+ }
+
_thd.airport = _selected_airport_type;
_thd.airport_template = true;
@@ -182,10 +219,37 @@
if (_station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
+ SetDParam(0, GetFSMportsClassName(_railstation.fsmports_class));
DrawWindowWidgets(w);
+
+ y_offset = newstations ? 90 : 0;
+
// strings such as 'Size' and 'Coverage Area'
// 'Coverage Area'
- DrawStationCoverageAreaText(2, 206, SCT_ALL, rad);
+ DrawStationCoverageAreaText(2, 206 + y_offset, SCT_ALL, rad);
+
+ if (newstations) {
+ uint16 i;
+ uint y = 35;
+
+ for (i = w->vscroll.pos; i < _railstation.station_count && i < (uint)(w->vscroll.pos + w->vscroll.cap); i++) {
+ const FSMportsSpec *fsmportspec = GetCustomFSMportsSpec(_railstation.fsmports_class, i);
+
+ if (fsmportspec != NULL && fsmportspec->name != 0) {
+ if (HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) && GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) == 0) {
+ GfxFillRect(8, y - 2, 127, y + 10, (1 << PALETTE_MODIFIER_GREYOUT));
+ }
+
+ DrawStringTruncated(9, y, fsmportspec->name, i == _railstation.station_type ? 12 : 16, 118);
+ } else {
+ DrawStringTruncated(9, y, STR_STAT_CLASS_DFLT, i == _railstation.station_type ? 12 : 16, 118);
+ }
+
+ y += 14;
+ }
+ }
+
+
break;
}
@@ -205,9 +269,52 @@
SndPlayFx(SND_15_BEEP);
SetWindowDirty(w);
break;
+ case 23:
+ case 24:
+ ShowDropDownMenu(w, BuildFSMportsClassDropdown(), _railstation.fsmports_class, 24, 0, 1 << FSMPORTS_CLASS_WAYP);
+ break;
+
+ case 25: {
+ const FSMportsSpec *fsmportspec;
+ int y = (e->we.click.pt.y - 32) / 14;
+
+ if (y >= w->vscroll.cap) return;
+ y += w->vscroll.pos;
+ if (y >= _railstation.station_count) return;
+
+ /* Check station availability callback */
+ fsmportspec = GetCustomFSMportsSpec(_railstation.fsmports_class, y);
+ if (fsmportspec != NULL &&
+ HASBIT(fsmportspec->callbackmask, CBM_STATION_AVAIL) &&
+ GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportspec, NULL, INVALID_TILE) == 0) return;
+
+ _railstation.station_type = y;
+ _railstation.numtracks = fsmportspec->width;
+ _railstation.platlength = fsmportspec->lengths;
+ _railstation.newstation_selected = true;
+
+ SndPlayFx(SND_15_BEEP);
+ SetWindowDirty(w);
+ break;
+ }
}
} break;
+ case WE_DROPDOWN_SELECT: {
+ if (_railstation.fsmports_class != e->we.dropdown.index) {
+ _railstation.fsmports_class = (FSMportsClassID)e->we.dropdown.index;
+ _railstation.station_type = 0;
+ _railstation.station_count = GetNumCustomFSMports(_railstation.fsmports_class);
+
+ w->vscroll.count = _railstation.station_count;
+ w->vscroll.pos = _railstation.station_type;
+ }
+
+ SndPlayFx(SND_15_BEEP);
+ SetWindowDirty(w);
+ } break;
+
+
case WE_MOUSELOOP: {
if (WP(w, def_d).close) {
DeleteWindow(w);
@@ -250,6 +357,39 @@
{ WIDGETS_END},
};
+static const Widget _newports_build_airport_picker_widgets[] = {
+{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
+{ WWT_CAPTION, RESIZE_NONE, 7, 11, 147, 0, 13, STR_3001_AIRPORT_SELECTION, STR_018C_WINDOW_TITLE_DRAG_THIS},
+{ WWT_PANEL, RESIZE_NONE, 7, 0, 147, 14, 142, 0x0, STR_NULL},
+{ WWT_PANEL, RESIZE_NONE, 7, 0, 147, 143, 179, 0x0, STR_NULL},
+{ WWT_PANEL, RESIZE_NONE, 7, 0, 147, 180, 217, 0x0, STR_NULL},
+{ WWT_PANEL, RESIZE_NONE, 7, 0, 147, 218, 267, 0x0, STR_NULL},
+{ WWT_PANEL, RESIZE_NONE, 7, 0, 147, 268, 329, 0x0, STR_NULL},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 117, 128, STR_SMALL_AIRPORT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 155, 166, STR_CITY_AIRPORT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 231, 242, STR_HELIPORT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 167, 178, STR_METRO_AIRPORT , STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 193, 204, STR_INTERNATIONAL_AIRPORT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 129, 140, STR_COMMUTER_AIRPORT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 255, 266, STR_HELIDEPOT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 205, 216, STR_INTERCONTINENTAL_AIRPORT, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 2, 145, 243, 254, STR_HELISTATION, STR_3058_SELECT_SIZE_TYPE_OF_AIRPORT},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 14, 73, 281, 292, STR_02DB_OFF, STR_3065_DON_T_HIGHLIGHT_COVERAGE},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 74, 133, 281, 292, STR_02DA_ON, STR_3064_HIGHLIGHT_COVERAGE_AREA},
+{ WWT_LABEL, RESIZE_NONE, 7, 0, 147, 104, 117, STR_SMALL_AIRPORTS, STR_NULL},
+{ WWT_LABEL, RESIZE_NONE, 7, 0, 147, 142, 155, STR_LARGE_AIRPORTS, STR_NULL},
+{ WWT_LABEL, RESIZE_NONE, 7, 0, 147, 180, 193, STR_HUB_AIRPORTS, STR_NULL},
+{ WWT_LABEL, RESIZE_NONE, 7, 0, 147, 218, 231, STR_HELIPORTS, STR_NULL},
+{ WWT_LABEL, RESIZE_NONE, 7, 0, 147, 268, 281, STR_3066_COVERAGE_AREA_HIGHLIGHT, STR_NULL},
+/* newstations gui additions */
+//TODO: modify for airport specific texts
+{ WWT_INSET, RESIZE_NONE, 14, 7, 140, 17, 28, STR_02BD, STR_SELECT_STATION_CLASS_TIP},
+{ WWT_TEXTBTN, RESIZE_NONE, 14, 129, 139, 18, 27, STR_0225, STR_SELECT_STATION_CLASS_TIP},
+{ WWT_MATRIX, RESIZE_NONE, 14, 7, 128, 32, 102, 0x501, STR_SELECT_STATION_TYPE_TIP},
+{ WWT_SCROLLBAR, RESIZE_NONE, 14, 129, 140, 32, 102, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
+{ WIDGETS_END},
+};
+
static const WindowDesc _build_airport_desc = {
WDP_AUTO, WDP_AUTO, 148, 240,
WC_BUILD_STATION, WC_BUILD_TOOLBAR,
@@ -258,9 +398,30 @@
BuildAirportPickerWndProc
};
+static const WindowDesc _newports_build_airport_desc = {
+ WDP_AUTO, WDP_AUTO, 148, 330,
+ WC_BUILD_STATION, WC_BUILD_TOOLBAR,
+ WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
+ _newports_build_airport_picker_widgets,
+ BuildAirportPickerWndProc
+};
+
static void ShowBuildAirportPicker()
{
- AllocateWindowDesc(&_build_airport_desc);
+ Window *w;
+ if (GetNumFSMportsClasses() == 0) {
+ w = AllocateWindowDesc(&_build_airport_desc);
+ _railstation.newstations = false;
+ } else
+ {
+ w = AllocateWindowDesc(&_newports_build_airport_desc);
+ _railstation.newstations = true;
+ _railstation.station_count = GetNumCustomFSMports(_railstation.fsmports_class);
+
+ w->vscroll.count = _railstation.station_count;
+ w->vscroll.cap = 5;
+ w->vscroll.pos = clamp(_railstation.station_type - 2, 0, w->vscroll.count - w->vscroll.cap);
+ }
}
void InitializeAirportGui()
--- a/src/newgrf.cpp Mon Jul 16 00:31:25 2007 +0000
+++ b/src/newgrf.cpp Mon Jul 16 00:39:06 2007 +0000
@@ -45,6 +45,7 @@
#include "newgrf_commons.h"
#include "newgrf_townname.h"
#include "newgrf_industries.h"
+#include "newgrf_fsmports.h"
/* TTDPatch extended GRF format codec
* (c) Petr Baudis 2004 (GPL'd)
@@ -2044,9 +2045,7 @@
static bool FSMPortChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
{
- StationSpec **statspec;
byte *buf = *bufp;
- int i;
bool ret = false;
if (stid + numinfo > MAX_FSM_PORTS) {
@@ -2055,36 +2054,36 @@
}
/* Allocate station specs if necessary */
- if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
+ if (_cur_grffile->fsmports == NULL) _cur_grffile->fsmports = CallocT<FSMportsSpec*>(MAX_FSM_PORTS);
for (int i = 0; i < numinfo; i++) {
- StationSpec *statspec = _cur_grffile->stations[stid + i];
+ FSMportsSpec *fsmportspec = _cur_grffile->fsmports[stid + i];
/* Check that the station we are modifying is defined. */
- if (statspec == NULL && prop != 0x08) {
+ if (fsmportspec == NULL && prop != 0x08) {
grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
continue;
}
switch (prop) {
case 0x08: { // Class ID
- StationSpec **spec = &_cur_grffile->stations[stid + i];
+ FSMportsSpec **fsmportspec = &_cur_grffile->fsmports[stid + i];
/* Property 0x08 is special; it is where the station is allocated */
- if (*spec == NULL) *spec = CallocT<StationSpec>(1);
+ if (*fsmportspec == NULL) *fsmportspec = CallocT<FSMportsSpec>(1);
/* Swap classid because we read it in BE meaning WAYP or DFLT */
uint32 classid = grf_load_dword(&buf);
- (*spec)->sclass = AllocateStationClass(BSWAP32(classid));
+ (*fsmportspec)->sclass = AllocateFSMportsClass(BSWAP32(classid));
} break;
case 0x09: // Define sprite layout
- statspec->tiles = grf_load_extended(&buf);
- statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
- statspec->copied_renderdata = false;
-
- for (uint t = 0; t < statspec->tiles; t++) {
- DrawTileSprites *dts = &statspec->renderdata[t];
+ fsmportspec->tiles = grf_load_extended(&buf);
+ fsmportspec->renderdata = CallocT<DrawTileSprites>(fsmportspec->tiles);
+ fsmportspec->copied_renderdata = false;
+
+ for (uint t = 0; t < fsmportspec->tiles; t++) {
+ DrawTileSprites *dts = &fsmportspec->renderdata[t];
uint seq_count = 0;
dts->seq = NULL;
@@ -2133,58 +2132,43 @@
case 0x0A: { // Copy sprite layout
byte srcid = grf_load_byte(&buf);
- const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
-
- statspec->tiles = srcstatspec->tiles;
- statspec->renderdata = srcstatspec->renderdata;
- statspec->copied_renderdata = true;
+ const FSMportsSpec *srcfsmportspec = _cur_grffile->fsmports[srcid];
+
+ fsmportspec->tiles = srcfsmportspec->tiles;
+ fsmportspec->renderdata = srcfsmportspec->renderdata;
+ fsmportspec->copied_renderdata = true;
} break;
case 0x0B: // Callback mask
- statspec->callbackmask = grf_load_byte(&buf);
- break;
-
- case 0x0C: // Disallowed number of platforms
- statspec->disallowed_platforms = grf_load_byte(&buf);
- break;
-
- case 0x0D: // Disallowed platform lengths
- statspec->disallowed_lengths = grf_load_byte(&buf);
+ fsmportspec->callbackmask = grf_load_byte(&buf);
break;
case 0x0E: // Define custom layout
- statspec->copied_layouts = false;
+ fsmportspec->copied_layouts = false;
while (buf < *bufp + len) {
byte length = grf_load_byte(&buf);
byte number = grf_load_byte(&buf);
- StationLayout layout;
+ FSMportsLayout layout;
uint l, p;
if (length == 0 || number == 0) break;
- //debug("l %d > %d ?", length, stat->lengths);
- if (length > statspec->lengths) {
- statspec->platforms = ReallocT(statspec->platforms, length);
- memset(statspec->platforms + statspec->lengths, 0, length - statspec->lengths);
-
- statspec->layouts = ReallocT(statspec->layouts, length);
- memset(statspec->layouts + statspec->lengths, 0,
- (length - statspec->lengths) * sizeof(*statspec->layouts));
-
- statspec->lengths = length;
+ l = length - 1; // index is zero-based
+
+ //debug("l %d > %d ?", length, fsmportspec->lengths);
+ if (length > fsmportspec->lengths) {
+ fsmportspec->layouts = ReallocT(fsmportspec->layouts, length);
+ memset(fsmportspec->layouts + fsmportspec->lengths, 0,
+ (length - fsmportspec->lengths) * sizeof(*fsmportspec->layouts));
+
+ /* reallocate memory for graphics layout */
+ fsmportspec->layouts[l] = ReallocT(fsmportspec->layouts[l], number);
+ memset(fsmportspec->layouts[l] + number - 1, 0, (sizeof(**fsmportspec->layouts)));
}
- l = length - 1; // index is zero-based
-
- //debug("p %d > %d ?", number, stat->platforms[l]);
- if (number > statspec->platforms[l]) {
- statspec->layouts[l] = ReallocT(statspec->layouts[l], number);
- /* We expect NULL being 0 here, but C99 guarantees that. */
- memset(statspec->layouts[l] + statspec->platforms[l], 0,
- (number - statspec->platforms[l]) * sizeof(**statspec->layouts));
-
- statspec->platforms[l] = number;
- }
+
+ fsmportspec->lengths = number;
+ fsmportspec->width = length;
p = 0;
layout = MallocT<byte>(length * number);
@@ -2196,43 +2180,27 @@
l--;
p--;
- free(statspec->layouts[l][p]);
- statspec->layouts[l][p] = layout;
+ free(fsmportspec->layouts[l][p]);
+ fsmportspec->layouts[l][p] = layout;
}
break;
case 0x0F: { // Copy custom layout
byte srcid = grf_load_byte(&buf);
- const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
-
- statspec->lengths = srcstatspec->lengths;
- statspec->platforms = srcstatspec->platforms;
- statspec->layouts = srcstatspec->layouts;
- statspec->copied_layouts = true;
+ const FSMportsSpec *srcfsmportspec = _cur_grffile->fsmports[srcid];
+
+ fsmportspec->lengths = srcfsmportspec->lengths;
+ fsmportspec->width = srcfsmportspec->width;
+ fsmportspec->layouts = srcfsmportspec->layouts;
+ fsmportspec->copied_layouts = true;
} break;
case 0x10: // Little/lots cargo threshold
- statspec->cargo_threshold = grf_load_word(&buf);
- break;
-
- case 0x11: // Pylon placement
- statspec->pylons = grf_load_byte(&buf);
- break;
-
- case 0x12: // Cargo types for random triggers
- statspec->cargo_triggers = grf_load_dword(&buf);
+ fsmportspec->cargo_threshold = grf_load_word(&buf);
break;
case 0x13: // General flags
- statspec->flags = grf_load_byte(&buf);
- break;
-
- case 0x14: // Overhead wire placement
- statspec->wires = grf_load_byte(&buf);
- break;
-
- case 0x15: // Blocked tiles
- statspec->blocked = grf_load_byte(&buf);
+ fsmportspec->flags = grf_load_byte(&buf);
break;
case 0x16: // @todo Animation info
@@ -2753,6 +2721,7 @@
case GSF_STATION:
case GSF_CANAL:
case GSF_CARGOS:
+ case GSF_FSMPORTS:
{
byte sprites = _cur_grffile->spriteset_numents;
byte num_loaded = type;
@@ -3223,6 +3192,53 @@
}
}
+static void FSMportsMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
+{
+ for (uint i = 0; i < idcount; i++) {
+ uint8 stid = buf[3 + i];
+ FSMportsSpec *fsmportsspec = _cur_grffile->fsmports[stid];
+ byte *bp = &buf[4 + idcount];
+
+ for (uint c = 0; c < cidcount; c++) {
+ uint8 ctype = grf_load_byte(&bp);
+ uint16 groupid = grf_load_word(&bp);
+
+ if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
+ grfmsg(1, "StationMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
+ groupid, _cur_grffile->spritegroups_count);
+ continue;
+ }
+
+ ctype = TranslateCargo(GSF_STATION, ctype);
+ if (ctype == CT_INVALID) continue;
+
+ fsmportsspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
+ }
+ }
+
+ {
+ byte *bp = &buf[4 + idcount + cidcount * 3];
+ uint16 groupid = grf_load_word(&bp);
+
+ if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
+ grfmsg(1, "StationMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
+ groupid, _cur_grffile->spritegroups_count);
+ return;
+ }
+
+ for (uint i = 0; i < idcount; i++) {
+ uint8 stid = buf[3 + i];
+ FSMportsSpec *fsmportsspec = _cur_grffile->fsmports[stid];
+
+ fsmportsspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
+ fsmportsspec->grffile = _cur_grffile;
+ fsmportsspec->localidx = stid;
+ SetCustomFSMportsSpec(fsmportsspec);
+ }
+ }
+}
+
+
/* Action 0x03 */
static void FeatureMapSpriteGroup(byte *buf, int len)
@@ -3298,6 +3314,10 @@
CargoMapSpriteGroup(buf, idcount, cidcount);
return;
+ case GSF_FSMPORTS:
+ FSMportsMapSpriteGroup(buf, idcount, cidcount);
+ return;
+
default:
grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
return;
@@ -3399,6 +3419,23 @@
}
break;
+ case 0xCD: // FSMport class name
+ if (_cur_grffile->fsmports == NULL || _cur_grffile->fsmports[GB(id, 0, 8)] == NULL) {
+ grfmsg(1, "FeatureNewName: Attempt to name undefined fsmport class 0x%X, ignoring", GB(id, 0, 8));
+ } else {
+ FSMportsClassID sclass = _cur_grffile->fsmports[GB(id, 0, 8)]->sclass;
+ SetFSMportsClassName(sclass, AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED));
+ }
+ break;
+
+ case 0xCE: // FSMport name
+ if (_cur_grffile->fsmports == NULL || _cur_grffile->fsmports[GB(id, 0, 8)] == NULL) {
+ grfmsg(1, "FeatureNewName: Attempt to name undefined fsmport 0x%X, ignoring", GB(id, 0, 8));
+ } else {
+ _cur_grffile->fsmports[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
+ }
+ break;
+
case 0xD0:
case 0xDC:
AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
@@ -5151,6 +5188,9 @@
ResetStationClasses();
ResetCustomStations();
+ /* Reset FSMports classes */
+ ResetFSMportsClasses();
+
/* Reset canal sprite groups */
memset(_canal_sg, 0, sizeof(_canal_sg));
--- a/src/newgrf.h Mon Jul 16 00:31:25 2007 +0000
+++ b/src/newgrf.h Mon Jul 16 00:39:06 2007 +0000
@@ -11,6 +11,7 @@
#include "helpers.hpp"
#include "cargotype.h"
#include "industry.h"
+#include "newgrf_fsmports.h"
enum GrfLoadingStage {
GLS_FILESCAN,
@@ -72,6 +73,7 @@
HouseSpec **housespec;
IndustrySpec **industryspec;
IndustryTileSpec **indtspec;
+ FSMportsSpec **fsmports;
uint32 param[0x80];
uint param_end; ///< one more than the highest set parameter
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/newgrf_fsmports.cpp Mon Jul 16 00:39:06 2007 +0000
@@ -0,0 +1,652 @@
+/* $Id$ */
+
+/** @file newgrf_station.cpp Functions for dealing with station classes and custom stations. */
+
+#include "stdafx.h"
+#include "openttd.h"
+#include "variables.h"
+#include "functions.h"
+#include "landscape.h"
+#include "debug.h"
+#include "sprite.h"
+#include "table/sprites.h"
+#include "table/strings.h"
+#include "station.h"
+#include "station_map.h"
+#include "newgrf.h"
+#include "newgrf_callbacks.h"
+#include "newgrf_commons.h"
+#include "newgrf_station.h"
+#include "newgrf_spritegroup.h"
+#include "date.h"
+#include "helpers.hpp"
+#include "cargotype.h"
+#include "town_map.h"
+#include "newgrf_town.h"
+
+static FSMportsClass fsmports_classes[FSMPORTS_CLASS_MAX];
+
+enum {
+ MAX_SPECLIST = 255,
+};
+
+/**
+ * Reset station classes to their default state.
+ * This includes initialising the Default and Waypoint classes with an empty
+ * entry, for standard stations and waypoints.
+ */
+void ResetFSMportsClasses()
+{
+ for (FSMportsClassID i = FSMPORTS_CLASS_BEGIN; i < FSMPORTS_CLASS_MAX; i++) {
+ fsmports_classes[i].id = 0;
+ fsmports_classes[i].name = STR_EMPTY;
+ fsmports_classes[i].FSMports = 0;
+
+ free(fsmports_classes[i].spec);
+ fsmports_classes[i].spec = NULL;
+ }
+
+ /* Set up initial data */
+ //fsmports_classes[0].id = 'DFLT';
+ //fsmports_classes[0].name = STR_STAT_CLASS_DFLT;
+ //fsmports_classes[0].FSMports = 1;
+ //fsmports_classes[0].spec = MallocT<FSMportsSpec*>(1);
+ //fsmports_classes[0].spec[0] = NULL;
+
+ //fsmports_classes[1].id = 'WAYP';
+ //fsmports_classes[1].name = STR_STAT_CLASS_WAYP;
+ //fsmports_classes[1].FSMports = 1;
+ //fsmports_classes[1].spec = MallocT<FSMportsSpec*>(1);
+ //fsmports_classes[1].spec[0] = NULL;
+}
+
+/**
+ * Allocate a station class for the given class id.
+ * @param cls A 32 bit value identifying the class.
+ * @return Index into station_classes of allocated class.
+ */
+FSMportsClassID AllocateFSMportsClass(uint32 cls)
+{
+ for (FSMportsClassID i = FSMPORTS_CLASS_BEGIN; i < FSMPORTS_CLASS_MAX; i++) {
+ if (fsmports_classes[i].id == cls) {
+ /* ClassID is already allocated, so reuse it. */
+ return i;
+ } else if (fsmports_classes[i].id == 0) {
+ /* This class is empty, so allocate it to the ClassID. */
+ fsmports_classes[i].id = cls;
+ return i;
+ }
+ }
+
+ grfmsg(2, "FSMportsClassAllocate: already allocated %d classes, using default", FSMPORTS_CLASS_MAX);
+ return FSMPORTS_CLASS_DFLT;
+}
+
+/** Set the name of a custom station class */
+void SetFSMportsClassName(FSMportsClassID sclass, StringID name)
+{
+ assert(sclass < FSMPORTS_CLASS_MAX);
+ fsmports_classes[sclass].name = name;
+}
+
+/** Retrieve the name of a custom station class */
+StringID GetFSMportsClassName(FSMportsClassID sclass)
+{
+ assert(sclass < FSMPORTS_CLASS_MAX);
+ return fsmports_classes[sclass].name;
+}
+
+/** Build a list of station class name StringIDs to use in a dropdown list
+ * @return Pointer to a (static) array of StringIDs
+ */
+StringID *BuildFSMportsClassDropdown()
+{
+ /* Allow room for all station classes, plus a terminator entry */
+ static StringID names[FSMPORTS_CLASS_MAX + 1];
+ uint i;
+
+ /* Add each name */
+ for (i = 0; i < FSMPORTS_CLASS_MAX && fsmports_classes[i].id != 0; i++) {
+ names[i] = fsmports_classes[i].name;
+ }
+ /* Terminate the list */
+ names[i] = INVALID_STRING_ID;
+
+ return names;
+}
+
+/**
+ * Get the number of station classes in use.
+ * @return Number of station classes.
+ */
+uint GetNumFSMportsClasses()
+{
+ uint i;
+ for (i = 0; i < FSMPORTS_CLASS_MAX && fsmports_classes[i].id != 0; i++);
+ return i;
+}
+
+/**
+ * Return the number of stations for the given station class.
+ * @param sclass Index of the station class.
+ * @return Number of stations in the class.
+ */
+uint GetNumCustomFSMports(FSMportsClassID sclass)
+{
+ assert(sclass < FSMPORTS_CLASS_MAX);
+ return fsmports_classes[sclass].FSMports;
+}
+
+/**
+ * Tie a fsmport spec to its class.
+ * @param fsmportspec The station spec.
+ */
+void SetCustomFSMportsSpec(FSMportsSpec *fsmportspec)
+{
+ FSMportsClass *fsmports_class;
+ int i;
+
+ /* If the station has already been allocated, don't reallocate it. */
+ if (fsmportspec->allocated) return;
+
+ assert(fsmportspec->sclass < FSMPORTS_CLASS_MAX);
+ fsmports_class = &fsmports_classes[fsmportspec->sclass];
+
+ i = fsmports_class->FSMports++;
+ fsmports_class->spec = ReallocT(fsmports_class->spec, fsmports_class->FSMports);
+
+ fsmports_class->spec[i] = fsmportspec;
+ fsmportspec->allocated = true;
+}
+
+/**
+ * Retrieve a station spec from a class.
+ * @param sclass Index of the station class.
+ * @param station The station index with the class.
+ * @return The station spec.
+ */
+const FSMportsSpec *GetCustomFSMportsSpec(FSMportsClassID sclass, uint fsmport)
+{
+ assert(sclass < FSMPORTS_CLASS_MAX);
+ if (fsmport < fsmports_classes[sclass].FSMports)
+ return fsmports_classes[sclass].spec[fsmport];
+
+ /* If the custom station isn't defined any more, then the GRF file
+ * probably was not loaded. */
+ return NULL;
+}
+
+
+const FSMportsSpec *GetCustomFSMportsSpecByGrf(uint32 grfid, byte localidx)
+{
+ uint j;
+
+ for (FSMportsClassID i = FSMPORTS_CLASS_BEGIN; i < FSMPORTS_CLASS_MAX; i++) {
+ for (j = 0; j < fsmports_classes[i].FSMports; j++) {
+ const FSMportsSpec *fsmportspec = fsmports_classes[i].spec[j];
+ if (fsmportspec == NULL) continue;
+ if (fsmportspec->grffile->grfid == grfid && fsmportspec->localidx == localidx) return fsmportspec;
+ }
+ }
+
+ return NULL;
+}
+
+
+/* FSMports Resolver Functions */
+static uint32 FSMportsGetRandomBits(const ResolverObject *object)
+{
+ const Station *st = object->u.station.st;
+ const TileIndex tile = object->u.station.tile;
+ return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
+}
+
+
+static uint32 FSMportsGetTriggers(const ResolverObject *object)
+{
+ const Station *st = object->u.station.st;
+ return st == NULL ? 0 : st->waiting_triggers;
+}
+
+
+static void FSMportsSetTriggers(const ResolverObject *object, int triggers)
+{
+ Station *st = (Station*)object->u.station.st;
+ assert(st != NULL);
+ st->waiting_triggers = triggers;
+}
+
+/**
+ * FSMports variable cache
+ * This caches 'expensive' station variable lookups which iterate over
+ * several tiles that may be called multiple times per Resolve().
+ */
+static struct {
+ uint32 v40;
+ uint32 v41;
+ uint32 v45;
+ uint32 v46;
+ uint32 v47;
+ uint32 v49;
+ uint8 valid;
+} _svc;
+
+static uint32 FSMportsGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
+{
+ const Station *st = object->u.station.st;
+ TileIndex tile = object->u.station.tile;
+
+ if (object->scope == VSG_SCOPE_PARENT) {
+ /* Pass the request on to the town of the station */
+ Town *t;
+
+ if (st != NULL) {
+ t = st->town;
+ } else if (tile != INVALID_TILE) {
+ t = GetTownByTile(tile);
+ } else {
+ *available = false;
+ return UINT_MAX;
+ }
+
+ return TownGetVariable(variable, parameter, available, t);
+ }
+
+ if (st == NULL) {
+ /* Station does not exist, so we're in a purchase list */
+ switch (variable) {
+ case 0x40:
+ case 0x41:
+ case 0x46:
+ case 0x47:
+ case 0x49: return 0x2110000; // Platforms, tracks & position
+ case 0x42: return 0; // Rail type (XXX Get current type from GUI?)
+ case 0x43: return _current_player; // Station owner
+ case 0x44: return 2; // PBS status
+ case 0xFA: return clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
+ }
+
+ *available = false;
+ return UINT_MAX;
+ }
+
+ switch (variable) {
+ /* Calculated station variables */
+ case 0x42: return GetTerrainType(tile) | (GetRailType(tile) << 8);
+ case 0x43: return st->owner; // Station owner
+ case 0x48: { // Accepted cargo types
+ CargoID cargo_type;
+ uint32 value = 0;
+
+ for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
+ if (st->goods[cargo_type].acceptance) SETBIT(value, cargo_type);
+ }
+ return value;
+ }
+ /* Variables which use the parameter */
+ /* Variables 0x60 to 0x65 are handled separately below */
+
+ /* General station properties */
+ case 0x82: return 50;
+ case 0x84: return st->string_id;
+ case 0x86: return 0;
+ case 0x8A: return st->had_vehicle_of_type;
+ case 0xF0: return st->facilities;
+ case 0xF1: return st->airport_type;
+ case 0xF2: return st->truck_stops->status;
+ case 0xF3: return st->bus_stops->status;
+ case 0xF6: return st->airport_flags;
+ case 0xF7: return GB(st->airport_flags, 8, 8);
+ case 0xFA: return clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
+ }
+
+ /* Handle cargo variables with parameter, 0x60 to 0x65 */
+ if (variable >= 0x60 && variable <= 0x65) {
+ CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grffile);
+
+ if (c == CT_INVALID) return 0;
+ const GoodsEntry *ge = &st->goods[c];
+
+ switch (variable) {
+ case 0x60: return min(ge->cargo.Count(), 4095);
+ case 0x61: return ge->days_since_pickup;
+ case 0x62: return ge->rating;
+ case 0x63: return ge->cargo.DaysInTransit();
+ case 0x64: return ge->last_speed | (ge->last_age << 8);
+ case 0x65: return ge->acceptance << 3;
+ }
+ }
+
+ /* Handle cargo variables (deprecated) */
+ if (variable >= 0x8C && variable <= 0xEC) {
+ const GoodsEntry *g = &st->goods[GB(variable - 0x8C, 3, 4)];
+ switch (GB(variable - 0x8C, 0, 3)) {
+ case 0: return g->cargo.Count();
+ case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (g->acceptance << 7);
+ case 2: return g->days_since_pickup;
+ case 3: return g->rating;
+ case 4: return g->cargo.Source();
+ case 5: return g->cargo.DaysInTransit();
+ case 6: return g->last_speed;
+ case 7: return g->last_age;
+ }
+ }
+
+ DEBUG(grf, 1, "Unhandled station property 0x%X", variable);
+
+ *available = false;
+ return UINT_MAX;
+}
+
+
+static const SpriteGroup *FSMportsResolveReal(const ResolverObject *object, const SpriteGroup *group)
+{
+ const Station *st = object->u.station.st;
+ const FSMportsSpec *fsmportspec = object->u.station.fsmportspec;
+ uint set;
+
+ uint cargo = 0;
+ CargoID cargo_type = object->u.station.cargo_type;
+
+ if (st == NULL || fsmportspec->sclass == FSMPORTS_CLASS_WAYP) {
+ return group->g.real.loading[0];
+ }
+
+ switch (cargo_type) {
+ case CT_INVALID:
+ case CT_DEFAULT_NA:
+ case CT_PURCHASE:
+ cargo = 0;
+ break;
+
+ case CT_DEFAULT:
+ for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
+ cargo += st->goods[cargo_type].cargo.Count();
+ }
+ break;
+
+ default:
+ cargo = st->goods[cargo_type].cargo.Count();
+ break;
+ }
+
+ if (HASBIT(fsmportspec->flags, 1)) cargo /= (st->trainst_w + st->trainst_h);
+ cargo = min(0xfff, cargo);
+
+ if (cargo > fsmportspec->cargo_threshold) {
+ if (group->g.real.num_loading > 0) {
+ set = ((cargo - fsmportspec->cargo_threshold) * group->g.real.num_loading) / (4096 - fsmportspec->cargo_threshold);
+ return group->g.real.loading[set];
+ }
+ } else {
+ if (group->g.real.num_loaded > 0) {
+ set = (cargo * group->g.real.num_loaded) / (fsmportspec->cargo_threshold + 1);
+ return group->g.real.loaded[set];
+ }
+ }
+
+ return group->g.real.loading[0];
+}
+
+
+static void NewFSMportsResolver(ResolverObject *res, const FSMportsSpec *fsmportspec, const Station *st, TileIndex tile)
+{
+ res->GetRandomBits = FSMportsGetRandomBits;
+ res->GetTriggers = FSMportsGetTriggers;
+ res->SetTriggers = FSMportsSetTriggers;
+ res->GetVariable = FSMportsGetVariable;
+ res->ResolveReal = FSMportsResolveReal;
+
+ res->u.station.st = st;
+ res->u.station.fsmportspec = fsmportspec;
+ res->u.station.tile = tile;
+
+ res->callback = 0;
+ res->callback_param1 = 0;
+ res->callback_param2 = 0;
+ res->last_value = 0;
+ res->trigger = 0;
+ res->reseed = 0;
+}
+
+static const SpriteGroup *ResolveFSMports(ResolverObject *object)
+{
+ const SpriteGroup *group;
+ CargoID ctype = CT_DEFAULT_NA;
+
+ if (object->u.station.st == NULL) {
+ /* No station, so we are in a purchase list */
+ ctype = CT_PURCHASE;
+ } else {
+ /* Pick the first cargo that we have waiting */
+ for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) {
+ const CargoSpec *cs = GetCargo(cargo);
+ if (cs->IsValid() && object->u.station.fsmportspec->spritegroup[cargo] != NULL &&
+ !object->u.station.st->goods[cargo].cargo.Empty()) {
+ ctype = cargo;
+ break;
+ }
+ }
+ }
+
+ group = object->u.station.fsmportspec->spritegroup[ctype];
+ if (group == NULL) {
+ ctype = CT_DEFAULT;
+ group = object->u.station.fsmportspec->spritegroup[ctype];
+ }
+
+ if (group == NULL) return NULL;
+
+ /* Remember the cargo type we've picked */
+ object->u.station.cargo_type = ctype;
+
+ /* Invalidate all cached vars */
+ _svc.valid = 0;
+
+ return Resolve(group, object);
+}
+
+SpriteID GetCustomFSMportsRelocation(const FSMportsSpec *fsmportspec, const Station *st, TileIndex tile)
+{
+ const SpriteGroup *group;
+ ResolverObject object;
+
+ NewFSMportsResolver(&object, fsmportspec, st, tile);
+
+ group = ResolveFSMports(&object);
+ if (group == NULL || group->type != SGT_RESULT) return 0;
+ return group->g.result.sprite - 0x42D;
+}
+
+
+SpriteID GetCustomFSMportsGroundRelocation(const FSMportsSpec *fsmportspec, const Station *st, TileIndex tile)
+{
+ const SpriteGroup *group;
+ ResolverObject object;
+
+ NewFSMportsResolver(&object, fsmportspec, st, tile);
+ object.callback_param1 = 1; // Indicate we are resolving the ground sprite
+
+ group = ResolveFSMports(&object);
+ if (group == NULL || group->type != SGT_RESULT) return 0;
+ return group->g.result.sprite - 0x42D;
+}
+
+
+uint16 GetFSMportsCallback(uint16 callback, uint32 param1, uint32 param2, const FSMportsSpec *fsmportspec, const Station *st, TileIndex tile)
+{
+ const SpriteGroup *group;
+ ResolverObject object;
+
+ NewFSMportsResolver(&object, fsmportspec, st, tile);
+
+ object.callback = callback;
+ object.callback_param1 = param1;
+ object.callback_param2 = param2;
+
+ group = ResolveFSMports(&object);
+ if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
+ return group->g.callback.result;
+}
+
+
+/**
+ * Allocate a FSMportsSpec to a Station. This is called once per build operation.
+ * @param statspec FSMportsSpec to allocate.
+ * @param st Station to allocate it to.
+ * @param exec Whether to actually allocate the spec.
+ * @return Index within the FSMports's spec list, or -1 if the allocation failed.
+ */
+int AllocateFSMportsSpecToStation(const FSMportsSpec *fsmportspec, Station *st, bool exec)
+{
+ uint i;
+
+ if (fsmportspec == NULL) return 0;
+
+ /* Check if this spec has already been allocated */
+ for (i = 1; i < st->num_fsmportsspecs && i < MAX_SPECLIST; i++) {
+ if (st->fsmportsspeclist[i].spec == fsmportspec) return i;
+ }
+
+ for (i = 1; i < st->num_fsmportsspecs && i < MAX_SPECLIST; i++) {
+ if (st->fsmportsspeclist[i].spec == NULL && st->fsmportsspeclist[i].grfid == 0) break;
+ }
+
+ if (i == MAX_SPECLIST) return -1;
+
+ if (exec) {
+ if (i >= st->num_fsmportsspecs) {
+ st->num_fsmportsspecs = i + 1;
+ st->fsmportsspeclist = ReallocT(st->fsmportsspeclist, st->num_fsmportsspecs);
+
+ if (st->num_fsmportsspecs == 2) {
+ /* Initial allocation */
+ st->fsmportsspeclist[0].spec = NULL;
+ st->fsmportsspeclist[0].grfid = 0;
+ st->fsmportsspeclist[0].localidx = 0;
+ }
+ }
+
+ st->fsmportsspeclist[i].spec = fsmportspec;
+ st->fsmportsspeclist[i].grfid = fsmportspec->grffile->grfid;
+ st->fsmportsspeclist[i].localidx = fsmportspec->localidx;
+ }
+
+ return i;
+}
+
+
+/** Deallocate a FSMportsSpec from a Station. Called when removing a single station tile.
+ * @param st Station to work with.
+ * @param specindex Index of the custom station within the FSMports's spec list.
+ * @return Indicates whether the FSMportsSpec was deallocated.
+ */
+void DeallocateSpecFromFSMports(Station* st, byte specindex)
+{
+ /* specindex of 0 (default) is never freeable */
+ if (specindex == 0) return;
+
+ /* Check all tiles over the station to check if the specindex is still in use */
+ BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) {
+ if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && GetCustomStationSpecIndex(tile) == specindex) {
+ return;
+ }
+ } END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile)
+
+ /* This specindex is no longer in use, so deallocate it */
+ st->speclist[specindex].spec = NULL;
+ st->speclist[specindex].grfid = 0;
+ st->speclist[specindex].localidx = 0;
+
+ /* If this was the highest spec index, reallocate */
+ if (specindex == st->num_specs - 1) {
+ for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--);
+
+ if (st->num_specs > 1) {
+ st->speclist = ReallocT(st->speclist, st->num_specs);
+ } else {
+ free(st->speclist);
+ st->num_specs = 0;
+ st->speclist = NULL;
+ }
+ }
+}
+
+/** Draw representation of a station tile for GUI purposes.
+ * @param x Position x of image.
+ * @param y Position y of image.
+ * @param axis Axis.
+ * @param railtype Rail type.
+ * @param sclass, station Type of station.
+ * @param station station ID
+ * @return True if the tile was drawn (allows for fallback to default graphic)
+ */
+bool DrawFSMportsTile(int x, int y, RailType railtype, Axis axis, FSMportsClassID sclass, uint station)
+{
+ const FSMportsSpec *fsmportsspec;
+ const DrawTileSprites *sprites;
+ const DrawTileSeqStruct *seq;
+ const RailtypeInfo *rti = GetRailTypeInfo(railtype);
+ SpriteID relocation;
+ SpriteID image;
+ SpriteID pal = PLAYER_SPRITE_COLOR(_local_player);
+ uint tile = 2;
+
+ fsmportsspec = GetCustomFSMportsSpec(sclass, station);
+ if (fsmportsspec == NULL) return false;
+
+ relocation = GetCustomFSMportsRelocation(fsmportsspec, NULL, INVALID_TILE);
+
+ if (HASBIT(fsmportsspec->callbackmask, CBM_CUSTOM_LAYOUT)) {
+ uint16 callback = GetFSMportsCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, fsmportsspec, NULL, INVALID_TILE);
+ if (callback != CALLBACK_FAILED) tile = callback;
+ }
+
+ if (fsmportsspec->renderdata == NULL) {
+ sprites = GetStationTileLayout(tile + axis);
+ } else {
+ sprites = &fsmportsspec->renderdata[(tile < fsmportsspec->tiles) ? tile + axis : (uint)axis];
+ }
+
+ image = sprites->ground_sprite;
+ if (HASBIT(image, SPRITE_MODIFIER_USE_OFFSET)) {
+ image += GetCustomFSMportsGroundRelocation(fsmportsspec, NULL, INVALID_TILE);
+ image += rti->custom_ground_offset;
+ } else {
+ image += rti->total_offset;
+ }
+
+ DrawSprite(image, PAL_NONE, x, y);
+
+ foreach_draw_tile_seq(seq, sprites->seq) {
+ Point pt;
+ image = seq->image;
+ if (HASBIT(image, SPRITE_MODIFIER_USE_OFFSET)) {
+ image += rti->total_offset;
+ } else {
+ image += relocation;
+ }
+
+ if ((byte)seq->delta_z != 0x80) {
+ pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
+ DrawSprite(image, pal, x + pt.x, y + pt.y);
+ }
+ }
+
+ return true;
+}
+
+
+static const FSMportsSpec* GetFSMportsSpec(TileIndex t)
+{
+ const Station* st;
+ uint specindex;
+
+ if (!IsCustomStationSpecIndex(t)) return NULL;
+
+ st = GetStationByTile(t);
+ specindex = GetCustomStationSpecIndex(t);
+ return specindex < st->num_fsmportsspecs ? st->fsmportsspeclist[specindex].spec : NULL;
+ //TODO: expect quirky behaviour here. specindex is one var, but pointing into two speclists... need to resolve which
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/newgrf_fsmports.h Mon Jul 16 00:39:06 2007 +0000
@@ -0,0 +1,133 @@
+/* $Id:$ */
+
+/** @file newgrf_fsmports.h Header file for NewGRF Finite FSMPORTSe Machine Ports (airports, seaports, landports??) */
+
+#ifndef NEWGRF_FSMPORTS_H
+#define NEWGRF_FSMPORTS_H
+
+#include "engine.h"
+#include "newgrf_cargo.h"
+#include "helpers.hpp"
+#include "sprite.h"
+
+enum FSMportsClassID {
+ FSMPORTS_CLASS_BEGIN = 0, ///< the lowest valid value
+ FSMPORTS_CLASS_DFLT = 0, ///< Default FSMports class.
+ FSMPORTS_CLASS_WAYP, ///< Waypoint class.
+ FSMPORTS_CLASS_MAX = 32, ///< Maximum number of classes.
+};
+
+/** Define basic enum properties */
+template <> struct EnumPropsT<FSMportsClassID> : MakeEnumPropsT<FSMportsClassID, byte, FSMPORTS_CLASS_BEGIN, FSMPORTS_CLASS_MAX, FSMPORTS_CLASS_MAX> {};
+typedef TinyEnumT<FSMportsClassID> FSMportsClassIDByte;
+
+/** Allow incrementing of FSMportsClassID variables */
+DECLARE_POSTFIX_INCREMENT(FSMportsClassID);
+
+/* FSMports layout for given dimensions - it is a two-dimensional array
+ * where index is computed as (x * platforms) + platform. */
+typedef byte *FSMportsLayout;
+
+struct FSMportsSpec {
+ const struct GRFFile *grffile; ///< ID of GRF file FSMports belongs to.
+ int localidx; ///< Index within GRF file of FSMports.
+
+ bool allocated; ///< Flag whether this FSMport has been added to a FSMports class list
+
+ FSMportsClassID sclass; ///< The class to which this spec belongs.
+ StringID name; ///< Name of this FSMport.
+
+ /** Number of tile layouts.
+ * A minimum of 8 is required is required for FSMportss.
+ * 0-1 = plain platform
+ * 2-3 = platform with building
+ * 4-5 = platform with roof, left side
+ * 6-7 = platform with roof, right side
+ */
+ uint tiles;
+ DrawTileSprites *renderdata; ///< Array of tile layouts.
+ bool copied_renderdata;
+
+ /** Cargo threshold for choosing between little and lots of cargo
+ * @note little/lots are equivalent to the moving/loading FSMPORTSes for vehicles
+ */
+ uint16 cargo_threshold;
+
+ uint32 cargo_triggers; ///< Bitmask of cargo types which cause trigger re-randomizing
+
+ byte callbackmask; ///< Bitmask of callbacks to use, @see newgrf_callbacks.h
+
+ byte flags; ///< Bitmask of flags, bit 0: use different sprite set; bit 1: divide cargo about by FSMports size
+
+ byte lengths;
+ byte width;
+
+ FSMportsLayout **layouts;
+ bool copied_layouts;
+
+ /**
+ * NUM_CARGO real cargo plus three pseudo cargo sprite groups.
+ * Used for obtaining the sprite offset of custom sprites, and for
+ * evaluating callbacks.
+ */
+ const struct SpriteGroup *spritegroup[NUM_CARGO + 3];
+};
+
+/**
+ * Struct containing information relating to FSMports classes.
+ */
+struct FSMportsClass {
+ uint32 id; ///< ID of this class, e.g. 'DFLT', 'WAYP', etc.
+ StringID name; ///< Name of this class.
+ uint FSMports; ///< Number of FSMportss in this class.
+ FSMportsSpec **spec; ///< Array of FSMports specifications.
+};
+
+void ResetFSMportsClasses();
+FSMportsClassID AllocateFSMportsClass(uint32 cls);
+void SetFSMportsClassName(FSMportsClassID sclass, StringID name);
+StringID GetFSMportsClassName(FSMportsClassID sclass);
+StringID *BuildFSMportsClassDropdown();
+
+uint GetNumFSMportsClasses();
+uint GetNumCustomFSMports(FSMportsClassID sclass);
+
+void SetCustomFSMportsSpec(FSMportsSpec *FSMPORTSspec);
+const FSMportsSpec *GetCustomFSMportsSpec(FSMportsClassID sclass, uint FSMports);
+const FSMportsSpec *GetCustomFSMportsSpecByGrf(uint32 grfid, byte localidx);
+
+//* Evaluate a tile's position within a FSMports, and return the result a bitstuffed format. */
+//uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred);
+
+/* Get sprite offset for a given custom FSMports and FSMports structure (may be
+ * NULL - that means we are in a build dialog). The FSMports structure is used
+ * for variational sprite groups. */
+SpriteID GetCustomFSMportsRelocation(const FSMportsSpec *FSMPORTSspec, const Station *st, TileIndex tile);
+SpriteID GetCustomFSMportsGroundRelocation(const FSMportsSpec *FSMPORTSspec, const Station *st, TileIndex tile);
+uint16 GetFSMportsCallback(uint16 callback, uint32 param1, uint32 param2, const FSMportsSpec *FSMPORTSspec, const Station *st, TileIndex tile);
+
+/* Allocate a FSMportsSpec to a Station. This is called once per build operation. */
+int AllocateFSMportsSpecToStation(const FSMportsSpec *FSMPORTSspec, Station *st, bool exec);
+
+/* Deallocate a FSMportsSpec from a FSMports. Called when removing a single FSMports tile. */
+void DeallocateSpecFromFSMports(Station* st, byte specindex);
+
+/* Set the spec index, and mark tile as an fsmport */
+static inline void SetCustomFSMportsSpecIndex(TileIndex t, byte specindex)
+{
+ assert(IsTileType(t, MP_STATION));
+ _m[t].m4 = specindex;
+ _m[t].m6 |= 0x80; // set top bit of m6 to indicate it is an fsmport
+}
+
+/* Check if tile has a specindex and is an fsmport */
+static inline bool IsCustomFSMportsSpecIndex(TileIndex t)
+{
+ assert(IsTileType(t, MP_STATION));
+ return ((_m[t].m4 != 0) && ((_m[t].m6 & 0x80) == 0x80));
+}
+
+/* Draw representation of a FSMports tile for GUI purposes. */
+bool DrawFSMportsTile(int x, int y, RailType railtype, Axis axis, FSMportsClassID sclass, uint FSMports);
+
+#endif /* NEWGRF_FSMPORTS_H */
--- a/src/newgrf_spritegroup.h Mon Jul 16 00:31:25 2007 +0000
+++ b/src/newgrf_spritegroup.h Mon Jul 16 00:39:06 2007 +0000
@@ -7,6 +7,7 @@
#include "town.h"
#include "industry.h"
+#include "newgrf_fsmports.h"
/**
* Gets the value of a so-called newgrf "register".
@@ -212,6 +213,7 @@
TileIndex tile;
const struct Station *st;
const struct StationSpec *statspec;
+ const struct FSMportsSpec *fsmportspec;
CargoID cargo_type;
} station;
struct {
--- a/src/station.h Mon Jul 16 00:31:25 2007 +0000
+++ b/src/station.h Mon Jul 16 00:39:06 2007 +0000
@@ -12,6 +12,7 @@
#include "tile.h"
#include "road.h"
#include "newgrf_station.h"
+#include "newgrf_fsmports.h"
#include "cargopacket.h"
#include <list>
@@ -82,6 +83,12 @@
uint8 localidx; ///< Station ID within GRF of station
};
+struct FSMportsSpecList {
+ const FSMportsSpec *spec;
+ uint32 grfid; ///< GRF ID of this custom station
+ uint8 localidx; ///< Station ID within GRF of station
+};
+
/** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
struct StationRect : public Rect {
enum StationRectMode
@@ -144,6 +151,8 @@
/** List of custom stations (StationSpecs) allocated to the station */
uint8 num_specs;
StationSpecList *speclist;
+ uint8 num_fsmportsspecs;
+ FSMportsSpecList *fsmportsspeclist;
Date build_date;
--- a/src/station_cmd.cpp Mon Jul 16 00:31:25 2007 +0000
+++ b/src/station_cmd.cpp Mon Jul 16 00:39:06 2007 +0000
@@ -44,6 +44,7 @@
#include "strings.h"
#include "table/station_air.h"
#include "airport_states.h"
+#include "newgrf_fsmports.h"
/**
* Called if a new block is added to the station-pool
@@ -1470,21 +1471,43 @@
return ret;
}
+static void GetFSMportsLayout(byte *layout, int width, int length, const FSMportsSpec *fsmportspec)
+{
+ if (fsmportspec != NULL) {
+ /* Custom layout defined, follow it. */
+ memcpy(layout, fsmportspec->layouts[width - 1][length - 1],
+ length * width);
+ return;
+ }
+
+ while (--width >= 0) {
+ layout = CreateMulti(layout, length, 4);
+ layout = CreateMulti(layout, length, 6);
+ }
+}
+
/** Place an Airport.
* @param tile tile where airport will be built
* @param flags operation to perform
- * @param p1 airport type, @see airport.h
- * @param p2 (bit 0) - allow airports directly adjacent to other airports.
+ * @param p1 various bitstuffed elements
+ * - p1 = (bit 0) - orientation (Axis)
+ * - p1 = (bit 8-15) - length
+ * - p1 = (bit 16-23) - width
+ * - p1 = (bit 24) - allow stations directly adjacent to other stations.
+ * @param p2 various bitstuffed elements
+ * - p2 = (bit 0- 3) - airport type
+ * - p2 = (bit 8-15) - custom fsmport class
+ * - p2 = (bit 16-23) - custom fsmport id
*/
CommandCost CmdBuildAirport(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
bool airport_upgrade = true;
- const uint airport_type_for_flat_check = 0x80 | (p1 << 3);
+ const uint airport_type_for_flat_check = 0x80 | (GB(p2, 0, 4) << 3);
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
/* Check if a valid, buildable airport was chosen for construction */
- if (p1 > lengthof(_airport_sections) || !HASBIT(GetValidAirports(), p1)) return CMD_ERROR;
+ //if (p1 > lengthof(_airport_sections) || !HASBIT(GetValidAirports(), p1)) return CMD_ERROR;
if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile))
return CMD_ERROR;
@@ -1505,17 +1528,30 @@
}
}
- const AirportFTAClass *afc = GetAirport(p1);
+ const AirportFTAClass *afc = GetAirport(GB(p2, 0, 4));
int w = afc->size_x;
int h = afc->size_y;
- CommandCost ret = CheckFlatLandBelow(tile, w, h, flags, airport_type_for_flat_check, NULL);
- if (CmdFailed(ret)) return ret;
- CommandCost cost(ret.GetCost());
-
Station *st = NULL;
- if (!_patches.adjacent_stations || !HASBIT(p2, 0)) {
+ /* Check if the given station class is valid */
+ if (GB(p2, 8, 8) >= FSMPORTS_CLASS_MAX) return CMD_ERROR;
+
+ /* Check if we can allocate a custom stationspec to this station */
+ const FSMportsSpec *fsmportsspec = GetCustomFSMportsSpec((FSMportsClassID)GB(p2, 8, 8), GB(p2, 16, 8));
+
+ if (fsmportsspec != NULL) {
+ /* Perform NewStation checks */
+ w = fsmportsspec->width;
+ h = fsmportsspec->lengths;
+
+ /* Check if the station is buildable */
+ if (HASBIT(fsmportsspec->callbackmask, CBM_STATION_AVAIL) && GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportsspec, NULL, INVALID_TILE) == 0) {
+ return CMD_ERROR;
+ }
+ }
+
+ if (!_patches.adjacent_stations || !HASBIT(p1, 24)) {
st = GetStationAround(tile, w, h, INVALID_STATION);
if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
}
@@ -1565,9 +1601,16 @@
}
}
+
+ CommandCost ret = CheckFlatLandBelow(tile, w, h, flags, airport_type_for_flat_check, NULL);
+ if (CmdFailed(ret)) return ret;
+ CommandCost cost(ret.GetCost());
+
cost.AddCost(_price.build_airport * w * h);
if (flags & DC_EXEC) {
+ byte *layout_ptr;
+
st->airport_tile = tile;
st->AddFacility(FACIL_AIRPORT, tile);
st->airport_type = (byte)p1;
@@ -1584,11 +1627,17 @@
*/
if (airport_upgrade) UpdateAirplanesOnNewStation(st);
+ int fsmportspecindex = AllocateFSMportsSpecToStation(fsmportsspec, st, flags & DC_EXEC);
+ if (fsmportspecindex == -1) return CMD_ERROR;
+
+ layout_ptr = (byte*)alloca(w * h);
+ GetFSMportsLayout(layout_ptr, w, h, fsmportsspec);
+
{
- const byte *b = _airport_sections[p1];
-
BEGIN_TILE_LOOP(tile_cur, w, h, tile) {
- MakeAirport(tile_cur, st->owner, st->index, *b++);
+ byte layout = *layout_ptr++;
+ MakeAirport(tile_cur, st->owner, st->index, layout);
+ SetCustomFSMportsSpecIndex(tile_cur, fsmportspecindex); //set top bit of m6 to indicate an fsmportsspec
} END_TILE_LOOP(tile_cur, w, h, tile)
}
@@ -1926,6 +1975,8 @@
uint32 relocation = 0;
const Station *st = NULL;
const StationSpec *statspec = NULL;
+ const FSMportsSpec *fsmportspec = NULL;
+ bool FSMport = false;
PlayerID owner = GetTileOwner(ti->tile);
SpriteID palette;
@@ -1943,7 +1994,13 @@
if (IsCustomStationSpecIndex(ti->tile)) {
// look for customization
st = GetStationByTile(ti->tile);
- statspec = st->speclist[GetCustomStationSpecIndex(ti->tile)].spec;
+ int specindex = GetCustomStationSpecIndex(ti->tile);
+ FSMport = IsCustomFSMportsSpecIndex(ti->tile);
+ if (FSMport) {
+ fsmportspec = st->fsmportsspeclist[specindex].spec;
+ } else {
+ statspec = st->speclist[specindex].spec;
+ }
//debug("Cust-o-mized %p", statspec);
@@ -1962,13 +2019,28 @@
t = &statspec->renderdata[tile < statspec->tiles ? tile : (uint)GetRailStationAxis(ti->tile)];
}
}
+ if (fsmportspec != NULL) {
+ uint tile = GetStationGfx(ti->tile);
+
+ relocation = GetCustomFSMportsRelocation(fsmportspec, st, ti->tile);
+
+ //if (HASBIT(fsmportspec->callbackmask, CBM_CUSTOM_LAYOUT)) {
+ // uint16 callback = GetFSMportsCallback(CBID_FSMPORTS_SPRITE_LAYOUT, 0, 0, fsmportspec, st, ti->tile);
+ // if (callback != CALLBACK_FAILED) tile = (callback & ~1) + GetRailStationAxis(ti->tile);
+ //}
+
+ /* Ensure the chosen tile layout is valid for this custom station */
+ if (fsmportspec->renderdata != NULL) {
+ t = &fsmportspec->renderdata[tile < fsmportspec->tiles ? tile : (uint)GetRailStationAxis(ti->tile)];
+ }
+ }
}
if (t == NULL || t->seq == NULL) t = &_station_display_datas[GetStationGfx(ti->tile)];
SpriteID image = t->ground_sprite;
if (HASBIT(image, SPRITE_MODIFIER_USE_OFFSET)) {
- image += GetCustomStationGroundRelocation(statspec, st, ti->tile);
+ image += (FSMport)? GetCustomFSMportsGroundRelocation(fsmportspec, st, ti->tile) : GetCustomStationGroundRelocation(statspec, st, ti->tile);
image += rti->custom_ground_offset;
} else {
image += rti->total_offset;
--- a/src/station_map.h Mon Jul 16 00:31:25 2007 +0000
+++ b/src/station_map.h Mon Jul 16 00:39:06 2007 +0000
@@ -274,6 +274,7 @@
{
assert(IsTileType(t, MP_STATION));
_m[t].m4 = specindex;
+ _m[t].m6 &= 0x7F; // clear top bit of m6 to indicate it is not an fsmport
}
static inline uint GetCustomStationSpecIndex(TileIndex t)