]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/miscfunctions.qc
Merge remote-tracking branch 'origin/master' into samual/notification_rewrite
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / miscfunctions.qc
index 7065542b1902db6b65b7faa35612b55d854aa5eb..e2d6bd0af76c5d5500c3838dec8b00f5a098bcec 100644 (file)
@@ -32,7 +32,6 @@ void WarpZone_crosshair_trace(entity pl)
 void() spawnfunc_info_player_deathmatch; // needed for the other spawnpoints
 void() spawnpoint_use;
 string GetMapname();
-string ColoredTeamName(float t);
 
 string admin_name(void)
 {
@@ -67,6 +66,16 @@ float DistributeEvenly_Get(float 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;
+}
 
 #define move_out_of_solid(e) WarpZoneLib_MoveOutOfSolid(e)
 
@@ -85,9 +94,12 @@ string STR_OBSERVER = "observer";
 #define FOR_EACH_CLIENT(v) FOR_EACH_CLIENTSLOT(v) if(v.flags & FL_CLIENT)
 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
 #define FOR_EACH_PLAYER(v) FOR_EACH_CLIENT(v) if(v.classname == STR_PLAYER)
+#define FOR_EACH_SPEC(v) FOR_EACH_CLIENT(v) if(v.classname != STR_PLAYER)
 #define FOR_EACH_REALPLAYER(v) FOR_EACH_REALCLIENT(v) if(v.classname == STR_PLAYER)
 #endif
 
+#define CENTER_OR_VIEWOFS(ent) (ent.origin + ((ent.classname == STR_PLAYER) ? ent.view_ofs : ((ent.mins + ent.maxs) * 0.5)))
+
 // copies a string to a tempstring (so one can strunzone it)
 string strcat1(string s) = #115; // FRIK_FILE
 
@@ -526,7 +538,7 @@ string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(string wo)
 }
 void GetCvars(float f)
 {
-       string s;
+       string s = string_null;
 
        if (f > 0)
                s = strcat1(argv(f));
@@ -598,85 +610,6 @@ void backtrace(string msg)
     cvar_set("prvm_backtraceforwarnings", ftos(war));
 }
 
-string Team_ColorCode(float teamid)
-{
-    if (teamid == COLOR_TEAM1)
-        return "^1";
-    else if (teamid == COLOR_TEAM2)
-        return "^4";
-    else if (teamid == COLOR_TEAM3)
-        return "^3";
-    else if (teamid == COLOR_TEAM4)
-        return "^6";
-    else
-        return "^7";
-}
-
-string Team_ColorName(float t)
-{
-    // fixme: Search for team entities and get their .netname's!
-    if (t == COLOR_TEAM1)
-        return "Red";
-    if (t == COLOR_TEAM2)
-        return "Blue";
-    if (t == COLOR_TEAM3)
-        return "Yellow";
-    if (t == COLOR_TEAM4)
-        return "Pink";
-    return "Neutral";
-}
-
-string Team_ColorNameLowerCase(float t)
-{
-    // fixme: Search for team entities and get their .netname's!
-    if (t == COLOR_TEAM1)
-        return "red";
-    if (t == COLOR_TEAM2)
-        return "blue";
-    if (t == COLOR_TEAM3)
-        return "yellow";
-    if (t == COLOR_TEAM4)
-        return "pink";
-    return "neutral";
-}
-
-float ColourToNumber(string team_colour)
-{
-       if (team_colour == "red")
-               return COLOR_TEAM1;
-
-       if (team_colour == "blue")
-               return COLOR_TEAM2;
-
-       if (team_colour == "yellow")
-               return COLOR_TEAM3;
-
-       if (team_colour == "pink")
-               return COLOR_TEAM4;
-
-       if (team_colour == "auto")
-               return 0;
-
-       return -1;
-}
-
-float NumberToTeamNumber(float number)
-{
-       if (number == 1)
-               return COLOR_TEAM1;
-
-       if (number == 2)
-               return COLOR_TEAM2;
-
-       if (number == 3)
-               return COLOR_TEAM3;
-
-       if (number == 4)
-               return COLOR_TEAM4;
-
-       return -1;
-}
-
 // decolorizes and team colors the player name when needed
 string playername(entity p)
 {
@@ -740,6 +673,7 @@ float g_pickup_healthmega_anyway;
 float g_pickup_ammo_anyway;
 float g_pickup_weapons_anyway;
 float g_weaponarena;
+WEPSET_DECLARE_A(g_weaponarena_weapons);
 float g_weaponarena_random;
 float g_weaponarena_random_with_laser;
 string g_weaponarena_list;
@@ -749,7 +683,9 @@ float g_weapondamagefactor;
 float g_weaponforcefactor;
 float g_weaponspreadfactor;
 
-float start_weapons;
+WEPSET_DECLARE_A(start_weapons);
+WEPSET_DECLARE_A(start_weapons_default);
+WEPSET_DECLARE_A(start_weapons_defaultmask);
 float start_items;
 float start_ammo_shells;
 float start_ammo_nails;
@@ -758,7 +694,9 @@ float start_ammo_cells;
 float start_ammo_fuel;
 float start_health;
 float start_armorvalue;
-float warmup_start_weapons;
+WEPSET_DECLARE_A(warmup_start_weapons);
+WEPSET_DECLARE_A(warmup_start_weapons_default);
+WEPSET_DECLARE_A(warmup_start_weapons_defaultmask);
 float warmup_start_ammo_shells;
 float warmup_start_ammo_nails;
 float warmup_start_ammo_rockets;
@@ -767,34 +705,48 @@ float warmup_start_ammo_fuel;
 float warmup_start_health;
 float warmup_start_armorvalue;
 float g_weapon_stay;
-float g_ghost_items;
 
 entity get_weaponinfo(float w);
 
 float want_weapon(string cvarprefix, entity weaponinfo, float allguns)
 {
        var float i = weaponinfo.weapon;
+       var float d = 0;
 
        if (!i)
                return 0;
 
-       var float t = cvar(strcat(cvarprefix, weaponinfo.netname));
-
-       if (t < 0) // "default" weapon selection
+       if (g_lms || g_ca || allguns)
        {
-               if (g_lms || g_ca || allguns)
-                       t = (weaponinfo.spawnflags & WEP_FLAG_NORMAL);
-               else if(t < -1)
-                       t = 0;
-               else if (g_cts)
-                       t = (i == WEP_SHOTGUN);
-               else if (g_nexball)
-                       t = 0; // weapon is set a few lines later
+               if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
+                       d = TRUE;
                else
-                       t = (i == WEP_LASER || i == WEP_SHOTGUN);
-               if(g_grappling_hook) // if possible, redirect off-hand hook to on-hand hook
-                       t |= (i == WEP_HOOK);
+                       d = FALSE;
        }
+       else if (g_cts)
+               d = (i == WEP_SHOTGUN);
+       else if (g_nexball)
+               d = 0; // weapon is set a few lines later
+       else
+               d = (i == WEP_LASER || i == WEP_SHOTGUN);
+               
+       if(g_grappling_hook) // if possible, redirect off-hand hook to on-hand hook
+               d |= (i == WEP_HOOK);
+       if(weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED) // never default mutator blocked guns
+               d = 0;
+
+       var float t = cvar(strcat(cvarprefix, weaponinfo.netname));
+       
+       //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
+       
+       // bit order in t:
+       // 1: want or not
+       // 2: is default?
+       // 4: is set by default?
+       if(t < 0)
+               t = 4 | (3 * d);
+       else
+               t |= (2 * d);
 
        return t;
 }
@@ -806,7 +758,9 @@ void readplayerstartcvars()
        string s;
 
        // initialize starting values for players
-       start_weapons = 0;
+       WEPSET_CLEAR_A(start_weapons);
+       WEPSET_CLEAR_A(start_weapons_default);
+       WEPSET_CLEAR_A(start_weapons_defaultmask);
        start_items = 0;
        start_ammo_shells = 0;
        start_ammo_nails = 0;
@@ -816,6 +770,8 @@ void readplayerstartcvars()
        start_armorvalue = cvar("g_balance_armor_start");
 
        g_weaponarena = 0;
+       WEPSET_CLEAR_A(g_weaponarena_weapons);
+
        s = cvar_string("g_weaponarena");
        if (s == "0" || s == "")
        {
@@ -833,34 +789,35 @@ void readplayerstartcvars()
        }
        else if (s == "all")
        {
+               g_weaponarena = 1;
                g_weaponarena_list = "All Weapons";
                for (j = WEP_FIRST; j <= WEP_LAST; ++j)
                {
                        e = get_weaponinfo(j);
-                       g_weaponarena |= e.weapons;
-                       weapon_action(e.weapon, WR_PRECACHE);
+                       if not(e.spawnflags & WEP_FLAG_MUTATORBLOCKED)
+                               WEPSET_OR_AW(g_weaponarena_weapons, j);
                }
        }
        else if (s == "most")
        {
+               g_weaponarena = 1;
                g_weaponarena_list = "Most Weapons";
                for (j = WEP_FIRST; j <= WEP_LAST; ++j)
                {
                        e = get_weaponinfo(j);
-                       if (e.spawnflags & WEP_FLAG_NORMAL)
-                       {
-                               g_weaponarena |= e.weapons;
-                               weapon_action(e.weapon, WR_PRECACHE);
-                       }
+                       if not(e.spawnflags & WEP_FLAG_MUTATORBLOCKED)
+                               if (e.spawnflags & WEP_FLAG_NORMAL)
+                                       WEPSET_OR_AW(g_weaponarena_weapons, j);
                }
        }
        else if (s == "none")
        {
+               g_weaponarena = 1;
                g_weaponarena_list = "No Weapons";
-               g_weaponarena = WEPBIT_ALL + 1; // this supports no single weapon bit!
        }
        else
        {
+               g_weaponarena = 1;
                t = tokenize_console(s);
                g_weaponarena_list = "";
                for (i = 0; i < t; ++i)
@@ -871,8 +828,7 @@ void readplayerstartcvars()
                                e = get_weaponinfo(j);
                                if (e.netname == s)
                                {
-                                       g_weaponarena |= e.weapons;
-                                       weapon_action(e.weapon, WR_PRECACHE);
+                                       WEPSET_OR_AW(g_weaponarena_weapons, j);
                                        g_weaponarena_list = strcat(g_weaponarena_list, e.message, " & ");
                                        break;
                                }
@@ -896,7 +852,7 @@ void readplayerstartcvars()
                g_minstagib = 0; // incompatible
                g_pinata = 0; // incompatible
                g_weapon_stay = 0; // incompatible
-               start_weapons = g_weaponarena;
+               WEPSET_COPY_AA(start_weapons, g_weaponarena_weapons);
                if(!(g_lms || g_ca))
                        start_items |= IT_UNLIMITED_AMMO;
        }
@@ -907,8 +863,7 @@ void readplayerstartcvars()
                g_bloodloss = 0; // incompatible
                start_health = 100;
                start_armorvalue = 0;
-               start_weapons = WEPBIT_MINSTANEX;
-               weapon_action(WEP_MINSTANEX, WR_PRECACHE);
+               WEPSET_COPY_AW(start_weapons, WEP_MINSTANEX);
                g_minstagib_invis_alpha = cvar("g_minstagib_invis_alpha");
                start_items |= IT_UNLIMITED_SUPERWEAPONS;
 
@@ -920,8 +875,13 @@ void readplayerstartcvars()
                for (i = WEP_FIRST; i <= WEP_LAST; ++i)
                {
                        e = get_weaponinfo(i);
-                       if(want_weapon("g_start_weapon_", e, FALSE))
-                               start_weapons |= e.weapons;
+                       float w = want_weapon("g_start_weapon_", e, FALSE);
+                       if(w & 1)
+                               WEPSET_OR_AW(start_weapons, i);
+                       if(w & 2)
+                               WEPSET_OR_AW(start_weapons_default, i);
+                       if(w & 4)
+                               WEPSET_OR_AW(start_weapons_defaultmask, i);
                }
        }
 
@@ -979,7 +939,9 @@ void readplayerstartcvars()
                warmup_start_ammo_fuel = start_ammo_fuel;
                warmup_start_health = start_health;
                warmup_start_armorvalue = start_armorvalue;
-               warmup_start_weapons = start_weapons;
+               WEPSET_COPY_AA(warmup_start_weapons, start_weapons);
+               WEPSET_COPY_AA(warmup_start_weapons_default, start_weapons_default);
+               WEPSET_COPY_AA(warmup_start_weapons_defaultmask, start_weapons_defaultmask);
 
                if (!g_weaponarena && !g_minstagib && !g_ca)
                {
@@ -990,17 +952,29 @@ void readplayerstartcvars()
                        warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
                        warmup_start_health = cvar("g_warmup_start_health");
                        warmup_start_armorvalue = cvar("g_warmup_start_armor");
-                       warmup_start_weapons = 0;
+                       WEPSET_CLEAR_A(warmup_start_weapons);
+                       WEPSET_CLEAR_A(warmup_start_weapons_default);
+                       WEPSET_CLEAR_A(warmup_start_weapons_defaultmask);
                        for (i = WEP_FIRST; i <= WEP_LAST; ++i)
                        {
                                e = get_weaponinfo(i);
-                               if(want_weapon("g_start_weapon_", e, cvar("g_warmup_allguns")))
-                                       warmup_start_weapons |= e.weapons;
+                               float w = want_weapon("g_start_weapon_", e, cvar("g_warmup_allguns"));
+                               if(w & 1)
+                                       WEPSET_OR_AW(warmup_start_weapons, i);
+                               if(w & 2)
+                                       WEPSET_OR_AW(warmup_start_weapons_default, i);
+                               if(w & 4)
+                                       WEPSET_OR_AW(warmup_start_weapons_defaultmask, i);
                        }
                }
        }
 
-       if (g_jetpack || (g_grappling_hook && (start_weapons & WEPBIT_HOOK)))
+       if (g_jetpack)
+               start_items |= IT_JETPACK;
+
+       MUTATOR_CALLHOOK(SetStartItems);
+
+       if ((start_items & IT_JETPACK) || (g_grappling_hook && WEPSET_CONTAINS_AW(start_weapons, WEP_HOOK)))
        {
                g_grappling_hook = 0; // these two can't coexist, as they use the same button
                start_items |= IT_FUEL_REGEN;
@@ -1008,16 +982,11 @@ void readplayerstartcvars()
                warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
        }
 
-       if (g_jetpack)
-               start_items |= IT_JETPACK;
-
-       MUTATOR_CALLHOOK(SetStartItems);
-
        for (i = WEP_FIRST; i <= WEP_LAST; ++i)
        {
                e = get_weaponinfo(i);
-               if(e.weapons & (start_weapons | warmup_start_weapons))
-                       weapon_action(e.weapon, WR_PRECACHE);
+               if(WEPSET_CONTAINS_AW(start_weapons, i) || WEPSET_CONTAINS_AW(warmup_start_weapons, i))
+                       weapon_action(i, WR_PRECACHE);
        }
 
        start_ammo_shells = max(0, start_ammo_shells);
@@ -1065,19 +1034,28 @@ float sv_pitch_fixyaw;
 string GetGametype(); // g_world.qc
 void readlevelcvars(void)
 {
-       // first load all the mutators
-       if(cvar("g_invincible_projectiles"))
-               MUTATOR_ADD(mutator_invincibleprojectiles);
-       if(cvar("g_nix"))
-               MUTATOR_ADD(mutator_nix);
+       g_minstagib = cvar("g_minstagib");
+
+       // load ALL the mutators
        if(cvar("g_dodging"))
                MUTATOR_ADD(mutator_dodging);
-       if(cvar("g_rocket_flying"))
-               MUTATOR_ADD(mutator_rocketflying);
-       if(cvar("g_vampire"))
-               MUTATOR_ADD(mutator_vampire);
        if(cvar("g_spawn_near_teammate"))
                MUTATOR_ADD(mutator_spawn_near_teammate);
+       if(!g_minstagib)
+       {
+               if(cvar("g_invincible_projectiles"))
+                       MUTATOR_ADD(mutator_invincibleprojectiles);
+               if(cvar("g_new_toys"))
+                       MUTATOR_ADD(mutator_new_toys);
+               if(cvar("g_nix"))
+                       MUTATOR_ADD(mutator_nix);
+               if(cvar("g_rocket_flying"))
+                       MUTATOR_ADD(mutator_rocketflying);
+               if(cvar("g_vampire"))
+                       MUTATOR_ADD(mutator_vampire);           
+               if(cvar("g_superspectate"))
+                       MUTATOR_ADD(mutator_superspec);
+       }
 
        // is this a mutator? is this a mode?
        if(cvar("g_sandbox"))
@@ -1113,7 +1091,6 @@ void readlevelcvars(void)
 #endif
 
        sv_clones = cvar("sv_clones");
-       sv_gentle = cvar("sv_gentle");
        sv_foginterval = cvar("sv_foginterval");
        g_cloaked = cvar("g_cloaked");
     if(g_cts)
@@ -1123,12 +1100,10 @@ void readlevelcvars(void)
        g_grappling_hook = cvar("g_grappling_hook");
        g_jetpack = cvar("g_jetpack");
        g_midair = cvar("g_midair");
-       g_minstagib = cvar("g_minstagib");
        g_norecoil = cvar("g_norecoil");
        g_bloodloss = cvar("g_bloodloss");
        sv_maxidle = cvar("sv_maxidle");
        sv_maxidle_spectatorsareidle = cvar("sv_maxidle_spectatorsareidle");
-       g_ctf_reverse = cvar("g_ctf_reverse");
        sv_autotaunt = cvar("sv_autotaunt");
        sv_taunt = cvar("sv_taunt");
 
@@ -1206,11 +1181,6 @@ void readlevelcvars(void)
     if(!g_weapon_stay)
         g_weapon_stay = cvar("g_weapon_stay");
 
-       g_ghost_items = cvar("g_ghost_items");
-
-       if(g_ghost_items >= 1)
-               g_ghost_items = 0.25; // default alpha value
-
        if not(inWarmupStage && !g_ca)
                game_starttime = cvar("g_start_delay");
 
@@ -1606,34 +1576,6 @@ void precache()
 #endif
 }
 
-// sorry, but using \ in macros breaks line numbers
-#define WRITESPECTATABLE_MSG_ONE_VARNAME(varname,statement) entity varname; varname = msg_entity; FOR_EACH_REALCLIENT(msg_entity) if(msg_entity == varname || (msg_entity.classname == STR_SPECTATOR && msg_entity.enemy == varname)) statement msg_entity = varname
-#define WRITESPECTATABLE_MSG_ONE(statement) WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement)
-#define WRITESPECTATABLE(msg,statement) if(msg == MSG_ONE) { WRITESPECTATABLE_MSG_ONE(statement); } else statement float WRITESPECTATABLE_workaround = 0
-
-
-void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
-{
-       if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
-       {
-               msg_entity = e;
-               WRITESPECTATABLE_MSG_ONE({
-                       WriteByte(MSG_ONE, SVC_TEMPENTITY);
-                       WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
-                       WriteByte(MSG_ONE, id);
-                       WriteString(MSG_ONE, s);
-                       if (id != 0 && s != "")
-                       {
-                               WriteByte(MSG_ONE, duration);
-                               WriteByte(MSG_ONE, countdown_num);
-                       }
-               });
-       }
-}
-void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
-{
-       Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
-}
 // WARNING: this kills the trace globals
 #define EXACTTRIGGER_TOUCH if(WarpZoneLib_ExactTrigger_Touch()) return
 #define EXACTTRIGGER_INIT  WarpZoneLib_ExactTrigger_Init()
@@ -1656,7 +1598,7 @@ void make_safe_for_remove(entity e)
 {
     if (e.initialize_entity)
     {
-        entity ent, prev;
+        entity ent, prev = world;
         for (ent = initialize_entity_first; ent; )
         {
             if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
@@ -1728,6 +1670,7 @@ void InitializeEntity(entity e, void(void) func, float order)
     e.initialize_entity_order = order;
 
     cur = initialize_entity_first;
+    prev = world;
     for (;;)
     {
         if (!cur || cur.initialize_entity_order > order)
@@ -1986,6 +1929,9 @@ float WarpZone_Projectile_Touch_ImpactFilter_Callback()
 }
 #define PROJECTILE_TOUCH if(WarpZone_Projectile_Touch()) return
 
+#define ITEM_TOUCH_NEEDKILL() (((trace_dpstartcontents | trace_dphitcontents) & DPCONTENTS_NODROP) || (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
+#define ITEM_DAMAGE_NEEDKILL(dt) (((dt) == DEATH_HURTTRIGGER) || ((dt) == DEATH_SLIME) || ((dt) == DEATH_LAVA) || ((dt) == DEATH_SWAMP))
+
 void URI_Get_Callback(float id, float status, string data)
 {
        if(url_URI_Get_Callback(id, status, data))
@@ -2075,7 +2021,7 @@ void race_writeTime(string map, float t, string myuid)
        float newpos;
        newpos = race_readPos(map, t);
 
-       float i, prevpos;
+       float i, prevpos = 0;
        for(i = 1; i <= RANKINGS_CNT; ++i)
        {
                if(race_readUID(map, i) == myuid)
@@ -2137,6 +2083,8 @@ float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, f
     org = world.mins;
     delta = world.maxs - world.mins;
 
+    start = end = org;
+
     for (i = 0; i < attempts; ++i)
     {
         start_x = org_x + random() * delta_x;
@@ -2353,8 +2301,8 @@ vector shotorg_adjust_values(vector vecs, float y_is_right, float visual, float
        {
                if (visual)
                {
-                       vecs_y = 0;
-                       vecs_z -= 2;
+                       if (autocvar_g_shootfromclient) { vecs = shotorg_adjustfromclient(vecs, y_is_right, (autocvar_g_shootfromclient >= 2), algn); }
+                       else { vecs_y = 0; vecs_z -= 2; }
                }
                else
                {
@@ -2393,12 +2341,8 @@ vector shotorg_adjust(vector vecs, float y_is_right, float visual)
 void attach_sameorigin(entity e, entity to, string tag)
 {
     vector org, t_forward, t_left, t_up, e_forward, e_up;
-    vector org0, ang0;
     float tagscale;
 
-    ang0 = e.angles;
-    org0 = e.origin;
-
     org = e.origin - gettaginfo(to, gettagindex(to, tag));
     tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
     t_forward = v_forward * tagscale;
@@ -2746,6 +2690,8 @@ float isPushable(entity e)
 {
        if(e.iscreature)
                return TRUE;
+       if(e.pushable)
+               return TRUE;
        switch(e.classname)
        {
                case "body":
@@ -2761,3 +2707,8 @@ float isPushable(entity e)
                return TRUE;
        return FALSE;
 }
+
+void dedicated_print(string input) // print(), but only print if the server is not local
+{
+       if not(server_is_local) { print(input); }
+}