aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_nodetimer.cpp
Commit message (Collapse)AuthorAge
* Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenuKahrl2013-08-14
|
* Move scriptapi to separate folder (by sapier)sapier2013-05-25
On the lua side, notably minetest.env:<function>(<args>) should now be replaced by minetest.<function>(<args>). The old way is and will stay supported for a long time. Also: Update and clean up lua_api.txt (by celeron55) Move EnvRef to lua and remove add_rat and add_firefly (by kahrl) Add separate src/util/CMakeLists.txt, other minor fixes (by kahrl)
f='#n90'>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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
walkway = {}
local speed = 0.4
minetest.register_entity("walkway:moving_dummy",{
			 initial_properties = {
			    hp_max = 1,
			    physical = false,
			    collisionbox = {0.005, -0.005, 0.005, -0.005, 0.005, -0.025},
			    visual = "cube",
			    visual_size = {x = 1, y = 1},
			    textures = {"blank.png","blank.png", "blank.png","blank.png","blank.png","blank.png"},
			    spritediv = {x = 1, y = 1},
			    initial_sprite_basepos = {x = 0, y = 0},
			    is_visible = true;
			 },
			 player = nil,
			 on_step = function (self, dtime)
			    local name = ""
			    if self.player and self.player:is_player() then
			       name = self.player:get_player_name()
			    elseif not self.player then
			       self.object:remove()
			    end
			    local pos = self.object:getpos()
			    local napos = minetest.get_node(pos) 
			    local dir = vector.new(minetest.facedir_to_dir(napos.param2)) -- a copy of the facedir so we don't overwrite the facedir table
			    local vx=0
			    local vz=0
			    if name ~="" then
			       -- based on code from the tower crane mod --
			       local ctrl = self.player:get_player_control()
			       local yaw = self.player:get_look_horizontal()
			       local pos = self.player:getpos()
			       local walk_speed = minetest.settings:get("movement_speed_walk") or 4
			       local speed_forward = 0
			       local speed_right= 0

			       if (ctrl.up or ctrl.down or ctrl.left or ctrl.right) then
				  default.player_set_animation(self.player, "walk" , 30)
			       else
				  default.player_set_animation(self.player, "stand" , 30)
			       end

			       if ctrl.up then             -- forward
				  speed_forward = walk_speed
			       elseif ctrl.down then       -- backward
				  speed_forward = -walk_speed
			       end

			       if ctrl.right then          -- right
				  speed_right = walk_speed
			       elseif ctrl.left then       -- left
				  speed_right = -walk_speed
			       end

			       -- calculate the direction vector
			       vx = math.cos(yaw+math.pi/2) * speed_forward + math.cos(yaw) * speed_right
			       vz = math.sin(yaw+math.pi/2) * speed_forward + math.sin(yaw) * speed_right
			       if dir.x == 0 then
				  vx = 0
			       elseif dir.z == 0 then
				  vz = 0
			       end
			    end

			    
			    if napos.name == "walkway:belt_straight" then
			       self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
			    elseif napos.name == "walkway:belt" then
			       if dir.x == 0 then
				  dir.x = (math.floor(pos.x + 0.5) - pos.x) * 2
			       elseif dir.z == 0 then
				  dir.z = (math.floor(pos.z + 0.5) - pos.z) * 2
			       end
			       self.object:setvelocity({x = dir.x / speed + vx, y = 0, z = dir.z / speed+vz})
			    else
			       if self.player then
				  self.player:set_detach()
				  self.object:remove()
			       else
				  self.object:remove()
			       end
			       if name ~="" then
				  default.player_attached[name] = false
			       end
			    end
			 end
})


minetest.register_node("walkway:belt", {
	description = "Moving Walkway",
	tiles = {{name="factory_belt_top_animation.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=0.2}}, "factory_belt_bottom.png", "factory_belt_side.png",
		"factory_belt_side.png", "factory_belt_side.png", "factory_belt_side.png"},
	groups = {cracky=1},
	drawtype = "nodebox",
	paramtype = "light",
	paramtype2 = "facedir",
	is_ground_content = true,
	legacy_facedir_simple = true,
	node_box = {
			type = "fixed",
			fixed = {{-0.5,-0.5,-0.5,0.5,0.0625,0.5},}
		},
})

-- minetest.register_node("walkway:belt_straight", {
-- 	description = "straight Conveyor Belt",
-- 	tiles = {{name="factory_belt_top_animation.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=0.4}}, "factory_belt_bottom.png", "factory_belt_side.png",
-- 		"factory_belt_side.png", "factory_belt_side.png", "factory_belt_side.png"},
-- 	groups = {cracky=1},
-- 	drawtype = "nodebox",
-- 	paramtype = "light",
-- 	paramtype2 = "facedir",
-- 	is_ground_content = true,
-- 	legacy_facedir_simple = true,
-- 	node_box = {
-- 			type = "fixed",
-- 			fixed = {{-0.5,-0.5,-0.5,0.5,0.0625,0.5},}
-- 		},
-- })

minetest.register_abm({
	nodenames = {"walkway:belt", "walkway:belt_straight"},
	neighbors = nil,
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local all_objects = minetest.get_objects_inside_radius(pos, 0.75)
		local _,obj
		for _,obj in ipairs(all_objects) do
			if not obj:is_player() and obj:get_luaentity() and obj:get_luaentity().name == "__builtin:item" then
				walkway.do_moving_item({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj:get_luaentity().itemstring)
				obj:get_luaentity().itemstring = ""
				obj:remove()
			end
		end
	end,
})

minetest.register_abm({
	nodenames = {"walkway:belt", "walkway:belt_straight"},
	neighbors = nil,
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local all_objects = minetest.get_objects_inside_radius(pos, 0.75)
		local _,obj
		for _,obj in ipairs(all_objects) do

		   if obj:is_player() and not obj:get_attach() then
		      dum = walkway.do_moving_dummy({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj):get_luaentity()
		      dum.player = obj
		      obj:set_attach(dum.object, "", {x=0,y=9,z=0}, {x=0,y=0,z=0})
		      local name = obj:get_player_name()
		      default.player_attached[name] = true
		   elseif obj:get_luaentity() and string.sub(obj:get_luaentity().name,1,string.len("mobs_animal"))  == "mobs_animal" then
		      dum = walkway.do_moving_dummy({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj):get_luaentity()
		      dum.player = obj
--		      minetest.chat_send_all("Attaching "..obj:get_luaentity().name)
		      obj:set_attach(dum.object, "", {x=0,y=-1,z=0}, {x=0,y=0,z=0})
		   end
		end
	end,
})

-- Based off of the pipeworks item code

function walkway.do_moving_item(pos, item)
	if item==":" then
		return
	end
	local stack = ItemStack(item)
	local obj = minetest.add_entity(pos, "walkway:moving_item")
	obj:get_luaentity():set_item(stack:to_string())
	return obj
end

function walkway.do_moving_dummy(pos, player)
        local stack = ItemStack(item)
	local obj = minetest.add_entity(pos, "walkway:moving_dummy")
	--	obj:attach(player)
	return obj
end


minetest.register_entity("walkway:moving_item", {
	initial_properties = {
		hp_max = 1,
		physical = false,
		collisionbox = {0.125, 0.125, 0.125, 0.125, 0.125, 0.125},
		visual = "wielditem",
		visual_size = {x = 0.5, y = 0.5},
		textures = {""},
		spritediv = {x = 1, y = 1},
		initial_sprite_basepos = {x = 0, y = 0},
		is_visible = false,
	},

	physical_state = true,
	itemstring = '',
	set_item = function(self, itemstring)
		self.itemstring = itemstring
		local stack = ItemStack(itemstring)
		local count = stack:get_count()
		local max_count = stack:get_stack_max()
		if count > max_count then
			count = max_count
			self.itemstring = stack:get_name().." "..max_count
		end
		local s = 0.15 + 0.15 * (count / max_count)
		local c = 0.8 * s
		local itemtable = stack:to_table()
		local itemname = nil
		if itemtable then
			itemname = stack:to_table().name
		end
		local item_texture = nil
		local item_type = ""
		if core.registered_items[itemname] then
			item_texture = core.registered_items[itemname].inventory_image
			item_type = core.registered_items[itemname].type
		end
		prop = {
			is_visible = true,
			visual = "wielditem",
			textures = {itemname},
			visual_size = {x = s, y = s},
			collisionbox = {-c, -c, -c, c, c, c},
			automatic_rotate = math.pi * 0.2,
		}
		self.object:set_properties(prop)
	end,

	get_staticdata = function(self)
		return core.serialize({
			itemstring = self.itemstring
		})
	end,

	on_activate = function(self, staticdata, dtime_s)
		if string.sub(staticdata, 1, string.len("return")) == "return" then
			local data = core.deserialize(staticdata)
			if data and type(data) == "table" then
				self.itemstring = data.itemstring
			end
		else
			self.itemstring = staticdata
		end
		self.object:set_armor_groups({immortal = 1})
		self:set_item(self.itemstring)
	end,

	on_step = function(self, dtime)
		local pos = self.object:getpos()
		local napos = minetest.get_node(pos) 
		local dir = vector.new(minetest.facedir_to_dir(napos.param2)) -- a copy of the facedir so we don't overwrite the facedir table
		if napos.name == "walkway:belt_straight" then
			self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
		elseif napos.name == "walkway:belt" then
			if dir.x == 0 then
				dir.x = (math.floor(pos.x + 0.5) - pos.x) * 2
			elseif dir.z == 0 then
				dir.z = (math.floor(pos.z + 0.5) - pos.z) * 2
			end
			self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
		else
			local stack = ItemStack(self.itemstring)
			local veldir = self.object:getvelocity();
			minetest.item_drop(stack, walkway.no_player, {x = pos.x + veldir.x / 3, y = pos.y, z = pos.z + veldir.z / 3})
			self.object:remove()
		end
	end
})

minetest.register_craft({
	output = "walkway:belt 12",
	recipe = {
		{"", "default:gold_ingot", ""},
		{"default:stone", "default:mese_crystal", "default:stone"},
		{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
	}
})