Advertisement
billysback

Pixel Boy Test/Beta thingy

May 31st, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.01 KB | None | 0 0
  1. --[[
  2.     Story ideas;
  3.         you are a child, in a house you have no recollection of being sent to
  4.         this person, the narrator, is unknown to you but they seem to be perfectly aquainted with you
  5.         the narrator speaks often of the "master", who is presumably the owner of this house
  6.         you attempt to get out, sometimes despite the narrators anger
  7.         it is made apparent that the narrator cannot directly harm you though he is not willing to let you out
  8.         you learn of your parents deaths and manage to escape the main house in to the basement
  9.         you find your sister just before and she becomes the new dominant narrator
  10.         she descrbies what happens and how these people were their relatives, looking for their wealth
  11.         after you began trying to escape, and succeding, your sister overheard them and also escaped then hid
  12.         because of their anger at losing them two their captors killed their parents
  13.         your sister starts blaming herself/you for your parents death
  14. ]]
  15.  
  16. local itemList = {
  17.     ["name"] = {
  18.         --item format ([item.colors], {item.info}, {info.image}, {item.size})
  19.     },
  20.     ["key"] = {
  21.         ["info"] = "Standard Key",
  22.         ["image"] = {
  23.             "<+>",
  24.             " | ",
  25.             " :="
  26.         },
  27.         ["size"] = 3,
  28.     },
  29. }
  30.  
  31. --[[
  32.      __  __               _____        __      
  33.     |  \/  |             |_   _|      / _|    
  34.     | \  / | __ _ _ __     | |  _ __ | |_ ___  
  35.     | |\/| |/ _` | '_ \    | | | '_ \|  _/ _ \
  36.     | |  | | (_| | |_) |  _| |_| | | | || (_) |
  37.     |_|  |_|\__,_| .__/  |_____|_| |_|_| \___/
  38.                  | |                          
  39.                  |_|            
  40. ]]
  41.  
  42.  
  43. local map1 = {
  44. "---------------",
  45. "-++-+++++------",
  46. "-++++-+++*-@---",
  47. "-++-+------+---",
  48. "----++++++++---",
  49. "-:-----#---#---",
  50. "-++++++++-+++:-",
  51. "-+----+++-+++--",
  52. "-+++*--++------",
  53. "--------------"
  54. }
  55. map1.pos = {2,3}
  56. map1.vision = 4
  57. map1.convert = {
  58.  ["-"] = 0,
  59.  ["+"] = 1,
  60.  ["#"] = 2,
  61.  ["*"] = 3,
  62.  [":"] = 4,
  63.  ["@"] = 5,
  64.  ["~"] = 6,
  65.  ["0"] = 0,
  66.  ["1"] = 1,
  67.  ["2"] = 2,
  68.  ["3"] = 3,
  69.  ["4"] = 4,
  70.  ["5"] = 5,
  71.  ["~"] = 6
  72. }
  73. map1.texts = {
  74. {{4,3}, " Welcome to your new home! Hopefully you find it... comfortable. Please, do explore!"},
  75. }
  76. map1.keys = {
  77. {{10,3}, {8,6}, 1},
  78. }
  79. map1.ikeys = {
  80. {{5,9}, {12,6}, 1, itemList["key"].info},
  81. }
  82. map1.items = {
  83. {{12,3}, itemList["key"]},
  84. }
  85.  
  86.  
  87. local map2 = {
  88. "---------------",
  89. "-:+++--+++++---",
  90. "--++++-+-+++---",
  91. "-----+++-*++---",
  92. "--+--#--+------",
  93. "--+++++++++++--",
  94. "-----+-----*+--",
  95. "-+++++++-------",
  96. "-+++-++++++----",
  97. "---------------"
  98. }
  99. map2.pos = {11,9}
  100. map2.vision = 4
  101. map2.convert = {
  102.  ["-"] = 0,
  103.  ["+"] = 1,
  104.  ["#"] = 2,
  105.  ["*"] = 3,
  106.  [":"] = 4,
  107.  ["@"] = 5,
  108.  ["~"] = 6,
  109.  ["0"] = 0,
  110.  ["1"] = 1,
  111.  ["2"] = 2,
  112.  ["3"] = 3,
  113.  ["4"] = 4,
  114.  ["5"] = 5,
  115.  ["6"] = 6
  116.  }
  117. map2.texts = {
  118. }
  119. map2.keys = {
  120. {{12,7}, {6,5}, 1},
  121. {{10,4}, {6,5}, 1}
  122. }
  123. map2.items = {
  124. }
  125.  
  126.  
  127. map2.tele = {
  128. {{2,2}, map1, {2,7}}
  129. }
  130.  
  131. map1.tele = {
  132. {{2,6}, map2, {3,2}},
  133. }
  134.  
  135.  
  136. --[[
  137.       _____                         _____          _      
  138.      / ____|                       / ____|        | |    
  139.     | |  __  __ _ _ __ ___   ___  | |     ___   __| | ___
  140.     | | |_ |/ _` | '_ ` _ \ / _ \ | |    / _ \ / _` |/ _ \
  141.     | |__| | (_| | | | | | |  __/ | |___| (_) | (_| |  __/
  142.      \_____|\__,_|_| |_| |_|\___|  \_____\___/ \__,_|\___|
  143.                                                      
  144. ]]
  145.  
  146. local function split(str, pat)
  147.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  148.     if str ~= nil then
  149.        local fpat = "(.-)" .. pat
  150.        local last_end = 1
  151.        local s, e, cap = str:find(fpat, 1)
  152.        while s do
  153.           if s ~= 1 or cap ~= "" then
  154.          table.insert(t,cap)
  155.           end
  156.           last_end = e+1
  157.           s, e, cap = str:find(fpat, last_end)
  158.        end
  159.        if last_end <= #str then
  160.           cap = str:sub(last_end)
  161.           table.insert(t, cap)
  162.        end
  163.     else
  164.         print("##ERROR failed to split ["..str.."] by:"..pat)
  165.     end
  166.     return t
  167. end
  168.  
  169. local function loadMap(maptab, o_pos)
  170.     local map = {}
  171.     for y=1,#maptab do
  172.         local line = maptab[y]
  173.         for x=1,string.len(line) do
  174.             local char = string.sub(line, x, x)
  175.             if map[x] == nil then map[x] = {} end
  176.             map[x][y] = {maptab.convert[char]}
  177.             if map[x][y][1] == nil then
  178.                 map[x][y] = {0}
  179.                 --map[x][y].textbox = nil
  180.             end
  181.         end
  182.     end
  183.     map.width = string.len(maptab[1])
  184.     map.height = #maptab
  185.     map.pos = maptab.pos
  186.     if o_pos ~= nil then map.pos = o_pos end
  187.     map.vision = maptab.vision
  188.     map.template = maptab
  189.    
  190.     if maptab.texts ~= nil then
  191.         for i=1,#maptab.texts do
  192.             if maptab.texts[i] ~= nil then
  193.                 local info = maptab.texts[i]
  194.                 local pos = info[1]
  195.                 local text = info[2]
  196.                 if map[pos[1]] ~= nil then
  197.                     if map[pos[1]][pos[2]] ~= nil then
  198.                         local block = map[pos[1]][pos[2]]
  199.                         if block.textbox == nil then block.textbox = {{text, i}} else block.textbox[#block.textbox + 1] = {text, i} end
  200.                         print("Textbox created at "..pos[1]..","..pos[2].." which says:")
  201.                         print("'"..text.."'")
  202.                         sleep(0.5)
  203.                     end
  204.                 end
  205.                 --sleep(3)
  206.             end
  207.         end
  208.     end
  209.     if maptab.tele ~= nil then
  210.         for i=1,#maptab.tele do
  211.             if maptab.tele[i] ~= nil then
  212.                 local info = maptab.tele[i]
  213.                 local pos = info[1]
  214.                 local nmap = info[2]
  215.                 local npos = info[3]
  216.                 if map[pos[1]] ~= nil then
  217.                     if map[pos[1]][pos[2]] ~= nil then
  218.                         local block = map[pos[1]][pos[2]]
  219.                         if block.tele == nil then block.tele = {{nmap, npos}} else block.tele[#block.tele + 1] = {nmap, npos} end
  220.                     end
  221.                 end
  222.             end
  223.         end
  224.     end
  225.     if maptab.keys ~= nil then
  226.         for i=1,#maptab.keys do
  227.             if maptab.keys[i] ~= nil then
  228.                 local info = maptab.keys[i]
  229.                 local pos1 = info[1]
  230.                 local pos2 = info[2]
  231.                 local ntyp = info[3]
  232.                 if map[pos1[1]] ~= nil then
  233.                     if map[pos1[1]][pos1[2]] ~= nil then
  234.                         if map[pos2[1]] ~= nil then
  235.                             if map[pos2[1]][pos2[2]] ~= nil then
  236.                                 local block = map[pos1[1]][pos1[2]]
  237.                                 if block.key == nil then block.key = {{pos2, ntyp}} else block.key[#block.key + 1] = {pos2, ntyp} end
  238.                             end
  239.                         end
  240.                     end
  241.                 end
  242.             end
  243.         end
  244.     end
  245.     if maptab.ikeys ~= nil then
  246.         for i=1,#maptab.ikeys do
  247.             if maptab.keys[i] ~= nil then
  248.                 local info = maptab.ikeys[i]
  249.                 local pos1 = info[1]
  250.                 local pos2 = info[2]
  251.                 local ntyp = info[3]
  252.                 local item = info[4]
  253.                 if map[pos1[1]] ~= nil then
  254.                     if map[pos1[1]][pos1[2]] ~= nil then
  255.                         if map[pos2[1]] ~= nil then
  256.                             if map[pos2[1]][pos2[2]] ~= nil then
  257.                                 local block = map[pos1[1]][pos1[2]]
  258.                                 if block.ikey == nil then block.ikey = {{pos2, ntyp, item}} else block.ikey[#block.ikey + 1] = {pos2, ntyp, item} end
  259.                             end
  260.                         end
  261.                     end
  262.                 end
  263.             end
  264.         end
  265.     end
  266.     if maptab.items ~= nil then
  267.         for i=1,#maptab.items do
  268.             if maptab.items[i] ~= nil then
  269.                 local info = maptab.items[i]
  270.                 local pos = info[1]
  271.                 local item = info[2]
  272.                 if map[pos[1]] ~= nil then
  273.                     if map[pos[1]][pos[2]] ~= nil then
  274.                         local block = map[pos[1]][pos[2]]
  275.                         if block.item == nil then block.item = {{item}} else block.item[#block.item + 1] = {item} end
  276.                     end
  277.                 end
  278.             end
  279.         end
  280.     end
  281.     return map
  282. end
  283. --[[
  284. local function genMap(width, height, rooms, coris, size)
  285.     local map = {}
  286.     map.width = width
  287.     map.height = height
  288.     map.pos = nil
  289.     for x=1,width do
  290.         map[x] = {}
  291.         for y=1,height do
  292.             map[x][y] = {0}
  293.             if y == 1 or y == height or x == 1 or x == width then map[x][y] = {1} end
  294.         end
  295.     end
  296.     local pos = {math.random(width-2)+1, math.random(height-2)+1}
  297.     local looping = true
  298.     for i=1,rooms do
  299.         if looping then
  300.             local size = math.random(size)+1
  301.             for x=pos[1]-size,pos[2]+size do
  302.                 for y=pos[2]-size,pos[2]+size do
  303.                     if x > 1 and x < width and y > 1 and y < height then
  304.                         map[x][y] = {1}
  305.                         if map.pos == nil then map.pos = {x, y} end
  306.                     end
  307.                 end
  308.             end
  309.             length = math.random(4)+(size*2)
  310.             local dirs = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} }
  311.             local dir = nil
  312.             for i=1,#dirs do
  313.                 if dir == nil then
  314.                     local n = math.random(#dirs)
  315.                     local cdir = dirs[n]
  316.                     --print("pos = "..type(pos))
  317.                     --print("cdir = "..type(cdir))
  318.                     local npos = {pos[1] + (cdir[1]*length), pos[2] + (cdir[2]*length)}
  319.                     if npos[1] > 1 and npos[1] < width and npos[2] > 1 and npos[2] < height then
  320.                         dir = cdir
  321.                     else
  322.                         ndirs = {}
  323.                         for i=1,#dirs do
  324.                             if i ~= n then ndirs[#ndirs + 1] = dirs[i] end
  325.                         end
  326.                         dirs = ndirs
  327.                     end
  328.                 end
  329.             end
  330.             if dir ~= nil then
  331.                 pos = {pos[1] + ((dir[1]*length)+size), pos[2] + ((dir[2]*length)+size)}
  332.             else
  333.                 pos = {math.random(width-2)+1, math.random(height-2)+1}
  334.                 --looping = false
  335.             end
  336.         end
  337.     end
  338.     return map
  339. end
  340. ]]
  341.  
  342. local function getVision(radius, map)
  343.     local position = map.pos
  344.     local vision = {}
  345.     vision.min = {position[1]-radius, position[2]-radius}
  346.     vision.max = {position[1]+radius, position[2]+radius}
  347.     -- (x-a)^2 + (y-B)^2 = r^2 (formula, where r is radius, x and y are coordinates and a and b is center)
  348.     local r2 = radius^2
  349.     for x=position[1]-radius, position[1]+radius do
  350.         for y=position[2]-radius, position[2]+radius do
  351.             if x > 0 and x <= map.width and y > 0 and y <= map.height then
  352.                 local xa = x-position[1]
  353.                 local yb = y-position[2]
  354.                 xa = xa^2
  355.                 yb = yb^2
  356.                 local cr2 = xa + yb
  357.                 if cr2 <= r2 then
  358.                     if vision[x] == nil then vision[x] = {} end
  359.                     vision[x][y] = {map[x][y][1], false}
  360.                     if cr2 == r2 then vision[x][y][2] = true end
  361.                 end
  362.             end
  363.         end
  364.     end
  365.     local done = {}
  366.     local temp = {{map.pos[1], map.pos[2], 0, false}}
  367.     while #temp > 0 do
  368.         local ntemp = {}
  369.         for i=1,#temp do
  370.             local x = temp[i][1]
  371.             local y = temp[i][2]
  372.             local n = temp[i][3]
  373.             local sl = temp[i][4]
  374.            
  375.             local dtyp = map[x][y][1]
  376.             if sl == false then
  377.                 for o=-1,1,2 do
  378.                     local nposs = {{x+o, y}, {x, y+o}}
  379.                     for j=1,#nposs do
  380.                         local npos = nposs[j]
  381.                         if map[npos[1]] ~= nil then
  382.                             if map[npos[1]][npos[2]] ~= nil then
  383.                                 local typ = map[npos[1]][npos[2]][1]
  384.                                 if typ == 1 or typ == 2 or typ == 3 or typ == 4 or typ == 5 or typ == 6 then
  385.                                     local ok = false
  386.                                     local stopLoop = false
  387.                                     if typ == 6 then stopLoop = true end
  388.                                     if done[npos[1]] == nil then
  389.                                         ok = true
  390.                                     else
  391.                                         if done[npos[1]][npos[2]] == nil then
  392.                                             ok = true
  393.                                         end
  394.                                     end
  395.                                     if ok then
  396.                                         if n+1 <= radius then
  397.                                             ntemp[#ntemp + 1] = {npos[1], npos[2], n+1, stopLoop}
  398.                                         end
  399.                                     end
  400.                                 end
  401.                             end
  402.                         end
  403.                     end
  404.                 end
  405.             end
  406.             if done[x] == nil then done[x] = {} end
  407.             done[x][y] = {dtyp, false, n}
  408.         end
  409.         temp = ntemp
  410.     end
  411.     done.pos = map.pos
  412.     done.min = {position[1]-radius, position[2]-radius}
  413.     done.max = {position[1]+radius, position[2]+radius}
  414.     done.radius = radius
  415.     done.text = map.text
  416.     return done
  417. end
  418.  
  419. local function draw(vision, screen, pos)
  420.     term.setBackgroundColor(colors.black)
  421.     term.setTextColor(colors.white)
  422.     term.clear()
  423.     term.setCursorPos(1,1)
  424.     print(pos[1]..", "..pos[2])
  425.     for x=pos[1]-math.floor(screen[1]/2), pos[1]+math.floor(screen[1]/2) do
  426.         for y=pos[2]-math.floor(screen[2]/2), pos[2]+math.floor(screen[2]/2) do
  427.             --print("(TEST) x:"..x.." ("..pos[1]..")  y:"..y.." ("..pos[2]..")")
  428.             if vision[x] ~= nil then
  429.                 if vision[x][y] ~= nil then
  430.                     --print("x:"..x.."  y:"..y)
  431.                     local i = vision[x][y][1]
  432.                     local nr = vision[x][y][3]
  433.                     local pColor = colors.white
  434.                     local sColor = colors.lightBlue
  435.                     local dColor = colors.brown
  436.                     local hdColor = colors.black
  437.                    
  438.                     if nr == 1 then
  439.                         hdColor = colors.black
  440.                     end
  441.                    
  442.                     if nr == vision.radius then
  443.                         pColor = colors.gray
  444.                         sColor = colors.blue
  445.                         dColor = colors.black
  446.                     end
  447.                    
  448.                     if nr == vision.radius-1 then
  449.                         pColor = colors.lightGray
  450.                         sColor = colors.blue
  451.                     end
  452.                    
  453.                    
  454.                     local text = " "
  455.                     if vision.pos[1] == x and vision.pos[2] == y then text = "+" end
  456.                     if i == 0 then
  457.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  458.                         term.setBackgroundColor(colors.black)
  459.                         term.setTextColor(colors.green)
  460.                         if text == " " then
  461.                             text = ","
  462.                             term.setTextColor(colors.white)
  463.                         end
  464.                         term.write(text)
  465.                     elseif i == 1 then
  466.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  467.                         term.setBackgroundColor(pColor)
  468.                         term.setTextColor(colors.green)
  469.                         term.write(text)
  470.                     elseif i == 2 then
  471.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  472.                         term.setBackgroundColor(pColor)
  473.                         term.setTextColor(colors.green)
  474.                         if text == " " then
  475.                             text = "#"
  476.                             term.setTextColor(sColor)
  477.                         end
  478.                         term.write(text)
  479.                     elseif i == 3 then
  480.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  481.                         term.setBackgroundColor(pColor)
  482.                         term.setTextColor(colors.green)
  483.                         if text == " " then
  484.                             text = "*"
  485.                             term.setTextColor(sColor)
  486.                         end
  487.                         term.write(text)
  488.                     elseif i == 4 then
  489.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  490.                         term.setBackgroundColor(pColor)
  491.                         term.setTextColor(colors.green)
  492.                         if text == " " then
  493.                             text = ":"
  494.                             term.setTextColor(dColor)
  495.                         end
  496.                         term.write(text)
  497.                     elseif i == 5 then
  498.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  499.                         term.setBackgroundColor(pColor)
  500.                         term.setTextColor(colors.green)
  501.                         if text == " " then
  502.                             text = "@"
  503.                             term.setTextColor(sColor)
  504.                         end
  505.                         term.write(text)
  506.                     elseif i == 6 then
  507.                         term.setCursorPos(x-(pos[1]-(screen[1]/2))+1, y-(pos[2]-(screen[2]/2))+1)
  508.                         term.setBackgroundColor(colors.black)
  509.                         term.setTextColor(colors.green)
  510.                         if text == " " then
  511.                             text = "."
  512.                             term.setTextColor(hdColor)
  513.                         end
  514.                         term.write(text)
  515.                     end
  516.                 end
  517.             end
  518.         end
  519.     end
  520.     term.setCursorPos(1, screen[2]-3)
  521.     term.setBackgroundColor(colors.black)
  522.     term.setTextColor(colors.white)
  523.     term.write(vision.text)
  524. end
  525.  
  526. --hp, ad, attack_speed, dodge
  527. local stats = {
  528.     ["zombie"] = {5, 2, 1, 5}
  529. }
  530.  
  531. local mob_keys = {}
  532. local function genMobKey()
  533.    
  534.     local key = tostring(math.random(9999))
  535.     while string.len(key) < 4 do
  536.         key = "0"..key
  537.     end
  538.    
  539.     local x = 0
  540.     while mob_keys[key] == nil and x < 100 do
  541.         local key = tostring(math.random(9999))
  542.         while string.len(key) < 4 do
  543.             key = "0"..key
  544.         end
  545.         x = x + 1
  546.     end
  547.     if x >= 100 then return nil else return key end
  548. end
  549.  
  550. local function genLoc(map, rempty)
  551.     local blocks = {}
  552.     for x=1,#map.width do
  553.         for y=1,#map.height do
  554.             local block = map[x][y]
  555.             if block[1] == 1 then
  556.                 if (rempty == true and block[2] == nil) or rempty == false then
  557.                     blocks[#blocks + 1] = {x, y}
  558.                 end
  559.             end
  560.         end
  561.     end
  562.     local block = blocks[math.random(#blocks)]
  563.     return block
  564. end
  565.  
  566. local function isValidMobType(mobtype)
  567.     return stats[mobtype] ~= nil
  568. end
  569.  
  570. local function createMob(mobtype, x, y)
  571.     local mob = {}
  572.     local stat = stats[mobtype]
  573.     mob.health = stat[1]
  574.     mob.attack = stat[2]
  575.     mob.attack_speed = stat[3]
  576.     mob.dodge = stat[4]
  577.     mob.loc = {x, y}
  578.     local key = genMobKey()
  579.     mob.key = key
  580.     if mob.key == nil then mob = nil end
  581.     return mob
  582. end
  583.  
  584. local function genMob(map, mobtype)
  585.     if map.mob_n < map.max_mob_n and isValidMobType(mobtype) then
  586.         local loc = genLoc(map, true)
  587.         local mob = createMob(mobtype, loc[1], loc[2])
  588.         map[loc[1]][loc[2]][2] = mob
  589.         map.mob_n = map.mob.n + 1
  590.         map.mobs[mob.key] = mob
  591.     end
  592. end
  593.  
  594. --[[
  595. colors:
  596.     colors.nBack = normal background colour
  597.     colors.nBackLit = normal background colour when item box is lit up
  598.     colors.nText = normal text colour
  599.     colors.nTextLit = normal text colour when lit
  600.     colors.sBack = side (border of item box) background colour
  601.     colors.sBackLit = side (border of item box) background colour when item box it lit up
  602.     colors.sText = side text colour
  603.     colors.sTextLit = side text colour when lit
  604. ]]
  605. dColorSch = {}
  606. dColorSch.nBack = colors.lightGray
  607. dColorSch.nBackLit = colors.white
  608. dColorSch.nText = colors.black
  609. dColorSch.nTextLit = colors.gray
  610. dColorSch.sBack = colors.gray
  611. dColorSch.sBackLit = colors.lightGray
  612. dColorSch.sText = colors.lightGray
  613. dColorSch.sTextLit = colors.white
  614.  
  615. local function createItemBox(size, colors, pos, info)
  616.     local itembox = {}
  617.     for x=1,size do
  618.         itembox[x] = {}
  619.         for y=1,size do
  620.             local tile = {}
  621.             --[[if x == 1 or x == size or y == 1 or y == size then
  622.                 tile.background = colors.sBack
  623.                 tile.text = colors.sText
  624.                 tile.lit = colors.sBackLit
  625.                 tile.litText = colors.sTextLit
  626.             else ]]
  627.                 tile.background = colors.nBack
  628.                 tile.text = colors.nText
  629.                 tile.lit = colors.nBackLit
  630.                 tile.litText = colors.nTextLit
  631.             --end
  632.             tile.char = " "
  633.             itembox[x][y] = tile
  634.         end
  635.     end
  636.     itembox.lit = false
  637.     itembox.pos = pos
  638.     itembox.size = size
  639.     itembox.info = info
  640.     itembox.colors = colors
  641.     itembox.draw = function(self)
  642.         for x=1,#self do
  643.             local tab = self[x]
  644.             for y=1,#tab do
  645.                 local tile = self[x][y]
  646.                 local bColor = tile.background
  647.                 local tColor = tile.text
  648.                 if self.lit == true then
  649.                     bColor = tile.lit
  650.                     tColor = tile.litText
  651.                 end
  652.                 term.setBackgroundColor(bColor)
  653.                 term.setTextColor(tColor)
  654.                 term.setCursorPos(self.pos[1]+x-1, self.pos[2]+y-1)
  655.                 term.write(tile.char)
  656.             end
  657.         end
  658.     end
  659.     itembox.setImage = function(self, image)
  660.         for y=1,#image do
  661.             local line = image[y]
  662.             for x=1,string.len(line) do
  663.                 if self[x] ~= nil then
  664.                     if self[x][y] ~= nil then
  665.                         local char = line:sub(x, x)
  666.                         self[x][y].char = char
  667.                     end
  668.                 end
  669.             end
  670.         end
  671.     end
  672.     itembox.setItem = function(self, item)
  673.         --item format ([item.colors], {item.info}, {info.image}, {item.size})
  674.         self.colors = item.colors
  675.         self.info = item.info
  676.         self.size = item.size
  677.         self:setImage(item.image)
  678.     end
  679.     return itembox
  680. end
  681.  
  682. local function createTextBox(text, screen)
  683.     local textbox = {}
  684.     textbox.screen = screen
  685.     local length = screen[1]
  686.     local splt = split(text, " ")
  687.     local lines = {}
  688.     local cur = ""
  689.     for i=1,#splt do
  690.         if string.len(cur.." "..splt[i]) <= length then
  691.             cur = cur.." "..splt[i]
  692.         else
  693.             lines[#lines + 1] = cur
  694.             cur = splt[i]
  695.         end
  696.     end
  697.     if cur ~= "" then lines[#lines + 1] = cur end
  698.     textbox.lines = lines
  699.    
  700.     textbox.cur = 1
  701.     textbox.draw = function(self)
  702.         local empty = ""
  703.         for i=1,self.screen[1] do empty = empty.." " end
  704.         local line1 = self.lines[self.cur]
  705.         local line2 = self.lines[self.cur+1]
  706.         term.setBackgroundColor(colors.white)
  707.         term.setTextColor(colors.black)
  708.         term.setCursorPos(1, self.screen[2]-1)
  709.         term.write(empty)
  710.         if line1 ~= nil then
  711.             term.setCursorPos(1, self.screen[2]-1)
  712.             term.write(line1)
  713.         end
  714.         term.setCursorPos(1, self.screen[2])
  715.         term.write(empty)
  716.         if line2 ~= nil then
  717.             term.setCursorPos(1, self.screen[2])
  718.             term.write(line2)
  719.         end
  720.     end
  721.     return textbox
  722. end
  723.  
  724. local sw, sh = term.getSize()
  725. local screen = {sw, sh}
  726.  
  727. local function move(map, x, y)
  728.     local pos = {map.pos[1], map.pos[2]}
  729.     if pos[1] + x > 0 and pos[1] + x <= map.width and pos[2] + y > 0 and pos[2] + y <= map.height then
  730.         if map[pos[1]+x][pos[2]+y][1] == 1 or map[pos[1]+x][pos[2]+y][1] == 3 or map[pos[1]+x][pos[2]+y][1] == 4 or map[pos[1]+x][pos[2]+y][1] == 5 or map[pos[1]+x][pos[2]+y][1] == 6 then
  731.             map.pos = {pos[1]+x, pos[2]+y}
  732.             local block = map[map.pos[1]][map.pos[2]]
  733.             if block ~= nil then
  734.                 if block.textbox ~= nil then
  735.                     for i=1,#block.textbox do
  736.                         map.textbox = createTextBox(block.textbox[i][1], screen)
  737.                         --print("Textbox found at "..map.pos[1]..","..map.pos[2])
  738.                         if map.template.texts ~= nil then
  739.                             local i = block.textbox[i][2]
  740.                             local old_i = #map.template.texts
  741.                             --print("Template text index = "..i)
  742.                             local ntab = {}
  743.                             for j=1,#map.template.texts do
  744.                                 if j ~= i then ntab[#ntab + 1] = map.template.texts[j] end
  745.                             end
  746.                             map.template.texts = ntab
  747.                             --print("There are now "..#map.template.texts.." template instances, down from "..old_i)
  748.                            
  749.                         end
  750.                         --sleep(3)
  751.                     end
  752.                     block.textbox = nil
  753.                 end
  754.                 if block.tele ~= nil then
  755.                     return true, block.tele[1]
  756.                 end
  757.                 if block.key ~= nil then
  758.                     for i=1,#block.key do
  759.                         local info = block.key[i]
  760.                         local opos = info[1]
  761.                         local ntyp = info[2]
  762.                         local block = map[opos[1]][opos[2]]
  763.                         block[1] = ntyp
  764.                         local line = map.template[opos[2]]
  765.                         local nline = line:sub(1, opos[1]-1)
  766.                         nline = nline..ntyp..line:sub(opos[1]+1)
  767.                         map.template[opos[2]] = nline
  768.                     end
  769.                 end
  770.                 if block.ikey ~= nil then
  771.                     for i=1,#block.ikey do
  772.                         local item = block.ikey[i][3]
  773.                         map.msg = item.info
  774.                     end
  775.                 end
  776.                 if block.item ~= nil then
  777.                     for i = 1,#block.item do
  778.                         local item = block.item[i][1]
  779.                         if item.colors == nil then item.colors = dColorSch end
  780.                         map.iboxs[3] = createItemBox(item.size, item.colors, {screen[1]-1-item.size, 2}, item.info)
  781.                         if item.image ~= nil then map.iboxs[3]:setImage(item.image) end
  782.                         map.iboxs[3].info = item.info
  783.                         map.iboxs[3].parent = block
  784.                     end
  785.                 end
  786.             end
  787.             return true
  788.         else
  789.             return false
  790.         end
  791.     end
  792.     return false
  793. end
  794.  
  795. local function useItem(map, slot, pos)
  796.     local ibox = map.iboxs[slot]
  797.     if map[pos[1]] ~= nil then
  798.         if map[pos[1]][pos[2]] ~= nil then
  799.             local block = map[pos[1]][pos[2]]
  800.             if block.ikey ~= nil then
  801.                 for i=1,#block.ikey do
  802.                     local ikey = block.ikey[i]
  803.                     local item = ikey[3]
  804.                     if item.info == ibox.info then
  805.                         local ipos = ikey[1]
  806.                         local ityp = ikey[2]
  807.                         map[ipos[1]][ipos[2]][1] = ityp
  808.                         map.iboxs[slot] = createItemBox(ibox.size, ibox.colors, ibox.pos, "")
  809.                         local line = map.template[ipos[2]]
  810.                         local nline = line:sub(1, ipos[1]-1)
  811.                         nline = nline..ntyp..line:sub(ipos[1]+1)
  812.                         map.template[ipos[2]] = nline
  813.                     end
  814.                 end
  815.             end
  816.         end
  817.     end
  818.     return false
  819. end
  820.  
  821. local map = loadMap(map1) --genMap(100, 100, 15, 15, 4)
  822. --if map[3][1].textbox ~= nil then print("Textbox found where it shouldn't be...") sleep(2) end
  823. map.screen = screen
  824. map.mob_n = 0
  825. map.max_mob_n = 8
  826. map.mobs = {}
  827. map.iboxs = {}
  828. map.iboxs[#map.iboxs + 1] = createItemBox(3, dColorSch, {2, 3}, "")
  829. map.iboxs[#map.iboxs + 1] = createItemBox(3, dColorSch, {6, 3}, "")
  830. map.iboxs[#map.iboxs].lit = true
  831. map.msg = ""
  832. map.iboxs[#map.iboxs]:setItem(itemList["key"])
  833. --map.textbox = createTextBox("text 1 text 2 text 3 text 4 text 5 text 6 text 7 text 8 text 9 text 10 text 11 text 12 text 13 text 14 text 15 text 16 text 17 text 18", screen)
  834. draw(getVision(map.vision, map), screen, map.pos)
  835. local on = true
  836. while on do
  837.     local event, p1 = os.pullEvent()
  838.     if event == "key" then
  839.         local key = p1
  840.         local cdr = false
  841.         local dir = nil
  842.         if key == 200 then --up
  843.             dir = {0, -1}
  844.             dcr = true
  845.         elseif key == 203 then --left
  846.             dir = {-1, 0}
  847.             dcr = true
  848.         elseif key == 208 then --down
  849.             dir = {0, 1}
  850.             dcr = true
  851.         elseif key == 205 then --right
  852.             dir = {1, 0}
  853.             dcr = true
  854.         elseif key == 29 then
  855.             on = false
  856.         elseif key == 16 then --q
  857.             if map.textbox ~= nil then
  858.                 if map.textbox.cur < #map.textbox.lines then
  859.                     map.textbox.cur = map.textbox.cur + 1
  860.                     dcr = true
  861.                 end
  862.             end
  863.         elseif key == 17 then --w
  864.             if map.textbox ~= nil then
  865.                 if map.textbox.cur > 1 then
  866.                     map.textbox.cur = map.textbox.cur - 1
  867.                     dcr = true
  868.                 end
  869.             end
  870.         elseif key == 18 then --e
  871.             map.textbox = nil
  872.             dcr = true
  873.         elseif key == 44 then --z
  874.             local pos = map.pos
  875.             useItem(map, 1, pos)
  876.         elseif key == 45 then --x
  877.             local pos = map.pos
  878.             useItem(map, 2, pos)
  879.         end
  880.         if dcr == true then
  881.             if dir ~= nil then
  882.                 local result, extra = move(map, dir[1], dir[2])
  883.                 if result == true and extra ~= nil then
  884.                     map = loadMap(extra[1], extra[2])
  885.                 end
  886.             end
  887.             draw(getVision(map.vision, map), screen, map.pos)
  888.             if map.textbox ~= nil then map.textbox:draw() end
  889.             if map.iboxs ~= nil then for i=1,#map.iboxs do
  890.                 map.iboxs[i]:draw()
  891.             end end
  892.         end
  893.     end
  894. end
  895. term.setBackgroundColor(colors.black)
  896. term.setTextColor(colors.white)
  897. term.clear()
  898. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement