Skip to content

Instantly share code, notes, and snippets.

@ernierasta

ernierasta/AI.lua

Created Aug 23, 2018
Embed
What would you like to do?
testing Sulis AI possibilities
function find_closest_target(parent, targets)
closest_dist = 1000
closest_target = nil
for i = 1, #targets do
target = targets[i]
dist = parent:dist_to_entity(target)
if dist < closest_dist then
closest_dist = dist
closest_target = target
end
end
game:log("attacker: " .. parent:name() .. " enemy: " .. closest_target:name() .. ", level: " .. closest_target:stats().
level .. ", base class: " .. closest_target:base_class())
return closest_target
end
-- Find weakest char
function find_weakest_target(parent, targets)
weakest_target = nil
smallest_hp = 10000
for i = 1, #targets do
--find weakest
health = targets[i]:stats().current_hp
if health < smallest_hp then
smallest_hp = health
weakest_target = targets[i]
end
end
game:log("attacker: " .. parent:name() .. " enemy: " .. weakest_target:name() .. ", level: " .. weakest_target:stat
s().level .. ", base class: " .. weakest_target:base_class())
return weakest_target
end
-- Find most wounded (less then 30% hp)
function find_wounded_target(parent, targets)
best_frac = 1.0
best_target = nil
for i = 1, #targets do
stats = targets[i]:stats()
frac = stats.current_hp / stats.max_hp
if frac < best_frac then
best_frac = frac
best_target = targets[i]
end
end
if best_frac < 0.3 then
game:log("attacker: " .. parent:name() .. " enemy: " .. best_target:name() .. ", level: " .. best_target:st
ats().level .. ", base class: " .. best_target:base_class())
return best_target
else
return nil
end
end
-- find target dispatcher
function find_target(parent, targets)
wounded = find_wounded_target(parent, targets)
if wounded ~= nil then
game:log("found wounded!")
return wounded
end
math.randomseed(os.time())
r = math.random()
if r <= 0.33 then
game:log("going for closest target!")
return find_closest_target(parent, targets)
elseif (r > 0.33) and (r <= 0.44) then
-- get random target! :-)
math.randomseed(os.time())
t = math.random(1,#targets)
game:log("fiha! random target: " .. targets[t]:name())
return targets[t]
else
weakest = find_weakest_target(parent, targets)
dist = parent:dist_to_entity(weakest)
visibility_dist = parent:vis_dist() - 2
game:log("vis:" .. visibility_dist)
-- if target is visible and relatively close, then go for it
if dist > visibility_dist then
game:log("too far, not weakest, but closest target!")
return find_closest_target(parent, targets)
else
game:log("going for weakest target!")
return weakest
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment