pastebin

Paste cactusguy, #zi9 -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
--yo, mobs
local t = "default_cactus_top.png"
local s = "default_cactus_side.png"

local offsets = {
   [0]={x=-0.1, z= 0},
   [1]={x= 0,   z=-0.1},
   [2]={x= 0.1, z= 0},
   [3]={x= 0,   z= 0.1},
   }

minetest.register_entity("wobblycactus:mob",
                         {
                            physical = true,
                            collisionbox = { 0, 0, 0, 0, 0, 0 },
                            visual = "cube",
                            textures = { t, t, s, s, s, s },
                            visual_size = { x = 1, y = 1 },
                            automatic_rotate = false,

                            on_activate = function(self, staticdata)
                               self.num = math.random(2, 4)
                               local pos = self.object:getpos()

                               self.segments = {}
                               self.step = 1
                               self.pop = moven

                               for i=0, self.num do
                                  local np = { x=pos.x, y=pos.y+i, z=pos.z }
                                  local seg = minetest.env:add_entity(np, "wobblycactus:seg"):get_luaentity()
                                  seg.parent = self
                                  seg.off = i
                                  seg.on_step = wobble
                                  table.insert(self.segments, seg)
                               end
                            end,

                            on_punch = function (self)
                               print(dump(self.num))
                            end,

                            on_step = function (self, dtime)
                               self.step = self.step + 1
                               if self.step%20 ~= 0 then return end
                               self.step = 1
                               self:pop()
                            end
                         })

minetest.register_entity("wobblycactus:seg",
                         {
                            physical = true,
                            collisionbox = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
                            visual = "cube",
                            textures = { t, t, s, s, s, s },
                            visual_size = { x = 1, y = 1 },
                            automatic_rotate = false,

                            parent,
                            off,
                            
                            on_punch = function(self, puncher)
                               self.parent:pop()
                            end
                         })

wobble = function(self, dtime)
   if self.off == 0 then return end
   if self.parent.step % 5 ~= 0 then return end
   local pos = self.object:getpos()
   local wob = offsets[(self.parent.step+self.off)%3]
   local wpos = {x=pos.x+wob.x, y=pos.y, z=pos.z+wob.z}
   self.object:moveto(wpos, true)
end

moven = function(self, puncher)
   local pos = self.object:getpos()
   local inf = {x=pos.x+math.random(-1,1), y=pos.y, z=pos.z+math.random(-1,1)}
   local und = {x=inf.x, y=pos.y-1.0, z=inf.z}
   local infn = minetest.env:get_node(inf)
   local undn = minetest.env:get_node(und)
   local npos = pos
   if minetest.registered_nodes[minetest.env:get_node({x=pos.x,y=pos.y-1,z=pos.z}).name].walkable == false then
      npos = {x=pos.x,y=pos.y-1,z=pos.z}
   elseif minetest.registered_nodes[infn.name].walkable == false and minetest.registered_nodes[undn.name].walkable then
      -- Create node and remove entity
      if undn.name == 'air' then
         local uu = minetest.env:get_node({und.x,und.y-1,und.z})
         if minetest.registered_nodes[uu.name].walkable == false then return end
         inf.y = pos.y-1
      end
      npos = inf
   elseif minetest.registered_nodes[infn.name].walkable then
      local abv = {x=inf.x,y=inf.y+1,z=inf.z}
      local abvn = minetest.env:get_node(abv)
      if abvn.name ~= 'air' then return end
      npos = abv
   end

   self.object:moveto(npos, true)
   for i=0, #self.segments-1 do
      local epos = {x=npos.x, y=npos.y+i, z=npos.z}
      self.segments[i+1].object:moveto(epos, true)
   end
   
end

minetest.register_abm({
                         nodenames = { "default:dirt_with_grass" },
                         interval = 600,
                         chance = 30,
                         action = function (pos)
                            minetest.env:add_entity(pos, "wobblycactus:mob")
                         end
                      })