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/ZScript.zs b/ZScript.zs index fa00ac4..a62cfd4 100644 --- a/ZScript.zs +++ b/ZScript.zs @@ -2402,7 +2402,7 @@ class ZTBotController : Actor { void Subroutine_Flee() { if (DodgeAndUse()) { if (currNode) - navDest = currNode.RandomNeighbor(); + navDest = currNode.RandomNeighborRoughlyToward(vel.xy, 0.5); else navDest = null; @@ -2668,7 +2668,15 @@ 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 == null || zbp.cont == null) { @@ -2798,15 +2806,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); @@ -2816,27 +2828,25 @@ 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) { - SetWanderNavdest(); - } + DebugLog(LT_VERBOSE, "picking destination"); + navDest = currNode.RandomNeighborRoughlyToward(vel.xy, 4); } ZTPathNode LastVisited[LAST_VISITED_LENGTH]; @@ -2855,8 +2865,21 @@ class ZTBotController : Actor { if (alist.Get(listIdx) == LastVisited[checkIdx]) { aList.Remove(listIdx); } - } } + + 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 + } if (aList.IsEmpty()) { return; diff --git a/ZetaCode/Pathing.zs b/ZetaCode/Pathing.zs index 120b06c..68e0fdc 100644 --- a/ZetaCode/Pathing.zs +++ b/ZetaCode/Pathing.zs @@ -857,6 +857,60 @@ class ZTPathNode : ZTPositionMarker return res; } + + 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; + + 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; + } + + // 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 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