Andy73

indv.lua

May 11th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. individual={
  2. pos={
  3.  x=1,y=1
  4. },
  5. rot=3,
  6. fuel=15,
  7. new=function(this,...)
  8.         object = {}
  9.         setmetatable(object,this)
  10.         this.__index=this
  11.         return object
  12. end,
  13. go=function(this,world)
  14.   if not world then error("specify world please",2) end
  15.   if this.fuel<=0 then print("out of fuel") return false end
  16.   if this.rot==1 and world[this.pos.y-1] then                 --up
  17.     this.pos.y=this.pos.y-1
  18.   elseif this.rot==2 and world[this.pos.y][this.pos.x-1] then --left
  19.     this.pos.x=this.pos.x-1
  20.   elseif this.rot==3 and world[this.pos.y+1] then             --down
  21.     this.pos.y=this.pos.y+1
  22.   elseif this.rot==4 and world[this.pos.y][this.pos.x+1] then --right
  23.     this.pos.x=this.pos.x+1
  24.   else
  25.     print"can't reach destination"
  26.     return false
  27.   end
  28.   this.fuel=this.fuel-1
  29.   return true
  30. end,
  31. turnLeft=function(this)
  32.   if this.rot~=4 then this.rot=this.rot+1
  33.   else this.rot=1 end
  34. end,
  35. turnRight=function(this)
  36.   if this.rot~=1 then this.rot=this.rot-1
  37.   else this.rot=4 end
  38. end,
  39. feed=function(this,world)
  40.   if not world then error("specify world please",2) end
  41.   if this.rot==1 then
  42.   if type(world[this.pos.y-1][this.pos.x])=="number" and world[this.pos.y-1][this.pos.x]==1 then
  43.     this.fuel=this.fuel+5
  44.     world[this.pos.y-1][this.pos.x]=0
  45.     return world
  46.   end
  47.   elseif this.rot==2 then
  48.   if world[this.pos.y][this.pos.x-1] and world[this.pos.y][this.pos.x-1]==1 then
  49.     this.fuel=this.fuel+5
  50.     world[this.pos.y][this.pos.x-1]=0
  51.     return world
  52.   end
  53.   elseif this.rot==3 then
  54.   if world[this.pos.y+1][this.pos.x] and world[this.pos.y+1][this.pos.x]==1 then
  55.     this.fuel=this.fuel+5
  56.     world[this.pos.y+1][this.pos.x]=0
  57.     return world
  58.   end
  59.   elseif this.rot==4 then
  60.   if world[this.pos.y][this.pos.x+1] and world[this.pos.y][this.pos.x+1]==1 then
  61.     this.fuel=this.fuel+5
  62.     world[this.pos.y][this.pos.x+1]=0
  63.     return world
  64.   end
  65.   return false
  66.   end
  67. end,
  68. }
Advertisement
Add Comment
Please, Sign In to add comment