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.

Revisions

  1. sotsugov revised this gist Feb 25, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dmg_output.lua
    Original file line number Diff line number Diff line change
    @@ -111,7 +111,7 @@ function message_output(clicked_object, player, description)
    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, crit_mod)
    function (dice, upper, lower, base)
    return roll_for_damage(dice, upper, lower, base, is_targeted, player)
    end
    )
  2. sotsugov revised this gist Feb 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dmg_output.lua
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,7 @@ function roll_to_hit(player, bN)
    elseif to_hit == 1 then
    to_hit_msg = to_hit_msg .. " and critically misses"
    printToAll(to_hit_msg)
    return - 1
    return -1
    else
    to_hit_msg = to_hit_msg .. " and misses"
    printToAll(to_hit_msg)
  3. sotsugov revised this gist Feb 24, 2019. 2 changed files with 124 additions and 80 deletions.
    124 changes: 124 additions & 0 deletions dmg_output.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    -- 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, crit_mod)
    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
    80 changes: 0 additions & 80 deletions smart_dice.lua
    Original file line number Diff line number Diff line change
    @@ -1,80 +0,0 @@
    -- generating uniform random numbers before using them
    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"))
  4. sotsugov revised this gist Jan 30, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions smart_dice.lua
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    -- generating uniform random numbers before using them
    math.randomseed(os.time())
    math.random(); math.random(); math.random()

  5. sotsugov revised this gist Jan 30, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions smart_dice.lua
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    math.randomseed(os.time())
    math.random(); math.random(); math.random()

    function roll_for_damage(dice, lower, upper, base, is_targeted)
    dN = tonumber(dice)
  6. sotsugov created this gist Jan 29, 2019.
    78 changes: 78 additions & 0 deletions smart_dice.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    math.randomseed(os.time())

    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"))