KaoSDlanor

Game start

Oct 5th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.00 KB | None | 0 0
  1. tCreatures={
  2.  
  3. hostile={
  4.  
  5. orc={
  6. status='hostile';
  7. symbol='O';
  8. sight=5;
  9. str=5;
  10. dex=3;
  11. con=6;
  12. int=2;
  13. spd=1;};
  14.  
  15. elf={
  16. status='hostile';
  17. symbol='E';
  18. sight=5;
  19. str=3;
  20. dex=6;
  21. con=4;
  22. int=5;
  23. spd=1;};
  24.  
  25. };
  26.  
  27. neutral={
  28.  
  29. civ={
  30. status='neutral';
  31. symbol='C';
  32. sight=5;
  33. str=4;
  34. dex=3;
  35. con=3;
  36. int=4;
  37. spd=1;};
  38.  
  39. };
  40.  
  41. friendly={
  42.  
  43. guard={
  44. status='friendly';
  45. symbol='G';
  46. sight=5;
  47. str=4;
  48. dex=4;
  49. con=5;
  50. int=3;
  51. spd=1;};
  52.  
  53. };
  54.  
  55.  
  56. }
  57. local tCreature_mt={}
  58. for sType,tList in pairs(tCreatures) do
  59.     for sName,tMob in pairs(tList) do
  60.         tList[sName]=setmetatable(tMob,tCreature_mt)
  61.     end
  62. end
  63. tCreatures.random={unpack(tCreatures.hostile),unpack(tCreatures.neutral);unpack(tCreatures.friendly)};
  64.  
  65. local tMap_mt={}
  66. local tMap=setmetatable({},tMap_mt)
  67. local tSpawns={}
  68. local tPlayer=setmetatable({symbol='@';type='player'},tCreature_mt)
  69. local tMobs_mt={}
  70. local tMobs=setmetatable({constant={tPlayer};temp={}},tMobs_mt)
  71. local tRenderHandlers=setmetatable({s=' ';h=' ';n=' ';f=' ';r=' '},{__index=function(table,index) return index end})
  72. local tHandlers=setmetatable({[' ']='walk';d='walk';D='open';['#']='wall';w='wall'},{__index=function(table,index) return rawget(table,rawget(tRenderHandlers,index)) end})
  73. local mapx
  74. local mapy
  75.  
  76. local function hit(tMob,tTarget)
  77.    
  78. end
  79.  
  80. function tMobs_mt.__call(table,x,y)
  81.     for sType,tList in pairs(table) do
  82.         for num,tMob in pairs(tList) do
  83.             if tMob.cds.x==x and tMob.cds.y==y then
  84.                 return true,tMob
  85.             end
  86.         end
  87.     end
  88.     return false
  89. end
  90.  
  91. function tCreature_mt.__call(table,x,y,isNeut)
  92.     if not y then return false end
  93.     if tHandlers[tMap[y][x]]=='walk' then
  94.         local tResult={tMobs(x,y)}
  95.         if not tResult[1] then
  96.             table.cds.x=x
  97.             table.cds.y=y
  98.             return true
  99.         elseif not isNeut then
  100.             hit(tMob,tResult[2])
  101.             return true
  102.         else
  103.             return false
  104.         end
  105.     else
  106.         return false
  107.     end
  108. end
  109.  
  110. local function copyT(var)
  111.     if type(var)=='table' then
  112.         local tTemp={}
  113.         local MT=getmetatable(var)
  114.         for k,v in pairs(var) do
  115.             tTemp[k]=v
  116.         end
  117.         return setmetatable(tTemp,MT)
  118.     end
  119. end
  120.  
  121. local function selectRandom(tList)
  122.     local intRef=0
  123.     local tReference={}
  124.     for k,v in pairs(tList) do
  125.         intRef=intRef+1
  126.         tReference[intRef]=v
  127.     end
  128.     return tReference[math.random(1,intRef)]
  129. end
  130.  
  131. local function calcDist(pt1,pt2)
  132.     return math.sqrt(math.pow(pt2.cds.x-pt1.cds.x,2)+math.pow(pt2.cds.y-pt1.cds.y,2))
  133. end
  134.  
  135. function drawline(pt1,pt2,main)
  136.     local height=pt2.y-pt1.y
  137.     local width=pt2.x-pt1.x
  138.     local grad=height/width
  139.     local disp=pt1.y-pt1.x*grad
  140.     if main.y-(main.x*grad+disp)<=1 and main.y-(main.x*grad+disp)>=-1 then
  141.         return true
  142.     end
  143. end
  144.  
  145. local function checkIfVisible(tMob,target)
  146.     local visible=true
  147.     for x=tMob.cds.x,target.cds.x,((tMob.cds.x<target.cds.x and 1) or -1) do
  148.         for y=tMob.cds.y,target.cds.y,((tMob.cds.y<target.cds.y and 1) or -1) do
  149.             if drawline(tMob.cds,tPlayer.cds,{x=x,y=y}) and tHandlers[tMap[y][x]]=='wall' then
  150.                 visible=false
  151.             end
  152.         end
  153.     end
  154.     return visible
  155. end
  156.  
  157. local function mosy(tMob)
  158.     if math.random(1,2)==1 then
  159.         local tMoves={{tMob.cds.x-1;tMob.cds.y};{tMob.cds.x+1;tMob.cds.y};{tMob.cds.x;tMob.cds.y-1};{tMob.cds.x;tMob.cds.y+1};}
  160.         tMob(unpack(tMoves[math.random(1,4)]))
  161.     end
  162. end
  163.  
  164. local function moveTo(tMob,tTarget,isNeut)
  165.     local function checkH()
  166.         if tMob.cds.x>tTarget.cds.x then
  167.             return tMob(tMob.cds.x-1,tMob.cds.y,isNeut)
  168.         else
  169.             return tMob(tMob.cds.x+1,tMob.cds.y,isNeut)
  170.         end
  171.     end
  172.    
  173.     local function checkV()
  174.         if tMob.cds.y>tTarget.cds.y then
  175.             return tMob(tMob.cds.x,tMob.cds.y-1,isNeut)
  176.         else
  177.             return tMob(tMob.cds.x,tMob.cds.y+1,isNeut)
  178.         end
  179.     end
  180.    
  181.     if ((tMob.cds.x>tTarget.cds.x and tMob.cds.x-tTarget.cds.x) or tTarget.cds.x-tMob.cds.x)>=((tMob.cds.y>tTarget.cds.y and tMob.cds.y-tTarget.cds.y) or tTarget.cds.y-tMob.cds.y) then
  182.         if not checkH() then checkV() end
  183.     else
  184.         if not checkV() then checkH() end
  185.     end
  186. end
  187.  
  188. local function canSeeType(tMob,sType)
  189.     local tSeen={}
  190.     for x=tMob.cds.x-tMob.sight,tMob.cds.x+tMob.sight do
  191.         for y=tMob.cds.y-tMob.sight,tMob.cds.y+tMob.sight do
  192.             local tResult={tMobs(x,y)}
  193.             if tResult[1]==true and checkIfVisible(tMob,tResult[2]) and tResult[2].status==sType and tResult[2]~=tMob then
  194.                 tSeen[#tSeen+1]=tResult[2]
  195.             end
  196.         end
  197.     end
  198.     if tSeen[1] then
  199.         local closest={'none',math.sqrt(math.pow(tMob.sight,2)*2)}
  200.         for num,tTarget in pairs(tSeen) do
  201.             if calcDist(tMob,tTarget)<closest[2] then
  202.                 closest[1]=num
  203.                 closest[2]=calcDist(tMob,tTarget)
  204.             end
  205.         end
  206.         if tSeen[closest[1]] then
  207.             return true, tSeen[closest[1]]
  208.         end
  209.     else
  210.         return false
  211.     end
  212. end
  213.  
  214. local function doAI()
  215.     for num,tMob in pairs(tMobs.temp) do
  216.         if tMob.status=='hostile' then
  217.             local tResult={canSeeType(tMob,'friendly')}
  218.             if checkIfVisible(tMob,tPlayer) and calcDist(tMob,tPlayer)<=tMob.sight and not tPlayer.invisible and math.random(1,4)~=1 then
  219.                 moveTo(tMob,tPlayer,calcDist(tMob,tPlayer)>1 and true)
  220.             elseif tResult[1]==true then
  221.                 moveTo(tMob,tResult[2])
  222.             else
  223.                 mosy(tMob)
  224.             end
  225.         elseif tMob.status=='neutral' then
  226.             mosy(tMob)
  227.         elseif tMob.status=='friendly' then
  228.             local tResult={canSeeType(tMob,'hostile')}
  229.             if tResult[1]==true then
  230.                 moveTo(tMob,tResult[2],(calcDist(tMob,tResult[2])>1 and true))
  231.             elseif calcDist(tMob,tPlayer)>2 then
  232.                 moveTo(tMob,tPlayer,true)
  233.             end
  234.         end
  235.     end
  236. end
  237.  
  238. local function load(file)
  239.     if fs.exists(file) then
  240.         tSpawns={player={};hostile={};friendly={};neutral={};random={}}
  241.         for k,v in pairs(tMap) do
  242.             tMap[k]=nil
  243.         end
  244.         local oFile=io.open(file,'r')
  245.         local tRows={}
  246.         while true do
  247.             local temp=oFile:read()
  248.             if temp then
  249.                 tRows[#tRows+1]=temp
  250.             else
  251.                 break
  252.             end
  253.         end
  254.         for y,sRow in pairs(tRows) do
  255.             tMap[y]={}
  256.             for x=1,#sRow do
  257.                 tMap[y][x]=string.sub(sRow,x,x)
  258.                 if tMap[y][x]=='s' then tSpawns.player[#tSpawns.player+1]={x=x;y=y} end
  259.                 if tMap[y][x]=='h' then tSpawns.hostile[#tSpawns.hostile+1]={x=x;y=y} end
  260.                 if tMap[y][x]=='f' then tSpawns.friendly[#tSpawns.friendly+1]={x=x;y=y} end
  261.                 if tMap[y][x]=='n' then tSpawns.neutral[#tSpawns.neutral+1]={x=x;y=y} end
  262.                 if tMap[y][x]=='r' then tSpawns.random[#tSpawns.random+1]={x=x;y=y} end
  263.             end
  264.         end
  265.         mapy=#tMap
  266.         mapx=0
  267.         for y,tColumn in pairs(tMap) do
  268.             for x,sValue in pairs(tColumn) do
  269.                 if x>mapx then
  270.                     mapx=x
  271.                 end
  272.             end
  273.         end
  274.     else
  275.         return false
  276.     end
  277. end
  278.  
  279. local function render(offsetX,offsetY)
  280.     offsetX=offsetX or 1
  281.     offsetY=offsetY or 1
  282.     while true do
  283.         for y,tRow in pairs(tMap) do
  284.             for x,sValue in pairs(tRow) do
  285.                 term.setCursorPos(x+offsetX-1,y+offsetY-1)
  286.                 write(tRenderHandlers[sValue])
  287.             end
  288.         end
  289.         for sType,tList in pairs(tMobs) do
  290.             for num,tMob in pairs(tList) do
  291.                 if tMob.cds and tMob.symbol then
  292.                     term.setCursorPos(tMob.cds.x+offsetX-1,tMob.cds.y+offsetY-1)
  293.                     write(tMob.symbol)
  294.                 end
  295.             end
  296.         end
  297.         os.pullEvent()
  298.     end
  299. end
  300.  
  301. local function spawn()
  302.     if not tSpawns.player[1] then
  303.         return false
  304.     else
  305.         tPlayer.cds=tSpawns.player[math.random(1,#tSpawns.player)]
  306.         tPlayer.stats={}
  307.         tPlayer.stats.HP=100
  308.     end
  309.     for type,tSpawnList in pairs(tSpawns) do
  310.         if type~='player' then
  311.             for num,cds in pairs(tSpawnList) do
  312.                 tMobs.temp[#tMobs.temp+1]=copyT(selectRandom(tCreatures[type]))
  313.                 tMobs.temp[#tMobs.temp].cds=cds
  314.             end
  315.         end
  316.     end
  317. end
  318.  
  319. local function interface()
  320.     while true do
  321.         local acted=true
  322.         local _,key=os.pullEvent('char')
  323.         if key=='w' then
  324.             tPlayer(tPlayer.cds.x,tPlayer.cds.y-1)
  325.         elseif key=='a' then
  326.             tPlayer(tPlayer.cds.x-1,tPlayer.cds.y)
  327.         elseif key=='s' then
  328.             tPlayer(tPlayer.cds.x,tPlayer.cds.y+1)
  329.         elseif key=='d' then
  330.             tPlayer(tPlayer.cds.x+1,tPlayer.cds.y)
  331.         else
  332.             acted=nil
  333.         end
  334.         if acted then
  335.             doAI()
  336.         end
  337.     end
  338. end
  339.  
  340. term.clear()
  341. load('map')
  342. spawn()
  343. local tThreads={interface;render}
  344. for num,fThread in pairs(tThreads) do
  345.     tThreads[num]=coroutine.create(fThread)
  346. end
  347. local tEvents={}
  348. while true do
  349.     for num,cRoutine in pairs(tThreads) do
  350.         coroutine.resume(cRoutine,unpack(tEvents))
  351.     end
  352.     tEvents={os.pullEvent()}
  353. end
Advertisement
Add Comment
Please, Sign In to add comment