From 2c7f540787d3d06c1c21f80b758e32f0d16f61e4 Mon Sep 17 00:00:00 2001 From: Marco Hladik Date: Sat, 27 Mar 2021 07:48:46 +0100 Subject: [PATCH] botlib: add USER linkflag. When a bot passes that link, he'll USE the closest func_button nearby. --- src/botlib/bot.h | 1 + src/botlib/bot.qc | 55 +++++++++++++++++++++++++++++++++++++++++++++++ src/botlib/way.h | 2 +- src/botlib/way.qc | 32 +++++++++++++++++++++++++-- src/client/way.qc | 5 ++++- 5 files changed, 91 insertions(+), 4 deletions(-) diff --git a/src/botlib/bot.h b/src/botlib/bot.h index d02ac9f2..fbcd4f61 100644 --- a/src/botlib/bot.h +++ b/src/botlib/bot.h @@ -57,6 +57,7 @@ class bot:player virtual void(void) CheckRoute; virtual void(void) PreFrame; virtual void(void) PostFrame; + virtual void(void) UseButton; }; entity Bot_AddQuick(void); diff --git a/src/botlib/bot.qc b/src/botlib/bot.qc index 44afb047..269b2a9f 100644 --- a/src/botlib/bot.qc +++ b/src/botlib/bot.qc @@ -78,6 +78,57 @@ bot::BrainThink(int enemyvisible, int enemydistant) } } +void +bot::UseButton(void) +{ +#if 1 + float best; + func_button best_button = __NULL__; + + best = COST_INFINITE; + for (entity e = world; (e = find(e, ::classname, "func_button"));) { + float dist; + vector pos; + pos[0] = e.absmin[0] + (0.5 * (e.absmax[0] - e.absmin[0])); + pos[1] = e.absmin[1] + (0.5 * (e.absmax[1] - e.absmin[1])); + pos[2] = e.absmin[2] + (0.5 * (e.absmax[2] - e.absmin[2])); + dist = vlen(origin - pos); + + if (dist < best) { + best = dist; + best_button = (func_button)e; + } + } + + if (best_button == __NULL__) + return; + + best_button.Trigger(this, TRIG_TOGGLE); + sound(this, CHAN_ITEM, "common/wpn_select.wav", 0.25, ATTN_IDLE); +#else + float best; + vector foo; + + best = COST_INFINITE; + for (entity e = world; (e = find(e, ::classname, "func_button"));) { + float dist; + vector pos; + pos[0] = e.absmin[0] + (0.5 * (e.absmax[0] - e.absmin[0])); + pos[1] = e.absmin[1] + (0.5 * (e.absmax[1] - e.absmin[1])); + pos[2] = e.absmin[2] + (0.5 * (e.absmax[2] - e.absmin[2])); + dist = vlen(origin - pos); + + if (dist < best) { + best = dist; + foo = pos; + } + } + + v_angle = vectoangles(origin - foo); + Player_UseDown(); +#endif +} + void bot::SeeThink(void) { @@ -157,6 +208,10 @@ bot::CheckRoute(void) /* if a node is flagged as jumpy, jump! */ if (m_pRoute[m_iCurNode].m_iFlags & LF_JUMP) input_buttons |= INPUT_BUTTON2; + + /* find the nearest usable item (func_button) and use them */ + if (m_pRoute[m_iCurNode].m_iFlags & LF_USER) + UseButton(); } #if 0 diff --git a/src/botlib/way.h b/src/botlib/way.h index 8252b8c2..b9a12fe1 100644 --- a/src/botlib/way.h +++ b/src/botlib/way.h @@ -22,5 +22,5 @@ #define LF_TELEPORT 0x00000008i #define LF_WALK 0x00000010i #define LF_AIM 0x00000020i -#define LF_USER 0x7fffff00i +#define LF_USER 0x00000040i #define LF_DESTINATION 0x80000000i diff --git a/src/botlib/way.qc b/src/botlib/way.qc index 44fc88ef..b50b071a 100644 --- a/src/botlib/way.qc +++ b/src/botlib/way.qc @@ -288,6 +288,31 @@ Way_FlagAim(void) } } +void +Way_FlagUse(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) { + for (int b = 0i; b < g_pWaypoints[g_way1].m_numNeighbours; b++) { + if (g_pWaypoints[g_way1].m_pNeighbour[b].m_iNode == g_way2) { + g_pWaypoints[g_way1].m_pNeighbour[b].m_iFlags |= LF_USER; + env_message_single(self, "^2Walk-linked the two points!\n"); + } + } + } else { + env_message_single(self, "^1Failed to link, the two points are the same!\n"); + } + g_way1 = g_way2 = -1; + } +} + void Way_HelperSpawns() { @@ -338,7 +363,7 @@ Way_SaveFile(string filename) fputs(file, sprintf("%v %f %i\n", g_pWaypoints[i].m_vecOrigin, g_pWaypoints[i].m_flRadius, g_pWaypoints[i].m_numNeighbours)); for(int j = 0i; j < g_pWaypoints[i].m_numNeighbours; j++) { - fputs(file, sprintf(" %i %f %x\n", g_pWaypoints[i].m_pNeighbour[j].m_iNode, g_pWaypoints[i].m_pNeighbour[j].m_flCost, (float)g_pWaypoints[i].m_pNeighbour[j].m_iFlags)); + fputs(file, sprintf(" %i %f %i\n", g_pWaypoints[i].m_pNeighbour[j].m_iNode, g_pWaypoints[i].m_pNeighbour[j].m_flCost, g_pWaypoints[i].m_pNeighbour[j].m_iFlags)); } } @@ -373,7 +398,7 @@ Way_ReadFile(string strFile) tokenize(fgets(file)); g_pWaypoints[i].m_pNeighbour[j].m_iNode = stoi(argv(0)); g_pWaypoints[i].m_pNeighbour[j].m_flCost = stof(argv(1)); - g_pWaypoints[i].m_pNeighbour[j].m_iFlags = stoh(argv(2)); + g_pWaypoints[i].m_pNeighbour[j].m_iFlags = stoi(argv(2)); } } fclose(file); @@ -601,6 +626,9 @@ Way_Cmd(void) case "linkaim": Way_FlagAim(); break; + case "linkuse": + Way_FlagUse(); + break; case "move": vector p; int n = Way_FindClosestNode(self.origin); diff --git a/src/client/way.qc b/src/client/way.qc index 6909d6f8..941689a2 100644 --- a/src/client/way.qc +++ b/src/client/way.qc @@ -110,7 +110,7 @@ Way_Init(void) "2.\tFlag ^2CROUCH^7 (2 steps)\n" \ "3.\tFlag ^1WALK^7 (2 steps)\n" \ "4.\tFlag ^4AIM^7 (2 steps)\n" \ - "\n" \ + "5.\tFlag ^4USE^7 (2 steps)\n" \ "\n" \ "\n" \ "\n" \ @@ -282,6 +282,9 @@ WAY_FLAGS(int n) case 4: localcmd("sv way linkaim\n"); break; + case 5: + localcmd("sv way linkuse\n"); + break; case 9: Textmenu_Call("WAY_MENU"); break;