diff --git a/disaster_cmd.c b/disaster_cmd.c index 2d4579b525..f0683b8734 100644 --- a/disaster_cmd.c +++ b/disaster_cmd.c @@ -477,7 +477,7 @@ static void DisasterTick_3(Vehicle *v) TileIndex tile; uint ind; - x = v->x_pos - 15 * TILE_SIZE; + x = v->x_pos + (15 * TILE_SIZE); y = v->y_pos; if ( (uint)x > MapMaxX() * TILE_SIZE - 1) @@ -672,7 +672,7 @@ static void DisasterTick_5_and_6(Vehicle *v) tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction)); if (IsValidTile(tile) && - (r=GetTileTrackStatus(tile,TRANSPORT_WATER),(byte)(r+(r >> 8)) == 0x3F) && + (r=GetTileTrackStatus(tile,TRANSPORT_WATER),(byte)(r|(r >> 8)) == 0x3F) && !CHANCE16(1,90)) { GetNewVehiclePos(v, &gp); SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos); diff --git a/misc.c b/misc.c index ca7b368fdb..4cf69e0bfd 100644 --- a/misc.c +++ b/misc.c @@ -266,16 +266,18 @@ void InitializeLandscapeVariables(bool only_constants) int FindFirstBit(uint32 value) { - // This is much faster than the one that was before here. - // Created by Darkvater.. blame him if it is wrong ;) - // Btw, the macro FINDFIRSTBIT is better to use when your value is - // not more than 128. + // The macro FIND_FIRST_BIT is better to use when your value is + // not more than 128. byte i = 0; - if (value & 0xffff0000) { value >>= 16; i += 16; } - if (value & 0x0000ff00) { value >>= 8; i += 8; } - if (value & 0x000000f0) { value >>= 4; i += 4; } - if (value & 0x0000000c) { value >>= 2; i += 2; } - if (value & 0x00000002) { i += 1; } + + if (value == 0) return 0; + + if ((value & 0x0000ffff) == 0) { value >>= 16; i += 16; } + if ((value & 0x000000ff) == 0) { value >>= 8; i += 8; } + if ((value & 0x0000000f) == 0) { value >>= 4; i += 4; } + if ((value & 0x00000003) == 0) { value >>= 2; i += 2; } + if ((value & 0x00000001) == 0) { i += 1; } + return i; } diff --git a/openttd.c b/openttd.c index e4e44a6f17..a44f4b2aa2 100644 --- a/openttd.c +++ b/openttd.c @@ -253,9 +253,15 @@ static void InitializeDynamicVariables(void) _industry_sort = NULL; } -static void UnInitializeDynamicVariables(void) + +static void UnInitializeGame(void) { - /* Dynamic stuff needs to be free'd somewhere... */ + UnInitWindowSystem(); + + /* Uninitialize airport state machines */ + UnInitializeAirports(); + + /* Uninitialize variables that are allocated dynamically */ CleanPool(&_Town_pool); CleanPool(&_Industry_pool); CleanPool(&_Station_pool); @@ -265,11 +271,6 @@ static void UnInitializeDynamicVariables(void) free((void*)_town_sort); free((void*)_industry_sort); -} - -static void UnInitializeGame(void) -{ - UnInitWindowSystem(); free(_config_file); } @@ -518,18 +519,14 @@ int ttd_main(int argc, char *argv[]) SaveToConfig(); SaveToHighScore(); - // uninitialize airport state machines - UnInitializeAirports(); - - /* uninitialize variables that are allocated dynamic */ - UnInitializeDynamicVariables(); + /* Reset windowing system and free config file */ + UnInitializeGame(); /* stop the AI */ AI_Uninitialize(); /* Close all and any open filehandles */ FioCloseAll(); - UnInitializeGame(); return 0; } diff --git a/station_gui.c b/station_gui.c index 72fae3c477..0139d73c11 100644 --- a/station_gui.c +++ b/station_gui.c @@ -264,6 +264,8 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e) const PlayerID owner = w->window_number; static byte facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK; static uint16 cargo_filter = CARGO_ALL_SELECTED; + static Listing station_sort = {0, 0}; + plstations_d *sl = &WP(w, plstations_d); switch (e->event) { @@ -281,7 +283,8 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e) sl->sort_list = NULL; sl->flags = SL_REBUILD; - sl->sort_type = 0; + sl->sort_type = station_sort.criteria; + if (station_sort.order) sl->flags |= SL_ORDER; sl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; break; } @@ -434,6 +437,7 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e) } case STATIONLIST_WIDGET_SORTBY: /*flip sorting method asc/desc*/ TOGGLEBIT(sl->flags, 0); //DESC-flag + station_sort.order = GB(sl->flags, 0, 1); sl->flags |= SL_RESORT; w->flags4 |= 5 << WF_TIMEOUT_SHL; LowerWindowWidget(w, STATIONLIST_WIDGET_SORTBY); @@ -469,6 +473,7 @@ static void PlayerStationsWndProc(Window *w, WindowEvent *e) if (sl->sort_type != e->we.dropdown.index) { // value has changed -> resort sl->sort_type = e->we.dropdown.index; + station_sort.criteria = sl->sort_type; sl->flags |= SL_RESORT; } SetWindowDirty(w); diff --git a/window.c b/window.c index 3a89dda763..2dd12169f4 100644 --- a/window.c +++ b/window.c @@ -876,12 +876,19 @@ void InitWindowSystem(void) void UnInitWindowSystem(void) { Window* const *wz; - // delete all malloced widgets + +restart_search: + /* Delete all windows, reset z-array. + *When we find the window to delete, we need to restart the search + * as deleting this window could cascade in deleting (many) others + * anywhere in the z-array. We call DeleteWindow() so that it can properly + * release own alloc'd memory, which otherwise could result in memleaks */ FOR_ALL_WINDOWS(wz) { - free((*wz)->widget); - (*wz)->widget = NULL; - (*wz)->widget_count = 0; + DeleteWindow(*wz); + goto restart_search; } + + assert(_last_z_window == _z_windows); } void ResetWindowSystem(void)