src/town_gui.cpp
branchnoai
changeset 9723 eee46cb39750
parent 9722 ebf0ece7d8f6
child 9724 b39bc69bb2f2
--- a/src/town_gui.cpp	Fri Nov 23 16:59:30 2007 +0000
+++ b/src/town_gui.cpp	Wed Jan 09 18:11:12 2008 +0000
@@ -5,20 +5,21 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "debug.h"
-#include "functions.h"
-#include "strings.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "town.h"
-#include "window.h"
-#include "gfx.h"
-#include "viewport.h"
+#include "viewport_func.h"
+#include "gfx_func.h"
 #include "gui.h"
-#include "command.h"
+#include "window_gui.h"
+#include "textbuf_gui.h"
+#include "command_func.h"
 #include "player.h"
 #include "network/network.h"
 #include "variables.h"
-#include "helpers.hpp"
+#include "strings_func.h"
+#include "economy_func.h"
+#include "core/alloc_func.hpp"
 
 enum TownAuthorityWidget {
 	TWA_CLOSEBOX = 0,
@@ -109,15 +110,23 @@
 	return buttons;
 }
 
+/**
+ * Get the position of the Nth set bit.
+ *
+ * If there is no Nth bit set return -1
+ *
+ * @param bits The value to search in
+ * @param n The Nth set bit from which we want to know the position
+ * @return The position of the Nth set bit
+ */
 static int GetNthSetBit(uint32 bits, int n)
 {
-	int i = 0;
-
 	if (n >= 0) {
-		do {
-			if (bits & 1 && --n < 0) return i;
-			i++;
-		} while (bits >>= 1);
+		uint i;
+		FOR_EACH_SET_BIT(i, bits) {
+			n--;
+			if (n < 0) return i;
+		}
 	}
 	return -1;
 }
@@ -132,10 +141,10 @@
 
 		SetVScrollCount(w, numact + 1);
 
-		if (WP(w,def_d).data_1 != -1 && !HasBit(buttons, WP(w,def_d).data_1))
-			WP(w,def_d).data_1 = -1;
+		if (WP(w, def_d).data_1 != -1 && !HasBit(buttons, WP(w,def_d).data_1))
+			WP(w, def_d).data_1 = -1;
 
-		SetWindowWidgetDisabledState(w, 6, WP(w, def_d).data_1 == -1);
+		w->SetWidgetDisabledState(6, WP(w, def_d).data_1 == -1);
 
 		{
 			int y;
@@ -198,7 +207,7 @@
 		}
 
 		{
-			int i = WP(w,def_d).data_1;
+			int i = WP(w, def_d).data_1;
 
 			if (i != -1) {
 				SetDParam(1, (_price.build_industry >> 8) * _town_action_costs[i]);
@@ -216,11 +225,11 @@
 			const Town *t = GetTown(w->window_number);
 			int y = (e->we.click.pt.y - 0x6B) / 10;
 
-			if (!IS_INT_INSIDE(y, 0, 5)) return;
+			if (!IsInsideMM(y, 0, 5)) return;
 
 			y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_player, t), y + w->vscroll.pos - 1);
 			if (y >= 0) {
-				WP(w,def_d).data_1 = y;
+				WP(w, def_d).data_1 = y;
 				SetWindowDirty(w);
 			}
 			/* Fall through to clicking in case we are double-clicked */
@@ -228,7 +237,7 @@
 		}
 
 		case TWA_EXECUTE: {
-			DoCommandP(GetTown(w->window_number)->xy, w->window_number, WP(w,def_d).data_1, NULL, CMD_DO_TOWN_ACTION | CMD_MSG(STR_00B4_CAN_T_DO_THIS));
+			DoCommandP(GetTown(w->window_number)->xy, w->window_number, WP(w, def_d).data_1, NULL, CMD_DO_TOWN_ACTION | CMD_MSG(STR_00B4_CAN_T_DO_THIS));
 			break;
 		}
 		}
@@ -254,7 +263,7 @@
 
 	if (w != NULL) {
 		w->vscroll.cap = 5;
-		WP(w,def_d).data_1 = -1;
+		WP(w, def_d).data_1 = -1;
 	}
 }
 
@@ -265,7 +274,7 @@
 	switch (e->event) {
 	case WE_PAINT:
 		/* disable renaming town in network games if you are not the server */
-		SetWindowWidgetDisabledState(w, 8, _networking && !_network_server);
+		w->SetWidgetDisabledState(8, _networking && !_network_server);
 
 		SetDParam(0, t->index);
 		DrawWindowWidgets(w);
@@ -441,7 +450,6 @@
 
 	/* Create array for sorting */
 	_town_sort = ReallocT(_town_sort, GetMaxTownIndex() + 1);
-	if (_town_sort == NULL) error("Could not allocate memory for the town-sorting-list");
 
 	FOR_ALL_TOWNS(t) _town_sort[n++] = t;