350 { WIDGETS_END}, |
350 { WIDGETS_END}, |
351 }; |
351 }; |
352 |
352 |
353 |
353 |
354 // used to get a sorted list of the towns |
354 // used to get a sorted list of the towns |
355 static byte _town_sort[lengthof(_towns)]; |
355 static uint16 _town_sort[lengthof(_towns)]; |
356 static uint _num_town_sort; |
356 static uint _num_town_sort; |
357 |
357 |
358 static char _bufcache[64]; |
358 static char _bufcache[64]; |
359 static byte _last_town_idx; |
359 static uint16 _last_town_idx; |
360 |
360 |
361 static int CDECL TownNameSorter(const void *a, const void *b) |
361 static int CDECL TownNameSorter(const void *a, const void *b) |
362 { |
362 { |
363 char buf1[64]; |
363 char buf1[64]; |
364 const Town *t; |
364 const Town *t; |
365 byte val; |
365 byte val; |
366 int r; |
366 int r; |
367 |
367 |
368 t = DEREF_TOWN(*(const byte*)a); |
368 t = DEREF_TOWN(*(const uint16*)a); |
369 SetDParam(0, t->townnameparts); |
369 SetDParam(0, t->townnameparts); |
370 GetString(buf1, t->townnametype); |
370 GetString(buf1, t->townnametype); |
371 |
371 |
372 if ( (val=*(const byte*)b) != _last_town_idx) { |
372 if ( (val=*(const uint16*)b) != _last_town_idx) { |
373 _last_town_idx = val; |
373 _last_town_idx = val; |
374 t = DEREF_TOWN(val); |
374 t = DEREF_TOWN(val); |
375 SetDParam(0, t->townnameparts); |
375 SetDParam(0, t->townnameparts); |
376 GetString(_bufcache, t->townnametype); |
376 GetString(_bufcache, t->townnametype); |
377 } |
377 } |
381 return r; |
381 return r; |
382 } |
382 } |
383 |
383 |
384 static int CDECL TownPopSorter(const void *a, const void *b) |
384 static int CDECL TownPopSorter(const void *a, const void *b) |
385 { |
385 { |
386 const Town *ta = DEREF_TOWN(*(const byte*)a); |
386 const Town *ta = DEREF_TOWN(*(const uint16*)a); |
387 const Town *tb = DEREF_TOWN(*(const byte*)b); |
387 const Town *tb = DEREF_TOWN(*(const uint16*)b); |
388 int r = ta->population - tb->population; |
388 int r = ta->population - tb->population; |
389 if (_town_sort_order & 1) r = -r; |
389 if (_town_sort_order & 1) r = -r; |
390 return r; |
390 return r; |
391 } |
391 } |
392 |
392 |
393 static void MakeSortedTownList() |
393 static void MakeSortedTownList() |
394 { |
394 { |
395 Town *t; |
395 Town *t; |
396 int n = 0; |
396 int n = 0; |
397 FOR_ALL_TOWNS(t) if(t->xy) _town_sort[n++] = t->index; |
397 FOR_ALL_TOWNS(t) |
|
398 if(t->xy) |
|
399 _town_sort[n++] = t->index; |
|
400 |
398 _num_town_sort = n; |
401 _num_town_sort = n; |
399 |
402 |
400 _last_town_idx = 0; // used for "cache" |
403 _last_town_idx = 0; // used for "cache" |
401 qsort(_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter); |
404 qsort(_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter); |
402 |
405 |