-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
272 lines (228 loc) · 7.4 KB
/
init.lua
File metadata and controls
272 lines (228 loc) · 7.4 KB
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
local SEXTANT_USES = 128
local CALIBRATED_SEXTANT_USES = 256
local RANDOM_POSITION_SKEW = 64
local LOOK_SKY_ANGLE_TOLERANCE_RADIANS = math.rad(6)
local LOOK_HORIZON_ANGLE_TOLERANCE_RADIANS = math.rad(8)
local LOOK_TIME_TOLERANCE_SECONDS = 10
local TIME_NOON = 0.5
local TIME_SUN_APPEARS = (4 + (30 / 60)) / 24
local TIME_SUNRISE = 5 / 24
local TIME_MOON_DISAPPEARS = (5 + (30 / 60)) / 24
local TIME_MOON_APPEARS = (18 + (30 / 60)) / 24
local TIME_SUNSET = 19 / 24
local TIME_SUN_DISAPPEARS = (19 + (30 / 60)) / 24
local ANGLE_WEST = math.pi / 2
local ANGLE_EAST = 3 * math.pi / 2
-- Return whether two directions (v0, h0) and (v1, h1) are within the given
-- angular tolerance of each other.
local function is_within_angle(v0, h0, v1, h1, tolerance)
local dot = math.cos(v0) * math.cos(v1) * math.cos(h0 - h1) + math.sin(v0) * math.sin(v1)
if dot > 1 then
dot = 1
end
if dot < -1 then
dot = -1
end
return dot >= math.cos(tolerance)
end
local function get_sun_yaw(time)
if time <= TIME_NOON then
return ANGLE_EAST
else
return ANGLE_WEST
end
end
local function get_moon_yaw(time)
if time > TIME_NOON then
return ANGLE_EAST
else
return ANGLE_WEST
end
end
local function get_sun_angle(time)
local day_half = (TIME_SUNSET - TIME_SUNRISE) / 2
return -math.asin(math.cos((time - TIME_NOON) * ((math.pi / 2) / day_half)))
end
local function get_moon_angle(time)
local day_half = (1 - (TIME_SUNSET - TIME_SUNRISE)) / 2
return -math.asin(math.cos((((time + 0.5) % 1) - TIME_NOON) * ((math.pi / 2) / day_half)))
end
local function sun_is_visible(time)
return time > TIME_SUN_APPEARS and time < TIME_SUN_DISAPPEARS
end
local function moon_is_visible(time)
return time > TIME_MOON_APPEARS or time < TIME_MOON_DISAPPEARS
end
local function looking_at_sun_or_moon(v, h, time, tolerance)
if sun_is_visible(time) then
local target_angle = get_sun_angle(time)
local target_yaw = get_sun_yaw(time)
if is_within_angle(v, h, target_angle, target_yaw, tolerance) then
return true, "sun"
end
end
if moon_is_visible(time) then
local target_angle = get_moon_angle(time)
local target_yaw = get_moon_yaw(time)
if is_within_angle(v, h, target_angle, target_yaw, tolerance) then
return true, "moon"
end
end
return false
end
local function looking_at_horizon(v, tolerance)
return math.abs(v) < tolerance
end
local function skew_position(x, z)
local rng = PcgRandom(core.get_us_time())
local skewed_x = rng:next(x - RANDOM_POSITION_SKEW, x + RANDOM_POSITION_SKEW)
local skewed_z = rng:next(z - RANDOM_POSITION_SKEW, z + RANDOM_POSITION_SKEW)
return skewed_x, skewed_z
end
local function say_player_pos(player, is_approx)
local pos = player:get_pos()
local x
local z
if is_approx then
x, z = skew_position(pos.x, pos.z)
else
x = pos.x
z = pos.z
end
local north_south_direction
if z >= 0 then
north_south_direction = "N"
else
north_south_direction = "S"
end
local east_west_direction
if x >= 0 then
east_west_direction = "E"
else
east_west_direction = "W"
end
local approx_suffix
if is_approx then
approx_suffix = "(approximate)"
else
approx_suffix = "(exact)"
end
local message = string.format(
"%d %s, %d %s %s",
math.abs(z),
north_south_direction,
math.abs(x),
east_west_direction,
approx_suffix
)
core.chat_send_player(player:get_player_name(), message)
end
local function take_durability(item, max_uses)
item:add_wear_by_uses(max_uses)
end
local player_sightings = {}
local function take_reading(itemstack, player, tool_uses, is_approx)
local player_name = player:get_player_name()
local prev_state = player_sightings[player_name]
or {
time = 0,
sighted_sky = false,
sighted_horizon = false,
}
local now = core.get_gametime()
local is_fresh = now <= prev_state.time + LOOK_TIME_TOLERANCE_SECONDS
local next_state
if is_fresh then
next_state = {
time = now,
sighted_sky = prev_state.sighted_sky,
sighted_horizon = prev_state.sighted_horizon,
}
else
next_state = {
time = now,
sighted_sky = false,
sighted_horizon = false,
}
end
local sighted_sky, body_name = looking_at_sun_or_moon(
player:get_look_vertical(),
player:get_look_horizontal(),
core.get_timeofday(),
LOOK_SKY_ANGLE_TOLERANCE_RADIANS
)
local sighted_horizon = looking_at_horizon(player:get_look_vertical(), LOOK_HORIZON_ANGLE_TOLERANCE_RADIANS)
-- If the player is looking at both the sky and the horizon at the same time
-- (possible around sunrise or sunset), they should still have to click
-- twice. The first click should register as looking at the sun/moon, and the
-- second click should register as looking at the horizon.
if (sighted_sky and not sighted_horizon) or (sighted_sky and not next_state.sighted_sky) then
if body_name == "sun" then
core.chat_send_player(player_name, "Sun sighted")
else
core.chat_send_player(player_name, "Moon sighted")
end
next_state.sighted_sky = true
elseif sighted_horizon then
core.chat_send_player(player_name, "Horizon sighted")
next_state.sighted_horizon = true
end
if next_state.sighted_sky and next_state.sighted_horizon then
say_player_pos(player, is_approx)
next_state.sighted_sky = false
next_state.sighted_horizon = false
end
player_sightings[player_name] = next_state
core.sound_play({
name = "celestial_sextant",
to_player = player_name,
gain = 0.2,
})
take_durability(itemstack, tool_uses)
return itemstack
end
core.register_tool("celestial:sextant", {
description = "Crude Sextant",
_doc_items_longdesc = "Find your approximate coordinates using the sun or moon.",
_doc_items_usagehelp = "Right-click on the sun or moon, then right-click on the horizon to take a reading. A cruse sextant will only give you approximate coordinates. Craft a calibrated sextant for exact coordinates.",
inventory_image = "sextant.png",
on_secondary_use = function(itemstack, player, _)
return take_reading(itemstack, player, SEXTANT_USES, true)
end,
})
if
core.get_modpath("mcl_core")
and core.get_modpath("xpanes")
and core.get_modpath("mcl_copper")
and core.get_modpath("mcl_mobitems")
then
core.register_craft({
type = "shaped",
output = "celestial:sextant 1",
recipe = {
{ "mcl_core:stick", "xpanes:pane_natural_flat", "mcl_core:stick" },
{ "mcl_copper:copper_ingot", "mcl_mobitems:string", "mcl_copper:copper_ingot" },
{ "", "mcl_copper:copper_ingot", "" },
},
})
end
core.register_tool("celestial:calibrated_sextant", {
description = "Calibrated Sextant",
_doc_items_longdesc = "Find your exact coordinates using the sun or moon.",
_doc_items_usagehelp = "Right-click on the sun or moon, then right-click on the horizon to take a reading. A calibrated sextant will give you exact coordinates.",
inventory_image = "calibrated_sextant.png",
on_secondary_use = function(itemstack, player, _)
return take_reading(itemstack, player, CALIBRATED_SEXTANT_USES, false)
end,
})
if core.get_modpath("mcl_spyglass") and core.get_modpath("mcl_nether") and core.get_modpath("mcl_clock") then
core.register_craft({
type = "shaped",
output = "celestial:calibrated_sextant 1",
recipe = {
{ "", "mcl_spyglass:spyglass", "" },
{ "mcl_nether:quartz", "celestial:sextant", "mcl_nether:quartz" },
{ "", "group:clock", "" },
},
})
end