Skip to content
This repository has been archived by the owner on Jan 11, 2018. It is now read-only.

Commit

Permalink
No desync AI
Browse files Browse the repository at this point in the history
AI is now running synced, in the sense that end-turn in not send from
within the AI, but instead from the script_engine, when the AI runs out
of scope. Also end-turn now triggers a 10 second delay, such that the AI
does not start running again, before the enemy has played his turn.
(Previously is was possible for the AI to run for something like 1
second (typically until first delay call)).
  • Loading branch information
Skeen committed Mar 20, 2014
1 parent b6e71c1 commit e877f38
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
17 changes: 14 additions & 3 deletions projects/Bot/src/AIBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private bool mulligan()

// End mulligan
MulliganManager.Get().EndMulligan();
API.end_turn();
end_turn();

// Report progress
Log.say("Mulligan Ended : " + replace.Count + " cards changed");
Expand Down Expand Up @@ -314,19 +314,30 @@ private void game_over()
}
}

private void end_turn()
{
InputManager.Get().DoEndTurnButton();
Delay(10000);
}

// Called to invoke AI
private void run_ai()
{
// We're in normal game state, and it's our turn
try
{
// Run the AI, check if it requests a pause
bool should_delay = api.run();
if(should_delay)
api.run();
if(api.was_critical_pause_requested())
{
// Delay 2.0 seconds
Delay(2000);
}
if(api.was_end_turn_requested())
{
// Go ahead and end the turn
end_turn();
}
}
catch(Exception e)
{
Expand Down
32 changes: 11 additions & 21 deletions projects/Bot/src/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public API()
// Attack
lua.RegisterFunction("__csharp_do_attack", this, typeof(API).GetMethod("__csharp_do_attack"));

// End Turn
lua.RegisterFunction("__csharp_end_turn", this, typeof(API).GetMethod("__csharp_end_turn"));

Log.log("Loading Main.lua...");
lua.DoFile(lua_script_path + "Main.lua");
Log.log("Done");
Expand Down Expand Up @@ -206,33 +203,38 @@ public Card __csharp_enemy_hero_card()
return hero_card;
}

private bool was_critical_pause_requested()
public bool was_critical_pause_requested()
{
bool critical_puase_requested = (bool) lua["__critical_pause"];
lua["__critical_pause"] = false;
return critical_puase_requested;
}

public bool run()
public bool was_end_turn_requested()
{
bool end_turn_requested = (bool) lua["__end_turn"];
lua["__end_turn"] = false;
return end_turn_requested;
}

public void run()
{
try
{
LuaFunction f = lua.GetFunction("turn_start");
if(f == null)
{
Log.log("Lua function not found!");
return false;
return;
}
object[] args = f.Call();
string error = (string) args[0];
if(error != null)
{
Log.log("LUA EXCEPTION");
Log.log(error);
return false;
return;
}

return was_critical_pause_requested();
}
catch(LuaException e)
{
Expand All @@ -244,7 +246,6 @@ public bool run()
{
Log.error(e.ToString());
}
return false;
}

public List<Card> mulligan(List<Card> cards)
Expand Down Expand Up @@ -310,11 +311,6 @@ public bool __csharp_is_card_tank(Card c)
return entity.HasTaunt();
}

public void __csharp_end_turn()
{
end_turn();
}

public bool __use_hero_power()
{
Card c1 = getOurPlayer().GetHeroPowerCard();
Expand Down Expand Up @@ -528,11 +524,5 @@ public bool drop_card(Card c, bool pickup)
}
return false;
}

public static void end_turn()
{
InputManager.Get().DoEndTurnButton();
// TODO: Delay for 10 seconds, to avoid running AI out of sync
}
}
}
1 change: 0 additions & 1 deletion projects/Release/LuaScripts/AI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ turn_start_function = function()

print_to_log("AI ended" .. i);
end
end_turn();
print_to_log("End of Turn Called");
end

Expand Down
5 changes: 1 addition & 4 deletions projects/Release/LuaScripts/API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ OurHero = "OUR_HERO"
EnemyHero = "ENEMY_HERO"

__critical_pause = false
__end_turn = false

-- Return a list of cards in a given region
function getCards(where)
Expand Down Expand Up @@ -136,10 +137,6 @@ function isTank(card)
return is_tank
end

function end_turn()
__csharp_end_turn()
end

function use_hero_power()
local succes = __use_hero_power()
coroutine.yield()
Expand Down
2 changes: 1 addition & 1 deletion projects/Release/LuaScripts/script_engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function turn_start()
-- If it has run all the way through
if(coroutine.status(co_routine) == "dead") then
-- Create a new routine
__critical_pause = false
__end_turn = true
co_routine = coroutine.create(turn_start_function)
end
return nil
Expand Down

0 comments on commit e877f38

Please sign in to comment.