Advertisement
billysback

village

Nov 10th, 2012
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.31 KB | None | 0 0
  1. local moff = 1
  2.  
  3. local mox = 0
  4. local moy = 0
  5.  
  6. local mw = 60
  7. local mh = 60
  8.  
  9. local width = 15
  10. local height = 13
  11.  
  12. local houses = {}
  13. local people = {}
  14. local ptext = {}
  15.  
  16. local function checkPTCol(pixt, pixel)
  17.     for i=1,#pixt do
  18.         local pix = pixt[i]
  19.         if pix.x == pixel.x and pix.y == pixel.y then
  20.             return true
  21.         end
  22.     end
  23.     return false
  24. end
  25.  
  26. local function checkPCol(pixa1, pixel)
  27.     for i=1,#pixa1.pixs do
  28.         local pix = pixa1.pixs[i]
  29.         if pix.x == pixel.x and pix.y == pixel.y then
  30.             return true
  31.         end
  32.     end
  33.     return false
  34. end
  35.  
  36. local function checkCol(pixa1, pixa2)
  37.     for i=1,#pixa1.pixs do
  38.         local pix1 = pixa1.pixs[i]
  39.         for j=1,#pixa2.pixs do
  40.             local pix2 = pixa2.pixs[j]
  41.             if pix1.x == pix2.x and pix1.y == pix2.y then
  42.                 return true
  43.             end
  44.         end
  45.     end
  46.     return false
  47. end
  48.  
  49. local function makePerson(xoff, yoff)
  50.     local amt = math.random(5)
  51.     local texts = {}
  52.     for i=1,amt do
  53.         local type = math.random(10)
  54.         local text = nil
  55.         if type == 1 then text = {"Whats up?"}
  56.         elseif type == 2 then text = {"Hello!"}
  57.         elseif type == 3 then text = {"Hello,", "Good weather we're having!"}
  58.         elseif type == 4 then text = {"Your a new face...", "not many of those these days"}
  59.         elseif type == 5 then text = {"I havn't seen you around", "here before..."}
  60.         elseif type == 6 then text = {"Do you like our village?", "I've never been outside it...", "Mummy won't let me."}
  61.         elseif type == 7 then text = {"The houses around here", "are really pretty,", "don't you think so?"}
  62.         elseif type == 8 then text = {"I can't talk right now,", "Sorry!"}
  63.         elseif type == 9 then text = {"Good day!"}
  64.         elseif type == 10 then text = {"Hi."} end
  65.         texts[#texts + 1] = text
  66.     end
  67.    
  68.     local pixel = art.createPixel(xoff, yoff, nil, colors.yellow, "A")
  69.     return {pixel, texts}
  70. end
  71.  
  72. local function makeHouse(xoff, yoff, colors)
  73.     local type = math.random(3)
  74.     local house = nil
  75.     local blup = nil
  76.     local cur = colors[1]
  77.     if type == 1 then
  78.         blup = {"@____@",
  79.                 "/____\\",
  80.                 "|=||=|"}
  81.     elseif type == 2 then
  82.         blup = {"______",
  83.                 "|=  =|",
  84.                 "|=||=|"}
  85.     elseif type == 3 then
  86.         blup = {"@@@.@@@",
  87.                 "@_/+\\_@",
  88.                 "|+ _ +|",
  89.                 "|_|:|_|"}
  90.     elseif type == 4 then
  91.         blup = {"2@#@@",
  92.                 "@###@",
  93.                 "@1|@@"}
  94.         color = colors.green
  95.     end
  96.     if blup ~= nil then
  97.         house = {}
  98.         for y=1,#blup do
  99.             local line = blup[y]
  100.             for x=1,string.len(line) do
  101.                 local c = string.sub(line, x, x)
  102.                 if c ~= "@" and c ~= "" and c ~= nil then
  103.                     local cn = tonumber(c)
  104.                     if cn ~= nil then
  105.                         cur = colors[cn]
  106.                     else
  107.                         house[#house + 1] = art.setPixel(x+xoff, y+yoff, nil, cur, c)
  108.                     end
  109.                 end
  110.             end
  111.         end
  112.     end
  113.     if house ~= nil then
  114.         house = art.createPixelArray(house)
  115.     end
  116.     return house
  117. end
  118.  
  119. houses[1] = makeHouse(3, 3, {colors.brown, colors.lime})
  120. ptext[1] = makePerson(2, 2)
  121. people[1] = ptext[1][1]
  122. print("OK")
  123.  
  124. --create HOUSES
  125. for n=1,math.random(10, 30) do
  126.     local x = math.random(moff, moff+mw)
  127.     local y = math.random(moff, moff+mh)
  128.     local house = makeHouse(x, y, {colors.brown, colors.lime})
  129.     local ok = true
  130.     if #houses > 0 then
  131.         for i=1,#houses do
  132.             if checkCol(houses[i], house) then
  133.                 ok = false
  134.             end
  135.         end
  136.     end
  137.     if ok then houses[#houses + 1] = house end
  138. end
  139.  
  140. --create PEOPLE
  141. for n=1,math.random(30) do
  142.     local x = math.random(moff, moff+mw)
  143.     local y = math.random(moff, moff+mh)
  144.     local person = makePerson(x, y)
  145.     local ok = true
  146.     for i=1,#houses do
  147.         local house = houses[i]
  148.         if checkPCol(house, person) then ok = false end
  149.     end
  150.     if checkPTCol(people, person[1]) then ok = false end
  151.     if ok then
  152.         people[#people + 1] = person[1]
  153.         ptext[#ptext + 1] = person
  154.     end
  155. end
  156.  
  157. local function getTextBox(text)
  158.     local tx = (moff*2) + width + 2
  159.     local ty = 2
  160.     local tw = tx + 3
  161.     for i=1,#text do
  162.         local l = string.len(text[i])
  163.         if (l+tx+1) > tw then tw = (l+tx+1) end
  164.     end
  165.     local pixels = {}
  166.     local th = #text + 1 + ty
  167.     for x=tx,tw do
  168.         for y=ty,th do
  169.             if x == tx or x == tw or y == ty or y == th then
  170.                 pixels[#pixels + 1] = art.setPixel(x, y, nil, colors.blue, "#")
  171.             end
  172.         end
  173.     end
  174.     for i=1,#text do
  175.         pixels[#pixels + 1] = art.setPixel(tx + 1, ty+i, nil, colors.red, text[i])
  176.     end
  177.     textbox = art.createPixelArray(pixels)
  178.     return textbox
  179. end
  180.  
  181. local function getMap(ox, oy)
  182.     local pixels = {}
  183.     for i=1,#houses do
  184.         local house = houses[i]
  185.         for j=1,#house.pixs do
  186.             local pix = house.pixs[j]
  187.             if pix.x > ox and pix.x <= ox+width and pix.y > oy and pix.y <= oy+height then
  188.                 pixels[#pixels + 1] = art.setPixel((pix.x)-ox+1, (pix.y)-oy+1, pix.background_color, pix.text_color, pix.character)
  189.             end
  190.         end
  191.     end
  192.     for i=1,#people do
  193.         local pix = people[i]
  194.         if pix.x > ox and pix.x <= ox+width and pix.y > oy and pix.y <= oy+height then
  195.             pixels[#pixels + 1] = art.setPixel((pix.x)-ox+1, (pix.y)-oy+1, pix.background_color, pix.text_color, pix.character)
  196.         end
  197.     end
  198.     local pixarr = art.createPixelArray(pixels)
  199.     --local npixels = {}
  200.     --for i=1,#pixels do
  201.     --  local pix = pixels[i]
  202.     --  npixels[#npixels + 1] = art.createPixel(pix:getX()-ox, pix:getY()-oy, pix:getBackgroundColor(), pix:getTextColor(), pix:getChar())
  203.     --end
  204.     return pixarr
  205. end
  206.  
  207. local background
  208. local function generate()
  209.     local pixels = {}
  210.     for x=1,(width+(moff*2)) do
  211.         for y=1, (height+(moff*2)) do
  212.             local color = colors.green
  213.             local ch = "+"
  214.             if x == 1 or x == width+(moff*2) or y == 1 or y == height+(moff*2) then
  215.                 color = colors.blue
  216.                 ch = "#"
  217.             end
  218.             pixels[#pixels + 1] = art.setPixel(x, y, nil, color, ch)
  219.         end
  220.     end
  221.     background = art.createPixelArray(pixels)
  222. end
  223. generate()
  224.  
  225. local textbox = nil
  226.  
  227.  
  228. local player = art.createPixel(1+moff, 1+moff, nil, colors.red, "@")
  229. local function check(fromx, fromy)
  230.     if player.x < 1+moff then player:setX(1+moff) end
  231.     if player.x > mw+moff then player:setX(mw+moff) end
  232.     if player.y < 1+moff then player:setY(1+moff) end
  233.     if player.y > mh+moff then player:setY(mh+moff) end
  234.    
  235.     for i=1,#houses do
  236.         local house = houses[i]
  237.         for j=1,#house.pixs do
  238.             local pixel = house.pixs[j]
  239.             if (player.x)-1 == pixel.x and (player.y)-1 == pixel.y then
  240.                 player:setX(fromx)
  241.                 player:setY(fromy)
  242.             end
  243.         end
  244.     end
  245.     for i=1,#ptext do
  246.         local person = ptext[i][1]
  247.         local text = ptext[i][2]
  248.         if person.x == (player.x)-1 and person.y == (player.y)-1 then
  249.             player:setX(fromx)
  250.             player:setY(fromy)
  251.             textbox = getTextBox(text[math.random(#text)])
  252.         end
  253.     end
  254. end
  255.  
  256. local function checkClip()
  257.     if player.x <= 1 then player:setX(2) end
  258.     if player.x >= width+2 then player:setX(width+1) end
  259.     if player.y <= 1 then player:setY(2) end
  260.     if player.y >= height+2 then player.setY(height+1) end
  261. end
  262. local map = getMap(mox, moy)
  263. local function updateOffset()
  264.     local oox = mox
  265.     local ooy = moy
  266.     if player.x <= mox+1 then mox = mox - 1 end
  267.     if player.x > mox+1+width then mox = mox + 1 end
  268.     if player.y <= moy+1 then moy = moy - 1 end
  269.     if player.y > moy+1+height then moy = moy + 1 end
  270.     if oox ~= mox or ooy ~= moy or map == nil then
  271.         local difx = oox-mox
  272.         local dify = ooy-moy
  273.         difx = difx * -1
  274.         dify = dify * -1
  275.         return getMap(mox, moy)
  276.     end
  277.     return nil
  278. end
  279.  
  280. local otextbox = nil
  281. local on = true
  282.  
  283. local timer = os.startTimer(0)
  284.  
  285. local interval = 0.1
  286. while on do
  287.     local event, p1, p2 = os.pullEvent()
  288.     if event == "timer" and p1 == timer then
  289.         --term.clear()
  290.         --term.setCursorPos(1,1)
  291.         timer = os.startTimer(interval)
  292.         if otextbox ~= textbox and textbox ~= nil then
  293.             term.clear()
  294.             term.setCursorPos(1,1)
  295.             textbox:draw()
  296.             otextbox = textbox
  297.         end
  298.         background:draw()
  299.         map:draw()
  300.         local oldx = player:getX()
  301.         local oldy = player:getY()
  302.         player:setX(player:getX() - mox)
  303.         player:setY(player:getY() - moy)
  304.        
  305.         player:draw()
  306.         player:setX(oldx)
  307.         player:setY(oldy)
  308.         term.setCursorPos(1, height + 3)
  309.         print(oldx..":"..oldy)
  310.         print(mox..":"..moy)
  311.     elseif event == "key" then
  312.         local key = p1
  313.         local ox = player.x
  314.         local oy = player.y
  315.         if key == 200 then --up
  316.             player:setY(player.y - 1)
  317.         elseif key == 203 then --left
  318.             player:setX(player.x - 1)
  319.         elseif key == 208 then --down
  320.             player:setY(player.y + 1)
  321.         elseif key == 205 then --right
  322.             player:setX(player.x + 1)
  323.         elseif key == 29 then --control
  324.             on = false
  325.         end
  326.         check(ox, oy)
  327.         local nmap = updateOffset()
  328.         if nmap ~= nil then map = nmap end
  329.     end
  330. end
  331. term.clear()
  332. term.setCursorPos(1,1)
  333. art.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement