From: TimePath Date: Thu, 5 Nov 2015 08:42:39 +0000 (+1100) Subject: Cleanup X-Git-Tag: xonotic-v0.8.2~1711 X-Git-Url: https://git.xonotic.org/?a=commitdiff_plain;h=89259e3a8b5e3802b5ab08b6f75e7a8a8399f7cf;p=xonotic%2Fxonotic-data.pk3dir.git Cleanup --- diff --git a/qcsrc/lib/bits.qh b/qcsrc/lib/bits.qh index 33d719a07..001984a63 100644 --- a/qcsrc/lib/bits.qh +++ b/qcsrc/lib/bits.qh @@ -1,5 +1,6 @@ #ifndef BITS_H #define BITS_H +#include "log.qh" #define BIT(n) (1 << (n)) #define BITS(n) (BIT(n) - 1) @@ -19,4 +20,106 @@ int lowestbit(int f) return f; } +int randombit(int bits) +{ + if (!(bits & (bits - 1))) // this ONLY holds for powers of two! + return bits; + + int r = random(); + int b = 0; + int n = 0; + + for (int f = 1; f <= bits; f *= 2) + { + if (bits & f) + { + ++n; + r *= n; + if (r <= 1) b = f; + else r = (r - 1) / (n - 1); + } + } + return b; +} + +int randombits(int bits, int k, bool error_return) +{ + int r = 0; + while (k > 0 && bits != r) + { + r += randombit(bits - r); + --k; + } + if (error_return) + if (k > 0) return -1; + // all + return r; +} + +void randombit_test(int bits, int iter) +{ + while (iter > 0) + { + LOG_INFO(ftos(randombit(bits)), "\n"); + --iter; + } +} + +enum { + OP_SET, + OP_MIN, + OP_MAX, + OP_PLUS, + OP_MINUS +}; + +bool GiveBit(entity e, .int fld, int bit, int op, int val) +{ + int v0 = (e.(fld) & bit); + switch (op) + { + case OP_SET: + if (val > 0) e.(fld) |= bit; + else e.(fld) &= ~bit; + break; + case OP_MIN: + case OP_PLUS: + if (val > 0) e.(fld) |= bit; + break; + case OP_MAX: + if (val <= 0) e.(fld) &= ~bit; + break; + case OP_MINUS: + if (val > 0) e.(fld) &= ~bit; + break; + } + int v1 = (e.(fld) & bit); + return v0 != v1; +} + +bool GiveValue(entity e, .int fld, int op, int val) +{ + int v0 = e.(fld); + switch (op) + { + case OP_SET: + e.(fld) = val; + break; + case OP_MIN: + e.(fld) = max(e.(fld), val); // min 100 cells = at least 100 cells + break; + case OP_MAX: + e.(fld) = min(e.(fld), val); + break; + case OP_PLUS: + e.(fld) += val; + break; + case OP_MINUS: + e.(fld) -= val; + break; + } + int v1 = e.(fld); + return v0 != v1; +} + #endif diff --git a/qcsrc/lib/math.qh b/qcsrc/lib/math.qh index 24d004166..ea968e84c 100644 --- a/qcsrc/lib/math.qh +++ b/qcsrc/lib/math.qh @@ -184,6 +184,13 @@ float almost_in_bounds(float a, float b, float c) return b == median(a - eps, b, c + eps); } +float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d) +{ + if (halflifedist > 0) return pow(0.5, (bound(mindist, d, maxdist) - mindist) / halflifedist); + else if (halflifedist < 0) return pow(0.5, (bound(mindist, d, maxdist) - maxdist) / halflifedist); + else return 1; +} + float power2of(float e) { return pow(2, e); diff --git a/qcsrc/lib/random.qc b/qcsrc/lib/random.qc index 6ae0f784d..be0f80a08 100644 --- a/qcsrc/lib/random.qc +++ b/qcsrc/lib/random.qc @@ -31,6 +31,40 @@ void RandomSelection_Add(entity e, float f, string s, float weight, float priori } } +float DistributeEvenly_amount; +float DistributeEvenly_totalweight; + +void DistributeEvenly_Init(float amount, float totalweight) +{ + if (DistributeEvenly_amount) + { + LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for "); + LOG_TRACE(ftos(DistributeEvenly_totalweight), " left!)\n"); + } + if (totalweight == 0) DistributeEvenly_amount = 0; + else DistributeEvenly_amount = amount; + DistributeEvenly_totalweight = totalweight; +} + +float DistributeEvenly_Get(float weight) +{ + float f; + if (weight <= 0) return 0; + f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); + DistributeEvenly_totalweight -= weight; + DistributeEvenly_amount -= f; + return f; +} + +float DistributeEvenly_GetRandomized(float weight) +{ + float f; + if (weight <= 0) return 0; + f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); + DistributeEvenly_totalweight -= weight; + DistributeEvenly_amount -= f; + return f; +} // prandom - PREDICTABLE random number generator (not seeded yet) diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index 2bf9ac1f7..048b5c721 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -184,4 +184,29 @@ int u8_strsize(string s) return l; } +bool isInvisibleString(string s) +{ + s = strdecolorize(s); + bool utf8 = cvar("utf8_enable"); + for (int i = 0, n = strlen(s); i < n; ++i) + { + int c = str2chr(s, i); + switch (c) + { + case 0: + case 32: // space + break; + case 192: // charmap space + if (!utf8) break; + return false; + case 160: // space in unicode fonts + case 0xE000 + 192: // utf8 charmap space + if (utf8) break; + default: + return false; + } + } + return true; +} + #endif diff --git a/qcsrc/server/cl_client.qc b/qcsrc/server/cl_client.qc index 6cf716107..a604055b8 100644 --- a/qcsrc/server/cl_client.qc +++ b/qcsrc/server/cl_client.qc @@ -1635,19 +1635,20 @@ void SetZoomState(float z) } void GetPressedKeys() -{SELFPARAM(); +{ + SELFPARAM(); MUTATOR_CALLHOOK(GetPressedKeys); - #define X(var,bit,flag) (flag ? var |= bit : var &= ~bit) - X(self.pressedkeys, KEY_FORWARD, self.movement_x > 0); - X(self.pressedkeys, KEY_BACKWARD, self.movement_x < 0); - X(self.pressedkeys, KEY_RIGHT, self.movement_y > 0); - X(self.pressedkeys, KEY_LEFT, self.movement_y < 0); - - X(self.pressedkeys, KEY_JUMP, PHYS_INPUT_BUTTON_JUMP(self)); - X(self.pressedkeys, KEY_CROUCH, PHYS_INPUT_BUTTON_CROUCH(self)); - X(self.pressedkeys, KEY_ATCK, PHYS_INPUT_BUTTON_ATCK(self)); - X(self.pressedkeys, KEY_ATCK2, PHYS_INPUT_BUTTON_ATCK2(self)); - #undef X + int keys = this.pressedkeys; + keys = BITSET(keys, KEY_FORWARD, this.movement.x > 0); + keys = BITSET(keys, KEY_BACKWARD, this.movement.x < 0); + keys = BITSET(keys, KEY_RIGHT, this.movement.y > 0); + keys = BITSET(keys, KEY_LEFT, this.movement.y < 0); + + keys = BITSET(keys, KEY_JUMP, PHYS_INPUT_BUTTON_JUMP(this)); + keys = BITSET(keys, KEY_CROUCH, PHYS_INPUT_BUTTON_CROUCH(this)); + keys = BITSET(keys, KEY_ATCK, PHYS_INPUT_BUTTON_ATCK(this)); + keys = BITSET(keys, KEY_ATCK2, PHYS_INPUT_BUTTON_ATCK2(this)); + this.pressedkeys = keys; } /* @@ -2138,32 +2139,6 @@ void PlayerUseKey() MUTATOR_CALLHOOK(PlayerUseKey); } -float isInvisibleString(string s) -{ - float i, n, c; - s = strdecolorize(s); - for((i = 0), (n = strlen(s)); i < n; ++i) - { - c = str2chr(s, i); - switch(c) - { - case 0: - case 32: // space - break; - case 192: // charmap space - if (!autocvar_utf8_enable) - break; - return false; - case 160: // space in unicode fonts - case 0xE000 + 192: // utf8 charmap space - if (autocvar_utf8_enable) - break; - default: - return false; - } - } - return true; -} /* ============= diff --git a/qcsrc/server/miscfunctions.qc b/qcsrc/server/miscfunctions.qc index de7559222..4df487c0e 100644 --- a/qcsrc/server/miscfunctions.qc +++ b/qcsrc/server/miscfunctions.qc @@ -60,40 +60,6 @@ string admin_name() return "SERVER ADMIN"; } -void DistributeEvenly_Init(float amount, float totalweight) -{ - if (DistributeEvenly_amount) - { - LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for "); - LOG_TRACE(ftos(DistributeEvenly_totalweight), " left!)\n"); - } - if (totalweight == 0) - DistributeEvenly_amount = 0; - else - DistributeEvenly_amount = amount; - DistributeEvenly_totalweight = totalweight; -} -float DistributeEvenly_Get(float weight) -{ - float f; - if (weight <= 0) - return 0; - f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); - DistributeEvenly_totalweight -= weight; - DistributeEvenly_amount -= f; - return f; -} -float DistributeEvenly_GetRandomized(float weight) -{ - float f; - if (weight <= 0) - return 0; - f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); - DistributeEvenly_totalweight -= weight; - DistributeEvenly_amount -= f; - return f; -} - void GameLogEcho(string s) { @@ -1678,68 +1644,6 @@ void shockwave_spawn(string m, vector org, float sz, float t1, float t2) return modeleffect_spawn(m, 0, 0, org, '0 0 0', '0 0 0', '0 0 0', 0, sz, 1, t1, t2); } -float randombit(float bits) -{ - if(!(bits & (bits-1))) // this ONLY holds for powers of two! - return bits; - - float n, f, b, r; - - r = random(); - b = 0; - n = 0; - - for(f = 1; f <= bits; f *= 2) - { - if(bits & f) - { - ++n; - r *= n; - if(r <= 1) - b = f; - else - r = (r - 1) / (n - 1); - } - } - - return b; -} - -float randombits(float bits, float k, float error_return) -{ - float r; - r = 0; - while(k > 0 && bits != r) - { - r += randombit(bits - r); - --k; - } - if(error_return) - if(k > 0) - return -1; // all - return r; -} - -void randombit_test(float bits, float iter) -{ - while(iter > 0) - { - LOG_INFO(ftos(randombit(bits)), "\n"); - --iter; - } -} - -float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d) -{ - if(halflifedist > 0) - return pow(0.5, (bound(mindist, d, maxdist) - mindist) / halflifedist); - else if(halflifedist < 0) - return pow(0.5, (bound(mindist, d, maxdist) - maxdist) / halflifedist); - else - return 1; -} - - .string aiment_classname; .float aiment_deadflag; void SetMovetypeFollow(entity ent, entity e) diff --git a/qcsrc/server/miscfunctions.qh b/qcsrc/server/miscfunctions.qh index c857c2dc3..bb19cafff 100644 --- a/qcsrc/server/miscfunctions.qh +++ b/qcsrc/server/miscfunctions.qh @@ -39,9 +39,6 @@ void write_recordmarker(entity pl, float tstart, float dt); void play2all(string samp); -void DistributeEvenly_Init(float amount, float totalweight); -float DistributeEvenly_Get(float weight); - void modeleffect_spawn(string m, float s, float f, vector o, vector v, vector ang, vector angv, float s0, float s2, float a, float t1, float t2); void shockwave_spawn(string m, vector org, float sz, float t1, float t2); @@ -62,10 +59,7 @@ void InitializeEntitiesRun(); void stopsoundto(float _dest, entity e, float chan); void soundtoat(float _dest, entity e, vector o, float chan, string samp, float vol, float _atten); -float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d); -float DistributeEvenly_amount; -float DistributeEvenly_totalweight; void objerror(string s); void droptofloor(); void() SUB_Remove; diff --git a/qcsrc/server/t_items.qc b/qcsrc/server/t_items.qc index cd06f2da8..5d2509102 100644 --- a/qcsrc/server/t_items.qc +++ b/qcsrc/server/t_items.qc @@ -1578,62 +1578,6 @@ float GiveWeapon(entity e, float wpn, float op, float val) return (v0 != v1); } -float GiveBit(entity e, .float fld, float bit, float op, float val) -{ - float v0, v1; - v0 = (e.(fld) & bit); - switch(op) - { - case OP_SET: - if(val > 0) - e.(fld) |= bit; - else - e.(fld) &= ~bit; - break; - case OP_MIN: - case OP_PLUS: - if(val > 0) - e.(fld) |= bit; - break; - case OP_MAX: - if(val <= 0) - e.(fld) &= ~bit; - break; - case OP_MINUS: - if(val > 0) - e.(fld) &= ~bit; - break; - } - v1 = (e.(fld) & bit); - return (v0 != v1); -} - -float GiveValue(entity e, .float fld, float op, float val) -{ - float v0, v1; - v0 = e.(fld); - switch(op) - { - case OP_SET: - e.(fld) = val; - break; - case OP_MIN: - e.(fld) = max(e.(fld), val); // min 100 cells = at least 100 cells - break; - case OP_MAX: - e.(fld) = min(e.(fld), val); - break; - case OP_PLUS: - e.(fld) += val; - break; - case OP_MINUS: - e.(fld) -= val; - break; - } - v1 = e.(fld); - return (v0 != v1); -} - void GiveSound(entity e, float v0, float v1, float t, string snd_incr, string snd_decr) { if(v1 == v0) diff --git a/qcsrc/server/t_items.qh b/qcsrc/server/t_items.qh index 35a176800..718944952 100644 --- a/qcsrc/server/t_items.qh +++ b/qcsrc/server/t_items.qh @@ -115,12 +115,6 @@ void _StartItem(entity this, entity def, float defaultrespawntime, float default void target_items_use (); -const float OP_SET = 0; -const float OP_MIN = 1; -const float OP_MAX = 2; -const float OP_PLUS = 3; -const float OP_MINUS = 4; - float GiveWeapon(entity e, float wpn, float op, float val); float GiveBit(entity e, .float fld, float bit, float op, float val);