Created
May 22, 2024 12:32
-
-
Save zinderud/82dc964a1868b60217833338c99e9cde to your computer and use it in GitHub Desktop.
aw boot quest-3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Initializing global variables to store the latest game state and game host process. | |
| LatestGameState = LatestGameState or nil | |
| Game = Game or nil | |
| CRED = "Sa0iBLPNyJQrwpTTG-tWLQU-1QeUAJA73DdxGGiKoJc" | |
| colors = { | |
| red = "\27[31m", | |
| green = "\27[32m", | |
| blue = "\27[34m", | |
| reset = "\27[0m", | |
| gray = "\27[90m" | |
| } | |
| -- Checks if two points are within a given range. | |
| -- @param x1, y1: Coordinates of the first point. | |
| -- @param x2, y2: Coordinates of the second point. | |
| -- @param range: The maximum allowed distance between the points. | |
| -- @return: Boolean indicating if the points are within the specified range. | |
| function inRange(x1, y1, x2, y2, range) | |
| return math.abs(x1 - x2) <= range and math.abs(y1 - y2) <= range | |
| end | |
| -- Decides the next action based on player proximity and energy. | |
| -- If any player is within range, it initiates an attack; otherwise, moves randomly. | |
| function decideNextAction() | |
| local player = LatestGameState.Players[ao.id] | |
| local targetInRange = false | |
| for target, state in pairs(LatestGameState.Players) do | |
| if target ~= ao.id and inRange(player.x, player.y, state.x, state.y, 1) then | |
| targetInRange = true | |
| break | |
| end | |
| end | |
| if player.energy > 5 and targetInRange then | |
| print(colors.red .. "Player in range. Attacking." .. colors.reset) | |
| ao.send({Target = Game, Action = "PlayerAttack", AttackEnergy = tostring(player.energy)}) | |
| else | |
| print(colors.red .. "No player in range or insufficient energy. Moving randomly." .. colors.reset) | |
| local directionMap = {"Up", "Down", "Left", "Right", "UpRight", "UpLeft", "DownRight", "DownLeft"} | |
| local randomIndex = math.random(#directionMap) | |
| ao.send({Target = Game, Action = "PlayerMove", Direction = directionMap[randomIndex]}) | |
| end | |
| end | |
| -- Handler to print game announcements and trigger game state updates. | |
| Handlers.add( | |
| "PrintAnnouncements", | |
| Handlers.utils.hasMatchingTag("Action", "Announcement"), | |
| function (msg) | |
| if msg.Event == "Started-Waiting-Period" then | |
| ao.send({Target = ao.id, Action = "AutoPay"}) | |
| elseif (msg.Event == "Tick" or msg.Event == "Started-Game") then | |
| ao.send({Target = Game, Action = "GetGameState"}) | |
| end | |
| print(colors.green .. msg.Event .. ": " .. msg.Data .. colors.reset) | |
| end | |
| ) | |
| -- Handler to trigger game state updates. | |
| Handlers.add( | |
| "GetGameStateOnTick", | |
| Handlers.utils.hasMatchingTag("Action", "Tick"), | |
| function () | |
| print(colors.gray .. "Getting game state..." .. colors.reset) | |
| ao.send({Target = Game, Action = "GetGameState"}) | |
| end | |
| ) | |
| -- Handler to automate payment confirmation when waiting period starts. | |
| Handlers.add( | |
| "AutoPay", | |
| Handlers.utils.hasMatchingTag("Action", "AutoPay"), | |
| function (msg) | |
| print("Auto-paying confirmation fees.") | |
| ao.send({ Target = CRED, Action = "Transfer", Recipient = Game, Quantity = "1000"}) | |
| end | |
| ) | |
| -- Handler to update the game state upon receiving game state information. | |
| Handlers.add( | |
| "UpdateGameState", | |
| Handlers.utils.hasMatchingTag("Action", "GameState"), | |
| function (msg) | |
| local json = require("json") | |
| LatestGameState = json.decode(msg.Data) | |
| ao.send({Target = ao.id, Action = "UpdatedGameState"}) | |
| print("Game state updated. Print \'LatestGameState\' for detailed view.") | |
| end | |
| ) | |
| -- Handler to decide the next best action. | |
| Handlers.add( | |
| "decideNextAction", | |
| Handlers.utils.hasMatchingTag("Action", "UpdatedGameState"), | |
| function () | |
| print("Deciding next action.") | |
| decideNextAction() | |
| ao.send({Target = ao.id, Action = "Tick"}) | |
| end | |
| ) | |
| -- Handler to automatically attack when hit by another player. | |
| Handlers.add( | |
| "ReturnAttack", | |
| Handlers.utils.hasMatchingTag("Action", "Hit"), | |
| function (msg) | |
| local playerEnergy = LatestGameState.Players[ao.id].energy | |
| if playerEnergy == undefined then | |
| print(colors.red .. "Unable to read energy." .. colors.reset) | |
| ao.send({Target = Game, Action = "Attack-Failed", Reason = "Unable to read energy."}) | |
| elseif playerEnergy == 0 then | |
| print(colors.red .. "Player has insufficient energy." .. colors.reset) | |
| ao.send({Target = Game, Action = "Attack-Failed", Reason = "Player has no energy."}) | |
| else | |
| print(colors.red .. "Returning attack." .. colors.reset) | |
| ao.send({Target = Game, Action = "PlayerAttack", AttackEnergy = tostring(playerEnergy)}) | |
| end | |
| ao.send({Target = ao.id, Action = "Tick"}) | |
| end | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment