summaryrefslogtreecommitdiff
path: root/pipeworks/luaentity.lua
blob: c11c030594680a753876103d64fef2d360f93ee1 (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
local max_entity_id = 1000000000000 -- If you need more, there's a problem with your code

local luaentity = {}
pipeworks.luaentity = luaentity

luaentity.registered_entities = {}

local filename = minetest.get_worldpath().."/luaentities"
local function read_file()
	local f = io.open(filename, "r")
	if f == nil then return {} end
    	local t = f:read("*all")
    	f:close()
	if t == "" or t == nil then return {} end
	return minetest.deserialize(t) or {}
end

local function write_file(tbl)
	local f = io.open(filename, "w")
    	f:write(minetest.serialize(tbl))
    	f:close()
end

local function read_entities()
	local t = read_file()
	for _, entity in pairs(t) do

		local x=entity.start_pos.x
		local y=entity.start_pos.y
		local z=entity.start_pos.z

		x=math.max(-30912,x)
		y=math.max(-30912,y)
		z=math.max(-30912,z)
		x=math.min(30927,x)
		y=math.min(30927,y)
		z=math.min(30927,z)

		entity.start_pos.x = x                 
		entity.start_pos.y = y
		entity.start_pos.z = z

		setmetatable(entity, luaentity.registered_entities[entity.name])
	end
	return t
end

local function write_entities()
	for _, entity in pairs(luaentity.entities) do
		setmetatable(entity, nil)
		for _, attached in pairs(entity._attached_entities) do
			if attached.entity then
				attached.entity:remove()
				attached.entity = nil
			end
		end
		entity._attached_entities_master = nil
	end
	write_file(luaentity.entities)
end

minetest.register_on_shutdown(write_entities)
luaentity.entities_index = 0

local function get_blockpos(pos)
	return {x = math.floor(pos.x / 16),
	        y = math.floor(pos.y / 16),
	        z = math.floor(pos.z / 16)}
end

local active_blocks = {} -- These only contain active blocks near players (i.e., not forceloaded ones)

local move_entities_globalstep_part1 = function(dtime)
	local active_block_range = tonumber(minetest.settings:get("active_block_range")) or 2
	local new_active_blocks = {}
	for _, player in ipairs(minetest.get_connected_players()) do
		local blockpos = get_blockpos(player:get_pos())
		local minp = vector.subtract(blockpos, active_block_range)
		local maxp = vector.add(blockpos, active_block_range)

		for x = minp.x, maxp.x do
		for y = minp.y, maxp.y do
		for z = minp.z, maxp.z do
			local pos = {x = x, y = y, z = z}
			new_active_blocks[minetest.hash_node_position(pos)] = pos
		end
		end
		end
	end
	active_blocks = new_active_blocks
	-- todo: callbacks on block load/unload
end

local function is_active(pos)
	return active_blocks[minetest.hash_node_position(get_blockpos(pos))] ~= nil
end

local entitydef_default = {
	_attach = function(self, attached, attach_to)
		local attached_def = self._attached_entities[attached]
		local attach_to_def = self._attached_entities[attach_to]
		attached_def.entity:set_attach(
			attach_to_def.entity, "",
			vector.subtract(attached_def.offset, attach_to_def.offset), -- todo: Does not work because is object space
			vector.new(0, 0, 0)
		)
	end,
	_set_master = function(self, index)
		self._attached_entities_master = index
		if not index then
			return
		end
		local def = self._attached_entities[index]
		if not def.entity then
			return
		end
		def.entity:set_pos(vector.add(self._pos, def.offset))
		def.entity:set_velocity(self._velocity)
		def.entity:set_acceleration(self._acceleration)
	end,
	_attach_all = function(self)
		local master = self._attached_entities_master
		if not master then
			return
		end
		for id, entity in pairs(self._attached_entities) do
			if id ~= master and entity.entity then
				self:_attach(id, master)
			end
		end
	end,
	_detach_all = function(self)
		local master = self._attached_entities_master
		for id, entity in pairs(self._attached_entities) do
			if id ~= master and entity.entity then
				entity.entity:set_detach()
			end
		end
	end,
	_add_attached = function(self, index)
		local entity = self._attached_entities[index]
		if entity.entity then
			return
		end
		local entity_pos = vector.add(self._pos, entity.offset)
		if not is_active(entity_pos) then
			return
		end
		local ent = minetest.add_entity(entity_pos, entity.name):get_luaentity()
		ent:from_data(entity.data)
		ent.parent_id = self._id
		ent.attached_id = index
		entity.entity = ent.object
		local master = self._attached_entities_master
		if master then
			self:_attach(index, master)
		else
			self:_set_master(index)
		end
	end,
	_remove_attached = function(self, index)
		local master = self._attached_entities_master
		local entity = self._attached_entities[index]
		local ent = entity and entity.entity
		if entity then entity.entity = nil end
		if index == master then
			self:_detach_all()
			local newmaster
			for id, attached in pairs(self._attached_entities) do
				if id ~= master and attached.entity then
					newmaster = id
					break
				end
			end
			self:_set_master(newmaster)
			self:_attach_all()
		elseif master and ent then
			ent:set_detach()
		end
		if ent then
			ent:remove()
		end
	end,
	_add_loaded = function(self)
		for id, _ in pairs(self._attached_entities) do
			self:_add_attached(id)
		end
	end,
	get_id = function(self)
		return self._id
	end,
	get_pos = function(self)
		return vector.new(self._pos)
	end,
	set_pos = function(self, pos)
		self._pos = vector.new(pos)
		--for _, entity in pairs(self._attached_entities) do
		--	if entity.entity then
		--		entity.entity:set_pos(vector.add(self._pos, entity.offset))
		--	end
		--end
		local master = self._attached_entities_master
		if master then
			local master_def = self._attached_entities[master]
			master_def.entity:set_pos(vector.add(self._pos, master_def.offset))
		end
	end,
	get_velocity = function(self)
		return vector.new(self._velocity)	
	end,
	set_velocity = function(self, velocity)
		self._velocity = vector.new(velocity)
		local master = self._attached_entities_master
		if master then
			self._attached_entities[master].entity:set_velocity(self._velocity)
		end
	end,
	get_acceleration = function(self)
		return vector.new(self._acceleration)
	end,
	set_acceleration = function(self, acceleration)
		self._acceleration = vector.new(acceleration)
		local master = self._attached_entities_master
		if master then
			self._attached_entities[master].entity:set_acceleration(self._acceleration)
		end
	end,
	remove = function(self)
		self:_detach_all()
		for _, entity in pairs(self._attached_entities) do
			if entity.entity then
				entity.entity:remove()
			end
		end
		luaentity.entities[self._id] = nil
	end,
	add_attached_entity = function(self, name, data, offset)
		local index = #self._attached_entities + 1
		self._attached_entities[index] = {
			name = name,
			data = data,
			offset = vector.new(offset),
		}
		self:_add_attached(index)
		return index
	end,
	remove_attached_entity = function(self, index)
		self:_remove_attached(index)
		self._attached_entities[index] = nil
	end,
}

function luaentity.register_entity(name, prototype)
	-- name = check_modname_prefix(name)
	prototype.name = name
	setmetatable(prototype, {__index = entitydef_default})
	prototype.__index = prototype -- Make it possible to use it as metatable
	luaentity.registered_entities[name] = prototype
end

-- function luaentity.get_entity_definition(entity)
--	 return luaentity.registered_entities[entity.name]
-- end

function luaentity.add_entity(pos, name)
	if not luaentity.entities then
		minetest.after(0, luaentity.add_entity, vector.new(pos), name)
		return
	end
	local index = luaentity.entities_index
	while luaentity.entities[index] do
		index = index + 1
		if index >= max_entity_id then
			index = 0
		end
	end
	luaentity.entities_index = index

	local entity = {
		name = name,
		_id = index,
		_pos = vector.new(pos),
		_velocity = {x = 0, y = 0, z = 0},
		_acceleration = {x = 0, y = 0, z = 0},
		_attached_entities = {},
	}
	
	local prototype = luaentity.registered_entities[name]
	setmetatable(entity, prototype) -- Default to prototype for other methods
	luaentity.entities[index] = entity

	if entity.on_activate then
		entity:on_activate()
	end
	return entity
end

-- todo: check if remove in get_staticdata works
function luaentity.get_staticdata(self)
	local parent = luaentity.entities[self.parent_id]
	if parent and parent._remove_attached then
		parent:_remove_attached(self.attached_id)
	end
	return "toremove"
end

function luaentity.on_activate(self, staticdata)
	if staticdata == "toremove" then
		self.object:remove()
	end
end

function luaentity.get_objects_inside_radius(pos, radius)
	local objects = {}
	local index = 1
	for id, entity in pairs(luaentity.entities) do
		if vector.distance(pos, entity:get_pos()) <= radius then
			objects[index] = entity
			index = index + 1
		end
	end
end

local move_entities_globalstep_part2 = function(dtime)
	if not luaentity.entities then
		luaentity.entities = read_entities()
	end
	for id, entity in pairs(luaentity.entities) do
		local master = entity._attached_entities_master
		local master_def = master and entity._attached_entities[master]
		local master_entity = master_def and master_def.entity
		local master_entity_pos = master_entity and master_entity:get_pos()
		if master_entity_pos then
			entity._pos = vector.subtract(master_entity_pos, master_def.offset)
			entity._velocity = master_entity:get_velocity()
			entity._acceleration = master_entity:get_acceleration()
		else
			entity._pos = vector.add(vector.add(
				entity._pos,
				vector.multiply(entity._velocity, dtime)),
				vector.multiply(entity._acceleration, 0.5 * dtime * dtime))
			entity._velocity = vector.add(
				entity._velocity,
				vector.multiply(entity._acceleration, dtime))
		end
		if master and not master_entity_pos then -- The entity has somehow been cleared
			if pipeworks.delete_item_on_clearobject then
				entity:remove()
			else
				entity:_remove_attached(master)
				entity:_add_loaded()
				if entity.on_step then
					entity:on_step(dtime)
				end
			end
		else
			entity:_add_loaded()
			if entity.on_step then
				entity:on_step(dtime)
			end
		end
	end
end

local handle_active_blocks_timer = 0.1

minetest.register_globalstep(function(dtime)
	handle_active_blocks_timer = handle_active_blocks_timer + dtime
	if dtime < 0.2 or handle_active_blocks_timer >= (dtime * 3) then
		handle_active_blocks_timer = 0.1
		move_entities_globalstep_part1(dtime)
		move_entities_globalstep_part2(dtime)
	end
end)