Skip to content

Instantly share code, notes, and snippets.

@sotsugov
Last active February 25, 2019 11:45
Show Gist options
  • Save sotsugov/0d2a109e6363d5e6f6b1a33ac795c206 to your computer and use it in GitHub Desktop.
Save sotsugov/0d2a109e6363d5e6f6b1a33ac795c206 to your computer and use it in GitHub Desktop.
Calculate damage output
math.randomseed(os.time())
math.random(); math.random(); math.random()
function roll_for_damage(dice, lower, upper, base, is_targeted)
dN = tonumber(dice)
bN = tonumber(base)
message = ""
total_hits = 0
print("Player's base is " .. bN)
-- if a single shot is made check if shot is targeted
if dN == 1 then
if is_targeted then
targeted_shot()
-- otherwise just return to hit
else
to_hit_single = math.random(1, 10)
if to_hit_single >= bN then
total_hits = 1
end
end
-- if more than one shot is targeted
-- for each shot calculate to hit
else
for i = 1, dN, 1 do
to_hit_msg = "Player rolls "
to_hit = math.random(1, 10)
to_hit_msg = to_hit_msg .. tostring(to_hit)
if to_hit >= bN then
total_hits = total_hits + 1
to_hit_msg = to_hit_msg .. " and hits"
else
to_hit_msg = to_hit_msg .. " and misses"
end
print(to_hit_msg)
end
-- print(total_hits)
end
-- sum all successful hits and calculate damage
result = ''
result_sum = 0
for j = 1, total_hits, 1 do
n = math.random(lower, upper)
result = result .. n
result_sum = result_sum + n
if j < dN then
result = result .. ', '
end
end
-- print(result)
if total_hits == 0 then
message = message .. "Player misses their shot(s)"
else
message = message .. "Player hits " .. total_hits .. " shot(s)"
.. ", dealing " .. tostring(result_sum) .. " total damage"
end
return message
end
function targeted_shot(dice, faces)
print("targeted shot tbd")
end
function message_output(text)
is_targeted = false
if string.find(text, "T") then
is_targeted = true
end
message = string.gsub(text, "(%d+)d%[(%d+)%-(%d+)%]b(%d+)",
function (dice, upper, lower, base)
return roll_for_damage(dice, upper, lower, base, is_targeted)
end
)
return message
end
print(message_output("5d[10-22]b7"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment