src/industry_gui.cpp
changeset 6636 fec086b5c340
parent 6624 880e29b1f25e
child 6815 3cc63cbcad5b
equal deleted inserted replaced
6635:73064d1771ed 6636:fec086b5c340
   285 	return i->production_rate[pt] == 255;
   285 	return i->production_rate[pt] == 255;
   286 }
   286 }
   287 
   287 
   288 static inline bool IsProductionAlterable(const Industry *i)
   288 static inline bool IsProductionAlterable(const Industry *i)
   289 {
   289 {
       
   290 	const IndustrySpec *ind = GetIndustrySpec(i->type);
   290 	return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
   291 	return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
   291 		     (i->accepts_cargo[0] == CT_INVALID || i->accepts_cargo[0] == CT_VALUABLES));
   292 			(ind->accepts_cargo[0] == CT_INVALID || ind->accepts_cargo[0] == CT_VALUABLES));
   292 }
   293 }
   293 
   294 
   294 static void IndustryViewWndProc(Window *w, WindowEvent *e)
   295 static void IndustryViewWndProc(Window *w, WindowEvent *e)
   295 {
   296 {
   296 	/* WP(w,vp2_d).data_1 is for the editbox line
   297 	/* WP(w,vp2_d).data_1 is for the editbox line
   298 	 * WP(w,vp2_d).data_3 is for the click pos (left or right) */
   299 	 * WP(w,vp2_d).data_3 is for the click pos (left or right) */
   299 
   300 
   300 	switch (e->event) {
   301 	switch (e->event) {
   301 	case WE_PAINT: {
   302 	case WE_PAINT: {
   302 		const Industry *i = GetIndustry(w->window_number);
   303 		const Industry *i = GetIndustry(w->window_number);
       
   304 		const IndustrySpec *ind = GetIndustrySpec(i->type);
   303 
   305 
   304 		SetDParam(0, w->window_number);
   306 		SetDParam(0, w->window_number);
   305 		DrawWindowWidgets(w);
   307 		DrawWindowWidgets(w);
   306 
   308 
   307 		if (i->accepts_cargo[0] != CT_INVALID) {
   309 		if (ind->accepts_cargo[0] != CT_INVALID) {
   308 			StringID str;
   310 			StringID str;
   309 
   311 
   310 			SetDParam(0, GetCargo(i->accepts_cargo[0])->name);
   312 			SetDParam(0, GetCargo(ind->accepts_cargo[0])->name);
   311 			str = STR_4827_REQUIRES;
   313 			str = STR_4827_REQUIRES;
   312 			if (i->accepts_cargo[1] != CT_INVALID) {
   314 			if (ind->accepts_cargo[1] != CT_INVALID) {
   313 				SetDParam(1, GetCargo(i->accepts_cargo[1])->name);
   315 				SetDParam(1, GetCargo(ind->accepts_cargo[1])->name);
   314 				str = STR_4828_REQUIRES;
   316 				str = STR_4828_REQUIRES;
   315 				if (i->accepts_cargo[2] != CT_INVALID) {
   317 				if (ind->accepts_cargo[2] != CT_INVALID) {
   316 					SetDParam(2, GetCargo(i->accepts_cargo[2])->name);
   318 					SetDParam(2, GetCargo(ind->accepts_cargo[2])->name);
   317 					str = STR_4829_REQUIRES;
   319 					str = STR_4829_REQUIRES;
   318 				}
   320 				}
   319 			}
   321 			}
   320 			DrawString(2, 107, str, 0);
   322 			DrawString(2, 107, str, 0);
   321 		}
   323 		}
   322 
   324 
   323 		if (i->produced_cargo[0] != CT_INVALID) {
   325 		if (ind->produced_cargo[0] != CT_INVALID) {
   324 			DrawString(2, 117, STR_482A_PRODUCTION_LAST_MONTH, 0);
   326 			DrawString(2, 117, STR_482A_PRODUCTION_LAST_MONTH, 0);
   325 
   327 
   326 			SetDParam(0, i->produced_cargo[0]);
   328 			SetDParam(0, ind->produced_cargo[0]);
   327 			SetDParam(1, i->total_production[0]);
   329 			SetDParam(1, i->total_production[0]);
   328 
   330 
   329 			SetDParam(2, i->pct_transported[0] * 100 >> 8);
   331 			SetDParam(2, i->pct_transported[0] * 100 >> 8);
   330 			DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), 127, STR_482B_TRANSPORTED, 0);
   332 			DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), 127, STR_482B_TRANSPORTED, 0);
   331 			/* Let's put out those buttons.. */
   333 			/* Let's put out those buttons.. */
   332 			if (IsProductionAlterable(i)) {
   334 			if (IsProductionAlterable(i)) {
   333 				DrawArrowButtons(5, 127, 3, (WP(w, vp2_d).data_2 == 1) ? WP(w, vp2_d).data_3 : 0,
   335 				DrawArrowButtons(5, 127, 3, (WP(w, vp2_d).data_2 == 1) ? WP(w, vp2_d).data_3 : 0,
   334 						!isProductionMinimum(i, 0), !isProductionMaximum(i, 0));
   336 						!isProductionMinimum(i, 0), !isProductionMaximum(i, 0));
   335 			}
   337 			}
   336 
   338 
   337 			if (i->produced_cargo[1] != CT_INVALID) {
   339 			if (ind->produced_cargo[1] != CT_INVALID) {
   338 				SetDParam(0, i->produced_cargo[1]);
   340 				SetDParam(0, ind->produced_cargo[1]);
   339 				SetDParam(1, i->total_production[1]);
   341 				SetDParam(1, i->total_production[1]);
   340 				SetDParam(2, i->pct_transported[1] * 100 >> 8);
   342 				SetDParam(2, i->pct_transported[1] * 100 >> 8);
   341 				DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), 137, STR_482B_TRANSPORTED, 0);
   343 				DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), 137, STR_482B_TRANSPORTED, 0);
   342 				/* Let's put out those buttons.. */
   344 				/* Let's put out those buttons.. */
   343 				if (IsProductionAlterable(i)) {
   345 				if (IsProductionAlterable(i)) {
   363 			/* We should work if needed.. */
   365 			/* We should work if needed.. */
   364 			if (!IsProductionAlterable(i)) return;
   366 			if (!IsProductionAlterable(i)) return;
   365 
   367 
   366 			x = e->we.click.pt.x;
   368 			x = e->we.click.pt.x;
   367 			line = (e->we.click.pt.y - 127) / 10;
   369 			line = (e->we.click.pt.y - 127) / 10;
   368 			if (e->we.click.pt.y >= 127 && IS_INT_INSIDE(line, 0, 2) && i->produced_cargo[line] != CT_INVALID) {
   370 			if (e->we.click.pt.y >= 127 && IS_INT_INSIDE(line, 0, 2) &&
       
   371 					GetIndustrySpec(i->type)->produced_cargo[line] != CT_INVALID) {
   369 				if (IS_INT_INSIDE(x, 5, 25) ) {
   372 				if (IS_INT_INSIDE(x, 5, 25) ) {
   370 					/* Clicked buttons, decrease or increase production */
   373 					/* Clicked buttons, decrease or increase production */
   371 					if (x < 15) {
   374 					if (x < 15) {
   372 						if (isProductionMinimum(i, line)) return;
   375 						if (isProductionMinimum(i, line)) return;
   373 						i->production_rate[line] = max(i->production_rate[line] / 2, 1);
   376 						i->production_rate[line] = max(i->production_rate[line] / 2, 1);
   414 	}
   417 	}
   415 }
   418 }
   416 
   419 
   417 static void UpdateIndustryProduction(Industry *i)
   420 static void UpdateIndustryProduction(Industry *i)
   418 {
   421 {
   419 	if (i->produced_cargo[0] != CT_INVALID)
   422 	const IndustrySpec *ind = GetIndustrySpec(i->type);
   420 		i->total_production[0] = 8 * i->production_rate[0];
   423 
   421 
   424 	for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
   422 	if (i->produced_cargo[1] != CT_INVALID)
   425 		if (ind->produced_cargo[j] != CT_INVALID) {
   423 		i->total_production[1] = 8 * i->production_rate[1];
   426 			i->total_production[j] = 8 * i->production_rate[j];
       
   427 		}
       
   428 	}
   424 }
   429 }
   425 
   430 
   426 static const Widget _industry_view_widgets[] = {
   431 static const Widget _industry_view_widgets[] = {
   427 {   WWT_CLOSEBOX,   RESIZE_NONE,     9,     0,    10,     0,    13, STR_00C5,          STR_018B_CLOSE_WINDOW},
   432 {   WWT_CLOSEBOX,   RESIZE_NONE,     9,     0,    10,     0,    13, STR_00C5,          STR_018B_CLOSE_WINDOW},
   428 {    WWT_CAPTION,   RESIZE_NONE,     9,    11,   247,     0,    13, STR_4801,          STR_018C_WINDOW_TITLE_DRAG_THIS},
   433 {    WWT_CAPTION,   RESIZE_NONE,     9,    11,   247,     0,    13, STR_4801,          STR_018C_WINDOW_TITLE_DRAG_THIS},
   480 
   485 
   481 static int CDECL GeneralIndustrySorter(const void *a, const void *b)
   486 static int CDECL GeneralIndustrySorter(const void *a, const void *b)
   482 {
   487 {
   483 	const Industry* i = *(const Industry**)a;
   488 	const Industry* i = *(const Industry**)a;
   484 	const Industry* j = *(const Industry**)b;
   489 	const Industry* j = *(const Industry**)b;
       
   490 	const IndustrySpec *ind_i = GetIndustrySpec(i->type);
       
   491 	const IndustrySpec *ind_j = GetIndustrySpec(j->type);
   485 	int r;
   492 	int r;
   486 
   493 
   487 	switch (_industry_sort_order >> 1) {
   494 	switch (_industry_sort_order >> 1) {
   488 		default: NOT_REACHED();
   495 		default: NOT_REACHED();
   489 		case 0: /* Sort by Name (handled later) */
   496 		case 0: /* Sort by Name (handled later) */
   493 		case 1: /* Sort by Type */
   500 		case 1: /* Sort by Type */
   494 			r = i->type - j->type;
   501 			r = i->type - j->type;
   495 			break;
   502 			break;
   496 
   503 
   497 		case 2: /* Sort by Production */
   504 		case 2: /* Sort by Production */
   498 			if (i->produced_cargo[0] == CT_INVALID) {
   505 			if (ind_i->produced_cargo[0] == CT_INVALID) {
   499 				r = (j->produced_cargo[0] == CT_INVALID ? 0 : -1);
   506 				r = (ind_j->produced_cargo[0] == CT_INVALID ? 0 : -1);
   500 			} else {
   507 			} else {
   501 				if (j->produced_cargo[0] == CT_INVALID) {
   508 				if (ind_j->produced_cargo[0] == CT_INVALID) {
   502 					r = 1;
   509 					r = 1;
   503 				} else {
   510 				} else {
   504 					r =
   511 					r =
   505 						(i->total_production[0] + i->total_production[1]) -
   512 						(i->total_production[0] + i->total_production[1]) -
   506 						(j->total_production[0] + j->total_production[1]);
   513 						(j->total_production[0] + j->total_production[1]);
   507 				}
   514 				}
   508 			}
   515 			}
   509 			break;
   516 			break;
   510 
   517 
   511 		case 3: /* Sort by transported fraction */
   518 		case 3: /* Sort by transported fraction */
   512 			if (i->produced_cargo[0] == CT_INVALID) {
   519 			if (ind_i->produced_cargo[0] == CT_INVALID) {
   513 				r = (j->produced_cargo[0] == CT_INVALID ? 0 : -1);
   520 				r = (ind_j->produced_cargo[0] == CT_INVALID ? 0 : -1);
   514 			} else {
   521 			} else {
   515 				if (j->produced_cargo[0] == CT_INVALID) {
   522 				if (ind_j->produced_cargo[0] == CT_INVALID) {
   516 					r = 1;
   523 					r = 1;
   517 				} else {
   524 				} else {
   518 					int pi;
   525 					int pi;
   519 					int pj;
   526 					int pj;
   520 
   527 
   521 					pi = i->pct_transported[0] * 100 >> 8;
   528 					pi = i->pct_transported[0] * 100 >> 8;
   522 					if (i->produced_cargo[1] != CT_INVALID) {
   529 					if (ind_i->produced_cargo[1] != CT_INVALID) {
   523 						int p = i->pct_transported[1] * 100 >> 8;
   530 						int p = i->pct_transported[1] * 100 >> 8;
   524 						if (p < pi) pi = p;
   531 						if (p < pi) pi = p;
   525 					}
   532 					}
   526 
   533 
   527 					pj = j->pct_transported[0] * 100 >> 8;
   534 					pj = j->pct_transported[0] * 100 >> 8;
   528 					if (j->produced_cargo[1] != CT_INVALID) {
   535 					if (ind_j->produced_cargo[1] != CT_INVALID) {
   529 						int p = j->pct_transported[1] * 100 >> 8;
   536 						int p = j->pct_transported[1] * 100 >> 8;
   530 						if (p < pj) pj = p;
   537 						if (p < pj) pj = p;
   531 					}
   538 					}
   532 
   539 
   533 					r = pi - pj;
   540 					r = pi - pj;
   604 		p = w->vscroll.pos;
   611 		p = w->vscroll.pos;
   605 		n = 0;
   612 		n = 0;
   606 
   613 
   607 		while (p < _num_industry_sort) {
   614 		while (p < _num_industry_sort) {
   608 			const Industry* i = _industry_sort[p];
   615 			const Industry* i = _industry_sort[p];
       
   616 			const IndustrySpec *ind = GetIndustrySpec(i->type);
   609 
   617 
   610 			SetDParam(0, i->index);
   618 			SetDParam(0, i->index);
   611 			if (i->produced_cargo[0] != CT_INVALID) {
   619 			if (ind->produced_cargo[0] != CT_INVALID) {
   612 				SetDParam(1, i->produced_cargo[0]);
   620 				SetDParam(1, ind->produced_cargo[0]);
   613 				SetDParam(2, i->total_production[0]);
   621 				SetDParam(2, i->total_production[0]);
   614 
   622 
   615 				if (i->produced_cargo[1] != CT_INVALID) {
   623 				if (ind->produced_cargo[1] != CT_INVALID) {
   616 					SetDParam(3, i->produced_cargo[1]);
   624 					SetDParam(3, ind->produced_cargo[1]);
   617 					SetDParam(4, i->total_production[1]);
   625 					SetDParam(4, i->total_production[1]);
   618 					SetDParam(5, i->pct_transported[0] * 100 >> 8);
   626 					SetDParam(5, i->pct_transported[0] * 100 >> 8);
   619 					SetDParam(6, i->pct_transported[1] * 100 >> 8);
   627 					SetDParam(6, i->pct_transported[1] * 100 >> 8);
   620 					DrawString(4, 28 + n * 10, STR_INDUSTRYDIR_ITEM_TWO, 0);
   628 					DrawString(4, 28 + n * 10, STR_INDUSTRYDIR_ITEM_TWO, 0);
   621 				} else {
   629 				} else {