EngiN33R

Asteroids

May 27th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. astimg=initArray2(512,0)
  2. astype=initArray2(512,0)
  3. asthealth=initArray2(512,0)
  4. astcomm=initArray2(512,0)
  5. astpos=doublearray(512,2,0)
  6. asts={}
  7.  
  8. function createAsteroid(id,x,y,type,mineral)
  9.     astimg[id]=image("gfx/space/asteroid_0"..type..".png",x*32+16,y*32+16,1)
  10.     astype[id]=type
  11.     asthealth[id]=type*100
  12.     astcomm[id]=mineral
  13.     astpos[id][1]=x
  14.     astpos[id][2]=y
  15.     table.insert(asts,id)
  16. end
  17.  
  18. function killAsteroid(id,ply)
  19.     freeimage(astimg[id])
  20.     astimg[id]=0
  21.     asthealth[id]=0
  22.     if (astcomm[id]~=0) then
  23.         commodities[ply][astcomm[id]]=commodities[ply][astcomm[id]]+astype[id]
  24.     end
  25.     astype[id]=0
  26.     astpos[id][1]=0
  27.     astpos[id][2]=0
  28.     table.remove(asts,table.find(asts,id))
  29. end
  30.  
  31. function createAsteroidField(density,mineral,x1,y1,x2,y2)
  32.     local area=(x1+x2)*(y1+y2)
  33.     if (area>=9) then
  34.         if (density==1) then
  35.             for ast=1,(math.floor(area/3)) do
  36.                 local function createast(x1,y1,x2,y2,mineral,id)
  37.                     local xx=math.random(x1,x2)
  38.                     local yy=math.random(y1,y2)
  39.                     local type=math.random(1,5)
  40.                     createAsteroid(xx,yy,type,mineral)
  41.                 end
  42.                 createast(x1,y1,x2,y2,mineral,ast)
  43.             end
  44.         elseif (density==2) then
  45.             for ast=1,(math.floor(area/2)) do
  46.                 local function createast(x1,y1,x2,y2,mineral,id)
  47.                     local xx=math.random(x1,x2)
  48.                     local yy=math.random(y1,y2)
  49.                     createAsteroid(id,xx,yy,(math.random(1,5)),mineral)
  50.                 end
  51.                 createast(x1,y1,x2,y2,mineral,ast)
  52.             end
  53.         elseif (density==3) then
  54.             for ast=1,(math.floor(area/1.5)) do
  55.                 local function createast(x1,y1,x2,y2,mineral,id)
  56.                     local xx=math.random(x1,x2)
  57.                     local yy=math.random(y1,y2)
  58.                     createAsteroid(id,xx,yy,(math.random(1,5)),mineral)
  59.                 end
  60.                 createast(x1,y1,x2,y2,mineral,ast)
  61.             end
  62.         end
  63.     end
  64. end
  65.  
  66. addhook("attack","killasteroids")
  67. function killasteroids(id)
  68.     for range=1,192 do
  69.         local x,y=csconvert(player(id,"rot"),player(id,"x"),player(id,"y"),range)
  70.         if (#asts>1) then
  71.             for aid=1,#asts do
  72.                 if (inRange(math.floor(x/32),math.floor(y/32),astpos[aid][1]-48,astpos[aid][2]-48,astpos[aid][1]+48,astpos[aid][2]+48)) then
  73.                     asthealth[aid]=asthealth[aid]-itemtype(player(id,"weapontype"),"dmg")
  74.                     msg("HIT! The health of asteroid #"..aid.." is now "..asthealth[aid])
  75.                     if (asthealth[aid]<=0) then
  76.                         killAsteroid(aid,id)
  77.                     end
  78.                 end
  79.             end
  80.         else
  81.             if (#asts==1) then
  82.                 if (inRange(math.floor(x/32),math.floor(y/32),astpos[1][1]-48,astpos[1][2]-48,astpos[1][1]+48,astpos[1][2]+48)) then
  83.                     asthealth[1]=asthealth[1]-itemtype(player(id,"weapontype"),"dmg")
  84.                     msg("HIT! The health of asteroid #"..aid.." is now "..asthealth[aid])
  85.                     if (asthealth[1]<=0) then
  86.                         killAsteroid(1,id)
  87.                     end
  88.                 end
  89.             end
  90.         end
  91.     end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment