Browse code

convert walking_light to ocd diff

* two potential forks after v0.6: myoung008 or petermaloney

Phil Morrell authored on 2018-09-05 18:45:38
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,144 @@
1
+local players = {}
2
+local player_positions = {}
3
+local last_wielded = {}
4
+
5
+function round(num) 
6
+	return math.floor(num + 0.5) 
7
+end
8
+
9
+minetest.register_on_joinplayer(function(player)
10
+	local player_name = player:get_player_name()
11
+	table.insert(players, player_name)
12
+	last_wielded[player_name] = player:get_wielded_item():get_name()
13
+	local pos = player:getpos()
14
+	local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
15
+	local wielded_item = player:get_wielded_item():get_name()
16
+	if wielded_item ~= "default:torch" and wielded_item ~= "walking_light:pick_mese" then
17
+		-- Neuberechnung des Lichts erzwingen
18
+		minetest.env:add_node(rounded_pos,{type="node",name="default:cobble"})
19
+		minetest.env:add_node(rounded_pos,{type="node",name="air"})
20
+	end
21
+	player_positions[player_name] = {}
22
+	player_positions[player_name]["x"] = rounded_pos.x;
23
+	player_positions[player_name]["y"] = rounded_pos.y;
24
+	player_positions[player_name]["z"] = rounded_pos.z;
25
+end)
26
+
27
+minetest.register_on_leaveplayer(function(player)
28
+	local player_name = player:get_player_name()
29
+	for i,v in ipairs(players) do
30
+		if v == player_name then 
31
+			table.remove(players, i)
32
+			last_wielded[player_name] = nil
33
+			-- Neuberechnung des Lichts erzwingen
34
+			local pos = player:getpos()
35
+			local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
36
+			minetest.env:add_node(rounded_pos,{type="node",name="default:cobble"})
37
+			minetest.env:add_node(rounded_pos,{type="node",name="air"})
38
+			player_positions[player_name]["x"] = nil
39
+			player_positions[player_name]["y"] = nil
40
+			player_positions[player_name]["z"] = nil
41
+			player_positions[player_name]["m"] = nil
42
+			player_positions[player_name] = nil
43
+		end
44
+	end
45
+end)
46
+
47
+minetest.register_globalstep(function(dtime)
48
+	for i,player_name in ipairs(players) do
49
+		local player = minetest.env:get_player_by_name(player_name)
50
+		local wielded_item = player:get_wielded_item():get_name()
51
+		if wielded_item == "default:torch" or wielded_item == "walking_light:pick_mese" then
52
+			-- Fackel ist in der Hand
53
+			local pos = player:getpos()
54
+			local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
55
+			if (last_wielded[player_name] ~= "default:torch" and last_wielded[player_name] ~= "walking_light:pick_mese") or (player_positions[player_name]["x"] ~= rounded_pos.x or player_positions[player_name]["y"] ~= rounded_pos.y or player_positions[player_name]["z"] ~= rounded_pos.z) then
56
+				-- Fackel gerade in die Hand genommen oder zu neuem Node bewegt
57
+				local is_air  = minetest.env:get_node_or_nil(rounded_pos)
58
+				if is_air == nil or (is_air ~= nil and (is_air.name == "air" or is_air.name == "walking_light:light")) then
59
+					-- wenn an aktueller Position "air" ist, Fackellicht setzen
60
+					minetest.env:add_node(rounded_pos,{type="node",name="walking_light:light"})
61
+				end
62
+				if (player_positions[player_name]["x"] ~= rounded_pos.x or player_positions[player_name]["y"] ~= rounded_pos.y or player_positions[player_name]["z"] ~= rounded_pos.z) then
63
+					-- wenn Position ge�nder, dann altes Licht l�schen
64
+					local old_pos = {x=player_positions[player_name]["x"], y=player_positions[player_name]["y"], z=player_positions[player_name]["z"]}
65
+					-- Neuberechnung des Lichts erzwingen
66
+					local is_light = minetest.env:get_node_or_nil(old_pos)
67
+					if is_light ~= nil and is_light.name == "walking_light:light" then
68
+						minetest.env:add_node(old_pos,{type="node",name="default:cobble"})
69
+						minetest.env:add_node(old_pos,{type="node",name="air"})
70
+					end
71
+				end
72
+				-- gemerkte Position ist nun die gerundete neue Position
73
+				player_positions[player_name]["x"] = rounded_pos.x
74
+				player_positions[player_name]["y"] = rounded_pos.y
75
+				player_positions[player_name]["z"] = rounded_pos.z
76
+			end
77
+
78
+			last_wielded[player_name] = wielded_item;
79
+		elseif last_wielded[player_name] == "default:torch" or last_wielded[player_name] == "walking_light:pick_mese" then
80
+			-- Fackel nicht in der Hand, aber beim letzten Durchgang war die Fackel noch in der Hand
81
+			local pos = player:getpos()
82
+			local rounded_pos = {x=round(pos.x),y=round(pos.y)+1,z=round(pos.z)}
83
+			repeat
84
+				local is_light  = minetest.env:get_node_or_nil(rounded_pos)
85
+				if is_light ~= nil and is_light.name == "walking_light:light" then
86
+					-- minetest.env:remove_node(rounded_pos)
87
+					-- Erzwinge Neuberechnung des Lichts
88
+					minetest.env:add_node(rounded_pos,{type="node",name="default:cobble"})
89
+					minetest.env:add_node(rounded_pos,{type="node",name="air"})
90
+				end
91
+			until minetest.env:get_node_or_nil(rounded_pos) ~= "walking_light:light"
92
+			local old_pos = {x=player_positions[player_name]["x"], y=player_positions[player_name]["y"], z=player_positions[player_name]["z"]}
93
+			repeat
94
+				is_light  = minetest.env:get_node_or_nil(old_pos)
95
+				if is_light ~= nil and is_light.name == "walking_light:light" then
96
+					-- minetest.env:remove_node(old_pos)
97
+					-- Erzwinge Neuberechnung des Lichts
98
+					minetest.env:add_node(old_pos,{type="node",name="default:cobble"})
99
+					minetest.env:add_node(old_pos,{type="node",name="air"})
100
+				end
101
+			until minetest.env:get_node_or_nil(old_pos) ~= "walking_light:light"
102
+			last_wielded[player_name] = wielded_item
103
+		end
104
+	end
105
+end)
106
+
107
+minetest.register_node("walking_light:light", {
108
+	drawtype = "glasslike",
109
+	tile_images = {"walking_light.png"},
110
+	-- tile_images = {"walking_light_debug.png"},
111
+	inventory_image = minetest.inventorycube("walking_light.png"),
112
+	paramtype = "light",
113
+	walkable = false,
114
+	is_ground_content = true,
115
+	light_propagates = true,
116
+	sunlight_propagates = true,
117
+	light_source = 14,
118
+	selection_box = {
119
+        type = "fixed",
120
+        fixed = {0, 0, 0, 0, 0, 0},
121
+    },
122
+})
123
+minetest.register_tool("walking_light:pick_mese", {
124
+	description = "Mese Pickaxe with light",
125
+	inventory_image = "walking_light_mesepick.png",
126
+	wield_image = "default_tool_mesepick.png",
127
+	tool_capabilities = {
128
+		full_punch_interval = 1.0,
129
+		max_drop_level=3,
130
+		groupcaps={
131
+			cracky={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3},
132
+			crumbly={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3},
133
+			snappy={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3}
134
+		}
135
+	},
136
+})
137
+
138
+minetest.register_craft({
139
+	output = 'walking_light:pick_mese',
140
+	recipe = {
141
+		{'default:torch'},
142
+		{'default:pick_mese'},
143
+	}
144
+})
0 145
new file mode 100644
... ...
@@ -0,0 +1,9 @@
1
+117c117
2
+< 	light_source = 13,
3
+---
4
+> 	light_source = 14,
5
+144c144
6
+< })
7
+\ No newline at end of file
8
+---
9
+> })
0 10
new file mode 100644
1 11
Binary files /dev/null and b/diff/walking_light.diff/textures/walking_light_mesepick.png differ
2 12
new file mode 100644
... ...
@@ -0,0 +1,9 @@
1
+117c117
2
+< 	light_source = 13,
3
+---
4
+> 	light_source = 14,
5
+144c144
6
+< })
7
+\ No newline at end of file
8
+---
9
+> })