Advertisement
Wyvern67

Jeu à la con

Jul 27th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. function clr()
  2.     scr.clear()
  3.     scr.setCursorPos(1,1)
  4. end
  5.  
  6. function getYourThings()
  7.     local luck = math.random(inventory.tool.luck,100)
  8.     if luck == 100 then
  9.         inventory.diamond = inventory.diamond+1
  10.     elseif luck >= 90 then
  11.         inventory.tin = inventory.tin+1
  12.     elseif luck >= 80 then
  13.         inventory.copper = inventory.copper+1
  14.     elseif luck >= 70 then
  15.         inventory.iron = inventory.iron+1
  16.     else
  17.         inventory.stone = inventory.stone+1
  18.     end
  19.    
  20.     displayUpdate()
  21. end
  22.  
  23. function console(text)
  24.     if sConsole then
  25.         print(tostring(text))
  26.     end
  27. end
  28.  
  29. function printStuff()
  30.     print("Stuff")
  31. end
  32.  
  33. function fileRead(dir)
  34.     local file = fs.open(dir, "r")
  35.     if file == nil then return end
  36.     local text = file.readAll()
  37.     file.close()
  38.     return text
  39. end
  40.  
  41. function save(name)
  42.     local serial = textutils.serialize(inventory)
  43.     local file = fs.open("savegames/"..name, "w")
  44.     file.write(serial)
  45.     file.close()
  46. end
  47.  
  48. function renderFile(dir)
  49.     if fs.exists(dir) then
  50.         local file = fs.open(dir, "r")
  51.         local x,y = scr.getCursorPos()
  52.         local line = ""
  53.         while line ~= nil do
  54.             scr.setCursorPos(x,y)
  55.             line = file.readLine()
  56.             scr.write(line)
  57.             y=y+1
  58.         end
  59.         file.close()
  60.         return true
  61.     else
  62.         return false
  63.     end
  64. end
  65.  
  66. function displayUpdate()
  67.     -- Displaying minerals in the inventory
  68.     scr.setCursorPos(1,1)
  69.     scr.clearLine()
  70.     scr.setTextColor(colors.gray)
  71.     scr.write("S:"..inventory.stone)
  72.     term.setTextColor(colors.lightGray)
  73.     scr.setCursorPos(0.2*scr.x,1)
  74.     scr.write("I:"..inventory.iron)
  75.     term.setTextColor(colors.orange)
  76.     scr.setCursorPos(0.4*scr.x,1)
  77.     scr.write("C:"..inventory.copper)
  78.     term.setTextColor(colors.white)
  79.     scr.setCursorPos(0.6*scr.x,1)
  80.     scr.write("T:"..inventory.tin)
  81.     term.setTextColor(colors.lightBlue)
  82.     scr.setCursorPos(0.8*scr.x,1)
  83.     scr.write("D:"..inventory.diamond)
  84.     term.setTextColor(colors.white)
  85.    
  86.     -- Displaying dwarves
  87.     for i=1,inventory.dwarves.mining do
  88.         if i<=#dwarvesX then
  89.             scr.setCursorPos(dwarvesX[i], dwarvesY[i])
  90.             renderFile("res/dwarfMining.txt")
  91.         end
  92.     end
  93.     for i=inventory.dwarves.mining,#dwarvesX do
  94.         scr.setCursorPos(dwarvesX[i], dwarvesY[i])
  95.         renderFile("res/dwarfGone.txt")
  96.     end
  97.     scr.setCursorPos(1, scr.x)
  98.     scr.clearLine()
  99.     scr.write("Dwarves: "..inventory.dwarves.mining)
  100. end
  101.  
  102. function getKeys()
  103.     while true do
  104.         local event, param1, param2, param3, param4 = os.pullEvent()
  105.         if event == "key" then
  106.             local key = param1
  107.             local char = keys.getName(key)
  108.            
  109.             if     char == "up" then
  110.                 inventory.dwarves.mining = inventory.dwarves.mining+1
  111.             elseif char == "down" then
  112.                 inventory.dwarves.mining = inventory.dwarves.mining-1
  113.             end
  114.         end
  115.     end
  116. end
  117.  
  118. function newGame(name)
  119.     inventory.stone = 0
  120.     inventory.iron = 0
  121.     inventory.copper = 0
  122.     inventory.tin = 0
  123.     inventory.diamond = 0
  124.     inventory.dwarves.mining = 1
  125.    
  126.     inventory.tool.name = "hand"
  127.     inventory.tool.speed = 1
  128.     inventory.tool.luck = 1
  129. end
  130.    
  131.  
  132. function load(name)
  133.     local file = fs.open("savegames/"..name, "r")
  134.     if file ~= nil then
  135.         local save = file.readAll()
  136.         file.close()
  137.         inventory = textutils.unserialize(save)
  138.     end
  139. end
  140.  
  141. function runEvery(delay, sFunc, param1)
  142.     if isInteger(tick/delay) then
  143.         sFunc(param1)
  144.     end
  145. end
  146.  
  147. function isInteger(x)
  148.     return math.floor(x)==x
  149. end
  150.  
  151. function main()
  152.     tick=tick+1
  153.     sleep(0.05) -- 1/20
  154.     runEvery(30, save, player) --30s
  155.     runEvery(20/inventory.dwarves.mining, getYourThings) --add new items -1s*dwarves
  156. end
  157.  
  158. scr = term
  159. scr.x, scr.y = scr.getSize()
  160. tick = 1
  161. sid = {}
  162. inventory = {}
  163. inventory.tool = {}
  164. inventory.dwarves = {}
  165. sConsole = true
  166.  
  167. dwarvesX = {29,36,38,40,42,44,46,48,50,31}
  168. dwarvesY = {18,07,07,07,07,07,07,07,07,05}
  169.  
  170. clr()
  171. scr.setCursorPos(1,10)
  172. print("Please identify")
  173. print("     yourself")
  174. scr.setCursorPos(18,1)
  175. renderFile("res/sidOpen.txt")
  176. event, player = os.pullEvent("player")
  177.  
  178. if fs.exists("savegames/"..player) then
  179.     load(player)
  180. else
  181.     newGame(player)
  182.     save(player)
  183. end
  184. print("")
  185. if sConsole then
  186.     print("Player "..player.." was loaded successfully !")
  187. else
  188.     textutils.slowPrint("Player "..player.." was loaded successfully !   ", 80)
  189. end
  190.  
  191. clr()
  192. scr.setCursorPos(20,2)
  193. renderFile("res/mountain.txt")
  194.  
  195. while true do
  196.     parallel.waitForAny(main, getKeys)
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement