summaryrefslogtreecommitdiff
path: root/teleporter/teleporter.lua
blob: 21c90c0235dd49eb312007f0802b4e905ce54a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
--
-- Teleporter
--
teleporter.config = {}
teleporter.config.filename = minetest.get_worldpath() .. "/teleporter.cfg"
teleporter.positions = {}

function teleporter:load()
	 local file, err = io.open(self.config.filename, "r")
	 if err then
			minetest.log('error', 'could not read ' .. self.config.filename )
			return err
	 end
	 self.positions = minetest.deserialize(file:read("*a"))
	 if type(self.positions) ~= "table" then
			minetest.log('error', 'teleporter_positions were not a table')
	 end

	 if self.positions == nil then
			self.positions = {}
	 end

	 file:close()
end

function teleporter:save()
	 local datastr = minetest.serialize(self.positions)
	 if not datastr then
			minetest.log("error", "[teleporter] Failed to serialize teleporter positions!")
			return
	 end
	 local file, err = io.open(self.config.filename, "w")
	 if err then
			return err
	 end
	 file:write(datastr)
	 file:close()
end

minetest.register_node("teleporter:teleporter",
{
	description = "Teleporter",
  buildable_to = false,
	use_texture_alpha = true,
  light_source = 4,
	drawtype = "nodebox",
	node_box = {
		type = "fixed",
		fixed = {
			 --    1--------(D)-----2
			 --    |\               |\
			 --    | \              | \
			 --    | (J)            | (I)
			 --    |   \            |   \
			 --   (A)   4-------(H)------3
			 --    |    |           |    |
			 --    |    |          (K)   |
			 --    |   (L)          |    |
			 --    |    |           |    |
			 --    5----|---(C)-----6   (G)
			 --     \   |            \   |
			 --     (B) |            (F) |
			 --       \ |              \ |
			 --        \|               \|
			 --         8-------(E)------7

			 -- Edges
			 {-0.5, -0.5, -0.5, -0.4, -0.4,  0.5}, -- A
			 {-0.5, -0.5, -0.5, -0.4,  0.5, -0.4}, -- B
			 {-0.5, -0.5, -0.5,  0.5, -0.4, -0.4}, -- C
			 {-0.5, -0.5, 0.5,  0.4, -0.4, 0.4}, -- D
			 {-0.5, 0.5, -0.5,  0.4, 0.4, -0.4}, -- E
			 {0.5, -0.5, -0.5,  0.4, 0.4, -0.4}, -- F
			 {0.5, 0.5, 0.5, 0.4, 0.4, -0.4}, -- G
			 {-0.5, 0.5, 0.5, 0.4, 0.4, 0.4}, -- H
			 {0.5, -0.5, 0.5, 0.4, 0.4, 0.4}, -- I
			 {-0.5, -0.5, 0.5, -0.4, 0.4, 0.4}, -- J
			 {0.5, -0.5, -0.5, 0.4, -0.4, 0.4}, -- K
			 {-0.5, 0.5, -0.5, -0.4, 0.4, 0.4}, -- L

			 -- Corners
			 { -0.55, -0.55, 0.55, -0.35, -0.35, 0.35 }, -- 1
			 { 0.55, -0.55, 0.55, 0.35, -0.35, 0.35 }, -- 2
			 { 0.55, 0.55, 0.55, 0.35, 0.35, 0.35 }, -- 3
			 { -0.55, 0.55, 0.55, -0.35, 0.35, 0.35 }, -- 4
			 { -0.55, -0.55, -0.55, -0.35, -0.35, -0.35 }, -- 5
			 { 0.55, -0.55, -0.55, 0.35, -0.35, -0.35 }, -- 6
			 { 0.55, 0.55, -0.55, 0.35, 0.35, -0.35 }, -- 7
			 { -0.55, 0.55, -0.55, -0.35, 0.35, -0.35 }, -- 8

			 -- Center box
			 {0.20, 0.20, 0.20, -0.20, -0.20, -0.20},
			},
		},
	tiles = { "teleporter.png" },
	groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
	on_construct =
		 function(pos)
		 end,
	on_place =
		 function(itemstack, placer, pointed_thing)
				local player_name = placer:get_player_name()

				if teleporter.positions[player_name] == nil then
					 teleporter.positions[player_name] = {}
				end

				local pos1 = teleporter.positions[player_name].first
				local pos2 = teleporter.positions[player_name].second
				if pos1 == nil or pos2 == nil then
					 minetest.item_place(itemstack, placer, pointed_thing)
				else
					 minetest.log('warning', 'Only two teleporters at a time')
				end

				return minetest.item_place(itemstack, placer, pointed_thing)
		 end,
	after_place_node =
		 function(pos, placer, itemstack, pointed_thing)
				local player_name = placer:get_player_name()

				if teleporter.positions[player_name] == nil then
					 teleporter.positions[player_name] = {}
				end

				local meta = minetest.get_meta(pos)
				meta:set_string("infotext", "Teleporter owned by " .. player_name)

				if teleporter.positions[player_name].first == nil then
					 teleporter.positions[player_name].first = pos
					 teleporter:save()
					 return
				end

				if teleporter.positions[player_name].second == nil then
					 teleporter.positions[player_name].second = pos
					 teleporter:save()
					 return
				end

				minetest.log('error', 'Only two teleporters!')
		 end,
	after_dig_node =
		 function(pos, oldnode, oldmetadata, digger)
				local player_name = digger:get_player_name()

				if teleporter.positions[player_name] == nil then
					 teleporter.positions[player_name] = {}
				end

				local pos1 = teleporter.positions[player_name].first
				local pos2 = teleporter.positions[player_name].second

				if pos1 ~= nil and
					 pos1.x == pos.x and pos1.y == pos.y and pos1.z == pos.z then
					 teleporter.positions[player_name].first = nil
					 print('deleting first')
					 teleporter:save()
					 return
				end

				if pos2 ~= nil and
					 pos2.x == pos.x and pos2.y == pos.y and pos2.z == pos.z then
					 teleporter.positions[player_name].second = nil
					 print('deleting second')
					 teleporter:save()
					 return
				end

				minetest.log('error', 'Teleporter not registered!')

				minetest.node_dig(pos, node, player)
		 end,
	can_dig =
		 function(pos, player)
				local player_name = player:get_player_name()

				if teleporter.positions[player_name] == nil then
					 teleporter.positions[player_name] = {}
				end

				local pos1 = teleporter.positions[player_name].first
				local pos2 = teleporter.positions[player_name].second

				local pos0 = nil
				if pos1 ~= nil and
					 pos1.x == pos.x and pos1.y == pos.y and pos1.z == pos.z then
					 return true
				end

				if pos2 ~= nil and
					 pos2.x == pos.x and pos2.y == pos.y and pos2.z == pos.z then
					 return true
				end

				return false
		 end,
	on_rightclick =
		 function(pos, node, player, itemstack, pointed_thing)
				--if player:get_wielded_item():to_string() ~= '' then
				--	 return
				--end

				local player_name = player:get_player_name()

				if teleporter.positions[player_name] == nil then
					 teleporter.positions[player_name] = {}
				end

				local pos1 = teleporter.positions[player_name].first
				local pos2 = teleporter.positions[player_name].second

				local pos0 = nil
				if pos1 ~= nil and
					 pos1.x == pos.x and pos1.y == pos.y and pos1.z == pos.z then
					 minetest.log('info', 'this is first, teleport to second')
					 pos0 = pos2
				end

				if pos2 ~= nil and
					 pos2.x == pos.x and pos2.y == pos.y and pos2.z == pos.z then
					 minetest.log('info', 'this is second, teleport to first')
					 pos0 = pos1
				end

				if pos0 ~= nil then
					 player:moveto(pos0)
					 return
				end

				minetest.log('error', 'Teleporter was not registered or only one teleporter was registered.')
		 end
})

minetest.register_craft({
	output = 'teleporter:teleporter 2',
	recipe = {
     { 'default:mese_crystal', 'default:steel_ingot',  'default:mese_crystal' },
     { 'default:steel_ingot',  'default:diamondblock', 'default:steel_ingot' },
     { 'default:mese_crystal', 'default:steel_ingot',  'default:mese_crystal'  }
	}
})