Andy73

indv.lua (pc)

May 11th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 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 return "out of fuel" 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.     return "can't reach destination"
  26.   end
  27.   this.fuel=this.fuel-1
  28.   return true
  29. end,
  30. turnLeft=function(this)
  31.   if this.rot~=4 then this.rot=this.rot+1
  32.   else this.rot=1 end
  33. end,
  34. turnRight=function(this)
  35.   if this.rot~=1 then this.rot=this.rot-1
  36.   else this.rot=4 end
  37. end,
  38. feed=function(this,world)
  39.   if not world then error("specify world please",2) end
  40.   if not this then error("'this' equals nil",2) end
  41.   if this.rot==1 then
  42.   ok,err=pcall(function() tostring(world[this.pos.y-1][this.pos.x]) end)
  43.   if pcall(function() tostring(world[this.pos.y-1][this.pos.x]) end) and world[this.pos.y-1][this.pos.x]==1 then
  44.     this.fuel=this.fuel+5
  45.     world[this.pos.y-1][this.pos.x]=0
  46.     return world
  47.   end
  48.   elseif this.rot==2 then
  49.   if pcall(function() tostring(world[this.pos.y][this.pos.x-1]) end) and world[this.pos.y][this.pos.x-1]==1 then
  50.     this.fuel=this.fuel+5
  51.     world[this.pos.y][this.pos.x-1]=0
  52.     return world
  53.   end
  54.   elseif this.rot==3 then
  55.   if pcall(function() tostring(world[this.pos.y+1][this.pos.x]) end) and world[this.pos.y+1][this.pos.x]==1 then
  56.     this.fuel=this.fuel+5
  57.     world[this.pos.y+1][this.pos.x]=0
  58.     return world
  59.   end
  60.   elseif this.rot==4 then
  61.   if pcall(function() tostring(world[this.pos.y][this.pos.x+1]) end) and world[this.pos.y][this.pos.x+1]==1 then
  62.     this.fuel=this.fuel+5
  63.     world[this.pos.y][this.pos.x+1]=0
  64.     return world
  65.   end
  66.   return false
  67.   end
  68. end,
  69. }
Advertisement
Add Comment
Please, Sign In to add comment