player = { name = 'Global player', health = 1000, mana = 1000, fire = function(self) self.health = self.health - 10 print(self.name, 'Health', self.health) end, } function math.randomReal(min, max) return math.random()*(max-min)+min end function math.round(value) return math.floor(value+0.5) end function math.dice(dice,side) local x = 0 for i=1,dice do x = x + math.random(1,side) end return x end function math.chance(chance) local x = math.random() if x <= chance then return true else return false end end function graph() local a = {} local x = 333 local calc local s='' for i=1, 1000 do a[i] = 0 end for i=1, x do --y = math.random(1,50) --calc = math.round(math.dice(3,y)/3) calc = math.dice(5,5) a[calc] = a[calc] + 1 end for i=1, x do if a[i]>0 then --for y=1, a[i] do s=string.rep('#',a[i]) --end print(("%d\t: %s"):format(i,s)) s='' end end return end playerEx = function(name) local health = 1000 local health_max = health local mana = 100 local mana_max = mana local armor = 10 local shield = 6 local shield_blocking = true local shield_block_chance = 0.25 local shield_mana = 20 -- fueled by mana local shield_mana_ratio = 2.0 -- 0.5 = 1 mana ochrani pred 2 dmg local evasion = 0.01 local miss = 0.03 local pain_chance = 0.5 -- pohyb nemeni, len sa prida duration do sekvencie co prebieha, aka mini-stun local pain_threshold = 12 -- dmg pod tuto hranicu bude sanca na pain deleno 4, (napr. berserk +50, alebo berserk znizi sancu?) local attribute = { str = 5, vit = 10, agi = 5, dex = 5, int = 5, pow = 5, } local resist = { normal = 0, spell = 0, fire = 0, explosive = 0, energy = 0, acid = 0, mental = 0, disease = 0, rad = 0, gas = 0, } local name = name or 'Default player name' local race = 'human' -- human, alien, undead, cyborg, robot, natural, unnatural return { resist = resist, attribute_vit = function() -- priklad funkcie ktora bude menit staty podla vitality local x = attribute['vit'] health_max = 1000 + x*5 health = health_max pain_threshold = math.round(5 + x*0.85) resist['disease'] = 0 + x*0.5 end, test = function(damage_input,ranged,weapon_type) --local weapon_type = 'energy' -- normal, heavy, energy, exotic local damage_output = damage_input*0.01 if ranged then --if weapon_type = 'normal' then -- return damage_output*attribute['dex'] --end --if weapon_type = 'heavy' then -- return damage_output*attribute['dex']*0.5 --end --if weapon_type = 'energy' then -- return damage_output*attribute['pow'] --end --if weapon_type = 'exotic' then -- return damage_output*attribute['int']*2 --end else return damage_output*attribute['str']*2 end return 0 end, fire = function(damage_input,damage_type,ranged) local damage_type = damage_type or 'normal' local ranged = ranged or false local shield_mana_calc = math.randomReal(shield_mana*0.66,shield_mana) shield_mana_calc = math.min(shield_mana_calc,mana/shield_mana_ratio) local mana_decrease = shield_mana_calc*shield_mana_ratio --math.min(shield_mana_calc,damage_input)*shield_mana_ratio --print(mana_decrease) --if mana*shield_mana_ratio < shield_mana_calc then --if mana<=0 then -- shield_mana_calc = 0 --end if mana-mana_decrease<0 then --shield_mana_calc = shield_mana_calc - (mana_decrease-mana)*shield_mana_ratio --shield_mana_calc = shield_mana_calc - mana*shield_mana_ratio mana = 0 else mana = mana - mana_decrease end if shield_mana_calc<0 then shield_mana_calc = 0 end local shield_calc = 0 local pain_threshold_bonus = 0 if shield_blocking then if math.chance(shield_block_chance) then shield_calc = 1000 -- 'chance to block' else shield_calc = math.randomReal(shield*0.5, shield) -- if blocking end else shield_calc = math.randomReal(shield*0.15, shield) -- if not blocking with shield activelly end local armor_calc = math.randomReal(armor*0.5, armor) local damage_output = math.max(damage_input - shield_mana_calc - shield_calc - armor_calc,1) local pain_bool = math.chance(pain_chance) damage_output = damage_output - (damage_output*resist[damage_type]) --if math.random() <= 0.33 then damage_output = damage_output * 1.525 end -- critical damage_output = math.max(math.floor(damage_output),1) --if damage < 1 then damage = 1 end health = health - damage_output if pain_bool then if shield_blocking then pain_threshold_bonus = shield_calc + math.dice(3,shield)/3 end -- krasne to berie do uvahy aj 'chance to block' if damage_output < (pain_threshold + pain_threshold_bonus) then pain_bool = math.chance(pain_chance/4) print(("---> Pain treshold %0.2f"):format(pain_threshold_bonus)) end end --print(name, 'Health:',health,'damage_input:',damage_input,'armor_calc:',armor_calc,'damage_output:',damage_output) print(("Name: %s\tHealth: %d\tMana: %d \tDMG_input: %d\tarmor_calc: %0.2f\tshield_calc: %0.2f\tshield_mana_calc: %0.2f\t\tDMG_output: %d\t\tPain: %s"):format(name, health, mana, damage_input, armor_calc, shield_calc, shield_mana_calc, damage_output, pain_bool)) end, } end function hitChance() local chance = 1.0 -- 80% ze trafi local result = math.random() if result <= chance then return true else return false end end math.randomseed(os.time()) dmg_base = 10 dmg_dice = 9 dmg_side = 6 local player1 = playerEx('John') local player2 = playerEx('Hrac 2') --player1.fire() --player2.fire() player1.attribute_vit() for i=1,20 do local result = math.chance(1.0) --hitChance() local dice = math.dice(dmg_dice,dmg_side) local dmg_bonus = player1.test(dmg_base+dice,false,'exotic') print(("#%d\tHit: %s\t\t\t\tDMG_input: %d+%d+%d"):format(i,result,dmg_base,dice,dmg_bonus)) -- if result then player1.fire(math.random(50,60)) end if result then player1.fire(dmg_base+dice+dmg_bonus) end end --player:fire() --player.fire(player) graph()