graph_gui.c
changeset 0 29654efe3188
child 2 104b2984cd3e
equal deleted inserted replaced
-1:000000000000 0:29654efe3188
       
     1 #include "stdafx.h"
       
     2 #include "ttd.h"
       
     3 #include "window.h"
       
     4 #include "gui.h"
       
     5 #include "gfx.h"
       
     6 #include "player.h"
       
     7 
       
     8 static uint _legend_showbits;
       
     9 static uint _legend_cargobits;
       
    10 
       
    11 /************************/
       
    12 /* GENERIC GRAPH DRAWER */
       
    13 /************************/
       
    14 
       
    15 typedef struct GraphDrawer {
       
    16 	uint sel;
       
    17 	byte num_dataset;
       
    18 	byte num_on_x_axis;
       
    19 	byte month;
       
    20 	byte year;
       
    21 	bool include_neg;
       
    22 	byte num_vert_lines;
       
    23 	uint16 unk61A;
       
    24 	uint16 unk61C;
       
    25 	int left, top;
       
    26 	uint height;
       
    27 	StringID format_str_y_axis;
       
    28 	byte color_3, color_2, bg_line_color;
       
    29 	byte colors[16];
       
    30 	uint32 cost[16][24];
       
    31 } GraphDrawer;
       
    32 
       
    33 void DrawGraph(GraphDrawer *gw)
       
    34 {
       
    35 	
       
    36 	int i,j,k;
       
    37 	int x,y,old_x,old_y;
       
    38 	int color;
       
    39 	int right, bottom;
       
    40 	int num_x, num_dataset;
       
    41 	uint32 *row_ptr, *col_ptr;
       
    42 	int32 mx;
       
    43 	int adj_height;
       
    44 	uint32 y_scaling, tmp;
       
    45 	int32 value;
       
    46 	int32 cur_val;
       
    47 	uint sel;
       
    48 
       
    49 	color = _color_list[gw->bg_line_color].window_color_1b;
       
    50 
       
    51 	/* draw the vertical lines */
       
    52 	i = gw->num_vert_lines; assert(i > 0);
       
    53 	x = gw->left + 66;
       
    54 	bottom = gw->top + gw->height - 1;
       
    55 	do {
       
    56 		GfxFillRect(x, gw->top, x, bottom, color);
       
    57 		x += 22;
       
    58 	} while (--i);
       
    59 	
       
    60 	/* draw the horizontal lines */
       
    61 	i = 9;
       
    62 	x = gw->left + 44;
       
    63 	y = gw->height + gw->top;
       
    64 	right = gw->left + 44 + gw->num_vert_lines*22-1;
       
    65 
       
    66 	do {
       
    67 		GfxFillRect(x, y, right, y, color);
       
    68 		y -= gw->height >> 3;
       
    69 	} while (--i);
       
    70 
       
    71 	/* draw vertical edge line */
       
    72 	GfxFillRect(x, gw->top, x, bottom, gw->color_2);
       
    73 
       
    74 	adj_height = gw->height;
       
    75 	if (gw->include_neg) adj_height >>= 1;
       
    76 
       
    77 	/* draw horiz edge line */
       
    78 	y = adj_height + gw->top;
       
    79 	GfxFillRect(x, y, right, y, gw->color_2);
       
    80 
       
    81 	/* find the max element */
       
    82 	if (gw->num_on_x_axis == 0)
       
    83 		return;
       
    84 
       
    85 	num_dataset = gw->num_dataset;assert(num_dataset > 0);
       
    86 	row_ptr = gw->cost[0];
       
    87 	mx = 0;
       
    88 	do {
       
    89 		num_x = gw->num_on_x_axis;assert(num_x > 0);
       
    90 		col_ptr = row_ptr;
       
    91 		do {
       
    92 			if (*col_ptr != 0x80000000) {
       
    93 				mx = max(mx, myabs(*col_ptr));
       
    94 			}
       
    95 		} while (col_ptr++, --num_x);
       
    96 	} while (row_ptr+=24, --num_dataset);
       
    97 	
       
    98 	/* setup scaling */
       
    99 	y_scaling = 0x80000000;
       
   100 	value = adj_height * 2;
       
   101 
       
   102 	if (mx > value) {
       
   103 		mx = (mx + 7) & ~7;
       
   104 		y_scaling = (uint32) (((uint64) (value>>1) << 32) / mx);
       
   105 		value = mx;
       
   106 	}
       
   107 
       
   108 	/* draw text strings on the y axis */
       
   109 	tmp = value;
       
   110 	if (gw->include_neg) tmp >>= 1;
       
   111 	x = gw->left + 45;
       
   112 	y = gw->top - 3;
       
   113 	i = 9;
       
   114 	do {
       
   115 		SET_DPARAM16(0, gw->format_str_y_axis);
       
   116 		SET_DPARAM32(1, tmp);
       
   117 		tmp -= (value >> 3);
       
   118 		DrawStringRightAligned(x, y, STR_0170, gw->color_3);
       
   119 		y += gw->height >> 3;
       
   120 	} while (--i);
       
   121 
       
   122 	/* draw strings on the x axis */
       
   123 	if (gw->month != 0xFF) {
       
   124 		x = gw->left + 44;
       
   125 		y = gw->top + gw->height + 1;
       
   126 		j = gw->month;
       
   127 		k = gw->year + 1920;
       
   128 		i = gw->num_on_x_axis;assert(i>0);
       
   129 		do {
       
   130 			SET_DPARAM16(2, k);
       
   131 			SET_DPARAM16(0, j + STR_0162_JAN);
       
   132 			SET_DPARAM16(1, j + STR_0162_JAN + 2);
       
   133 			DrawString(x, y, j == 0 ? STR_016F : STR_016E, gw->color_3);
       
   134 
       
   135 			j += 3;
       
   136 			if (j >= 12) {
       
   137 				j = 0;
       
   138 				k++;
       
   139 			}
       
   140 			x += 22;
       
   141 		} while (--i);
       
   142 	} else {
       
   143 		x = gw->left + 52;
       
   144 		y = gw->top + gw->height + 1;
       
   145 		j = gw->unk61A;
       
   146 		i = gw->num_on_x_axis;assert(i>0);
       
   147 		do {
       
   148 			SET_DPARAM16(0, j);
       
   149 			DrawString(x, y, STR_01CB, gw->color_3);
       
   150 			j += gw->unk61C;
       
   151 			x += 22;
       
   152 		} while (--i);
       
   153 	}
       
   154 
       
   155 	/* draw lines and dots */
       
   156 	i = 0;
       
   157 	row_ptr = gw->cost[0];
       
   158 	sel = gw->sel;
       
   159 	do {
       
   160 		if (!(sel & 1)) {
       
   161 			x = gw->left + 55;
       
   162 			j = gw->num_on_x_axis;assert(j>0);
       
   163 			col_ptr = row_ptr;
       
   164 			color = gw->colors[i];
       
   165 			old_y = old_x = 0x80000000;
       
   166 			do {
       
   167 				cur_val = *col_ptr++;
       
   168 				if (cur_val != (int32)0x80000000) {
       
   169 					y = adj_height - BIGMULSS(cur_val, y_scaling >> 1, 31) + gw->top;
       
   170 
       
   171 					GfxFillRect(x-1, y-1, x+1, y+1, color);
       
   172 					if (old_x != 0x80000000)
       
   173 						GfxDrawLine(old_x, old_y, x, y, color);
       
   174 
       
   175 					old_x = x;
       
   176 					old_y = y;
       
   177 				} else {
       
   178 					old_x = 0x80000000;
       
   179 				}
       
   180 			} while (x+=22,--j);
       
   181 		}
       
   182 	} while (sel>>=1,row_ptr+=24, ++i < gw->num_dataset);
       
   183 }
       
   184 
       
   185 /****************/
       
   186 /* GRAPH LEGEND */
       
   187 /****************/
       
   188 
       
   189 void DrawPlayerIcon(int p, int x, int y)
       
   190 {
       
   191 	DrawSprite(SPRITE_PALETTE(PLAYER_SPRITE_COLOR(p) + 0x2EB), x, y);
       
   192 }
       
   193 
       
   194 static void GraphLegendWndProc(Window *w, WindowEvent *e)
       
   195 {
       
   196 	Player *p;
       
   197 
       
   198 	switch(e->event) {
       
   199 	case WE_PAINT:
       
   200 		FOR_ALL_PLAYERS(p) {
       
   201 			if (!p->is_active)
       
   202 				SETBIT(_legend_showbits, p->index);
       
   203 		}
       
   204 		w->click_state = ((~_legend_showbits) << 3);
       
   205 		DrawWindowWidgets(w);
       
   206 
       
   207 		FOR_ALL_PLAYERS(p) {
       
   208 			if (!p->is_active)
       
   209 				continue;
       
   210 
       
   211 			DrawPlayerIcon(p->index, 4, 18+p->index*12);
       
   212 
       
   213 			SET_DPARAM16(0, p->name_1);
       
   214 			SET_DPARAM32(1, p->name_2);
       
   215 			SET_DPARAM16(2, GetPlayerNameString(p->index));
       
   216 			DrawString(21,17+p->index*12,STR_7021,HASBIT(_legend_showbits, p->index) ? 0x10 : 0xC);
       
   217 		}
       
   218 		break;
       
   219 
       
   220 	case WE_CLICK:
       
   221 		if (IS_INT_INSIDE(e->click.widget, 3, 11)) {
       
   222 			_legend_showbits ^= (1 << (e->click.widget-3));
       
   223 			SetWindowDirty(w);
       
   224 			InvalidateWindow(WC_INCOME_GRAPH, 0);
       
   225 			InvalidateWindow(WC_OPERATING_PROFIT, 0);
       
   226 			InvalidateWindow(WC_DELIVERED_CARGO, 0);
       
   227 			InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
       
   228 		}
       
   229 		break;
       
   230 	}
       
   231 }
       
   232 
       
   233 static const Widget _graph_legend_widgets[] = {
       
   234 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5, STR_018B_CLOSE_WINDOW},
       
   235 {    WWT_CAPTION,    14,    11,   249,     0,    13, STR_704E_KEY_TO_COMPANY_GRAPHS, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   236 {     WWT_IMGBTN,    14,     0,   249,    14,   113, 0x0},
       
   237 {     WWT_IMGBTN,    14,     2,   247,    16,    27, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   238 {     WWT_IMGBTN,    14,     2,   247,    28,    39, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   239 {     WWT_IMGBTN,    14,     2,   247,    40,    51, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   240 {     WWT_IMGBTN,    14,     2,   247,    52,    63, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   241 {     WWT_IMGBTN,    14,     2,   247,    64,    75, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   242 {     WWT_IMGBTN,    14,     2,   247,    76,    87, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   243 {     WWT_IMGBTN,    14,     2,   247,    88,    99, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   244 {     WWT_IMGBTN,    14,     2,   247,   100,   111, 0x0,STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
       
   245 {      WWT_LAST},
       
   246 };
       
   247 
       
   248 static const WindowDesc _graph_legend_desc = {
       
   249 	-1, -1, 250, 114,
       
   250 	WC_GRAPH_LEGEND,0,
       
   251 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
       
   252 	_graph_legend_widgets,
       
   253 	GraphLegendWndProc
       
   254 };
       
   255 
       
   256 static void ShowGraphLegend()
       
   257 {
       
   258 	AllocateWindowDescFront(&_graph_legend_desc, 0);
       
   259 }
       
   260 
       
   261 /********************/
       
   262 /* OPERATING PROFIT */
       
   263 /********************/
       
   264 
       
   265 static void SetupGraphDrawerForPlayers(GraphDrawer *gd)
       
   266 {
       
   267 	Player *p;
       
   268 	uint showbits = _legend_showbits;
       
   269 	int nums;
       
   270 	int mo,yr;
       
   271 
       
   272 	// Exclude the players which aren't valid	
       
   273 	FOR_ALL_PLAYERS(p) {
       
   274 		if (!p->is_active) CLRBIT(showbits,p->index);
       
   275 	}	
       
   276 	gd->sel = showbits;
       
   277 	gd->num_vert_lines = 24;
       
   278 
       
   279 	nums = 0;
       
   280 	FOR_ALL_PLAYERS(p) {
       
   281 		if (p->is_active) nums = max(nums,p->num_valid_stat_ent);
       
   282 	}
       
   283 	gd->num_on_x_axis = min(nums,24);
       
   284 
       
   285 	mo = (_cur_month/3-nums)*3;
       
   286 	yr = _cur_year;
       
   287 	while (mo < 0) {
       
   288 		yr--;
       
   289 		mo += 12;
       
   290 	}
       
   291 
       
   292 	gd->year = yr;
       
   293 	gd->month = mo;
       
   294 }
       
   295 
       
   296 static void OperatingProfitWndProc(Window *w, WindowEvent *e)
       
   297 {
       
   298 	switch(e->event) {
       
   299 	case WE_PAINT: {
       
   300 		GraphDrawer gd;
       
   301 		Player *p;
       
   302 		int i,j;
       
   303 		int numd;
       
   304 
       
   305 		DrawWindowWidgets(w);
       
   306 		
       
   307 		gd.left = 2;
       
   308 		gd.top = 18;
       
   309 		gd.height = 136;
       
   310 		gd.include_neg = true;
       
   311 		gd.format_str_y_axis = STR_7023;
       
   312 		gd.color_3 = 0x10;
       
   313 		gd.color_2 = 0xD7;
       
   314 		gd.bg_line_color = 0xE;
       
   315 		
       
   316 		SetupGraphDrawerForPlayers(&gd);
       
   317 
       
   318 		numd = 0;
       
   319 		FOR_ALL_PLAYERS(p) {
       
   320 			if (!p->is_active)
       
   321 				continue;
       
   322 			gd.colors[numd] = _color_list[p->player_color].window_color_bgb;
       
   323 			for(j=gd.num_on_x_axis,i=0; --j >= 0;) {
       
   324 				gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? 0x80000000 : 	(p->old_economy[j].income + p->old_economy[j].expenses);
       
   325 				i++;
       
   326 			}
       
   327 			numd++;
       
   328 		}
       
   329 		gd.num_dataset=numd;
       
   330 
       
   331 		DrawGraph(&gd);
       
   332 		break;
       
   333 	}
       
   334 
       
   335 	case WE_CLICK:
       
   336 		if (e->click.widget == 2)
       
   337 			ShowGraphLegend();
       
   338 		break;
       
   339 	}
       
   340 }
       
   341 
       
   342 static const Widget _operating_profit_widgets[] = {
       
   343 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5,STR_018B_CLOSE_WINDOW},
       
   344 {    WWT_CAPTION,    14,    11,   525,     0,    13, STR_7025_OPERATING_PROFIT_GRAPH, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   345 { WWT_PUSHTXTBTN,    14,   526,   575,     0,    13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
       
   346 {     WWT_IMGBTN,    14,     0,   575,    14,   173, 0x0},
       
   347 {      WWT_LAST},
       
   348 };
       
   349 
       
   350 static const WindowDesc _operating_profit_desc = {
       
   351 	-1, -1, 576, 174,
       
   352 	WC_OPERATING_PROFIT,0,
       
   353 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
       
   354 	_operating_profit_widgets,
       
   355 	OperatingProfitWndProc
       
   356 };
       
   357 
       
   358 
       
   359 void ShowOperatingProfitGraph()
       
   360 {
       
   361 	if (AllocateWindowDescFront(&_operating_profit_desc, 0)) {
       
   362 		InvalidateWindow(WC_GRAPH_LEGEND, 0);
       
   363 		_legend_showbits = 0;
       
   364 	}
       
   365 }
       
   366 
       
   367 
       
   368 /****************/
       
   369 /* INCOME GRAPH */
       
   370 /****************/
       
   371 
       
   372 static void IncomeGraphWndProc(Window *w, WindowEvent *e)
       
   373 {
       
   374 	switch(e->event) {
       
   375 	case WE_PAINT: {
       
   376 		GraphDrawer gd;
       
   377 		Player *p;
       
   378 		int i,j;
       
   379 		int numd;
       
   380 
       
   381 		DrawWindowWidgets(w);
       
   382 
       
   383 		gd.left = 2;
       
   384 		gd.top = 18;
       
   385 		gd.height = 104;
       
   386 		gd.include_neg = false;
       
   387 		gd.format_str_y_axis = STR_7023;
       
   388 		gd.color_3 = 0x10;
       
   389 		gd.color_2 = 0xD7;
       
   390 		gd.bg_line_color = 0xE;
       
   391 		SetupGraphDrawerForPlayers(&gd);
       
   392 
       
   393 		numd = 0;
       
   394 		FOR_ALL_PLAYERS(p) {
       
   395 			if (!p->is_active)
       
   396 				continue;
       
   397 			gd.colors[numd] = _color_list[p->player_color].window_color_bgb;
       
   398 			for(j=gd.num_on_x_axis,i=0; --j >= 0;) {
       
   399 				gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? 0x80000000 : (p->old_economy[j].income);
       
   400 				i++;
       
   401 			}
       
   402 			numd++;
       
   403 		}
       
   404 
       
   405 		gd.num_dataset = numd;
       
   406 
       
   407 		DrawGraph(&gd);
       
   408 		break;
       
   409 	}
       
   410 
       
   411 	case WE_CLICK:
       
   412 		if (e->click.widget == 2)
       
   413 			ShowGraphLegend();
       
   414 		break;
       
   415 	}
       
   416 }
       
   417 
       
   418 static const Widget _income_graph_widgets[] = {
       
   419 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5,STR_018B_CLOSE_WINDOW},
       
   420 {    WWT_CAPTION,    14,    11,   525,     0,    13, STR_7022_INCOME_GRAPH, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   421 { WWT_PUSHTXTBTN,    14,   526,   575,     0,    13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
       
   422 {     WWT_IMGBTN,    14,     0,   575,    14,   141, 0x0},
       
   423 {      WWT_LAST},
       
   424 };
       
   425 
       
   426 static const WindowDesc _income_graph_desc = {
       
   427 	-1, -1, 576, 142,
       
   428 	WC_INCOME_GRAPH,0,
       
   429 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
       
   430 	_income_graph_widgets,
       
   431 	IncomeGraphWndProc
       
   432 };
       
   433 
       
   434 void ShowIncomeGraph()
       
   435 {
       
   436 	if (AllocateWindowDescFront(&_income_graph_desc, 0)) {
       
   437 		InvalidateWindow(WC_GRAPH_LEGEND, 0);
       
   438 		_legend_showbits = 0;
       
   439 	}
       
   440 }
       
   441 
       
   442 /*******************/
       
   443 /* DELIVERED CARGO */
       
   444 /*******************/
       
   445 
       
   446 static void DeliveredCargoGraphWndProc(Window *w, WindowEvent *e)
       
   447 {
       
   448 	switch(e->event) {
       
   449 	case WE_PAINT: {
       
   450 		GraphDrawer gd;
       
   451 		Player *p;
       
   452 		int i,j;
       
   453 		int numd;
       
   454 
       
   455 		DrawWindowWidgets(w);
       
   456 
       
   457 		gd.left = 2;
       
   458 		gd.top = 18;
       
   459 		gd.height = 104;
       
   460 		gd.include_neg = false;
       
   461 		gd.format_str_y_axis = STR_7024;
       
   462 		gd.color_3 = 0x10;
       
   463 		gd.color_2 = 0xD7;
       
   464 		gd.bg_line_color = 0xE;
       
   465 		SetupGraphDrawerForPlayers(&gd);
       
   466 
       
   467 		numd = 0;
       
   468 		FOR_ALL_PLAYERS(p) {
       
   469 			if (!p->is_active)
       
   470 				continue;
       
   471 			gd.colors[numd] = _color_list[p->player_color].window_color_bgb;
       
   472 			for(j=gd.num_on_x_axis,i=0; --j >= 0;) {
       
   473 				gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? 0x80000000 : p->old_economy[j].delivered_cargo;
       
   474 				i++;
       
   475 			}
       
   476 			numd++;
       
   477 		}
       
   478 
       
   479 		gd.num_dataset = numd;
       
   480 
       
   481 		DrawGraph(&gd);
       
   482 		break;
       
   483 	}
       
   484 
       
   485 	case WE_CLICK:
       
   486 		if (e->click.widget == 2)
       
   487 			ShowGraphLegend();
       
   488 		break;
       
   489 	}
       
   490 }
       
   491 
       
   492 static const Widget _delivered_cargo_graph_widgets[] = {
       
   493 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5, STR_018B_CLOSE_WINDOW},
       
   494 {    WWT_CAPTION,    14,    11,   525,     0,    13, STR_7050_UNITS_OF_CARGO_DELIVERED, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   495 { WWT_PUSHTXTBTN,    14,   526,   575,     0,    13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
       
   496 {     WWT_IMGBTN,    14,     0,   575,    14,   141, 0x0},
       
   497 {      WWT_LAST},
       
   498 };
       
   499 
       
   500 static const WindowDesc _delivered_cargo_graph_desc = {
       
   501 	-1, -1, 576, 142,
       
   502 	WC_DELIVERED_CARGO,0,
       
   503 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
       
   504 	_delivered_cargo_graph_widgets,
       
   505 	DeliveredCargoGraphWndProc
       
   506 };
       
   507 
       
   508 void ShowDeliveredCargoGraph()
       
   509 {
       
   510 	if (AllocateWindowDescFront(&_delivered_cargo_graph_desc, 0)) {
       
   511 		InvalidateWindow(WC_GRAPH_LEGEND, 0);
       
   512 		_legend_showbits = 0;
       
   513 	}
       
   514 }	
       
   515 
       
   516 /***********************/
       
   517 /* PERFORMANCE HISTORY */
       
   518 /***********************/
       
   519 
       
   520 static void PerformanceHistoryWndProc(Window *w, WindowEvent *e)
       
   521 {
       
   522 	switch(e->event) {
       
   523 	case WE_PAINT: {
       
   524 		GraphDrawer gd;
       
   525 		Player *p;
       
   526 		int i,j;
       
   527 		int numd;
       
   528 
       
   529 		DrawWindowWidgets(w);
       
   530 
       
   531 		gd.left = 2;
       
   532 		gd.top = 18;
       
   533 		gd.height = 200;
       
   534 		gd.include_neg = false;
       
   535 		gd.format_str_y_axis = STR_7024;
       
   536 		gd.color_3 = 0x10;
       
   537 		gd.color_2 = 0xD7;
       
   538 		gd.bg_line_color = 0xE;
       
   539 		SetupGraphDrawerForPlayers(&gd);
       
   540 
       
   541 		numd = 0;
       
   542 		FOR_ALL_PLAYERS(p) {
       
   543 			if (!p->is_active)
       
   544 				continue;
       
   545 			gd.colors[numd] = _color_list[p->player_color].window_color_bgb;
       
   546 			for(j=gd.num_on_x_axis,i=0; --j >= 0;) {
       
   547 				gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? 0x80000000 : p->old_economy[j].performance_history;
       
   548 				i++;
       
   549 			}
       
   550 			numd++;
       
   551 		}
       
   552 		gd.num_dataset = numd;
       
   553 
       
   554 		DrawGraph(&gd);
       
   555 		break;
       
   556 	}
       
   557 
       
   558 	case WE_CLICK:
       
   559 		if (e->click.widget == 2)
       
   560 			ShowGraphLegend();
       
   561 		break;
       
   562 	}
       
   563 }
       
   564 
       
   565 static const Widget _performance_history_widgets[] = {
       
   566 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5, STR_018B_CLOSE_WINDOW},
       
   567 {    WWT_CAPTION,    14,    11,   525,     0,    13, STR_7051_COMPANY_PERFORMANCE_RATINGS, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   568 { WWT_PUSHTXTBTN,    14,   526,   575,     0,    13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
       
   569 {     WWT_IMGBTN,    14,     0,   575,    14,   237, 0x0},
       
   570 {      WWT_LAST},
       
   571 };
       
   572 
       
   573 static const WindowDesc _performance_history_desc = {
       
   574 	-1, -1, 576, 238,
       
   575 	WC_PERFORMANCE_HISTORY,0,
       
   576 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
       
   577 	_performance_history_widgets,
       
   578 	PerformanceHistoryWndProc
       
   579 };
       
   580 
       
   581 void ShowPerformanceHistoryGraph()
       
   582 {
       
   583 	if (AllocateWindowDescFront(&_performance_history_desc, 0)) {
       
   584 		InvalidateWindow(WC_GRAPH_LEGEND, 0);
       
   585 		_legend_showbits = 0;
       
   586 	}
       
   587 }
       
   588 
       
   589 /*****************/
       
   590 /* COMPANY VALUE */
       
   591 /*****************/
       
   592 
       
   593 static void CompanyValueGraphWndProc(Window *w, WindowEvent *e)
       
   594 {
       
   595 	switch(e->event) {
       
   596 	case WE_PAINT: {
       
   597 		GraphDrawer gd;
       
   598 		Player *p;
       
   599 		int i,j;
       
   600 		int numd;
       
   601 
       
   602 		DrawWindowWidgets(w);
       
   603 
       
   604 		gd.left = 2;
       
   605 		gd.top = 18;
       
   606 		gd.height = 200;
       
   607 		gd.include_neg = false;
       
   608 		gd.format_str_y_axis = STR_7023;
       
   609 		gd.color_3 = 0x10;
       
   610 		gd.color_2 = 0xD7;
       
   611 		gd.bg_line_color = 0xE;
       
   612 		SetupGraphDrawerForPlayers(&gd);
       
   613 
       
   614 		numd = 0;
       
   615 		FOR_ALL_PLAYERS(p) {
       
   616 			if (!p->is_active)
       
   617 				continue;
       
   618 			gd.colors[numd] = _color_list[p->player_color].window_color_bgb;
       
   619 			for(j=gd.num_on_x_axis,i=0; --j >= 0;) {
       
   620 				gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? 0x80000000 : (p->old_economy[j].company_value);
       
   621 				i++;
       
   622 			}
       
   623 			numd++;
       
   624 		}
       
   625 		gd.num_dataset = numd;
       
   626 
       
   627 
       
   628 		DrawGraph(&gd);
       
   629 		break;
       
   630 	}
       
   631 
       
   632 	case WE_CLICK:
       
   633 		if (e->click.widget == 2)
       
   634 			ShowGraphLegend();
       
   635 		break;
       
   636 	}
       
   637 }
       
   638 
       
   639 static const Widget _company_value_graph_widgets[] = {
       
   640 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5, STR_018B_CLOSE_WINDOW},
       
   641 {    WWT_CAPTION,    14,    11,   525,     0,    13, STR_7052_COMPANY_VALUES, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   642 { WWT_PUSHTXTBTN,    14,   526,   575,     0,    13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
       
   643 {     WWT_IMGBTN,    14,     0,   575,    14,   237, 0x0},
       
   644 {      WWT_LAST},
       
   645 };
       
   646 
       
   647 static const WindowDesc _company_value_graph_desc = {
       
   648 	-1, -1, 576, 238,
       
   649 	WC_COMPANY_VALUE,0,
       
   650 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
       
   651 	_company_value_graph_widgets,
       
   652 	CompanyValueGraphWndProc
       
   653 };
       
   654 
       
   655 void ShowCompanyValueGraph()
       
   656 {
       
   657 	if (AllocateWindowDescFront(&_company_value_graph_desc, 0)) {
       
   658 		InvalidateWindow(WC_GRAPH_LEGEND, 0);
       
   659 		_legend_showbits = 0;
       
   660 	}
       
   661 }
       
   662 
       
   663 /*****************/
       
   664 /* PAYMENT RATES */
       
   665 /*****************/
       
   666 
       
   667 static const byte _cargo_legend_colors[12] = {152, 32, 15, 174, 208, 194, 191, 84, 184, 10, 202, 215};
       
   668 
       
   669 static void CargoPaymentRatesWndProc(Window *w, WindowEvent *e)
       
   670 {
       
   671 	switch(e->event) {
       
   672 	case WE_PAINT: {
       
   673 		int i, j, x, y;
       
   674 		GraphDrawer gd;
       
   675 
       
   676 		gd.sel = _legend_cargobits;
       
   677 		w->click_state = (~_legend_cargobits) << 3;
       
   678 		DrawWindowWidgets(w);
       
   679 
       
   680 		x = 495;
       
   681 		y = 25;
       
   682 
       
   683 		for(i=0; i!=NUM_CARGO; i++) {
       
   684 			GfxFillRect(x, y, x+8, y+5, 0);
       
   685 			GfxFillRect(x+1, y+1, x+7, y+4, _cargo_legend_colors[i]);
       
   686 			SET_DPARAM16(0, _cargoc.names_s[i]);
       
   687 			DrawString(x+14, y, STR_7065, 0);
       
   688 			y += 8;
       
   689 		}
       
   690 
       
   691 		gd.left = 2;
       
   692 		gd.top = 24;
       
   693 		gd.height = 104;
       
   694 		gd.include_neg = false;
       
   695 		gd.format_str_y_axis = STR_7023;
       
   696 		gd.color_3 = 16;
       
   697 		gd.color_2 = 215;
       
   698 		gd.bg_line_color = 14;
       
   699 		gd.num_dataset = 12;
       
   700 		gd.num_on_x_axis = 20;
       
   701 		gd.num_vert_lines = 20;
       
   702 		gd.month = 0xFF;
       
   703 		gd.unk61A = 10;
       
   704 		gd.unk61C = 10;
       
   705 		
       
   706 		for(i=0; i!=NUM_CARGO; i++) {
       
   707 			gd.colors[i] = _cargo_legend_colors[i];
       
   708 			for(j=0; j!=20; j++) {
       
   709 				gd.cost[i][j] = GetTransportedGoodsIncome(10, 20, j*6+6,i);
       
   710 			}
       
   711 		}
       
   712 
       
   713 		DrawGraph(&gd);
       
   714 
       
   715 		DrawString(2 + 46, 24 + gd.height + 7, STR_7062_DAYS_IN_TRANSIT, 0);
       
   716 		DrawString(2 + 84, 24 - 9, STR_7063_PAYMENT_FOR_DELIVERING, 0);
       
   717 	} break;
       
   718 
       
   719 	case WE_CLICK: {
       
   720 		switch(e->click.widget) {
       
   721 		case 3: case 4: case 5: case 6:
       
   722 		case 7: case 8: case 9: case 10:
       
   723 		case 11: case 12: case 13: case 14:
       
   724 			_legend_cargobits ^= 1 << (e->click.widget - 3);
       
   725 			SetWindowDirty(w);
       
   726 			break;
       
   727 		}
       
   728 	} break;
       
   729 	}
       
   730 }
       
   731 
       
   732 static const Widget _cargo_payment_rates_widgets[] = {
       
   733 {   WWT_CLOSEBOX,    14,     0,    10,     0,    13, STR_00C5,											STR_018B_CLOSE_WINDOW},
       
   734 {    WWT_CAPTION,    14,    11,   567,     0,    13, STR_7061_CARGO_PAYMENT_RATES,	STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   735 {      WWT_PANEL,    14,     0,   567,    14,   141, 0x0,														0},
       
   736 {      WWT_PANEL,    12,   493,   562,    24,    31, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   737 {      WWT_PANEL,    12,   493,   562,    32,    39, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   738 {      WWT_PANEL,    12,   493,   562,    40,    47, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   739 {      WWT_PANEL,    12,   493,   562,    48,    55, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   740 {      WWT_PANEL,    12,   493,   562,    56,    63, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   741 {      WWT_PANEL,    12,   493,   562,    64,    71, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   742 {      WWT_PANEL,    12,   493,   562,    72,    79, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   743 {      WWT_PANEL,    12,   493,   562,    80,    87, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   744 {      WWT_PANEL,    12,   493,   562,    88,    95, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   745 {      WWT_PANEL,    12,   493,   562,    96,   103, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   746 {      WWT_PANEL,    12,   493,   562,   104,   111, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   747 {      WWT_PANEL,    12,   493,   562,   112,   119, 0x0,														STR_7064_TOGGLE_GRAPH_FOR_CARGO},
       
   748 {      WWT_LAST},
       
   749 };
       
   750 
       
   751 static const WindowDesc _cargo_payment_rates_desc = {
       
   752 	-1, -1, 568, 142,
       
   753 	WC_PAYMENT_RATES,0,
       
   754 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
       
   755 	_cargo_payment_rates_widgets,
       
   756 	CargoPaymentRatesWndProc
       
   757 };
       
   758 
       
   759 
       
   760 void ShowCargoPaymentRates()
       
   761 {
       
   762 	AllocateWindowDescFront(&_cargo_payment_rates_desc, 0);
       
   763 }
       
   764 
       
   765 /************************/
       
   766 /* COMPANY LEAGUE TABLE */
       
   767 /************************/
       
   768 
       
   769 static const StringID _performance_titles[] = {
       
   770 	STR_7066_ENGINEER,
       
   771 	STR_7066_ENGINEER,
       
   772 	STR_7067_TRAFFIC_MANAGER,
       
   773 	STR_7067_TRAFFIC_MANAGER,
       
   774 	STR_7068_TRANSPORT_COORDINATOR,
       
   775 	STR_7068_TRANSPORT_COORDINATOR,
       
   776 	STR_7069_ROUTE_SUPERVISOR,
       
   777 	STR_7069_ROUTE_SUPERVISOR,
       
   778 	STR_706A_DIRECTOR,
       
   779 	STR_706A_DIRECTOR,
       
   780 	STR_706B_CHIEF_EXECUTIVE,
       
   781 	STR_706B_CHIEF_EXECUTIVE,
       
   782 	STR_706C_CHAIRMAN,
       
   783 	STR_706C_CHAIRMAN,
       
   784 	STR_706D_PRESIDENT,
       
   785 	STR_706E_TYCOON,
       
   786 };
       
   787 
       
   788 static StringID GetPerformanceTitleFromValue(uint v)
       
   789 {
       
   790 	return _performance_titles[minu(v, 1000) >> 6];
       
   791 }
       
   792 
       
   793 static int CDECL _perf_hist_comp(const void *elem1, const void *elem2 ) {
       
   794 	Player *p1 = *(Player**)elem1;
       
   795 	Player *p2 = *(Player**)elem2;
       
   796 	int32 v = p2->old_economy[1].performance_history - p1->old_economy[1].performance_history;
       
   797 	return (v!=0) | (v >> (sizeof(int32)*8-1));
       
   798 }
       
   799 
       
   800 static void CompanyLeagueWndProc(Window *w, WindowEvent *e)
       
   801 {
       
   802 	switch(e->event) {
       
   803 	case WE_PAINT: {
       
   804 		Player *p;
       
   805 		Player *plist[MAX_PLAYERS];
       
   806 		size_t pl_num, i;
       
   807 
       
   808 		DrawWindowWidgets(w);
       
   809 		
       
   810 		pl_num=0;
       
   811 		FOR_ALL_PLAYERS(p) {
       
   812 			if (p->is_active)
       
   813 				plist[pl_num++] = p;
       
   814 		}
       
   815 		assert(pl_num > 0);
       
   816 		
       
   817 		qsort(plist, pl_num, sizeof(Player*), _perf_hist_comp);
       
   818 
       
   819 		i = 0;
       
   820 		do {
       
   821 			SET_DPARAM16(0, i + 1 + STR_01AB);
       
   822 			p = plist[i];
       
   823 			SET_DPARAM16(1, p->name_1);
       
   824 			SET_DPARAM32(2, p->name_2);
       
   825 			
       
   826 			SET_DPARAM16(3, GetPlayerNameString(p->index));
       
   827 			SET_DPARAM16(4, GetPerformanceTitleFromValue(p->old_economy[1].performance_history));
       
   828 
       
   829 			DrawString(2, 15 + i * 10, i == 0 ? STR_7054 : STR_7055, 0);
       
   830 			DrawPlayerIcon(p->index, 27, 16 + i * 10);
       
   831 		} while (++i != pl_num);
       
   832 
       
   833 		break;
       
   834 	}
       
   835 	}
       
   836 }
       
   837 
       
   838 
       
   839 static const Widget _company_league_widgets[] = {
       
   840 {    WWT_TEXTBTN,    14,     0,    10,     0,    13, STR_00C5, STR_018B_CLOSE_WINDOW},
       
   841 {    WWT_CAPTION,    14,    11,   399,     0,    13, STR_7053_COMPANY_LEAGUE_TABLE, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   842 {     WWT_IMGBTN,    14,     0,   399,    14,    96, 0x0},
       
   843 {      WWT_LAST},
       
   844 };
       
   845 
       
   846 static const WindowDesc _company_league_desc = {
       
   847 	-1, -1, 400, 97,
       
   848 	WC_COMPANY_LEAGUE,0,
       
   849 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
       
   850 	_company_league_widgets,
       
   851 	CompanyLeagueWndProc
       
   852 };
       
   853 
       
   854 void ShowCompanyLeagueTable()
       
   855 {
       
   856 	AllocateWindowDescFront(&_company_league_desc,0);
       
   857 }