Skip to content

Instantly share code, notes, and snippets.

minetest.register_entity("towntest_worker:worker", {
hp_max = 1,
physical = false,
collisionbox = {-0.4, -1, -0.4, 0.4, 1, 0.4},
visual = "upright_sprite",
visual_size = {x=1, y=2},
textures = {"towntest_worker.png", "towntest_worker_back.png"},
makes_footstep_sound = true,
target = nil,
@cornernote
cornernote / init.lua
Created September 29, 2012 14:42
Minetest - Get Files in a Directory
-- return a table of filenames
get_files = function(directory)
local modpath = minetest.get_modpath("my_mod")
local output
if os.getenv('HOME')~=nil then -- linux/mac
os.execute('\ls -a "'..modpath..'/'..directory..'/" | grep .we > "'..modpath..'/'..directory..'/_files"')
local file, err = io.open(modpath..'/'..directory..'/_files', "rb")
if err ~= nil then
return
end
@cornernote
cornernote / init.lua
Created October 2, 2012 01:48
Override Registered Nodes for Minetest
-- The reason this is needed is because the following code does not work as expected:
local node = minetest.registered_nodes["default:sapling"]
node.after_place_node = function(pos) print("TESTING123") end
minetest.register_node(":default:sapling", node)
--
-- inline solution
--
local node = {}
@cornernote
cornernote / sphere.lua
Created October 2, 2012 12:44
Generate a Sphere (based on sphere in multinode by mauvebic)
generate_sphere = function(pos,radius,nodename,hollow)
pos.x = math.floor(pos.x+0.5)
pos.y = math.floor(pos.y+0.5)
pos.z = math.floor(pos.z+0.5)
for x=-radius,radius do
for y=-radius,radius do
for z=-radius,radius do
if hollow ~= nil then
if x*x+y*y+z*z >= (radius-hollow) * (radius-hollow) + (radius-hollow) and x*x+y*y+z*z <= radius * radius + radius then
minetest.env:add_node({x=pos.x+x,y=pos.y+y,z=pos.z+z},{name=nodename})
@cornernote
cornernote / iteration.lua
Created October 2, 2012 12:47
pairs, ipairs and #tablename iteration
--[[
Tables in Lua may contain both ordered (numbered) and unordered (named) members. Most of the time, you'll use just named elements, or just numbered elements, but there are surprisingly frequent occasions when it's useful to use both. You then need to remember if you iterate through members that ...
- #tablename will only go through numbered elements, and will give you nil if there's a gap in the numbering
- pairs will take you through all elements with non-nil values, but won't give you any members which are explicitly or implicitly set to nil
- ipairs will start at the element numbered 1, and go up through numbered elements until it reaches a nil, at which point it will go no further.
@cornernote
cornernote / init.lua
Created October 2, 2012 12:50
Override Register Functions
local minetest_register_craft = minetest.register_craft -- preserve the old function
minetest.register_craft = function (options)
minetest_register_craft(options) -- call the old function
-- insert your code here --
end
@cornernote
cornernote / init.lua
Created October 2, 2012 12:51
Remove Items after 5 Minutes
-- Jeija wrote this for blockplanet a while back.
-- In item_entity.lua add timer = 0, under the line physical_state = true,
-- And in the beginning of on_step add this in:
self.timer = self.timer + dtime
if (self.timer > 300) then
self.object:remove()
end
@cornernote
cornernote / init.lua
Created October 2, 2012 12:54 — forked from PilzAdam/gist:3803584
NPC that Walks to a Given Pos
-- DEBUG
xp = function(self)
local p = self.object:getpos()
p.x = p.x + 5
self.moveto(self, p, 1, function(self)
zm(self)
end)
end
xm = function(self)
local p = self.object:getpos()
@cornernote
cornernote / init.lua
Created October 2, 2012 13:06
Read Settings from minetest.conf
-- custom settings go in minetest.conf like this:
my_setting = foobar
my_other_setting = 1
-- read custom or existing settings at load time
local my_setting = minetest.setting_get("my_setting") or "barfoo"
local my_other_setting= minetest.setting_getbool("my_other_setting") or false
@cornernote
cornernote / init.lua
Created October 3, 2012 01:50
superflat
super_flat = minetest.setting_getbool("super_flat")
minetest.register_node("super_flat:bedrock", {
description = "Bedrock (You hacker)",
tiles = {"default_cobble.png"},
is_ground_content = true,
groups = {unbreakable=1,not_in_creative_inventory=1},
drop = 'default:cobble',
sounds = default.node_sound_stone_defaults(),
})