#ifndef BITS_H
#define BITS_H
+#include "log.qh"
#define BIT(n) (1 << (n))
#define BITS(n) (BIT(n) - 1)
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
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);
}
}
+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)
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
}
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;
}
/*
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;
-}
/*
=============
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)
{
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)
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);
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;
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)
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);