Game Development Reference
In-Depth Information
end
return bestAction;
end
In this case, the world state is built every loop. This is not entirely uncommon in
situations where the world state is small and keeping track of it isn
'
t necessary. The
Build() function
s job is to grab what it needs from the world (in this case, the
teapot and the opponent) so the AI function can do its thing. Here
'
'
s a possible
Build() function:
function WorldState:Build(teapot, opponent)
self.opponentHp = opponent:GetHp();
self.opponentMp = opponent:GetMp();
self.survivalChance =
self:_CalculateSurvivalChance(teapot:GetHp(), opponent);
self.killChance =
1 - self:_CalculateSurvivalChance(opponent:GetHp(), teapot);
end
function WorldState:_CalculateSurvivalChance(defenderHp, attacker)
if (defenderHp > attacker:GetMaxDamage()) then
return 1;
elseif (defenderHp <= attacker:GetMinDamage()) then
return 0;
else
local range = attacker:GetMaxDamage() - attacker:GetMinDamage();
local chance = (defenderHp - attacker:GetMinDamage()) / range;
return chance;
end
end
The Build() function retrieves the hit points of the two combatants. It also calcu-
lates the survival chance of the teapot and the kill chance for the opponent (player).
The survival chance is the chance that the character can survive another round. The
kill chance is the reverse of that.
Finally, with the world state built, you can apply an action and get the utility from it.
Applying an action causes the AI to apply the average effect; in other words, the AI
considers that it will be healed the average amount of hit points or will inflict the
average amount of damage. A state in which the teapot attempts to run away gives
the agent a 50% survival chance, which makes it look pretty good as a last resort
when the hit points are low. Here
'
s a sample utility function applied to a given world
state:
Search WWH ::




Custom Search