Last active
February 25, 2019 11:45
-
-
Save sotsugov/0d2a109e6363d5e6f6b1a33ac795c206 to your computer and use it in GitHub Desktop.
Calculate damage output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- generating uniform random numbers before using them | |
math.randomseed(os.time()) | |
math.random(); math.random(); math.random() | |
dRoll = 0 | |
function onload(save_state) | |
if save_state != '' then | |
local sav = JSON.decode(save_state) | |
self.setDescription(sav.desc) | |
end | |
self.createButton({click_function = 'roll_button', | |
label = 'Roll', | |
function_owner = self, | |
position = {0, 0, 0}, | |
rotation = {0, 0, 0}, | |
width = 1000, | |
height = 1000, | |
font_size = 125}) | |
end | |
function roll_for_damage(dice, lower, upper, base, is_targeted, player) | |
dN = tonumber(dice) | |
bN = tonumber(base) | |
total_hits = 0 | |
result_sum = 0 | |
dmg_rolls = {} | |
message = "" | |
player = Player[player].steam_name | |
printToAll(string.rep("=", 30) .. "\n") | |
printToAll(player .. " base is " .. bN .. ", using " .. self.getName()) | |
-- if a single shot is made check if shot is targeted | |
if dN == 1 then | |
if is_targeted then | |
printToAll("targeted") | |
else | |
total_hits = roll_to_hit(player, bN) | |
end | |
else | |
for i = 1, dN, 1 do | |
total_hits = total_hits + roll_to_hit(player, bN) | |
end | |
end | |
-- sum all successful hits and calculate damage | |
for j = 1, total_hits, 1 do | |
n = math.random(lower, upper) | |
table.insert(dmg_rolls, n) | |
result_sum = result_sum + n | |
end | |
-- return message depending on the number of hits | |
-- critical miss | |
if total_hits < 0 then | |
message = player .. " critically misses" | |
-- if no shots connect | |
elseif total_hits == 0 then | |
message = player .. " misses all their shots" | |
-- hits less or equal to number of shots | |
elseif total_hits <= dN then | |
if total_hits > 1 then s = "shots" else s = "shot" end | |
message = player .. " hits " .. total_hits .. " " .. s | |
.. ", dealing " .. tostring(result_sum) .. | |
" (" .. table.concat(dmg_rolls, ", ") .. ")" .. " total damage" | |
-- criticals | |
elseif total_hits > dN then | |
message = player .. " critically hits for " .. tostring(result_sum) | |
.. " (" .. table.concat(dmg_rolls, ", ") .. ")" .. " total damage" | |
end | |
return message | |
end | |
function roll_to_hit(player, bN) | |
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 | |
if to_hit == 10 then | |
to_hit_msg = to_hit_msg .. " and critically hits" | |
printToAll(to_hit_msg) | |
return 2 | |
else | |
to_hit_msg = to_hit_msg .. " and hits" | |
printToAll(to_hit_msg) | |
return 1 | |
end | |
elseif to_hit == 1 then | |
to_hit_msg = to_hit_msg .. " and critically misses" | |
printToAll(to_hit_msg) | |
return -1 | |
else | |
to_hit_msg = to_hit_msg .. " and misses" | |
printToAll(to_hit_msg) | |
return 0 | |
end | |
end | |
function targeted_shot(bN) | |
targeted_roll = math.random(1, 12) | |
end | |
function roll_button(clicked_object, player) | |
message_output(clicked_object, player, self.getDescription()) | |
end | |
function message_output(clicked_object, player, description) | |
is_targeted = false | |
if string.find(description, "T") then is_targeted = true end | |
message = string.gsub(description, "(%d+)d%[(%d+)%-(%d+)%]b(%d+)", | |
function (dice, upper, lower, base) | |
return roll_for_damage(dice, upper, lower, base, is_targeted, player) | |
end | |
) | |
printToAll(message) | |
end | |
function onSave() | |
local data = JSON.encode({desc = self.getDescription()}) | |
return data | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment