summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2005-11-26 16:41:14 +0000
committerpeter1138 <peter1138@openttd.org>2005-11-26 16:41:14 +0000
commit87f54ebf92725c36c4ab0aa1b97fe9152e7db5c1 (patch)
treeccd73fcd302984e4abaf2d806bf28bf73a54e79f
parent9a381f4d5d7a575571943b94c94c27b3ae6cad2d (diff)
downloadopenttd-87f54ebf92725c36c4ab0aa1b97fe9152e7db5c1.tar.xz
(svn r3239) - Codechange: Introduce and use helper functions for engine replacement code.
-rw-r--r--aircraft_cmd.c4
-rw-r--r--openttd.c6
-rw-r--r--player.h6
-rw-r--r--players.c68
-rw-r--r--vehicle.c5
-rw-r--r--vehicle_gui.c24
6 files changed, 85 insertions, 28 deletions
diff --git a/aircraft_cmd.c b/aircraft_cmd.c
index c30cf1a5d..eaeeb0c84 100644
--- a/aircraft_cmd.c
+++ b/aircraft_cmd.c
@@ -1469,7 +1469,7 @@ static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *
// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
if (v->owner == _local_player && (
- p->engine_replacement[v->engine_type] != INVALID_ENGINE ||
+ EngineHasReplacement(p, v->engine_type) ||
(p->engine_renew && v->age - v->max_age > p->engine_renew_months * 30)
)) {
_current_player = _local_player;
@@ -1533,7 +1533,7 @@ static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *Airp
// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
// only the vehicle owner needs to calculate the rest (locally)
- if ((p->engine_replacement[v->engine_type] != INVALID_ENGINE) ||
+ if (EngineHasReplacement(p, v->engine_type) ||
(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
// send the aircraft to the hangar at next airport (bit 17 set)
_current_player = _local_player;
diff --git a/openttd.c b/openttd.c
index d93d5d968..a26d2d14e 100644
--- a/openttd.c
+++ b/openttd.c
@@ -1277,11 +1277,7 @@ bool AfterLoadGame(uint version)
* of course, we do need to initialize them for older savegames. */
if (CheckSavegameVersion(16)) {
FOR_ALL_PLAYERS(p) {
- EngineID i;
-
- for (i = 0; i < TOTAL_NUM_ENGINES; i++) {
- p->engine_replacement[i] = INVALID_ENGINE;
- }
+ InitialiseEngineReplacement(p);
p->engine_renew = false;
p->engine_renew_months = -6;
p->engine_renew_money = 100000;
diff --git a/player.h b/player.h
index 1bfaa0f04..1b8bbb279 100644
--- a/player.h
+++ b/player.h
@@ -263,4 +263,10 @@ void LoadFromHighScore(void);
int8 SaveHighScoreValue(const Player *p);
int8 SaveHighScoreValueNetwork(void);
+void InitialiseEngineReplacement(Player *p);
+EngineID EngineReplacement(const Player *p, EngineID engine);
+bool EngineHasReplacement(const Player *p, EngineID engine);
+int32 AddEngineReplacement(Player *p, EngineID old_engine, EngineID new_engine, uint32 flags);
+int32 RemoveEngineReplacement(Player *p, EngineID engine, uint32 flags);
+
#endif /* PLAYER_H */
diff --git a/players.c b/players.c
index b53ea1c27..de26ff4ac 100644
--- a/players.c
+++ b/players.c
@@ -465,7 +465,6 @@ static Player *AllocatePlayer(void)
Player *DoStartupNewPlayer(bool is_ai)
{
Player *p;
- int i;
p = AllocatePlayer();
if (p == NULL)
@@ -488,9 +487,7 @@ Player *DoStartupNewPlayer(bool is_ai)
p->face = Random();
/* Engine renewal settings */
- for (i = 0; i < TOTAL_NUM_ENGINES; i++)
- p->engine_replacement[i] = INVALID_ENGINE;
-
+ InitialiseEngineReplacement(p);
p->renew_keep_length = false;
p->engine_renew = false;
p->engine_renew_months = -6;
@@ -731,10 +728,10 @@ int32 CmdReplaceVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// make sure that the player can actually buy the new engine
if (!HASBIT(GetEngine(new_engine_type)->player_avail, _current_player))
return CMD_ERROR;
- }
- if (flags & DC_EXEC) {
- p->engine_replacement[old_engine_type] = new_engine_type;
+ return AddEngineReplacement(p, old_engine_type, new_engine_type, flags);
+ } else {
+ return RemoveEngineReplacement(p, old_engine_type, flags);
}
} break;
case 4:
@@ -1100,6 +1097,63 @@ void LoadFromHighScore(void)
_patches.ending_date = 2051;
}
+void InitialiseEngineReplacement(Player *p)
+{
+ EngineID engine;
+
+ for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++)
+ p->engine_replacement[engine] = INVALID_ENGINE;
+}
+
+/**
+ * Retrieve the engine replacement for the given player and original engine type.
+ * @param p Player.
+ * @param engine Engine type.
+ * @return Assigned replacement engine.
+ */
+EngineID EngineReplacement(const Player *p, EngineID engine)
+{
+ return p->engine_replacement[engine];
+}
+
+/**
+ * Check if an engine has a replacement set up.
+ * @param p Player.
+ * @param engine Engine type.
+ * @return True if there is a replacement for the original engine type.
+ */
+bool EngineHasReplacement(const Player *p, EngineID engine)
+{
+ return EngineReplacement(p, engine) != INVALID_ENGINE;
+}
+
+/**
+ * Add an engine replacement for the player.
+ * @param p Player.
+ * @param old_engine The original engine type.
+ * @param new_engine The replacement engine type.
+ * @param flags The calling command flags.
+ * @return 0 on success, CMD_ERROR on failure.
+ */
+int32 AddEngineReplacement(Player *p, EngineID old_engine, EngineID new_engine, uint32 flags)
+{
+ if (flags & DC_EXEC) p->engine_replacement[old_engine] = new_engine;
+ return 0;
+}
+
+/**
+ * Remove an engine replacement for the player.
+ * @param p Player.
+ * @param engine The original engine type.
+ * @param flags The calling command flags.
+ * @return 0 on success, CMD_ERROR on failure.
+ */
+int32 RemoveEngineReplacement(Player *p, EngineID engine, uint32 flags)
+{
+ if (flags & DC_EXEC) p->engine_replacement[engine] = INVALID_ENGINE;
+ return 0;
+}
+
// Save/load of players
static const SaveLoad _player_desc[] = {
SLE_VAR(Player,name_2, SLE_UINT32),
diff --git a/vehicle.c b/vehicle.c
index 99576a2a2..3c7f3156d 100644
--- a/vehicle.c
+++ b/vehicle.c
@@ -1618,7 +1618,8 @@ static int32 ReplaceVehicle(Vehicle **w, byte flags)
bool new_front = false;
Vehicle *new_v = NULL;
- new_engine_type = p->engine_replacement[old_v->engine_type] == INVALID_ENGINE ? old_v->engine_type : p->engine_replacement[old_v->engine_type];
+ new_engine_type = EngineReplacement(p, old_v->engine_type);
+ if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
cost = DoCommand(old_v->x_pos, old_v->y_pos, new_engine_type, 1, flags, CMD_BUILD_VEH(old_v->type));
if (CmdFailed(cost)) return cost;
@@ -1722,7 +1723,7 @@ static void MaybeReplaceVehicle(Vehicle *v)
if (!p->engine_renew ||
w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old
w->max_age == 0) { // rail cars got a max age of 0
- if (p->engine_replacement[w->engine_type] == INVALID_ENGINE) // updates to a new model
+ if (!EngineHasReplacement(p, w->engine_type)) // updates to a new model
continue;
}
diff --git a/vehicle_gui.c b/vehicle_gui.c
index 21ea66c32..d185e172b 100644
--- a/vehicle_gui.c
+++ b/vehicle_gui.c
@@ -421,7 +421,7 @@ static void train_engine_drawing_loop(int *x, int *y, int *pos, int *sel, int *s
const RailVehicleInfo *rvi = RailVehInfo(i);
const EngineInfo *info = &_engine_info[i];
- if (p->engine_replacement[i] == INVALID_ENGINE && _player_num_engines[i] == 0 && show_outdated) continue;
+ if (!EngineHasReplacement(p, i) && _player_num_engines[i] == 0 && show_outdated) continue;
if (rvi->power == 0 && !show_cars) // disables display of cars (works since they do not have power)
continue;
@@ -480,7 +480,7 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
const EngineInfo *info = &_engine_info[engine_id];
if (ENGINE_AVAILABLE && RailVehInfo(engine_id)->power && e->railtype == railtype) {
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
if (sel[0] == 0) selected_id[0] = engine_id;
count++;
sel[0]--;
@@ -503,7 +503,7 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
do {
info = &_engine_info[engine_id];
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
if (sel[0] == 0) selected_id[0] = engine_id;
count++;
sel[0]--;
@@ -536,7 +536,7 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
do {
info = &_engine_info[engine_id];
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
if (sel[0] == 0) selected_id[0] = engine_id;
count++;
sel[0]--;
@@ -571,7 +571,7 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
do {
info = &_engine_info[engine_id];
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
count++;
if (sel[0] == 0) selected_id[0] = engine_id;
sel[0]--;
@@ -650,7 +650,7 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int
do {
info = &_engine_info[engine_id];
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
DrawString(x+59, y+2, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
DrawRoadVehEngine(x+29, y+6, engine_id, _player_num_engines[engine_id] > 0 ? SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)) : PALETTE_CRASH);
@@ -687,7 +687,7 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int
do {
info = &_engine_info[engine_id];
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
DrawString(x+75, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
DrawShipEngine(x+35, y+10, engine_id, _player_num_engines[engine_id] > 0 ? SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)) : PALETTE_CRASH);
@@ -722,7 +722,7 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int
do {
info = &_engine_info[engine_id];
- if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+ if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
if (sel[0] == 0) selected_id[0] = engine_id;
if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
DrawString(x+62, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
@@ -835,7 +835,7 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
if (selected_id[0] == -1 ||
selected_id[1] == -1 ||
selected_id[0] == selected_id[1] ||
- p->engine_replacement[selected_id[0]] == selected_id[1]) {
+ EngineReplacement(p, selected_id[0]) == selected_id[1]) {
SETBIT(w->disabled_state, 4);
} else {
CLRBIT(w->disabled_state, 4);
@@ -845,7 +845,7 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
// The left list (existing vehicle) is empty
// or The selected vehicle has no replacement set up
if (selected_id[0] == -1 ||
- p->engine_replacement[selected_id[0]] == INVALID_ENGINE) {
+ !EngineHasReplacement(p, selected_id[0])) {
SETBIT(w->disabled_state, 6);
} else {
CLRBIT(w->disabled_state, 6);
@@ -863,10 +863,10 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
// sets up the string for the vehicle that is being replaced to
if (selected_id[0] != -1) {
- if (p->engine_replacement[selected_id[0]] == INVALID_ENGINE) {
+ if (!EngineHasReplacement(p, selected_id[0])) {
SetDParam(0, STR_NOT_REPLACING);
} else {
- SetDParam(0, GetCustomEngineName(p->engine_replacement[selected_id[0]]));
+ SetDParam(0, GetCustomEngineName(EngineReplacement(p, selected_id[0])));
}
} else {
SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);