Posted 19 January 2016 - 03:25 PM
hi guys,
this is just a little script I have been working on for the last few hours. and a warning, im not english so don't expect perfect grammar :)/>
little backstory, I love random loot with different rarities, so that's where this idea came from.
I wanted something like that in minecraft so i thought i'll try to make something in computercraft to buy random loot with
exp levels.
there are 5 options for the amount of exp levels you want to spend. the more exp you pay the higher chance to get a higher
rarity. it works best with a 3 by 1 monitor attached.
I haven't really put any items in it just yet but the mainscript is done and i want to share it because i'm kinda proud for creating something like this withouth really any coding knowledge :P/>
the pastebin is: cghauAg0
and here is the entire script (little warning though, as I said i have no coding experience whatsoever so there are bound to be things in here that can be done way better!)
edit:
so i've updated the program to be a bit neater? also instead of defining the items in the program itself it will now create a folder with files in wich you specify the items themselves so if you add a lot of items the script will stay the same.
I am working on some basic loot files so when they are done i might implement an auto import function to the program so that you will always have some loot specified even if you don't edit them yourself
this is just a little script I have been working on for the last few hours. and a warning, im not english so don't expect perfect grammar :)/>
little backstory, I love random loot with different rarities, so that's where this idea came from.
I wanted something like that in minecraft so i thought i'll try to make something in computercraft to buy random loot with
exp levels.
there are 5 options for the amount of exp levels you want to spend. the more exp you pay the higher chance to get a higher
rarity. it works best with a 3 by 1 monitor attached.
I haven't really put any items in it just yet but the mainscript is done and i want to share it because i'm kinda proud for creating something like this withouth really any coding knowledge :P/>
the pastebin is: cghauAg0
and here is the entire script (little warning though, as I said i have no coding experience whatsoever so there are bound to be things in here that can be done way better!)
edit:
so i've updated the program to be a bit neater? also instead of defining the items in the program itself it will now create a folder with files in wich you specify the items themselves so if you add a lot of items the script will stay the same.
Spoiler
--#
--#
--#
--Specify monitor + Size
local mon = peripheral.wrap("left")
local w, h = mon.getSize()
--The range the player must be in to interact
local range = 5
--This is to stop the commandblock chat spam
commands.exec("gamerule commandBlockOutput false")
--Rarity table, a random number between 1 and 100 will be picked
--and this table will determine how rare your item will be
--For now it's empty but it will be filled later on
local rarity = {}
--Costs table, this table will determine the Exp level cost to buy
--gear, the higher the cost the better chance at rarer gear.
local costs = {5, 10, 15, 20, 25}
--This will save a table to a file, this is needed just in case
--you don't have the files so it will create them for you
local function saveTable(tb, fn)
local file = fs.open("loot/"..tostring(fn), "w")
file.write(textutils.serialize(tb))
file.close()
end
--This is the function to load the tables into the program
local function loadTable(tn, fn)
if not fs.exists("loot") then
fs.makeDir("loot")
print("Loot folder created!")
sleep(1)
end
if not fs.exists("loot/"..tostring(fn)) then
local file = fs.open("loot/"..tostring(fn), "w")
file.write(textutils.serialize(tn))
file.close()
print(tostring(fn).." file created!")
sleep(1)
end
local file = fs.open("loot/"..tostring(fn), "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
--This will create an example file in the loot folder, here you can see
--how to specify items in the loot tables.
local function createExample()
local example = {}
table.insert(example, "name(if from mod put modid:item), amount, damage {optional nbt data}")
table.insert(example, "stick 1 0 {display:{Name:Common Sword}}")
table.insert(example, "wooden_shovel 1 0 {display:{Name:Common Club?}}")
saveTable(example, "example")
end
createExample()
--This will load all of the loot tables in the program so it can be used.
--the tables are located in "loot/" and will be named properly
--in there you can specify the items for the different rarities.
local common = loadTable(common, "common")
local uncommon = loadTable(uncommon, "uncommon")
local unique = loadTable(unique, "unqiue")
local rare = loadTable(rare, "rare")
local legendary = loadTable(legendary, "legendary")
--This is the function that determines what you get and how rare it is.
local function generateLoot()
local rn = math.random(100)
if rn <= rarity[1] then
-- Common Item
local rn = math.random(#common)
commands.exec("give @p[lm="..cost..",r="..range.."] "..common[rn])
commands.exec("tellraw @p[lm="..cost..",r="..range.."] "..'["",{"text":"You received a Common Item","color":"white"}]')
commands.exec("xp -"..cost.."L @p[lm="..cost..",r="..range.."]")
elseif rn > rarity[1] and rn <= rarity[2] then
-- Uncommon Item
local rn = math.random(#uncommon)
commands.exec("give @p[lm="..cost..",r="..range.."] "..uncommon[rn])
commands.exec("tellraw @p[lm="..cost..",r="..range.."] "..'["",{"text":"You received an Uncommon Item","color":"green"}]')
commands.exec("xp -"..cost.."L @p[lm="..cost..",r="..range.."]")
elseif rn > rarity[2] and rn <= rarity[3] then
-- Unique Item
local rn = math.random(#unique)
commands.exec("give @p[lm="..cost..",r="..range.."] "..unique[rn])
commands.exec("tellraw @p[lm="..cost..",r="..range.."] "..'["",{"text":"You received an Unique Item","color":"blue"}]')
commands.exec("xp -"..cost.."L @p[lm="..cost..",r="..range.."]")
elseif rn > rarity[3] and rn <= rarity[4] then
-- Rare Item
local rn = math.random(#rare)
commands.exec("give @p[lm="..cost..",r="..range.."] "..rare[rn])
commands.exec("tellraw @p[lm="..cost..",r="..range.."] "..'["",{"text":"You received a Rare Item","color":"dark_purple"}]')
commands.exec("xp -"..cost.."L @p[lm="..cost..",r="..range.."]")
elseif rn > rarity[4] then
-- Legendary Item
local rn = math.random(#legendary)
commands.exec("give @p[lm="..cost..",r="..range.."] "..legendary[rn])
commands.exec("tellraw @p[lm="..cost..",r="..range.."] "..'["",{"text":"You received a Legendary Item","color":"gold"}]')
commands.exec("xp -"..cost.."L @p[lm="..cost..",r="..range.."]")
end
end
--Here are some functions to create the screen
local function clearScreen()
for i=1, h do
mon.setBackgroundColor(colors.black)
mon.setCursorPos(1, i)
mon.clearLine()
end
end
local function writeLine(x, y, text, color, color2)
mon.setBackgroundColor(colors.black)
mon.setCursorPos(x, y)
mon.clearLine()
mon.setBackgroundColor(color2)
mon.setTextColor(color)
mon.setCursorPos(x, y)
mon.write(text)
end
local function createButton(x, y, maxX, maxY, text, color, color2)
mon.setTextColor(color)
mon.setBackgroundColor(color2)
mon.setCursorPos(x, y)
for i=1, maxY do
mon.setCursorPos(x, y+i-1)
for j=1, maxX do
mon.write(" ")
end
end
l = string.len(text)
mon.setCursorPos(maxX/2+x-l/2, maxY/2+y)
mon.write(text)
end
--Draw the screen
clearScreen()
sleep(1)
writeLine(1, 1, "Loot Machine", colors.blue, colors.black)
createButton(2, 2, 4, 3, tostring(costs[1]), colors.white, colors.lime)
createButton(7, 2, 4, 3, tostring(costs[2]), colors.white, colors.lime)
createButton(12, 2, 4, 3, tostring(costs[3]), colors.white, colors.lime)
createButton(17, 2, 4, 3, tostring(costs[4]), colors.white, colors.lime)
createButton(22, 2, 4, 3, tostring(costs[5]), colors.white, colors.lime)
createButton(w-2, h-2, 3, 3, "X", colors.black, colors.red)
--Events
while true do
event, sside, xx, yy = os.pullEvent()
if event=="monitor_touch" then
if yy > 1 and yy < 5 then
if xx > 1 and xx < 6 then
rarity = {80,90,97,99}
cost = costs[1]
generateLoot()
commands.exec("tellraw @p[l="..(cost-1)..",r="..range.."] "..'["",{"text":"You don\'t have enough Exp!","color":"red"}]')
elseif xx > 6 and xx < 11 then
rarity = {60,80,94,98}
cost = costs[2]
generateLoot()
commands.exec("tellraw @p[l="..(cost-1)..",r="..range.."] "..'["",{"text":"You don\'t have enough Exp!","color":"red"}]')
elseif xx > 11 and xx < 16 then
rarity = {30,70,88,96}
cost = costs[3]
generateLoot()
commands.exec("tellraw @p[l="..(cost-1)..",r="..range.."] "..'["",{"text":"You don\'t have enough Exp!","color":"red"}]')
elseif xx > 16 and xx < 21 then
rarity = {10,30,60,90}
cost = costs[4]
generateLoot()
commands.exec("tellraw @p[l="..(cost-1)..",r="..range.."] "..'["",{"text":"You don\'t have enough Exp!","color":"red"}]')
elseif xx > 21 and xx < 26 then
rarity = {2,7,10,80}
cost = costs[5]
generateLoot()
commands.exec("tellraw @p[l="..(cost-1)..",r="..range.."] "..'["",{"text":"You don\'t have enough Exp!","color":"red"}]')
end
elseif yy > h-2 then
if xx > w-2 then
os.reboot()
end
end
end
end
I am working on some basic loot files so when they are done i might implement an auto import function to the program so that you will always have some loot specified even if you don't edit them yourself
Edited on 19 January 2016 - 07:49 PM