forked from mt-mods/jumpdrive
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jump.lua
191 lines (147 loc) · 5.54 KB
/
jump.lua
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
local has_vizlib = minetest.get_modpath("vizlib")
jumpdrive.simulate_jump = function(pos, player, show_marker)
local targetPos = jumpdrive.get_meta_pos(pos)
if jumpdrive.check_mapgen(pos) then
return false, "Error: mapgen was active in this area, please try again later for your own safety!"
end
local meta = minetest.get_meta(pos)
if show_marker and has_vizlib and os.time() < meta:get_int("simulation_expiry") then
return false, "Error: simulation is still active! please wait before simulating again"
end
local radius = jumpdrive.get_radius(pos)
local distance = vector.distance(pos, targetPos)
local playername = meta:get_string("owner")
if player ~= nil then
playername = player:get_player_name()
end
local radius_vector = {x=radius, y=radius, z=radius}
local source_pos1 = vector.subtract(pos, radius_vector)
local source_pos2 = vector.add(pos, radius_vector)
local target_pos1 = vector.subtract(targetPos, radius_vector)
local target_pos2 = vector.add(targetPos, radius_vector)
local x_overlap = (target_pos1.x <= source_pos2.x and target_pos1.x >= source_pos1.x) or
(target_pos2.x <= source_pos2.x and target_pos2.x >= source_pos1.x)
local y_overlap = (target_pos1.y <= source_pos2.y and target_pos1.y >= source_pos1.y) or
(target_pos2.y <= source_pos2.y and target_pos2.y >= source_pos1.y)
local z_overlap = (target_pos1.z <= source_pos2.z and target_pos1.z >= source_pos1.z) or
(target_pos2.z <= source_pos2.z and target_pos2.z >= source_pos1.z)
if x_overlap and y_overlap and z_overlap then
return false, "Error: jump into itself! extend your jump target"
end
-- load chunk
minetest.get_voxel_manip():read_from_map(target_pos1, target_pos2)
if show_marker and has_vizlib then
vizlib.draw_cube(targetPos, radius + 0.5, { color = "#ff0000" })
vizlib.draw_cube(pos, radius + 0.5, { color = "#00ff00" })
local shape = vizlib.draw_point(targetPos, { color = "#0000ff" })
meta:set_int("simulation_expiry", shape.expiry)
end
local msg = nil
local success = true
local blacklisted_pos_list = minetest.find_nodes_in_area(source_pos1, source_pos2, jumpdrive.blacklist)
local _, nodepos = next(blacklisted_pos_list)
if nodepos then
return false, "Can't jump node @ " .. minetest.pos_to_string(nodepos)
end
if minetest.find_node_near(targetPos, radius, "vacuum:vacuum", true) then
msg = "Warning: Jump-target is in vacuum!"
end
if minetest.find_node_near(targetPos, radius, "ignore", true) then
return false, "Warning: Jump-target is in uncharted area"
end
if jumpdrive.is_area_protected(source_pos1, source_pos2, playername) then
return false, "Jump-source is protected!"
end
if jumpdrive.is_area_protected(target_pos1, target_pos2, playername) then
return false, "Jump-target is protected!"
end
local is_empty, empty_msg = jumpdrive.is_area_empty(target_pos1, target_pos2)
if not is_empty then
msg = "Jump-target is obstructed (" .. empty_msg .. ")"
success = false
end
-- check preflight conditions
local preflight_result = jumpdrive.preflight_check(pos, targetPos, radius, playername)
if not preflight_result.success then
-- check failed in customization
msg = "Preflight check failed!"
if preflight_result.message then
msg = preflight_result.message
end
success = false
end
local power_req = jumpdrive.calculate_power(radius, distance, pos, targetPos)
local powerstorage = meta:get_int("powerstorage")
if powerstorage < power_req then
-- not enough power
msg = "Not enough power: required=" .. math.floor(power_req) .. ", actual: " .. powerstorage .. " EU"
success = false
end
return success, msg
end
-- execute jump
jumpdrive.execute_jump = function(pos, player)
local meta = minetest.get_meta(pos)
local radius = jumpdrive.get_radius(pos)
local targetPos = jumpdrive.get_meta_pos(pos)
local distance = vector.distance(pos, targetPos)
local power_req = jumpdrive.calculate_power(radius, distance, pos, targetPos)
local radius_vector = {x=radius, y=radius, z=radius}
local source_pos1 = vector.subtract(pos, radius_vector)
local source_pos2 = vector.add(pos, radius_vector)
local target_pos1 = vector.subtract(targetPos, radius_vector)
local target_pos2 = vector.add(targetPos, radius_vector)
local success, msg = jumpdrive.simulate_jump(pos, player, false)
if not success then
return false, msg
end
-- consume power from storage
local powerstorage = meta:get_int("powerstorage")
meta:set_int("powerstorage", powerstorage - power_req)
local t0 = minetest.get_us_time()
minetest.sound_play("jumpdrive_engine", {
pos = pos,
max_hear_distance = 50,
gain = 0.7,
})
-- actual move
jumpdrive.move(source_pos1, source_pos2, target_pos1, target_pos2)
local t1 = minetest.get_us_time()
local time_micros = t1 - t0
minetest.log("action", "[jumpdrive] jump took " .. time_micros .. " us")
-- show animation in source
minetest.add_particlespawner({
amount = 200,
time = 2,
minpos = source_pos1,
maxpos = source_pos2,
minvel = {x = -2, y = -2, z = -2},
maxvel = {x = 2, y = 2, z = 2},
minacc = {x = -3, y = -3, z = -3},
maxacc = {x = 3, y = 3, z = 3},
minexptime = 0.1,
maxexptime = 5,
minsize = 1,
maxsize = 1,
texture = "spark.png",
glow = 5,
})
-- show animation in target
minetest.add_particlespawner({
amount = 200,
time = 2,
minpos = target_pos1,
maxpos = target_pos2,
minvel = {x = -2, y = -2, z = -2},
maxvel = {x = 2, y = 2, z = 2},
minacc = {x = -3, y = -3, z = -3},
maxacc = {x = 3, y = 3, z = 3},
minexptime = 0.1,
maxexptime = 5,
minsize = 1,
maxsize = 1,
texture = "spark.png",
glow = 5,
})
return true, time_micros
end