diff --git a/src/ai/api/ai_road.cpp b/src/ai/api/ai_road.cpp index 24789bcf12..96eb06fcaa 100644 --- a/src/ai/api/ai_road.cpp +++ b/src/ai/api/ai_road.cpp @@ -549,7 +549,7 @@ static bool NeighbourHasReachableRoad(::RoadTypes rts, TileIndex start_tile, Dia EnforcePrecondition(false, ::TileX(start) == ::TileX(end) || ::TileY(start) == ::TileY(end)); EnforcePrecondition(false, IsRoadTypeAvailable(GetCurrentRoadType())); - return AIObject::DoCommand(end, start, (::TileY(start) != ::TileY(end) ? 4 : 0) | (start < end ? 1 : 2) | (AIObject::GetRoadType() << 3), CMD_REMOVE_LONG_ROAD); + return AIObject::DoCommand(start, end, (::TileY(start) != ::TileY(end) ? 4 : 0) | (start < end ? 1 : 2) | (AIObject::GetRoadType() << 3), CMD_REMOVE_LONG_ROAD); } /* static */ bool AIRoad::RemoveRoadFull(TileIndex start, TileIndex end) @@ -559,7 +559,7 @@ static bool NeighbourHasReachableRoad(::RoadTypes rts, TileIndex start_tile, Dia EnforcePrecondition(false, ::TileX(start) == ::TileX(end) || ::TileY(start) == ::TileY(end)); EnforcePrecondition(false, IsRoadTypeAvailable(GetCurrentRoadType())); - return AIObject::DoCommand(end, start, (::TileY(start) != ::TileY(end) ? 4 : 0) | (start < end ? 2 : 1) | (AIObject::GetRoadType() << 3), CMD_REMOVE_LONG_ROAD); + return AIObject::DoCommand(start, end, (::TileY(start) != ::TileY(end) ? 4 : 0) | (start < end ? 2 : 1) | (AIObject::GetRoadType() << 3), CMD_REMOVE_LONG_ROAD); } /* static */ bool AIRoad::RemoveRoadDepot(TileIndex tile) diff --git a/src/console.cpp b/src/console.cpp index 67b6ffe47b..d486e780c1 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -50,7 +50,7 @@ static void IConsoleWriteToLogFile(const char *string) if (_iconsole_output_file != NULL) { /* if there is an console output file ... also print it there */ const char *header = GetLogPrefix(); - if (fwrite(header, strlen(header), 1, _iconsole_output_file) != 1 || + if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) || fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 || fwrite("\n", 1, 1, _iconsole_output_file) != 1) { fclose(_iconsole_output_file); diff --git a/src/fileio.cpp b/src/fileio.cpp index 3b18da7a67..253ad8ebb3 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -22,7 +22,7 @@ #include #include #else -#ifdef OPENBSD +#if defined(OPENBSD) || defined(DOS) #include #endif #include diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 598e9c6eda..571d49c269 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -2668,8 +2668,8 @@ static bool HandleChangeInfoResult(const char *caller, ChangeInfoResult cir, uin /* No debug message for an invalid ID, as it has already been output */ _skip_sprites = -1; _cur_grfconfig->status = GCS_DISABLED; - _cur_grfconfig->error = CallocT(1); - _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL); _cur_grfconfig->error->message = (cir == CIR_INVALID_ID) ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY; return true; } @@ -4198,16 +4198,10 @@ static void CfgApply(ByteReader *buf) */ static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c) { - if (c->error != NULL) { - free(c->error->custom_message); - free(c->error->data); - free(c->error); - } + delete c->error; c->status = GCS_DISABLED; - c->error = CallocT(1); + c->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC); c->error->data = strdup(_cur_grfconfig->name); - c->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - c->error->message = STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC; ClearTemporaryNewGRFData(GetFileByGRFID(c->grfid)); } @@ -4416,9 +4410,8 @@ static void GRFInfo(ByteReader *buf) if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) { _cur_grfconfig->status = GCS_DISABLED; - _cur_grfconfig->error = CallocT(1); - _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - _cur_grfconfig->error->message = STR_NEWGRF_ERROR_MULTIPLE_ACTION_8; + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_MULTIPLE_ACTION_8); _skip_sprites = -1; return; @@ -4556,9 +4549,7 @@ static void GRFLoadError(ByteReader *buf) return; } - GRFError *error = CallocT(1); - - error->severity = sevstr[severity]; + GRFError *error = new GRFError(sevstr[severity]); if (message_id == 0xFF) { /* This is a custom error message. */ @@ -5426,17 +5417,12 @@ static void TranslateGRFStrings(ByteReader *buf) if (c->status == GCS_INITIALISED) { /* If the file is not active but will be activated later, give an error * and disable this file. */ - GRFError *error = CallocT(1); + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_LOAD_AFTER); char tmp[256]; GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp)); - error->data = strdup(tmp); - - error->message = STR_NEWGRF_ERROR_LOAD_AFTER; - error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - - if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error); - _cur_grfconfig->error = error; + _cur_grfconfig->error->data = strdup(tmp); _cur_grfconfig->status = GCS_DISABLED; ClearTemporaryNewGRFData(_cur_grffile); @@ -5714,9 +5700,7 @@ static void ResetNewGRFErrors() { for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) { if (!HasBit(c->flags, GCF_COPY) && c->error != NULL) { - free(c->error->custom_message); - free(c->error->data); - free(c->error); + delete c->error; c->error = NULL; } } @@ -6189,9 +6173,8 @@ static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage) _skip_sprites = -1; _cur_grfconfig->status = GCS_DISABLED; - _cur_grfconfig->error = CallocT(1); - _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - _cur_grfconfig->error->message = STR_NEWGRF_ERROR_READ_BOUNDS; + delete _cur_grfconfig->error; + _cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_READ_BOUNDS); } } @@ -6221,9 +6204,7 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage) if (file_index > LAST_GRF_SLOT) { DEBUG(grf, 0, "'%s' is not loaded as the maximum number of GRFs has been reached", filename); config->status = GCS_DISABLED; - config->error = CallocT(1); - config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - config->error->message = STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED; + config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED); return; } @@ -6269,9 +6250,8 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage) if (_skip_sprites == 0) { grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling"); config->status = GCS_DISABLED; - config->error = CallocT(1); - config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL; - config->error->message = STR_NEWGRF_ERROR_UNEXPECTED_SPRITE; + delete config->error; + config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_UNEXPECTED_SPRITE); break; } diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index c996c3f049..28e67e7f50 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -26,6 +26,17 @@ GRFConfig *_grfconfig; GRFConfig *_grfconfig_newgame; GRFConfig *_grfconfig_static; +GRFError::GRFError(StringID severity, StringID message) : + message(message), + severity(severity) +{ +} + +GRFError::~GRFError() +{ + free(this->custom_message); + free(this->data); + } /** * Update the palettes of the graphics from the config file. @@ -101,12 +112,7 @@ void ClearGRFConfig(GRFConfig **config) free((*config)->filename); free((*config)->name); free((*config)->info); - - if ((*config)->error != NULL) { - free((*config)->error->custom_message); - free((*config)->error->data); - free((*config)->error); - } + delete (*config)->error; } free(*config); *config = NULL; @@ -139,8 +145,9 @@ GRFConfig *DuplicateGRFConfig(const GRFConfig *c) if (c->name != NULL) config->name = strdup(c->name); if (c->info != NULL) config->info = strdup(c->info); if (c->error != NULL) { - config->error = MallocT(1); - memcpy(config->error, c->error, sizeof(GRFError)); + config->error = new GRFError(c->error->severity, c->error->message); + config->error->num_params = c->error->num_params; + memcpy(config->error->param_value, c->error->param_value, sizeof(config->error->param_value)); if (c->error->data != NULL) config->error->data = strdup(c->error->data); if (c->error->custom_message != NULL) config->error->custom_message = strdup(c->error->custom_message); } diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 74e2d54ab6..fe3eab37ed 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -13,6 +13,7 @@ #define NEWGRF_CONFIG_H #include "strings_type.h" +#include "core/alloc_type.hpp" /** GRF config bit flags */ enum GCF_Flags { @@ -55,7 +56,10 @@ struct GRFIdentifier { }; /** Information about why GRF had problems during initialisation */ -struct GRFError { +struct GRFError : ZeroedMemoryAllocator { + GRFError(StringID severity, StringID message = 0); + ~GRFError(); + char *custom_message; ///< Custom message (if present) char *data; ///< Additional data for message and custom_message StringID message; ///< Default message diff --git a/src/newgrf_railtype.cpp b/src/newgrf_railtype.cpp index b76eb2aef8..931f544e9c 100644 --- a/src/newgrf_railtype.cpp +++ b/src/newgrf_railtype.cpp @@ -14,10 +14,13 @@ #include "newgrf_commons.h" #include "newgrf_railtype.h" #include "newgrf_spritegroup.h" +#include "core/bitmath_func.hpp" static uint32 RailTypeGetRandomBits(const ResolverObject *object) { - return 0; + TileIndex tile = object->u.routes.tile; + uint tmp = CountBits(tile + (TileX(tile) + TileY(tile)) * TILE_SIZE); + return GB(tmp, 0, 2); } static uint32 RailTypeGetTriggers(const ResolverObject *object) diff --git a/src/openttd.cpp b/src/openttd.cpp index a1559592ee..5a27ef46ce 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -446,7 +446,7 @@ int ttd_main(int argc, char *argv[]) * a letter means: it accepts that param (e.g.: -h) * a ':' behind it means: it need a param (e.g.: -m) * a '::' behind it means: it can optional have a param (e.g.: -d) */ - optformat = "m:s:v:b:hD::n::ei::I:S:t:d::r:g::G:c:xl:p:P:" + optformat = "m:s:v:b:hD::n::ei::I:S:M:t:d::r:g::G:c:xl:p:P:" #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32) "f" #endif diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 8ee878745c..9e9f9c1416 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -779,9 +779,9 @@ CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p } /** Remove a long piece of road. - * @param end_tile end tile of drag + * @param start_tile start tile of drag * @param flags operation to perform - * @param p1 start tile of drag + * @param p1 end tile of drag * @param p2 various bitstuffed elements * - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1) * - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2) @@ -790,13 +790,13 @@ CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p * @param text unused * @return the cost of this operation or an error */ -CommandCost CmdRemoveLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) +CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { CommandCost cost(EXPENSES_CONSTRUCTION); if (p1 >= MapSize()) return CMD_ERROR; - TileIndex start_tile = p1; + TileIndex end_tile = p1; RoadType rt = (RoadType)GB(p2, 3, 2); if (!IsValidRoadType(rt)) return CMD_ERROR; @@ -829,7 +829,7 @@ CommandCost CmdRemoveLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1 if (flags & DC_EXEC) { money -= ret.GetCost(); if (money < 0) { - _additional_cash_required = DoCommand(end_tile, start_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost(); + _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost(); return cost; } RemoveRoad(tile, flags, bits, rt, true, false); diff --git a/src/stdafx.h b/src/stdafx.h index 4ea5ddc586..649b80d8dc 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -225,6 +225,15 @@ #endif /* defined(_MSC_VER) */ +#if defined(DOS) + /* The DOS port does not have all signals/signal functions. */ + #define strsignal(sig) "" + /* Use 'no floating point' for bus errors; SIGBUS does not + * exist for does, SIGNOFP not for other platforms. So it's + * fairly safe the interchange those. */ + #define SIGBUS SIGNOFP +#endif + #if defined(WINCE) #define strdup _strdup #endif /* WINCE */ diff --git a/src/window.cpp b/src/window.cpp index dbe971c62d..602759189e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1757,7 +1757,7 @@ static bool HandleScrollbarScrolling() /* Find the item we want to move to and make sure it's inside bounds. */ int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity())); - if (rtl) pos = sb->GetCount() - sb->GetCapacity() - pos; + if (rtl) pos = max(0, sb->GetCount() - sb->GetCapacity() - pos); if (pos != sb->GetPosition()) { sb->SetPosition(pos); w->SetDirty();