From 40d95d507c5ffb99dbea2024a14bf08108dc3f2f Mon Sep 17 00:00:00 2001 From: Gustavo Rehermann Date: Mon, 18 Mar 2024 10:42:32 -0300 Subject: [PATCH 1/4] Make bots try to wander in a rough direction By using a dot-product weighted random choice in the process of picking a random neighbour to wander towards, better exploration should be achieved. TODO: custom class to affect the neighbour selection process, with a virtual function that affects the weight of each considered neighbour. --- ZScript.zs | 4 ++-- ZetaCode/Pathing.zs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/ZScript.zs b/ZScript.zs index 701bac0..5071fe4 100644 --- a/ZScript.zs +++ b/ZScript.zs @@ -2337,7 +2337,7 @@ class ZTBotController : Actor { void Subroutine_Flee() { if (DodgeAndUse()) { if (currNode) - navDest = currNode.RandomNeighbor(); + navDest = currNode.RandomNeighborRoughlyToward(vel.xy, 0.2); else navDest = null; @@ -2723,7 +2723,7 @@ class ZTBotController : Actor { if (!navDest) { if (currNode) { - navDest = currNode.RandomNeighbor(); + navDest = currNode.RandomNeighborRoughlyToward(vel.xy); if (navDest == currNode) { navDest = null; diff --git a/ZetaCode/Pathing.zs b/ZetaCode/Pathing.zs index 120b06c..4c60a0f 100644 --- a/ZetaCode/Pathing.zs +++ b/ZetaCode/Pathing.zs @@ -857,6 +857,42 @@ class ZTPathNode : ZTPositionMarker return res; } + + ZTPathNode RandomNeighborRoughlyToward(Vector2 direction, double preference = 0.5) + { + ActorList nb = NeighborsOutward(); + ZTPathNode res; + + if ( nb.Length() < 1 ) + return self; + + Array weights; + double total = 0.0; + + ZTPathnode neigh; + for (nb.iReset(), neigh = ZTPathNode(nb.iNext()); neigh; neigh = ZTPathNode(nb.iNext())) { + Vector2 offs = neigh.pos.xy - pos.xy; + double directionality = 1 + (1 + offs dot direction) * preference; + weights.push(directionality); + total += directionality; + } + + // weighted random + double choice = FRandom(0, total); + + for (int i = 0; i < weights.Size(); i++) { + if (choice < weights[i]) { + res = ZTPathNode(nb.Get(i)); + break; + } + + choice -= weights[i]; + } + + nb.Destroy(); // clean actorlists after use + + return res; + } void ShowPath(ZTPathNode otherNode) { uint segRes = 24; // constant From 360e0fd1322c8f5dc3c976485b16b01cec2a92e2 Mon Sep 17 00:00:00 2001 From: Gustavo Rehermann Date: Mon, 18 Mar 2024 11:40:24 -0300 Subject: [PATCH 2/4] Fix navigation directionality calculation --- ZScript.zs | 48 ++++++++++++++++++++++++++++++--------------- ZetaCode/Pathing.zs | 20 ++++++++++++++++++- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/ZScript.zs b/ZScript.zs index 5071fe4..b7affb8 100644 --- a/ZScript.zs +++ b/ZScript.zs @@ -2337,7 +2337,7 @@ class ZTBotController : Actor { void Subroutine_Flee() { if (DodgeAndUse()) { if (currNode) - navDest = currNode.RandomNeighborRoughlyToward(vel.xy, 0.2); + navDest = currNode.RandomNeighborRoughlyToward(vel.xy, 0.5); else navDest = null; @@ -2703,33 +2703,49 @@ class ZTBotController : Actor { return; } } - - PickCommander(); + + BotChat("IDLE", 2.25 / 100); + DodgeAndUse(); if (currNode == null) { SetCurrentNode(ClosestVisibleNode(possessed)); } - - DodgeAndUse(); - - if (currNode && possessed.Distance2D(currNode) < 200 && navDest && navDest != currNode) { - MoveToward(navDest, 5); // wander to this random neighbouring node + + if (!currNode) { + return; } - - else { + + if (navDest == currNode) { navDest = null; - RandomMove(); } if (!navDest) { - if (currNode) { - navDest = currNode.RandomNeighborRoughlyToward(vel.xy); + DebugLog(LT_VERBOSE, "picking destination"); + navDest = currNode.RandomNeighborRoughlyToward(vel.xy, 4); - if (navDest == currNode) { - navDest = null; - } + if (navDest == currNode) { + navDest = null; + //DebugLog(LT_VERBOSE, "tried to pick own node"); + } + + if (navDest) { + //DebugLog(LT_VERBOSE, "picked destination"); + } + + else { + //DebugLog(LT_VERBOSE, "failed to pick destination"); } } + + if (navDest) { + //DebugLog(LT_VERBOSE, "moving to destination"); + MoveToward(navDest, 5); // wander to this random neighbouring node + } + + else { + //DebugLog(LT_VERBOSE, "no destination"); + RandomMove(); + } } void FireAtBarrels() { diff --git a/ZetaCode/Pathing.zs b/ZetaCode/Pathing.zs index 4c60a0f..68e0fdc 100644 --- a/ZetaCode/Pathing.zs +++ b/ZetaCode/Pathing.zs @@ -858,10 +858,19 @@ class ZTPathNode : ZTPositionMarker return res; } - ZTPathNode RandomNeighborRoughlyToward(Vector2 direction, double preference = 0.5) + ZTPathNode RandomNeighborRoughlyToward(Vector2 direction, double preference = 1.0) { ActorList nb = NeighborsOutward(); ZTPathNode res; + + if (direction.Length() == 0) { + return RandomNeighbor(); + } + + direction = direction.Unit(); + preference /= 2.0; // due to how dot products work + + A_Log(""..direction); if ( nb.Length() < 1 ) return self; @@ -869,10 +878,19 @@ class ZTPathNode : ZTPositionMarker Array weights; double total = 0.0; + A_Log(""..direction); + ZTPathnode neigh; for (nb.iReset(), neigh = ZTPathNode(nb.iNext()); neigh; neigh = ZTPathNode(nb.iNext())) { + if (neigh == self) { + continue; + } + A_Log("--"); + A_Log(""..direction); Vector2 offs = neigh.pos.xy - pos.xy; + offs = offs.Unit(); double directionality = 1 + (1 + offs dot direction) * preference; + A_Log(offs.." "..direction); weights.push(directionality); total += directionality; } From dacbd6a03c1116c5f5df85f42e3cbd6c7e1ed080 Mon Sep 17 00:00:00 2001 From: Gustavo Rehermann Date: Mon, 18 Mar 2024 11:40:37 -0300 Subject: [PATCH 3/4] Improved commander logic --- ZScript.zs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/ZScript.zs b/ZScript.zs index b7affb8..adf0cc1 100644 --- a/ZScript.zs +++ b/ZScript.zs @@ -2571,10 +2571,8 @@ class ZTBotController : Actor { return true; } - - let ztcom = ZetaBotPawn(newCommander); - - if (ztcom && ztcom.cont && ztcom.cont.commander && ztcom.cont.commander == possessed) { + + if (Commands(newCommander)) { return false; } @@ -2583,14 +2581,26 @@ class ZTBotController : Actor { return true; } - bool Commands(Actor another) { + bool Commands(Actor another, int depth = 0) { + if (!another) { + return false; + } + + if (another == possessed) { + return true; + } + ZetaBotPawn zbp = ZetaBotPawn(another); - if (!zbp || !zbp.cont) { + if (!zbp || !zbp.cont || !zbp.cont.commander) { return false; } - return zbp.cont.commander == possessed; + if (zbp.cont.commander == possessed) return true; + + if (depth > 100) return false; + + return Commands(zbp.cont.commander, depth + 1); } void PickCommander() { @@ -2685,15 +2695,19 @@ class ZTBotController : Actor { navDest = null; } } - - void Subroutine_Wander() { + + void ForgetEnemies() { if (lastEnemyPos != null) { lastEnemyPos.Destroy(); lastEnemyPos = null; } enemy = null; - BotChat("IDLE", 2.25 / 100); + } + + void Subroutine_Wander() { + ForgetEnemies(); + PickCommander(); if (bstate != BS_FOLLOWING && ShouldFollow(commander)) { ConsiderSetBotState(BS_FOLLOWING); From e701e362d7bb079bee0b431dfd6ed45779284130 Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Mon, 24 Jun 2024 05:28:12 -0700 Subject: [PATCH 4/4] Build on macos --- .gitignore | 2 +- build | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 12832a7..cda789c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /Built/ .vscode /run - +.DS_Store diff --git a/build b/build index 60f0656..e77cfe3 100755 --- a/build +++ b/build @@ -18,6 +18,17 @@ LZMA=0 PLATFORM=$(uname -s | head -c 5) RUN=0 +REALPATH_CMD="realpath" +# check if we are on macos +if [ "$PLATFORM" == "Darwi" ]; then + REALPATH_CMD="grealpath" + # check if grealpath exists + if ! command -v grealpath &> /dev/null; then + echo "grealpath not found, please install coreutils using 'brew install coreutils' and try again." + exit 1 + fi +fi + while [[ $# -gt 0 ]]; do key="$1" @@ -103,10 +114,10 @@ BUILDDIR="" function ADDFILE { if [ $# -eq 1 ]; then if [ -n "$BUILDDIR" ]; then - f="$(realpath --relative-to="./$BUILDDIR" "./${BUILDDIR}/${1}")" + f="$($REALPATH_CMD --relative-to="./$BUILDDIR" "./${BUILDDIR}/${1}")" else - f="$(realpath --relative-to="." "./${1}")" + f="$($REALPATH_CMD --relative-to="." "./${1}")" fi