src/industry_gui.cpp
changeset 10701 39b1f26c238c
parent 10641 13148f508e6d
child 10703 2c998f3776d1
equal deleted inserted replaced
10700:b3a94898f55c 10701:39b1f26c238c
   719 
   719 
   720 static char _bufcache[96];
   720 static char _bufcache[96];
   721 static const Industry* _last_industry;
   721 static const Industry* _last_industry;
   722 static int _internal_sort_order;
   722 static int _internal_sort_order;
   723 
   723 
       
   724 /** Returns percents of cargo transported if industry produces this cargo, else -1
       
   725  * @param i industry to check
       
   726  * @param id cargo slot
       
   727  * @return percents of cargo transported, or -1 if industry doesn't use this cargo slot
       
   728  */
       
   729 static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
       
   730 {
       
   731 	assert(id < lengthof(i->produced_cargo));
       
   732 
       
   733 	if (i->produced_cargo[id] == CT_INVALID) return 101;
       
   734 	return i->last_month_pct_transported[id] * 100 >> 8;
       
   735 }
       
   736 
       
   737 /** Returns value representing industry's transported cargo
       
   738  * percentage for industry sorting
       
   739  * @param i industry to check
       
   740  * @return value used for sorting
       
   741  */
       
   742 static int GetCargoTransportedSortValue(const Industry *i)
       
   743 {
       
   744 	int p1 = GetCargoTransportedPercentsIfValid(i, 0);
       
   745 	int p2 = GetCargoTransportedPercentsIfValid(i, 1);
       
   746 
       
   747 	if (p1 > p2) Swap(p1, p2); // lower value has higher priority
       
   748 
       
   749 	return (p1 << 8) + p2;
       
   750 }
       
   751 
   724 static int CDECL GeneralIndustrySorter(const void *a, const void *b)
   752 static int CDECL GeneralIndustrySorter(const void *a, const void *b)
   725 {
   753 {
   726 	const Industry* i = *(const Industry**)a;
   754 	const Industry* i = *(const Industry**)a;
   727 	const Industry* j = *(const Industry**)b;
   755 	const Industry* j = *(const Industry**)b;
   728 	int r;
   756 	int r;
   750 				}
   778 				}
   751 			}
   779 			}
   752 			break;
   780 			break;
   753 
   781 
   754 		case 3: /* Sort by transported fraction */
   782 		case 3: /* Sort by transported fraction */
   755 			if (i->produced_cargo[0] == CT_INVALID) {
   783 			r = GetCargoTransportedSortValue(i) - GetCargoTransportedSortValue(j);
   756 				r = (j->produced_cargo[0] == CT_INVALID ? 0 : -1);
       
   757 			} else {
       
   758 				if (j->produced_cargo[0] == CT_INVALID) {
       
   759 					r = 1;
       
   760 				} else {
       
   761 					int pi;
       
   762 					int pj;
       
   763 
       
   764 					pi = i->last_month_pct_transported[0] * 100 >> 8;
       
   765 					if (i->produced_cargo[1] != CT_INVALID) {
       
   766 						int p = i->last_month_pct_transported[1] * 100 >> 8;
       
   767 						if (p < pi) pi = p;
       
   768 					}
       
   769 
       
   770 					pj = j->last_month_pct_transported[0] * 100 >> 8;
       
   771 					if (j->produced_cargo[1] != CT_INVALID) {
       
   772 						int p = j->last_month_pct_transported[1] * 100 >> 8;
       
   773 						if (p < pj) pj = p;
       
   774 					}
       
   775 
       
   776 					r = pi - pj;
       
   777 				}
       
   778 			}
       
   779 			break;
   784 			break;
   780 	}
   785 	}
   781 
   786 
   782 	/* default to string sorting if they are otherwise equal */
   787 	/* default to string sorting if they are otherwise equal */
   783 	if (r == 0) {
   788 	if (r == 0) {