From cad1dfbcfe8afe10c6b78786d40acaf4daac6c5e Mon Sep 17 00:00:00 2001 From: Marco Hladik Date: Sat, 18 Dec 2021 17:50:43 -0800 Subject: [PATCH] Waypoint-system: You can now unlink waypoints (either one or two way...) --- src/botlib/way.qc | 85 +++++++++++++++++++++++++++++++++++++++++++++++ src/client/way.qc | 10 ++++-- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/botlib/way.qc b/src/botlib/way.qc index 90cdb085..94e852f7 100644 --- a/src/botlib/way.qc +++ b/src/botlib/way.qc @@ -65,6 +65,44 @@ Way_LinkNodes(waypoint_t *wp, waypoint_t *w2) n->m_iFlags = 0; } +static void +Way_UnlinkNodes(waypoint_t *wp, waypoint_t *w2) +{ + int w2n = w2 - g_pWaypoints; + int nilled = 0; + + for (int i = 0i; i < wp->m_numNeighbours; i++) { + if (wp->m_pNeighbour[i].m_iNode == w2n) { + wp->m_pNeighbour[i].m_iNode = -1; + wp->m_pNeighbour[i].m_flCost = 0; + wp->m_pNeighbour[i].m_iFlags = 0; + nilled = 1; + } + } + + /* we nilled an entry, so let's recreate the neighbour list */ + if (nilled) { + int new_neighbours = wp->m_numNeighbours - 1; /* one less. */ + wpneighbour_s *new = (wpneighbour_s *)memalloc(sizeof(*wp->m_pNeighbour) * new_neighbours); + int b = 0; + + /* loop through all of our neighbours... */ + for (int i = 0i; i < wp->m_numNeighbours; i++) { + if (wp->m_pNeighbour[i].m_iNode != -1) { + new[b].m_iNode = wp->m_pNeighbour[i].m_iNode; + new[b].m_flCost = wp->m_pNeighbour[i].m_flCost; + new[b].m_iFlags = wp->m_pNeighbour[i].m_iFlags; + b++; + } + } + + /* assign our new neighbour list to the old one, which will be freed. */ + wp->m_numNeighbours = new_neighbours; + memfree(wp->m_pNeighbour); + wp->m_pNeighbour = new; + } +} + static void Way_AutoLink(int wpidx) { @@ -322,6 +360,47 @@ Way_FlagUse(void) } } +void +Way_Unlink(void) +{ + if (g_waylink_status == 0) { + g_way1 = Way_FindClosestNode(self.origin); + g_waylink_status = 1; + env_message_single(self, "^2Selected first waypoint!\n"); + } else if (g_waylink_status == 1) { + g_way2 = Way_FindClosestNode(self.origin); + g_waylink_status = 0; + + if (g_way1 != g_way2) { + Way_UnlinkNodes(&g_pWaypoints[g_way1], &g_pWaypoints[g_way2]); + } else { + env_message_single(self, "^1Failed to link, the two points are the same!\n"); + } + g_way1 = g_way2 = -1; + } +} + +void +Way_UnlinkTwo(void) +{ + if (g_waylink_status == 0) { + g_way1 = Way_FindClosestNode(self.origin); + g_waylink_status = 1; + env_message_single(self, "^2Selected first waypoint!\n"); + } else if (g_waylink_status == 1) { + g_way2 = Way_FindClosestNode(self.origin); + g_waylink_status = 0; + + if (g_way1 != g_way2) { + Way_UnlinkNodes(&g_pWaypoints[g_way1], &g_pWaypoints[g_way2]); + Way_UnlinkNodes(&g_pWaypoints[g_way2], &g_pWaypoints[g_way1]); + } else { + env_message_single(self, "^1Failed to link, the two points are the same!\n"); + } + g_way1 = g_way2 = -1; + } +} + void Way_HelperSpawns() { @@ -641,6 +720,12 @@ Way_Cmd(void) case "linkuse": Way_FlagUse(); break; + case "unlink1": + Way_Unlink(); + break; + case "unlink2": + Way_UnlinkTwo(); + break; case "move": vector p; int n = Way_FindClosestNode(self.origin); diff --git a/src/client/way.qc b/src/client/way.qc index cf146185..50881ba9 100644 --- a/src/client/way.qc +++ b/src/client/way.qc @@ -75,8 +75,8 @@ Way_Init(void) way_link.m_strName = "WAY_LINK"; way_link.m_strMessage = "1.\tLink 1-way (2 steps)\n" \ "2.\tLink 2-way (2 steps)\n" \ - "\n" \ - "\n" \ + "3.\tUnlink 1-way (2 steps)\n" \ + "4.\tUnlink 2-way (2 steps)\n" \ "\n" \ "\n" \ "7.\tAutolink closest\n" \ @@ -273,6 +273,12 @@ WAY_LINK(int n) case 2: localcmd("sv way connect2\n"); break; + case 3: + localcmd("sv way unlink1\n"); + break; + case 4: + localcmd("sv way unlink2\n"); + break; case 7: localcmd("sv way autolink\n"); break;