Page 1 of 1

[Mod] Game of Life [Experimental]

PostPosted: Mon Nov 02, 2015 23:11
by Jordach
Hello all, as you've noticed, I've been working on a copy of Conway's Game of Life within Minetest.

However, due to how ABMs work, this code fails to work properly. It does, however, have configuration.

Secondly, there is no current way to start and stop ABMs from functioning due to Minetest's limited abilities.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- Game of Life, WTFPL

-- configure game board height:

grid_height = 20

-- configure tick time

tick_timer = 4 -- in seconds

-- register me some nodes

minetest.register_node("gol:life", {
   description = "GoL default rules",
   tiles = {"default_grass.png"},
   groups = {cracky=3},
   --sounds = default.node_sounds_defaults(),
})

minetest.register_node("gol:dead", {
   description = "GoL default dead cell",
   tiles = {"default_dirt.png"},
   groups = {cracky=3},
   --sounds = default.node_sounds_defaults(),
})

-- create the rules with ABMs.

minetest.register_abm({
   nodenames = {"gol:life"},
   interval = tick_timer,
   chance = 1,
   action = function(pos)
      if pos.y ~= grid_height then
         return
      end
      
      if pos == nil then
         return
      end
      
      local surround_count = 0   
      
      for z=-1,1 do
         
         for x=-1,1 do
            local node = minetest.get_node({pos.x+x, pos.y, pos.z+z})
            
            if node.name == "gol:dead" then
               surround_count = surround_count+1
            end
            
            if pos.x+x == pos.x and pos.z+z == pos.z then
               surround_count = surround_count - 1
               minetest.chat_send_all("center point found")
            end
         end
      end
      
      if surround_count == 2 or surround_count == 3 then
         --minetest.set_node(pos, {name="gol:life"})
         return
      end
      
      if surround_count > 3 then
         minetest.set_node(pos, {name="gol:dead"})
         return
      end

      if surround_count < 2 then
         minetest.set_node(pos, {name="gol:dead"})
         return
      end
   end,
})

minetest.register_abm({
   nodenames = {"gol:dead"},
   interval = tick_timer,
   chance = 1,
   action = function(pos)
      if pos.y ~= grid_height then
         return
      end
      
      if pos == nil then
         return
      end
      
      local surround_count = 0   
      
      for z=-1,1 do
         for x=-1,1 do
            local node = minetest.get_node({pos.x+x, pos.y, pos.z+z})
            
            if node.name == "gol:life" then
               surround_count = surround_count+1
            end
            
            if pos.x+x == pos.x and pos.z+z == pos.z then
               surround_count = surround_count - 1
               --minetest.chat_send_all("center point found")
            end
         end
      end

      if surround_count == 3 then
         minetest.set_node(pos, {name="gol:life"})
         return
      end
   end,
})


To install this experimental mod, create a folder called gol, and a file called init.lua -- copy and paste the code into it, enable the mod via configure world - and it should run.

Dependancies:

Default, for textures.

Re: [Mod] Game of Life [Experimental]

PostPosted: Tue Nov 03, 2015 00:50
by qwertymine3
To 'disable' the abm you could just change all of the affected blocks into unaffected blocks, and back to 'reactive' - not very elegant, but shouldn't be to bad with the voxelmanipulator