]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/bot/default/waypoints.qc
Bot waypoints: remove all incoming links from waypoints with a support waypoint;...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / waypoints.qc
index edcfde35e223aff0ea44594ff7a603f795a6d1d9..f5c3b9f7b9a83cf370db3033efbc05991272260f 100644 (file)
@@ -345,11 +345,15 @@ void waypoint_setupmodel(entity wp)
                setsize(wp, m1, m2);
                wp.effects = EF_LOWPRECISION;
                if (wp.wpflags & WAYPOINTFLAG_ITEM)
-                       wp.colormod = '1 0 0';
+                       wp.colormod = '1 0 0'; // red
                else if (wp.wpflags & WAYPOINTFLAG_GENERATED)
-                       wp.colormod = '1 1 0';
+                       wp.colormod = '1 1 0'; // yellow
+               else if (wp.wpflags & WAYPOINTFLAG_SUPPORT)
+                       wp.colormod = '0 1 0'; // green
                else if (wp.wpflags & WAYPOINTFLAG_NORELINK)
                        wp.colormod = '1 0.5 0'; // orange
+               else if (wp.wpflags & WAYPOINTFLAG_CROUCH)
+                       wp.colormod = '0 1 1'; // cyan
                else if (waypoint_has_hardwiredlinks(wp))
                        wp.colormod = '0.5 0 1'; // purple
                else
@@ -404,7 +408,10 @@ entity waypoint_spawn(vector m1, vector m2, float f)
 
        if(!w.wpisbox)
        {
-               setsize(w, PL_MIN_CONST - '1 1 0', PL_MAX_CONST + '1 1 0');
+               if (f & WAYPOINTFLAG_CROUCH)
+                       setsize(w, PL_CROUCH_MIN_CONST - '1 1 0', PL_CROUCH_MAX_CONST + '1 1 0');
+               else
+                       setsize(w, PL_MIN_CONST - '1 1 0', PL_MAX_CONST + '1 1 0');
                if(!move_out_of_solid(w))
                {
                        if(!(f & WAYPOINTFLAG_GENERATED))
@@ -455,6 +462,7 @@ void waypoint_addlink_for_custom_jumppad(entity wp_from, entity wp_to)
 bool start_wp_is_spawned;
 vector start_wp_origin;
 bool start_wp_is_hardwired;
+bool start_wp_is_support;
 
 void waypoint_clear_start_wp(entity pl, bool warn)
 {
@@ -462,6 +470,7 @@ void waypoint_clear_start_wp(entity pl, bool warn)
        start_wp_origin = '0 0 0';
        pl.wp_locked = NULL;
        start_wp_is_hardwired = false;
+       start_wp_is_support = false;
        if (warn)
                LOG_INFO("^xf80Start waypoint has been cleared.\n");
 }
@@ -481,7 +490,7 @@ void waypoint_start_hardwiredlink(entity pl)
                start_wp_is_hardwired = false;
 }
 
-void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
+void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp, bool is_crouch_wp, bool is_support_wp)
 {
        if (WAYPOINT_VERSION < waypoint_version_loaded)
        {
@@ -496,13 +505,14 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
        {
                crosshair_trace(pl);
                org = trace_endpos - eZ * PL_MIN_CONST.z;
-               IL_EACH(g_jumppads, boxesoverlap(org + PL_MIN_CONST, org + PL_MAX_CONST, it.absmin, it.absmax),
-               {
-                       jp = it;
-                       break;
-               });
+               if (!start_wp_is_support)
+                       IL_EACH(g_jumppads, boxesoverlap(org + PL_MIN_CONST, org + PL_MAX_CONST, it.absmin, it.absmax),
+                       {
+                               jp = it;
+                               break;
+                       });
        }
-       if (jp || is_jump_wp)
+       if (jp || is_jump_wp || is_support_wp)
        {
                if (start_wp_is_spawned)
                        start_wp_is_spawned = false;
@@ -517,7 +527,7 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
                ctf_flags = 2;
        int wp_num = ctf_flags;
 
-       if(!PHYS_INPUT_BUTTON_CROUCH(pl) && !at_crosshair && !is_jump_wp)
+       if(!PHYS_INPUT_BUTTON_CROUCH(pl) && !at_crosshair && !is_jump_wp && !is_support_wp)
        {
                // snap waypoint to item's origin if close enough
                IL_EACH(g_items, true,
@@ -571,8 +581,20 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
                if (!pl.wp_locked)
                        pl.wp_locked = e;
        }
+       else if (is_support_wp)
+       {
+               entity wp_found = waypoint_get(org, org);
+               if (wp_found && !(wp_found.wpflags & WAYPOINTFLAG_SUPPORT))
+               {
+                       LOG_INFO("Error: can't spawn a support waypoint over an existent waypoint of a different type\n");
+                       return;
+               }
+               e = waypoint_spawn(org, org, WAYPOINTFLAG_SUPPORT | WAYPOINTFLAG_NORELINK);
+               if (!pl.wp_locked)
+                       pl.wp_locked = e;
+       }
        else
-               e = waypoint_spawn(org, org, 0);
+               e = waypoint_spawn(org, org, (is_crouch_wp) ? WAYPOINTFLAG_CROUCH : 0);
        if(!e)
        {
                LOG_INFOF("Couldn't spawn waypoint at %v\n", org);
@@ -618,7 +640,7 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
                                        LOG_INFO("Error: hardwired links can be created only between 2 existing (and unconnected) waypoints.\n");
                                        waypoint_remove(e);
                                        waypoint_clear_start_wp(pl, true);
-                                       waypoint_spawn_fromeditor(pl, at_crosshair, is_jump_wp);
+                                       waypoint_spawn_fromeditor(pl, at_crosshair, is_jump_wp, is_crouch_wp, is_support_wp);
                                        return;
                                }
                                if (start_wp == e)
@@ -640,12 +662,36 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
                        }
                }
                else
+               {
+                       if (start_wp_is_support)
+                       {
+                               if (e.SUPPORT_WP)
+                               {
+                                       LOG_INFOF("Waypoint %v has already a support waypoint, delete it first.\n", e.origin);
+                                       waypoint_clear_start_wp(pl, true);
+                                       return;
+                               }
+                               // clear all links to e
+                               IL_EACH(g_waypoints, it != e,
+                               {
+                                       if (waypoint_islinked(it, e) && !waypoint_is_hardwiredlink(it, e))
+                                               waypoint_removelink(it, e);
+                               });
+                       }
                        waypoint_addlink(start_wp, e);
+               }
        }
 
-       if (!(jp || is_jump_wp || start_wp_is_hardwired))
+       if (!(jp || is_jump_wp || is_support_wp || start_wp_is_hardwired))
                waypoint_schedulerelink(e);
-       bprint(strcat("Waypoint spawned at ", vtos(e.origin), "\n"));
+
+       string wp_type_str = "Waypoint";
+       if (is_crouch_wp)               wp_type_str = "Crouch waypoint";
+       else if (is_jump_wp)    wp_type_str = "Jump waypoint";
+       else if (is_support_wp) wp_type_str = "Support waypoint";
+       else if (jp)                    wp_type_str = "Custom jumppad waypoint";
+
+       bprint(strcat(wp_type_str, " spawned at ", vtos(e.origin), "\n"));
 
        if (start_wp_is_spawned)
        {
@@ -682,7 +728,7 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
                        goto add_wp;
                }
        }
-       if (jp || is_jump_wp)
+       if (jp || is_jump_wp || is_support_wp)
        {
                if (!start_wp_is_spawned)
                {
@@ -690,6 +736,8 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
                        // the next one created by the user will be the destination waypoint
                        start_wp_is_spawned = true;
                        start_wp_origin = initial_origin;
+                       if (is_support_wp)
+                               start_wp_is_support = true;
                }
        }
        else if (start_wp_is_spawned)
@@ -700,8 +748,16 @@ void waypoint_spawn_fromeditor(entity pl, bool at_crosshair, bool is_jump_wp)
 
 void waypoint_remove(entity wp)
 {
+       if (wp.SUPPORT_WP)
+               waypoint_remove(wp.SUPPORT_WP); // remove support waypoint too
+
        IL_EACH(g_waypoints, it != wp,
        {
+               if (it.SUPPORT_WP == wp)
+               {
+                       it.SUPPORT_WP = NULL;
+                       waypoint_schedulerelink(it); // restore incoming links
+               }
                if (waypoint_islinked(it, wp))
                {
                        if (waypoint_is_hardwiredlink(it, wp))
@@ -780,7 +836,7 @@ void waypoint_remove_fromeditor(entity pl)
 
 void waypoint_removelink(entity from, entity to)
 {
-       if (from == to || (from.wpflags & WAYPOINTFLAG_NORELINK && !(from.wpflags & WAYPOINTFLAG_JUMP)))
+       if (from == to || (from.wpflags & WAYPOINTFLAG_NORELINK && !(from.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT))))
                return;
 
        entity fromwp31_prev = from.wp31;
@@ -889,12 +945,18 @@ float waypoint_getlinearcost(float dist)
                return dist / (autocvar_sv_maxspeed * 1.25);
        return dist / autocvar_sv_maxspeed;
 }
+
 float waypoint_getlinearcost_underwater(float dist)
 {
        // NOTE: underwater speed factor is hardcoded in the engine too, see SV_WaterMove
        return dist / (autocvar_sv_maxspeed * 0.7);
 }
 
+float waypoint_getlinearcost_crouched(float dist)
+{
+       return dist / (autocvar_sv_maxspeed * 0.5);
+}
+
 float waypoint_gettravelcost(vector from, vector to, entity from_ent, entity to_ent)
 {
        bool submerged_from = navigation_check_submerged_state(from_ent, from);
@@ -903,12 +965,15 @@ float waypoint_gettravelcost(vector from, vector to, entity from_ent, entity to_
        if (submerged_from && submerged_to)
                return waypoint_getlinearcost_underwater(vlen(to - from));
 
+       if (from_ent.wpflags & WAYPOINTFLAG_CROUCH && to_ent.wpflags & WAYPOINTFLAG_CROUCH)
+               return waypoint_getlinearcost_crouched(vlen(to - from));
+
        float c = waypoint_getlinearcost(vlen(to - from));
 
        float height = from.z - to.z;
        if(height > jumpheight_vec.z && autocvar_sv_gravity > 0)
        {
-               float height_cost;
+               float height_cost; // fall cost
                if (boolean(from_ent.wpflags & WAYPOINTFLAG_JUMP))
                        height_cost = jumpheight_time + sqrt((height + jumpheight_vec.z) / (autocvar_sv_gravity / 2));
                else
@@ -918,8 +983,14 @@ float waypoint_gettravelcost(vector from, vector to, entity from_ent, entity to_
                        c = height_cost;
        }
 
+       // consider half path underwater
        if (submerged_from || submerged_to)
                return (c + waypoint_getlinearcost_underwater(vlen(to - from))) / 2;
+
+       // consider half path crouched
+       if (from_ent.wpflags & WAYPOINTFLAG_CROUCH || to_ent.wpflags & WAYPOINTFLAG_CROUCH)
+               return (c + waypoint_getlinearcost_crouched(vlen(to - from))) / 2;
+
        return c;
 }
 
@@ -950,7 +1021,7 @@ void waypoint_addlink_customcost(entity from, entity to, float c)
 {
        if (from == to || waypoint_islinked(from, to))
                return;
-       if (c == -1 && (from.wpflags & WAYPOINTFLAG_NORELINK) && !(from.wpflags & WAYPOINTFLAG_JUMP))
+       if (c == -1 && (from.wpflags & WAYPOINTFLAG_NORELINK) && !(from.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT)))
                return;
 
        if(c == -1)
@@ -993,10 +1064,13 @@ void waypoint_addlink_customcost(entity from, entity to, float c)
 
 void waypoint_addlink(entity from, entity to)
 {
-       if ((from.wpflags & WAYPOINTFLAG_NORELINK) && !(from.wpflags & (WAYPOINTFLAG_JUMP)))
+       if ((from.wpflags & WAYPOINTFLAG_NORELINK) && !(from.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT)))
                waypoint_addlink_for_custom_jumppad(from, to);
        else
                waypoint_addlink_customcost(from, to, -1);
+
+       if (from.wpflags & WAYPOINTFLAG_SUPPORT)
+               to.SUPPORT_WP = from;
 }
 
 // relink this spawnfunc_waypoint
@@ -1019,8 +1093,10 @@ void waypoint_think(entity this)
        {
                if (boxesoverlap(this.absmin, this.absmax, it.absmin, it.absmax))
                {
-                       waypoint_addlink(this, it);
-                       waypoint_addlink(it, this);
+                       if (!(this.wpflags & WAYPOINTFLAG_NORELINK))
+                               waypoint_addlink(this, it);
+                       if (!(it.wpflags & WAYPOINTFLAG_NORELINK))
+                               waypoint_addlink(it, this);
                }
                else
                {
@@ -1040,7 +1116,22 @@ void waypoint_think(entity this)
 
                        dv = ev - sv;
                        dv.z = 0;
-                       if(vdist(dv, >=, 1050)) // max search distance in XY
+                       int maxdist = 1050;
+                       vector m1 = PL_MIN_CONST;
+                       vector m2 = PL_MAX_CONST;
+
+                       if (this.wpflags & WAYPOINTFLAG_CROUCH || it.wpflags & WAYPOINTFLAG_CROUCH)
+                       {
+                               m1 = PL_CROUCH_MIN_CONST;
+                               m2 = PL_CROUCH_MAX_CONST;
+                               // links from crouch wp to normal wp (and viceversa) are very short to avoid creating many links
+                               // that would be wasted due to rough travel cost calculation (the longer link is, the higher cost is)
+                               // links from crouch wp to crouch wp can be as long as normal links
+                               if (!(this.wpflags & WAYPOINTFLAG_CROUCH && it.wpflags & WAYPOINTFLAG_CROUCH))
+                                       maxdist = 100;
+                       }
+
+                       if (vdist(dv, >=, maxdist)) // max search distance in XY
                        {
                                ++relink_lengthculled;
                                continue;
@@ -1050,21 +1141,28 @@ void waypoint_think(entity this)
 
                        //traceline(this.origin, it.origin, false, NULL);
                        //if (trace_fraction == 1)
-                       if (this.wpisbox || this.wpflags & WAYPOINTFLAG_JUMP)
+                       if (this.wpisbox || this.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT) // forbid outgoing links
+                               || it.SUPPORT_WP) // forbid incoming links
+                       {
                                relink_walkculled += 0.5;
+                       }
                        else
                        {
-                               if (tracewalk(this, sv, PL_MIN_CONST, PL_MAX_CONST, ev2, ev2_height, MOVE_NOMONSTERS))
+                               if (tracewalk(this, sv, m1, m2, ev2, ev2_height, MOVE_NOMONSTERS))
                                        waypoint_addlink(this, it);
                                else
                                        relink_walkculled += 0.5;
                        }
 
-                       if (it.wpisbox || it.wpflags & WAYPOINTFLAG_JUMP)
+                       // reverse direction
+                       if (it.wpisbox || it.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT) // forbid incoming links
+                               || this.SUPPORT_WP) // forbid outgoing links
+                       {
                                relink_walkculled += 0.5;
+                       }
                        else
                        {
-                               if (tracewalk(this, ev, PL_MIN_CONST, PL_MAX_CONST, sv2, sv2_height, MOVE_NOMONSTERS))
+                               if (tracewalk(this, ev, m1, m2, sv2, sv2_height, MOVE_NOMONSTERS))
                                        waypoint_addlink(it, this);
                                else
                                        relink_walkculled += 0.5;
@@ -1289,23 +1387,6 @@ bool waypoint_load_links()
        return true;
 }
 
-
-void waypoint_remove_hardwiredlinks()
-{
-       IL_EACH(g_waypoints, waypoint_has_hardwiredlinks(it),
-       {
-               if (it.wphw00) { waypoint_removelink(it, it.wphw00); it.wphw00 = NULL; }
-               if (it.wphw01) { waypoint_removelink(it, it.wphw01); it.wphw01 = NULL; }
-               if (it.wphw02) { waypoint_removelink(it, it.wphw02); it.wphw02 = NULL; }
-               if (it.wphw03) { waypoint_removelink(it, it.wphw03); it.wphw03 = NULL; }
-               if (it.wphw04) { waypoint_removelink(it, it.wphw04); it.wphw04 = NULL; }
-               if (it.wphw05) { waypoint_removelink(it, it.wphw05); it.wphw05 = NULL; }
-               if (it.wphw06) { waypoint_removelink(it, it.wphw06); it.wphw06 = NULL; }
-               if (it.wphw07) { waypoint_removelink(it, it.wphw07); it.wphw07 = NULL; }
-               waypoint_setupmodel(it);
-       });
-}
-
 void waypoint_load_hardwiredlinks()
 {
        string s;
@@ -1408,7 +1489,8 @@ void waypoint_load_hardwiredlinks()
                        waypoint_addlink(wp_from, wp_to);
                        waypoint_mark_hardwiredlink(wp_from, wp_to);
                } else if (wp_from.wpflags & WAYPOINTFLAG_NORELINK
-                       && (wp_from.wpflags & WAYPOINTFLAG_JUMP || (wp_from.wpisbox && wp_from.wpflags & WAYPOINTFLAG_TELEPORT)))
+                       && (wp_from.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT)
+                               || (wp_from.wpisbox && wp_from.wpflags & WAYPOINTFLAG_TELEPORT)))
                {
                        waypoint_addlink(wp_from, wp_to);
                }
@@ -1473,6 +1555,7 @@ void waypoint_save_hardwiredlinks()
        }
 
        // write hardwired links to file
+       int count = 0;
        fputs(file, "// HARDWIRED LINKS\n");
        IL_EACH(g_waypoints, waypoint_has_hardwiredlinks(it),
        {
@@ -1484,13 +1567,15 @@ void waypoint_save_hardwiredlinks()
                                // NOTE: vtos rounds vector components to 1 decimal place
                                string s = strcat(vtos(it.origin), "*", vtos(link.origin), "\n");
                                fputs(file, s);
+                               ++count;
                        }
                }
        });
 
        // write special links to file
+       int count2 = 0;
        fputs(file, "\n// SPECIAL LINKS\n");
-       IL_EACH(g_waypoints, it.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_CUSTOM_JP),
+       IL_EACH(g_waypoints, it.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT | WAYPOINTFLAG_CUSTOM_JP),
        {
                for (int j = 0; j < 32; ++j)
                {
@@ -1500,13 +1585,14 @@ void waypoint_save_hardwiredlinks()
                                // NOTE: vtos rounds vector components to 1 decimal place
                                string s = strcat("*", vtos(it.origin), "*", vtos(link.origin), "\n");
                                fputs(file, s);
+                               ++count2;
                        }
                }
        });
 
        fclose(file);
 
-       LOG_INFOF("saved hardwired waypoint links to %s", filename);
+       LOG_INFOF("saved %d hardwired links and %d special links to %s", count, count2, filename);
 }
 
 // Save all waypoint links to a file
@@ -1527,7 +1613,7 @@ void waypoint_save_links()
                fputs(file, strcat("//", "WAYPOINT_TIME ", waypoint_time, "\n"));
 
        int c = 0;
-       IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_CUSTOM_JP)),
+       IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT | WAYPOINTFLAG_CUSTOM_JP)),
        {
                for(int j = 0; j < 32; ++j)
                {
@@ -1884,7 +1970,7 @@ void waypoint_showlink(entity wp1, entity wp2, int display_type)
        if (!(wp1 && wp2))
                return;
 
-       if (waypoint_is_hardwiredlink(wp1, wp2))
+       if (waypoint_is_hardwiredlink(wp1, wp2) || wp1.wpflags & (WAYPOINTFLAG_JUMP | WAYPOINTFLAG_SUPPORT | WAYPOINTFLAG_CUSTOM_JP))
                te_beam(NULL, wp1.origin, wp2.origin);
        else if (display_type == 1)
                te_lightning2(NULL, wp1.origin, wp2.origin);