industry_gui.c
changeset 164 0cbdf3c9bde1
parent 69 f8c29cdb388e
child 176 84990c4b9212
equal deleted inserted replaced
163:deb9b58db3f4 164:0cbdf3c9bde1
   386 static char _bufcache[96];
   386 static char _bufcache[96];
   387 static byte _last_industry_idx;
   387 static byte _last_industry_idx;
   388 
   388 
   389 static byte _industry_sort_order;
   389 static byte _industry_sort_order;
   390 
   390 
   391 static int CDECL IndustrySorter(const void *a, const void *b)
   391 static int CDECL GeneralIndustrySorter(const void *a, const void *b)
   392 {
   392 {
   393 	char buf1[96];
   393 	char buf1[96];
   394 	Industry *i, *j;
       
   395 	byte val;
   394 	byte val;
       
   395 	Industry *i = DEREF_INDUSTRY(*(byte*)a);
       
   396 	Industry *j = DEREF_INDUSTRY(*(byte*)b);
   396 	int r = 0;
   397 	int r = 0;
   397 
   398 
   398 	i = DEREF_INDUSTRY(*(byte*)a);
       
   399 	j = DEREF_INDUSTRY(*(byte*)b);
       
   400 
       
   401 	switch (_industry_sort_order >> 1) {
   399 	switch (_industry_sort_order >> 1) {
   402 	case 0: 
   400 	/* case 0: Sort by Name (handled later) */
   403 		r = 0; 
   401 	case 1: /* Sort by Type */
   404 		break;
       
   405 	case 1: /* Case 1, sort by type */
       
   406 		r = i->type - j->type; 
   402 		r = i->type - j->type; 
   407 		break;
   403 		break;
   408 	case 2: /* Case 2, sort by production */
   404 	// FIXME - Production & Transported sort need to be inversed...but, WTF it does not wanna!
   409 		if (i->produced_cargo[0] != 0xFF && j->produced_cargo[0] != 0xFF) { //producing any cargo?
   405 	// FIXME - And no simple --> "if (!(_industry_sort_order & 1)) r = -r;" hack at the bottom!!
   410 				if (i->produced_cargo[1] == 0xFF) //producing one or two things?
   406 	case 2: { /* Sort by Production */
       
   407 		if (i->produced_cargo[0] != 0xFF && j->produced_cargo[0] != 0xFF) { // both industries produce cargo?
       
   408 				if (i->produced_cargo[1] == 0xFF) // producing one or two things?
   411 					r = j->total_production[0] - i->total_production[0];
   409 					r = j->total_production[0] - i->total_production[0];
   412 				else	
   410 				else	
   413 					r = (j->total_production[0] + j->total_production[1]) / 2 - (i->total_production[0] + i->total_production[1]) / 2;
   411 					r = (j->total_production[0] + j->total_production[1]) / 2 - (i->total_production[0] + i->total_production[1]) / 2;
   414 		} else if (i->produced_cargo[0] == 0xFF && j->produced_cargo[0] == 0xFF) //None of them producing anything, let them go to the name-sorting
   412 		} else if (i->produced_cargo[0] == 0xFF && j->produced_cargo[0] == 0xFF) // none of them producing anything, let them go to the name-sorting
   415 			r = 0;
   413 			r = 0;
   416 		else if (i->produced_cargo[0] == 0xFF) //Non-producers, end up last/first in list
   414 		else if (i->produced_cargo[0] == 0xFF) // end up the non-producer industry first/last in list
   417 			r = 1;
   415 			r = 1;
   418 		else
   416 		else
   419 			r = -1;
   417 			r = -1;
   420 		break;
   418 		break;
   421 	case 3: /* Case 3, sort by transportation */
   419 	}
   422 		if (i->produced_cargo[0] != 0xFF && j->produced_cargo[0] != 0xFF) { //producing any cargo?
   420 	case 3: /* Sort by Transported amount */
   423 				if (i->produced_cargo[1] == 0xFF) //producing one or two things?
   421 		if (i->produced_cargo[0] != 0xFF && j->produced_cargo[0] != 0xFF) { // both industries produce cargo?
       
   422 				if (i->produced_cargo[1] == 0xFF) // producing one or two things?
   424 					r = (j->pct_transported[0] * 100 >> 8) - (i->pct_transported[0] * 100 >> 8);
   423 					r = (j->pct_transported[0] * 100 >> 8) - (i->pct_transported[0] * 100 >> 8);
   425 				else 
   424 				else 
   426 					r = ((j->pct_transported[0] * 100 >> 8) + (j->pct_transported[1] * 100 >> 8)) / 2 - ((i->pct_transported[0] * 100 >> 8) + (i->pct_transported[1] * 100 >> 8)) / 2;
   425 					r = ((j->pct_transported[0] * 100 >> 8) + (j->pct_transported[1] * 100 >> 8)) / 2 - ((i->pct_transported[0] * 100 >> 8) + (i->pct_transported[1] * 100 >> 8)) / 2;
   427 		} else if (i->produced_cargo[0] == 0xFF && j->produced_cargo[0] == 0xFF) //None of them producing anything, let them go to the name-sorting
   426 		} else if (i->produced_cargo[0] == 0xFF && j->produced_cargo[0] == 0xFF) // none of them producing anything, let them go to the name-sorting
   428 			r = 0;
   427 			r = 0;
   429 		else if (i->produced_cargo[0] == 0xFF) //Non-producers, end up last/first in list
   428 		else if (i->produced_cargo[0] == 0xFF) // end up the non-producer industry first/last in list
   430 			r = 1;
   429 			r = 1;
   431 		else
   430 		else
   432 			r = -1;
   431 			r = -1;
   433 		break;
   432 		break;
   434 	}
   433 	}
   444 			GetString(_bufcache, j->town->townnametype);
   443 			GetString(_bufcache, j->town->townnametype);
   445 		}
   444 		}
   446 		r = strcmp(buf1, _bufcache);
   445 		r = strcmp(buf1, _bufcache);
   447 	}
   446 	}
   448 	
   447 	
   449 	if (_industry_sort_order & 1)
   448 	if (_industry_sort_order & 1) r = -r;	
   450 		r = -r;
       
   451 	
       
   452 	return r;
   449 	return r;
   453 }
   450 }
   454 
   451 
   455 static void MakeSortedIndustryList()
   452 static void MakeSortedIndustryList()
   456 {
   453 {
   462 		index++;
   459 		index++;
   463 	}
   460 	}
   464 	_num_industry_sort = n;
   461 	_num_industry_sort = n;
   465 	_last_industry_idx = 255; // used for "cache"
   462 	_last_industry_idx = 255; // used for "cache"
   466 
   463 
   467 	qsort(_industry_sort, n, 1, IndustrySorter);
   464 	qsort(_industry_sort, n, 1, GeneralIndustrySorter);
   468 
   465 
   469 	DEBUG(misc, 1) ("Resorting Industries list...");
   466 	DEBUG(misc, 1) ("Resorting Industries list...");
   470 }
   467 }
   471 
   468 
   472 
   469