Guest User

rpg

a guest
May 5th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.69 KB | None | 0 0
  1. -- Define Stuff.
  2. local playerName = "-YourName-"
  3. local xpGained = 0
  4. local xpLevel = 5
  5. local sword = "as"
  6. local armorEq = "le"
  7. local health = 20
  8. local mana = 20
  9. local armor = 0
  10.  
  11. local guySpeaking = "Tjakka5"
  12.  
  13. local allVariables = {}
  14. allVariables.playerName = playerName
  15. allVariables.xpGained = xpGained
  16. allVariables.xpLevel = xpLevel
  17. allVariables.sword = sword
  18. allVariables.armorAq = armorEq
  19. allVariables.health = health
  20. allVariables.mana = mana
  21. allVariables.armor = armor
  22.  
  23. local tableOfVariables = textulis.serialize(allVariables)
  24.  
  25.  
  26. local loadedMap = {}  --set loadedmap as empty table
  27. local loadedProps = {}
  28. local playerSpot
  29. local loadedMapName
  30.  
  31. -- testMap1.
  32.  
  33. local testMap1 =  {"[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",
  34.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  35.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  36.                    "[]", "  ", "[]", "  ", "  ", "  ", "[]", "  ", "[]",
  37.                    "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
  38.                    "[]", "  ", "[]", "  ", "  ", "  ", "[]", "  ", "[]",
  39.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  40.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  41.                    "[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",}
  42.  
  43. local testMap2 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
  44.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  45.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",      
  46.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  47.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  48.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  49.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  50.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  51.                    "[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]"}    
  52.  
  53. local testMap3 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
  54.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  55.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",      
  56.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  57.                    "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  58.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  59.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  60.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  61.                    "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}    
  62.  
  63. local testMap4 =  {"[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]",
  64.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  65.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",      
  66.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  67.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
  68.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  69.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  70.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  71.                    "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}    
  72.  
  73. local testMap5 =  {"[]", "[]", "[]", "[]", "  ", "[]", "[]", "[]", "[]",
  74.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  75.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",      
  76.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  77.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  78.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  79.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  80.                    "[]", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "[]",
  81.                    "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]", "[]"}    
  82.  
  83. function saveAll()
  84.   local file = fs.open(RPG_Game_SaveFile, "w")
  85.   file.write(tableOfVariables)
  86.   file.close()
  87. end
  88.  
  89. function gameMenu()
  90.   term.clear()
  91.   term.setCursorPos(1, 1)
  92.   print("+------------------------------------------------+")
  93.   print("| OPTIONS                                        |")
  94.   print("|                                                |")
  95.   print("|                                                |")
  96.   print("|                    Resume                      |")
  97.   print("|                    Save                        |")
  98.   print("|                    Load                        |")
  99.   print("|                    Exit                        |")
  100.   print("|                                                |")
  101.   print("+------------------------------------------------+")
  102.   inOptionScreen = 1
  103.   while inOptionScreen == 1 do
  104.     event, button, x, y = os.pullEvent("mouse_click")
  105.     if (x == 22) or (x == 23) or (x == 24) or (x == 25) or (x == 26) or (x == 27) then
  106.       if y == 5 then
  107.         inOptionScreen = 0
  108.       elseif y == 6 then
  109.         print("Save")
  110.       elseif y == 7 then
  111.         print("Load")
  112.       elseif y == 8 then
  113.         print("Exit")
  114.       end
  115.     end
  116.   end
  117.   waitOs()
  118. end
  119.  
  120. function printTalk()
  121.   term.setCursorPos(20, 11)
  122.   print("  " ..guySpeaking)
  123.   term.setCursorPos(20, 12)
  124.   print("+-----------------------------+")
  125.   term.setCursorPos(20, 13)
  126.   print("| Hey Guys!                   |")
  127.   term.setCursorPos(20, 14)
  128.   print("| This part is not quite done |")
  129.   term.setCursorPos(20, 15)
  130.   print("| but here you can see what   |")
  131.   term.setCursorPos(20, 16)
  132.   print("| the people have fun.        |")
  133.   term.setCursorPos(20, 17)
  134.   print("| Stay Tuned!                 |")
  135.   term.setCursorPos(20, 18)
  136.   print("+-----------------------------+")
  137. end
  138.  
  139. function checkStats()
  140.   if armorEq == "no" then
  141.     armor = 0
  142.   elseif armorEq == "le" then
  143.     armor = 1
  144.   end
  145. end
  146.  
  147. function printInv()
  148.   term.setCursorPos(20, 1)
  149.   print("+-----------------------------+")
  150.   term.setCursorPos(20, 2)
  151.   print("|                             |")
  152.   term.setCursorPos(20, 3)
  153.   term.write("| Sword:  ")
  154.  
  155.   if sword == "bs" then
  156.     print("Basic Sword         |")
  157.     term.setCursorPos(20, 4)
  158.     term.write("| This sword does basic damage|")
  159.  
  160.   elseif sword == "as" then
  161.     print("Advanced Sword      |")
  162.     term.setCursorPos(20, 4)
  163.     term.write("| Just a bit sharper...       |")
  164.  
  165.   elseif sword == "bk" then
  166.     print("Basic Knife         |")
  167.     term.setCursorPos(20, 4)
  168.     term.write("| Grose, it has blood on it!  |")
  169.  
  170.   elseif sword == "ls" then
  171.     print("Long Sword          |")
  172.     term.setCursorPos(20, 4)
  173.     term.write("| Distant killing!            |")
  174.   end
  175.  
  176.   term.setCursorPos(20, 5)
  177.   print("|                             |")
  178.   term.setCursorPos(20, 6)
  179.   term.write("| Armor:  ")
  180.  
  181.   if armorEq == "no" then
  182.     print("No armor equiped    |")
  183.     term.setCursorPos(20, 7)
  184.     term.write("| You're naked!               |")
  185.   elseif armorEq == "le" then
  186.     print("Leather armor       |")
  187.     term.setCursorPos(20, 7)
  188.     term.write("| Warm, cozy, weak.           |")
  189.   end
  190.  
  191.   term.setCursorPos(20, 8)
  192.   print("|                             |")
  193.   term.setCursorPos(20, 9)
  194.   print("+-----------------------------+")
  195. end
  196.    
  197. function printStats()
  198.   term.setCursorPos(1, 11)
  199.   print("  " ..playerName)
  200.   print("+----------------+")
  201.   print("|                |")
  202.   term.write("| Health:  " ..health)
  203.   if health > 9 then
  204.     print("    |")
  205.   else
  206.     print("     |")
  207.   end
  208.   term.write("| Mana:    " ..mana)
  209.   if mana > 9 then
  210.     print("    |")
  211.   else
  212.     print("     |")
  213.   end
  214.   print("| Armor:   " ..armor.. "     |")
  215.   term.write("| xpLvl:   " ..xpLevel)
  216.   if xpLevel > 9 then
  217.     print("    |")
  218.   else
  219.     print("     |")
  220.   end
  221.   print("+----------------+")
  222. end
  223.  
  224.  
  225. function loadMap(map)
  226.   for i = 1, 81 do
  227.    
  228.     if map == testMap1 then
  229.       loadedMapName = "testMap1"
  230.       loadedMap[i] = testMap1[i]
  231.       setProps(testMap1)
  232.    
  233.     elseif map == testMap2 then
  234.       loadedMapName = "testMap2"
  235.       loadedMap[i] = testMap2[i]
  236.       setProps(testMap2)
  237.    
  238.     elseif map == testMap3 then
  239.       loadedMapName = "testMap3"
  240.       loadedMap[i] = testMap3[i]
  241.       setProps(testMap3)
  242.      
  243.     elseif map == testMap4 then
  244.       loadedMapName = "testMap4"
  245.       loadedMap[i] = testMap4[i]
  246.       setProps(testMap4)
  247.    
  248.     elseif map == testMap5 then
  249.       loadedMapName = "testMap5"
  250.       loadedMap[i] = testMap5[i]
  251.       setProps(testMap)
  252.     end
  253.   end
  254. end
  255.  
  256.  
  257. function setProps(map)
  258.   for i = 1, 81 do
  259.     if map == testMap1 then
  260.       o = testMap1[i]
  261.     elseif map == testMap2 then
  262.       o = testMap2[i]
  263.     elseif map == testMap3 then
  264.       o = testMap3[i]
  265.     elseif map == testMap4 then
  266.       o = testMap4[i]
  267.     elseif map == testMap5 then
  268.       o = testMap5[i]
  269.     end  
  270.      
  271.     if o == "[]" then
  272.       loadedProps[i] = "s"
  273.     elseif o == "  " then
  274.       loadedProps[i] = "a"
  275.     elseif o == "o/" then
  276.       loadedProps[i] = "p"
  277.     end
  278.   end
  279. end
  280.                                      
  281. function printMap()
  282.   term.clear()
  283.   term.setCursorPos(1, 1)
  284.   for i = 1, 81 do
  285.     term.write("" ..loadedMap[i])
  286.     if (i == 9) or (i == 18) or (i == 27) or (i == 36) or (i == 45) or (i == 54) or (i == 63) or (i == 72) then
  287.       print("")
  288.     end                                                                          
  289.   end
  290. end
  291.  
  292. function checkPlayerPos()
  293.   for i = 1, 81 do
  294.     o = loadedProps[i]                
  295.     if o == "p" then
  296.       playerSpot = i
  297.       return(playerSpot)      
  298.     end
  299.   end
  300. end                                
  301.  
  302. function checkValidSpot(direction)
  303.   playerSpot = checkPlayerPos()
  304.   if direction == "up" then
  305.     if loadedProps[playerSpot-9] == "a" then
  306.       return(true)
  307.     else
  308.       return(false)
  309.     end
  310.   elseif direction == "down" then
  311.     if loadedProps[playerSpot+9] == "a" then
  312.       return(true)
  313.     else
  314.       return(false)      
  315.     end
  316.   elseif direction == "left"then
  317.     if loadedProps[playerSpot-1] == "a" then
  318.       return(true)
  319.     else
  320.       return(false)
  321.     end
  322.   elseif direction == "right" then
  323.     if loadedProps[playerSpot+1] == "a" then
  324.       return(true)
  325.     else
  326.       return(false)
  327.     end
  328.   end
  329. end
  330.                                                                                                                                
  331. function movePlayer(direction)
  332.   valid = checkValidSpot(direction)                                                                                                                                                                                                                                                                  
  333.   playerPos = checkPlayerPos()
  334.   if valid == true then
  335.     loadedMap[playerPos] = "  "
  336.     loadedProps[playerPos] = "a"
  337.     if direction == "up" then
  338.       loadedMap[playerPos-9] = "o/"
  339.       loadedProps[playerPos-9] = "p"
  340.     elseif direction == "down" then
  341.       loadedMap[playerPos+9] = "o/"
  342.       loadedProps[playerPos+9] = "p"
  343.     elseif direction == "left" then
  344.       loadedMap[playerPos-1] = "o/"
  345.       loadedProps[playerPos-1] = "p"
  346.     elseif direction == "right" then
  347.       loadedMap[playerPos+1] = "o/"
  348.       loadedProps[playerPos+1] = "p"
  349.     end
  350.   end
  351. end
  352.  
  353. function checkTp()
  354.   playerPos = checkPlayerPos()
  355.   if loadedMapName == "testMap1" then
  356.     if playerPos == 5 then
  357.       loadMap(testMap2)
  358.       loadedMap[68] = "o/"
  359.       loadedProps[68] = "p"
  360.     end
  361.     if playerPos == 45 then
  362.       loadMap(testMap3)
  363.       loadedMap[38] = "o/"
  364.       loadedProps[38] = "p"
  365.     end
  366.     if playerPos == 37 then
  367.       loadMap(testMap4)
  368.       loadedMap[44] = "o/"
  369.       loadedProps[44] = "p"
  370.     end
  371.     if playerPos == 77 then
  372.       loadMap(testMap5)
  373.       loadedMap[14] = "o/"
  374.       loadedProps[14] = "p"
  375.     end
  376.   end
  377.   if loadedMapName == "testMap2" then
  378.     if playerPos == 77 then
  379.       loadMap(testMap1)
  380.       loadedMap[14] = "o/"
  381.       loadedProps[14] = "p"
  382.     end
  383.   end
  384.   if loadedMapName == "testMap3" then
  385.     if playerPos == 37 then
  386.       loadMap(testMap1)
  387.       loadedMap[44] = "o/"
  388.       loadedProps[44] = "p"
  389.     end
  390.   end
  391.   if loadedMapName == "testMap4" then
  392.     if playerPos == 45 then
  393.       loadMap(testMap1)
  394.       loadedMap[38] = "o/"
  395.       loadedProps[38] = "p"
  396.     end
  397.   end
  398.   if loadedMapName == "testMap5" then
  399.     if playerPos == 5 then
  400.       loadMap(testMap1)
  401.       loadedMap[68] = "o/"
  402.       loadedProps[68] = "p"
  403.     end
  404.   end
  405. end
  406.  
  407. -- Update tick functions, Oh gawd, here is where the fun part starts.
  408.  
  409. function waitOs()
  410.   os.startTimer(0.15)
  411.   event, t, s = os.pullEvent()
  412.   if event == "mouse_click" then
  413.     local mouseX = t
  414.     local mouseY = s
  415.     os.pullEvent("timer")
  416.   elseif event == "key" then
  417.     pressedKey = t
  418.     if (pressedKey == 200) or (pressedKey == 17) then
  419.       movePlayer("up")
  420.     elseif (pressedKey == 203) or (pressedKey == 30) then
  421.       movePlayer("left")
  422.     elseif (pressedKey == 208) or (pressedKey == 31) then
  423.       movePlayer("down")
  424.     elseif (pressedKey == 205) or (pressedKey == 32) then
  425.       movePlayer("right")
  426.    
  427.     elseif pressedKey == 28 then
  428.       gameMenu()
  429.     end
  430.    
  431.     os.pullEvent("timer")
  432.   end
  433.   checkTp()
  434.   printMap()
  435.   checkStats()
  436.   printStats()
  437.   printInv()
  438.   printTalk()
  439.   waitOs()
  440. end  
  441.  
  442.  
  443. -- Function for middlePrinting, as many suggested...
  444. w, h = term.getSize()
  445.  
  446. function centerPrint(y, text)
  447.   term.setCursorPos(w/2 - #text/2, y)
  448.   write(text)
  449. end
  450.    
  451. -- Startup Stuff
  452. local gotPressKeyStartScreen
  453.  
  454. print("Baaad")
  455. loadMap(testMap1)
  456. loadedMap[68] = "o/"
  457. loadedProps[68] = "p"
  458. waitOs()
Advertisement
Add Comment
Please, Sign In to add comment