Allow switching with keys
| @@ -19,10 +19,45 @@ function boost_cart:is_rail(pos) | ||||
| 	return minetest.get_item_group(node, "rail") ~= 0 | ||||
| end | ||||
|  | ||||
| function boost_cart:get_rail_direction(pos_, dir_) | ||||
| function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch) | ||||
| 	local pos = vector.round(pos_) | ||||
| 	local dir = vector.new(dir_) | ||||
| 	local cur = nil | ||||
| 	local left_check, right_check = true, true | ||||
| 	old_switch = old_switch or 0 | ||||
| 	 | ||||
| 	-- Check left and right | ||||
| 	local left = {x=0, y=0, z=0} | ||||
| 	local right = {x=0, y=0, z=0} | ||||
| 	if dir.z ~= 0 and dir.x == 0 then | ||||
| 		left.x = -dir.z | ||||
| 		right.x = dir.z | ||||
| 	elseif dir.x ~= 0 and dir.z == 0 then | ||||
| 		left.z = dir.x | ||||
| 		right.z = -dir.x | ||||
| 	end | ||||
| 	 | ||||
| 	if ctrl then | ||||
| 		if old_switch == 1 then | ||||
| 			left_check = false | ||||
| 		elseif old_switch == 2 then | ||||
| 			right_check = false | ||||
| 		end | ||||
| 		if ctrl.left and left_check then | ||||
| 			cur = vector.add(pos, left) | ||||
| 			if boost_cart:is_rail(cur) then | ||||
| 				return left, 1 | ||||
| 			end | ||||
| 			left_check = false | ||||
| 		end | ||||
| 		if ctrl.right and right_check then | ||||
| 			cur = vector.add(pos, right) | ||||
| 			if boost_cart:is_rail(cur) then | ||||
| 				return right, 2 | ||||
| 			end | ||||
| 			right_check = true | ||||
| 		end | ||||
| 	end | ||||
| 	 | ||||
| 	-- Front | ||||
| 	dir.y = 0 | ||||
| @@ -45,58 +80,20 @@ function boost_cart:get_rail_direction(pos_, dir_) | ||||
| 		return dir | ||||
| 	end | ||||
| 	 | ||||
| 	-- Left, right | ||||
| 	dir.y = 0 | ||||
| 	 | ||||
| 	-- Check left and right | ||||
| 	local view, opposite, val | ||||
| 	 | ||||
| 	if dir.x == 0 and dir.z ~= 0 then | ||||
| 		view = "z" | ||||
| 		other = "x" | ||||
| 		if dir.z < 0 then | ||||
| 			val = {1, -1} | ||||
| 		else | ||||
| 			val = {-1, 1} | ||||
| 		end | ||||
| 	elseif dir.z == 0 and dir.x ~= 0 then | ||||
| 		view = "x" | ||||
| 		other = "z" | ||||
| 		if dir.x > 0 then | ||||
| 			val = {1, -1} | ||||
| 		else | ||||
| 			val = {-1, 1} | ||||
| 		end | ||||
| 	else | ||||
| 		return {x=0, y=0, z=0} | ||||
| 	end | ||||
| 	 | ||||
| 	dir[view] = 0 | ||||
| 	dir[other] = val[1] | ||||
| 	cur = vector.add(pos, dir) | ||||
| 	-- Left, if not already checked | ||||
| 	if left_check then | ||||
| 		cur = vector.add(pos, left) | ||||
| 		if boost_cart:is_rail(cur) then | ||||
| 		return dir | ||||
| 			return left | ||||
| 		end | ||||
| 	end | ||||
| 	 | ||||
| 	-- Down | ||||
| 	dir.y = -1 | ||||
| 	cur = vector.add(pos, dir) | ||||
| 	-- Right, if not already checked | ||||
| 	if right_check then | ||||
| 		cur = vector.add(pos, right) | ||||
| 		if boost_cart:is_rail(cur) then | ||||
| 		return dir | ||||
| 			return right | ||||
| 		end | ||||
| 	dir.y = 0 | ||||
| 	 | ||||
| 	dir[other] = val[2] | ||||
| 	cur = vector.add(pos, dir) | ||||
| 	if boost_cart:is_rail(cur) then | ||||
| 		return dir | ||||
| 	end | ||||
| 	 | ||||
| 	-- Down | ||||
| 	dir.y = -1 | ||||
| 	cur = vector.add(pos, dir) | ||||
| 	if boost_cart:is_rail(cur) then | ||||
| 		return dir | ||||
| 	end | ||||
| 	 | ||||
| 	return {x=0, y=0, z=0} | ||||
|   | ||||
							
								
								
									
										26
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						| @@ -29,7 +29,8 @@ boost_cart.cart = { | ||||
| 	punch = false, -- used to re-send velocity and position | ||||
| 	velocity = {x=0, y=0, z=0}, -- only used on punch | ||||
| 	old_dir = {x=0, y=0, z=0}, | ||||
| 	old_pos = nil | ||||
| 	old_pos = nil, | ||||
| 	old_switch = nil | ||||
| } | ||||
|  | ||||
| function boost_cart.cart:on_rightclick(clicker) | ||||
| @@ -42,7 +43,7 @@ function boost_cart.cart:on_rightclick(clicker) | ||||
| 		clicker:set_detach() | ||||
| 	elseif not self.driver then | ||||
| 		self.driver = player_name | ||||
| 		clicker:set_attach(self.object, "", {x=0, y=5, z=0}, {x=0, y=0, z=0}) | ||||
| 		clicker:set_attach(self.object, "", {x=0,y=0.5,z=0}, {x=0,y=0,z=0}) | ||||
| 	end | ||||
| end | ||||
|  | ||||
| @@ -57,6 +58,7 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti | ||||
|  | ||||
| 	if puncher:get_player_control().sneak then | ||||
| 		if self.driver then | ||||
| 			default.player_attached[self.driver] = nil | ||||
| 			local player = minetest.get_player_by_name(self.driver) | ||||
| 			if player then | ||||
| 				player:set_detach() | ||||
| @@ -111,7 +113,7 @@ function boost_cart.cart:on_step(dtime) | ||||
| 		return | ||||
| 	end | ||||
| 	 | ||||
| 	local dir = false | ||||
| 	local dir, last_switch = nil, nil | ||||
| 	local pos = self.object:getpos() | ||||
| 	if self.old_pos and not self.punch then | ||||
| 		local flo_pos = vector.floor(pos) | ||||
| @@ -120,12 +122,19 @@ function boost_cart.cart:on_step(dtime) | ||||
| 			return | ||||
| 		end | ||||
| 	end | ||||
| 	local ctrl = nil | ||||
| 	if self.driver then | ||||
| 		local player = minetest.get_player_by_name(self.driver) | ||||
| 		if player then | ||||
| 			ctrl = player:get_player_control() | ||||
| 		end | ||||
| 	end | ||||
| 	if self.old_pos then | ||||
| 		local diff = vector.subtract(self.old_pos, pos) | ||||
| 		for _,v in ipairs({"x","y","z"}) do | ||||
| 			if math.abs(diff[v]) > 1.2 then | ||||
| 				local expected_pos = vector.add(self.old_pos, self.old_dir) | ||||
| 				dir = boost_cart:get_rail_direction(pos, self.old_dir) | ||||
| 				dir, last_switch = boost_cart:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch) | ||||
| 				if vector.equals(dir, {x=0, y=0, z=0}) then | ||||
| 					dir = false | ||||
| 					pos = vector.new(expected_pos) | ||||
| @@ -154,7 +163,7 @@ function boost_cart.cart:on_step(dtime) | ||||
| 	 | ||||
| 	local max_vel = boost_cart.speed_max | ||||
| 	if not dir then | ||||
| 		dir = boost_cart:get_rail_direction(pos, cart_dir) | ||||
| 		dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch) | ||||
| 	end | ||||
| 	if vector.equals(dir, {x=0, y=0, z=0}) then | ||||
| 		vel = {x=0, y=0, z=0} | ||||
| @@ -194,9 +203,13 @@ function boost_cart.cart:on_step(dtime) | ||||
| 					end | ||||
| 				end | ||||
| 			end | ||||
| 			acc = acc + (speed_mod * 7) | ||||
| 			acc = acc + (speed_mod * 8) | ||||
| 		else | ||||
| 			acc = acc - 0.4 | ||||
| 			-- Handbrake | ||||
| 			if ctrl and ctrl.down then | ||||
| 				acc = acc - 0.8 | ||||
| 			end | ||||
| 		end | ||||
| 		 | ||||
| 		local new_acc = { | ||||
| @@ -210,6 +223,7 @@ function boost_cart.cart:on_step(dtime) | ||||
| 	 | ||||
| 	self.old_pos = vector.new(pos) | ||||
| 	self.old_dir = vector.new(dir) | ||||
| 	self.old_switch = last_switch | ||||
| 	 | ||||
| 	-- Limits | ||||
| 	for _,v in ipairs({"x","y","z"}) do | ||||
|   | ||||
| Before Width: | Height: | Size: 524 B After Width: | Height: | Size: 577 B | 
| Before Width: | Height: | Size: 521 B After Width: | Height: | Size: 522 B | 
| Before Width: | Height: | Size: 498 B After Width: | Height: | Size: 549 B | 
| Before Width: | Height: | Size: 496 B After Width: | Height: | Size: 495 B | 
| Before Width: | Height: | Size: 555 B After Width: | Height: | Size: 544 B | 
| Before Width: | Height: | Size: 487 B After Width: | Height: | Size: 490 B | 
| Before Width: | Height: | Size: 548 B After Width: | Height: | Size: 554 B | 
| Before Width: | Height: | Size: 506 B After Width: | Height: | Size: 514 B | 
| Before Width: | Height: | Size: 584 B After Width: | Height: | Size: 561 B | 
							
								
								
									
										
											BIN
										
									
								
								textures/default_rail_t_junction.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| After Width: | Height: | Size: 495 B |