diff options
Diffstat (limited to 'railcart')
| -rw-r--r-- | railcart/LICENSE.txt | 8 | ||||
| -rw-r--r-- | railcart/init.lua | 35 | ||||
| -rw-r--r-- | railcart/railcart.lua | 80 | 
3 files changed, 103 insertions, 20 deletions
| diff --git a/railcart/LICENSE.txt b/railcart/LICENSE.txt index 5f3db02..f0170d6 100644 --- a/railcart/LICENSE.txt +++ b/railcart/LICENSE.txt @@ -3,17 +3,19 @@ Minetest Mod - Railcart [railcart]  License Source Code: LGPL v2.1 -License of media (textures, sounds and models): CC-0 +License of media (textures, sounds and models not otherwise specified): CC-0  Authors of media files:  ----------------------- -kddekadenz: +HybridDog: WTFPL + +  cart.png    cart_bottom.png    cart_side.png    cart_top.png  Zeg9: +    cart.x -  cart.png diff --git a/railcart/init.lua b/railcart/init.lua index 07edea3..c72db24 100644 --- a/railcart/init.lua +++ b/railcart/init.lua @@ -68,6 +68,11 @@ minetest.register_entity("railcart:cart_entity", {  		if puncher:get_player_control().sneak then  			if self.cart then  				if self.cart.id then +					if self.cart.inv then +						if not self.cart.inv:is_empty("main") then +							return +						end +					end  					railcart:remove_cart(self.cart.id)  				end  			end @@ -83,6 +88,17 @@ minetest.register_entity("railcart:cart_entity", {  			return  		end  		if self.cart and direction then +			if self.driver then +				direction = {x=0, y=0, z=0} +				local ld = self.driver:get_look_dir() +				if ld.y > -0.99 then +					direction = { +						x = railtrack:get_sign(ld.x), +						z = railtrack:get_sign(ld.z), +						y = self.cart.dir.y +					} +				end +			end  			local pos = vector.round(self.object:getpos())  			local dir = vector.round(vector.normalize(direction))  			local speed = railcart:velocity_to_speed(self.cart.vel) @@ -154,26 +170,27 @@ minetest.register_craftitem("railcart:cart", {  		if not name or pointed_thing.type ~= "node" then  			return  		end -		local pos = pointed_thing.under -		if not railtrack:is_railnode(pos) then -			return -		end  		if not minetest.is_singleplayer() then  			if not minetest.check_player_privs(name, {carts=true}) then  				minetest.chat_send_player(name, "Requires carts privilege")  				return  			end  		end -		local cons = railtrack:get_connections(pos) +		local pos = pointed_thing.under +		if not railtrack:is_railnode(pos) then +			return +		end +		local carts = railcart:get_carts_in_radius(pos, 0.9) +		if #carts > 0 then +			return +		end  		local cart = railcart.cart:new()  		cart.id = railcart:get_new_id()  		cart.inv = railcart:create_detached_inventory(cart.id) -		cart.pos = pos +		cart.pos = vector.new(pos)  		cart.prev = vector.new(pos)  		cart.accel = railtrack:get_acceleration(pos) -		if cons[1] then -			cart.target = cons[1] -		end +		cart.dir = railcart:get_rail_direction(pos)  		table.insert(railcart.allcarts, cart)  		railcart:save()  		if not minetest.setting_getbool("creative_mode") then diff --git a/railcart/railcart.lua b/railcart/railcart.lua index c80b507..8f89649 100644 --- a/railcart/railcart.lua +++ b/railcart/railcart.lua @@ -14,7 +14,6 @@ railcart = {  railcart.cart = {  	id = nil, -	entity = {},  	pos = nil,  	target = nil,  	prev = nil, @@ -84,7 +83,6 @@ function railcart:save()  			end  		end  		ref.inv = inv -		ref.entity = nil  		table.insert(carts, ref)  	end  	local output = io.open(minetest.get_worldpath().."/railcart.txt",'w') @@ -126,6 +124,22 @@ function railcart:remove_cart(id)  	end  end +function railcart:get_rail_direction(pos) +	local target = nil +	local cons = railtrack:get_connections(pos) +	local ymax = pos.y +	for _, con in pairs(cons) do +		if con.y >= ymax then +			ymax = con.y +			target = con +		end +	end +	if target then +		return railtrack:get_direction(target, pos) +	end +	return {x=0, y=0, z=0} +end +  function railcart:get_new_id()  	local id = 0  	for _, cart in pairs(railcart.allcarts) do @@ -149,6 +163,39 @@ function railcart:get_cart_entity(id)  	return cart_ref  end +function railcart:get_carts_in_radius(pos, rad) +	local carts = {} +	for _, cart in pairs(railcart.allcarts) do +		local px = pos.x - cart.pos.x +		local py = pos.y - cart.pos.y +		local pz = pos.z - cart.pos.z +		if (px * px) + (py * py) + (pz * pz) <= rad * rad then +			table.insert(carts, cart) +		end +	end +	return carts +end + +function railcart:get_cart_in_sight(p1, p2) +	local ref = nil +	local dist = railtrack:get_distance(p1, p2) + 1 +	local dir = railtrack:get_direction(p2, p1) +	local carts = railcart:get_carts_in_radius(p1, dist) +	for _, cart in pairs(carts) do +		if not vector.equals(p1, cart.pos) then +			local dc = railtrack:get_direction(cart.pos, p1) +			if vector.equals(dc, dir) then +				local d = railtrack:get_distance(p1, cart.pos) +				if d < dist then +					dist = d +					ref = cart +				end +			end +		end +	end +	return ref +end +  function railcart:get_delta_time(vel, acc, dist)  	if vel > 0 then  		if acc == 0 then @@ -236,11 +283,23 @@ function railcart:update(cart, time, object)  	end  	local speed = railcart:velocity_to_speed(cart.vel)  	if cart.target then -		cart.dir = railtrack:get_direction(cart.target, cart.pos) +		if vector.equals(cart.target, cart.pos) then +			cart.dir = railcart:get_rail_direction(cart.pos) +		else +			cart.dir = railtrack:get_direction(cart.target, cart.pos) +		end  	else  		speed = 0  	end  	if speed > RAILCART_SPEED_MIN then +		local blocked = false +		local cis = railcart:get_cart_in_sight(cart.pos, cart.target) +		if cis then +			if railcart:velocity_to_speed(cis.vel) == 0 then +				cart.target = vector.subtract(cis.pos, cart.dir) +				blocked = true +			end +		end  		local d1 = railtrack:get_distance(cart.prev, cart.target)  		local d2 = railtrack:get_distance(cart.prev, cart.pos)  		local dist = d1 - d2 @@ -273,11 +332,16 @@ function railcart:update(cart, time, object)  				cart.pos = vector.add(cart.pos, vector.multiply(cart.dir, dp))  			end  		else -			cart.pos = vector.new(cart.target) -			cart.prev = vector.new(cart.target) -			cart.accel = railtrack:get_acceleration(cart.target) -			cart.target = nil -			return 0 +			if blocked and vector.equals(cart.target, cart.prev) then +				cart.vel = {x=0, y=0, z=0} +				cart.acc = {x=0, y=0, z=0} +			else +				cart.pos = vector.new(cart.target) +				cart.prev = vector.new(cart.target) +				cart.accel = railtrack:get_acceleration(cart.target) +				cart.target = nil +				return 0 +			end  		end  	else  		cart.vel = {x=0, y=0, z=0} | 
