Fix randomization of new state not working as intended

This commit is contained in:
sapier 2013-11-28 08:48:58 +01:00
parent f96188e06a
commit 529fea79b3

View File

@ -186,6 +186,9 @@ function mob_state.callback(entity,now,dstep)
end
end
dbg_mobf.mob_state_lvl2("MOBF: " .. entity.data.name .. " "
.. dump(#state_table) .. " states do pass pecondition check ")
--try to get a random state to change to
for i=1, #state_table, 1 do
@ -200,12 +203,22 @@ function mob_state.callback(entity,now,dstep)
end
end
if math.random() < current_chance then
local random_value = math.random()
if random_value < current_chance then
dbg_mobf.mob_state_lvl2("MOBF: " .. entity.data.name
.. " switching to state " .. state_table[rand_state].name)
mob_state.change_state(entity,state_table[rand_state])
return true
else
dbg_mobf.mob_state_lvl2("MOBF: " .. entity.data.name
.. " not switching to state " .. state_table[rand_state].name
.. " rand was: " .. random_value)
table.remove(state_table,rand_state)
end
end
dbg_mobf.mob_state_lvl2("MOBF: " .. entity.data.name
.. " no specific state selected switching to default state ")
--switch to default state (only reached if no change has been done
mob_state.change_state(entity,mob_state.get_state_by_name(entity,"default"))
else