From 697197bd5b8f954bc47de44ebd4498cc9d30efcf Mon Sep 17 00:00:00 2001 From: ac-stef Date: Sat, 3 Oct 2015 18:18:59 +0200 Subject: [PATCH] generalise sorting of lists of integer sets --- source/src/server.cpp | 10 ++++------ source/src/tools.cpp | 3 +++ source/src/tools.h | 5 +++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/source/src/server.cpp b/source/src/server.cpp index fb19fb05..e63df3d4 100644 --- a/source/src/server.cpp +++ b/source/src/server.cpp @@ -1148,9 +1148,7 @@ void htf_forceflag(int flag) int arenaround = 0, arenaroundstartmillis = 0; -struct twoint { int index, value; }; int cmpscore(const int *a, const int *b) { return clients[*a]->at3_score - clients[*b]->at3_score; } -int cmptwoint(const struct twoint *a, const struct twoint *b) { return a->value - b->value; } vector tdistrib; vector sdistrib; @@ -1172,15 +1170,15 @@ void distributeteam(int team) sdistrib.shrink(0); loopi(numsp) { - ti.index = i; - ti.value = rnd(0x1000000); + ti.val = i; + ti.key = rnd(0x1000000); sdistrib.add(ti); } - sdistrib.sort(cmptwoint); // random spawn order + sdistrib.sort(cmpintasc); // random spawn order int x = 0; loopv(tdistrib) { - clients[tdistrib[i]]->spawnindex = sdistrib[x++].index; + clients[tdistrib[i]]->spawnindex = sdistrib[x++].val; x %= sdistrib.length(); } } diff --git a/source/src/tools.cpp b/source/src/tools.cpp index 76a73b96..34ccf7e8 100644 --- a/source/src/tools.cpp +++ b/source/src/tools.cpp @@ -63,6 +63,9 @@ int fixmapheadersize(int version, int headersize) // we can't trust hdr.header mapdim mapdims; // min/max X/Y and delta X/Y and min/max Z +int cmpintasc(const int *a, const int *b) { return *a - *b; } // leads to ascending sort order +int cmpintdesc(const int *a, const int *b) { return *b - *a; } // leads to descending sort order + extern char *maplayout, *testlayout; extern int maplayout_factor, testlayout_factor, Mvolume, Marea, Mopen, SHhits; extern float Mheight; diff --git a/source/src/tools.h b/source/src/tools.h index 75b965ce..f0cdccc6 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -994,6 +994,11 @@ extern const char *hiddenpwd(const char *pwd, int showchars = 0); extern int getlistindex(const char *key, const char *list[], bool acceptnumeric = true, int deflt = -1); extern void parseupdatelist(hashtable &ht, char *buf, const char *prefix = NULL, const char *suffix = NULL); +struct twoint { int key, val; }; +struct threeint { int key, val1, val2; }; +int cmpintasc(const int *a, const int *b); // leads to ascending sort order +int cmpintdesc(const int *a, const int *b); // leads to descending sort order + #if defined(WIN32) && !defined(_DEBUG) && !defined(__GNUC__) extern void stackdumper(unsigned int type, EXCEPTION_POINTERS *ep); #endif