-
Notifications
You must be signed in to change notification settings - Fork 26
/
control.lua
543 lines (495 loc) · 20.2 KB
/
control.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
do
local modloader = remote.interfaces["modloader"]
if modloader and modloader[script.mod_name] then
return
end
end
local activation = require("__Ultracube__/scripts/activation")
local cube_fx = require("__Ultracube__/scripts/cube_fx")
local cube_search = require("__Ultracube__/scripts/cube_search")
local cube_management = require("__Ultracube__/scripts/cube_management")
local cubecam = require("__Ultracube__/scripts/cubecam")
local entity_cache = require("__Ultracube__/scripts/entity_cache")
local entity_combine = require("__Ultracube__/scripts/entity_combine")
local linked_entities = require("__Ultracube__/scripts/linked_entities")
local milestones = require("__Ultracube__/scripts/milestones")
local remote_ownership = require("__Ultracube__/scripts/remote_ownership")
local tech_unlock = require("__Ultracube__/scripts/tech_unlock")
local teleport = require("__Ultracube__/scripts/teleport")
local transition = require("__Ultracube__/scripts/transition")
local util = require("__core__/lualib/util.lua")
local function create_initial_cube(player)
local surface = player.surface
local chest = nil
for _ = 1, 100 do
local v = vector_add(player.position, from_polar(16 + 8 * math.random(), 2 * math.pi * math.random()))
chest = surface.create_entity {
name = "iron-chest",
position = v,
force = player.force,
}
if chest then
break
end
end
if chest then
chest.insert(cube_management.cubes.ultradense)
entity_cache.add(chest)
cube_search.hint_entity(chest)
else
error("Couldn't find suitable location to place starting entities.")
end
end
local function on_entity_removed(entity, old_surface, old_position)
entity_cache.remove(entity, old_surface, old_position)
entity_combine.destroyed(entity)
linked_entities.removed(entity)
cube_search.remove_entity(entity)
activation.remove_entity(entity)
end
local function on_entity_added(entity)
cube_fx.reset_ultralocomotion_fuel(entity)
linked_entities.added(entity)
entity_cache.add(entity)
entity_combine.created(entity)
end
local function on_entity_move(entity, old_surface, old_position)
on_entity_removed(entity, old_surface, old_position)
on_entity_added(entity)
end
local function on_picker_dolly_moved(e)
on_entity_move(e.moved_entity, nil, e.start_pos)
end
local picker_dollies_blacklist = {
"cube-local-turbine",
"cube-local-turbine-source",
"cube-local-turbine-transmitter",
"cube-local-turbine-generator",
"cube-nuclear-reactor",
"cube-nuclear-reactor-base",
"cube-nuclear-reactor-online",
"cube-cyclotron-interface",
"cube-beacon-fluid-source",
"cube-antimatter-reactor-port",
}
local function on_load()
activation.on_load()
entity_cache.on_load()
cube_fx.on_load()
cube_search.on_load()
linked_entities.on_load()
if remote.interfaces["PickerDollies"] then
if remote.interfaces["PickerDollies"]["dolly_moved_entity_id"] then
script.on_event(remote.call("PickerDollies", "dolly_moved_entity_id"), on_picker_dolly_moved)
end
if remote.interfaces["PickerDollies"] and remote.interfaces["PickerDollies"]["add_blacklist_name"] then
for _, name in pairs(picker_dollies_blacklist) do
remote.call("PickerDollies", "add_blacklist_name", name)
end
end
end
end
local function on_init()
-- Disable starting things.
if remote.interfaces.freeplay then
remote.call("freeplay", "set_disable_crashsite", true)
remote.call("freeplay", "set_custom_intro_message", {"cube-msg-intro"})
remote.call("freeplay", "set_created_items", {
["cube-synthesizer"] = 1,
["cube-fabricator"] = 1,
["construction-robot"] = 5,
})
remote.call("freeplay", "set_respawn_items", {})
end
game.set_win_ending_info {
image_path = "__Ultracube__/assets/victory.png",
title = {"gui-game-finished.victory"},
message = {"cube-msg-victory-0"},
bullet_points = {
{"cube-msg-victory-1"},
{"cube-msg-victory-2"},
},
final_message = {"cube-msg-victory-3"},
}
for _, interface in pairs {"silo_script", "better-victory-screen"} do
if remote.interfaces[interface] and remote.interfaces[interface]["set_no_victory"] then
remote.call(interface, "set_no_victory", true)
end
end
activation.refresh()
entity_cache.refresh()
cube_fx.refresh()
cube_search.refresh()
linked_entities.refresh()
for _, force in pairs(game.forces) do
tech_unlock.sync(force)
end
on_load()
end
local function on_player_created(e)
local player = game.get_player(e.player_index)
if not storage.cube_given then
storage.cube_given = true
create_initial_cube(player)
end
local inventory = player.get_inventory(defines.inventory.character_armor)
if inventory then
inventory.insert("cube-regulation-armor")
local grid = inventory[1].grid
grid.put {name = "cube-personal-roboport-equipment"}
grid.put {name = "cube-battery-equipment"}
grid.put {name = "cube-solar-panel-equipment"}
grid.put {name = "cube-solar-panel-equipment"}
for _, grid_equipment in pairs(grid.equipment) do
grid_equipment.energy = grid_equipment.max_energy
end
end
end
script.on_load(on_load)
script.on_init(on_init)
script.on_configuration_changed(on_init)
commands.add_command(
"cube_refresh", nil,
function()
on_init()
game.print("Refreshed data.")
end)
script.on_event(defines.events.on_player_created, on_player_created)
script.on_event(
{
defines.events.on_player_main_inventory_changed,
defines.events.on_player_trash_inventory_changed,
defines.events.on_player_cancelled_crafting,
defines.events.on_player_crafted_item,
defines.events.on_player_cursor_stack_changed,
defines.events.on_pre_player_crafted_item,
},
function(e)
cube_management.update_player(e.player_index)
end)
script.on_event(
{
defines.events.on_player_dropped_item,
defines.events.on_player_fast_transferred,
},
function(e)
cube_management.update_player(e.player_index)
cube_search.hint_entity(e.entity)
end)
script.on_event(
{
defines.events.on_pre_player_left_game,
},
function(e)
for _, entity in ipairs(cube_management.drop_before_leaving(e.player_index)) do
cube_search.hint_entity(entity)
end
end)
script.on_event(
defines.events.on_post_entity_died,
function(e)
for _, corpse in pairs(e.corpses) do
cube_search.hint_entity(corpse)
end
end,
{{filter = "type", type = "character"}})
script.on_event(
defines.events.on_character_corpse_expired,
function(e)
linked_entities.return_cubes(e.corpse, nil, true)
end)
script.on_event(
{
defines.events.on_built_entity,
defines.events.on_robot_built_entity,
},
function(e)
if not e.entity.unit_number then
return
end
on_entity_added(e.entity)
end)
script.on_event(
defines.events.on_entity_died,
function(e)
if not e.entity.unit_number then
return
end
linked_entities.return_cubes(e.entity, nil, true)
on_entity_removed(e.entity)
end)
script.on_event(
{
defines.events.script_raised_built,
defines.events.script_raised_revive,
},
function(e)
if not e.entity.unit_number then
return
end
on_entity_added(e.entity)
cube_search.hint_entity(e.entity)
end)
script.on_event(
defines.events.script_raised_destroy,
function(e)
if not e.entity.unit_number then
return
end
linked_entities.return_cubes(e.entity, nil, true)
on_entity_removed(e.entity)
end)
script.on_event(
defines.events.script_raised_teleported,
function(e)
if not e.entity.unit_number then
return
end
on_entity_move(e.entity, e.old_surface_index, e.old_position)
cube_search.hint_entity(e.entity)
end)
script.on_event(
{
defines.events.on_player_mined_entity,
defines.events.on_robot_mined_entity,
},
function(e)
if not e.entity.unit_number then
return
end
linked_entities.return_cubes(e.entity, e.buffer)
on_entity_removed(e.entity)
if e.player_index then
entity_combine.mined_final(e.entity, game.players[e.player_index])
end
end)
script.on_event(
{
defines.events.on_surface_cleared,
defines.events.on_surface_deleted,
},
function(e)
entity_cache.refresh()
-- Should really clear a bunch of structures, but other surfaces can
-- be added only by other (incompatible) mods.
end)
script.on_event(
defines.events.on_player_removed, function(e)
cubecam.remove_player(e.player_index)
end)
script.on_event(
defines.events.on_research_finished,
function(e)
tech_unlock.trigger(e.research.force, e.research.name, true)
end)
script.on_event(
defines.events.on_rocket_launch_ordered,
function(e)
cube_search.hint_entity(e.rocket.cargo_pod)
end)
script.on_event(
defines.events.on_rocket_launched, teleport.on_teleport)
script.on_event(defines.events.on_tick,
function(e)
local tick = e.tick
remote_ownership.tick(tick)
cube_fx.tick(tick)
transition.tick(tick)
linked_entities.tick(tick)
cubecam.tick(tick)
end)
-- Custom input events.
script.on_event("cube-toggle-alerts", function(e)
local settings = settings.get_player_settings(e.player_index)
local value = settings["cube-show-cube-alerts"].value
local new_value = not value
local player = game.players[e.player_index]
if new_value then
player.print({"cube-msg-alerts-enabled"})
else
player.print({"cube-msg-alerts-disabled"})
end
settings["cube-show-cube-alerts"] = {value = new_value}
end)
script.on_event("cube-open-cubecam", function(e)
cubecam.toggle_open(game.get_player(e.player_index), false)
end)
script.on_event("cube-open-cubecam-fullscreen", function(e)
cubecam.toggle_open(game.get_player(e.player_index), true)
end)
script.on_event(defines.events.on_lua_shortcut, function(e)
cubecam.toggle_open(game.get_player(e.player_index), false)
end)
script.on_event(defines.events.on_gui_click, function(e)
cubecam.on_click(game.get_player(e.player_index), e.element)
end)
script.on_event(defines.events.on_gui_value_changed, function(e)
cubecam.on_value_changed(game.get_player(e.player_index), e.element)
end)
script.on_event(defines.events.on_runtime_mod_setting_changed, function(e)
cubecam.on_settings_changed(game.get_player(e.player_index), e.setting)
end)
script.on_event(defines.events.on_player_display_scale_changed, function(e)
cubecam.on_settings_changed(game.get_player(e.player_index), "display-scale")
end)
local function remote_hint_entity(entity)
if entity then
cube_search.hint_entity(entity)
end
end
local function remote_cube_info()
return util.table.deepcopy(storage.cube_remote)
end
local function remote_cube_item_prototypes()
return cube_management.cube_info
end
local function remote_irreplaceable_item_prototypes()
return cube_management.cube_drop
end
-- Better victory screen support.
local function better_victory_screen_statistics()
local force = game.forces["player"]
local stats = {}
local victory_statistics = storage.victory_statistics
local distance_travelled_by_cube = victory_statistics.distance_travelled_by_cube
local cube_utilisation = victory_statistics.cube_working_samples /
(victory_statistics.cube_working_samples + victory_statistics.cube_idle_samples)
local fastest_helvetica = victory_statistics.fastest_helvetica
local production = force.item_production_statistics
local cubes_consumed = production.get_output_count("cube-ultradense-utility-cube")
local cubes_consumed_dormant = production.get_output_count("cube-dormant-utility-cube")
local cubes_consumed_phantom = production.get_output_count("cube-phantom-ultradense-constituent")
local cubes_consumed_phantom_dormant = production.get_output_count("cube-dormant-phantom-constituent")
local cubes_reconstructed = production.get_output_count("cube-legendary-iron-gear")
local cubes_consumed_total = cubes_consumed + cubes_consumed_dormant +
cubes_consumed_phantom + cubes_consumed_phantom_dormant
local matter_created = production.get_input_count("cube-basic-matter-unit")
stats["ultracube"] = {order = "a", stats = {
["cube-distance-travelled"] = {order = "a", value = distance_travelled_by_cube,
unit = "distance"},
["cube-utilisation"] = {order = "b", value = cube_utilisation,
unit = "percentage", has_tooltip = true},
["cubes-reconstructed"] = {order = "c", value = cubes_reconstructed},
["cube-fastest-helvetica"] = {order = "d", value = fastest_helvetica or 60,
unit = "time", ignore = fastest_helvetica == nil},
["cubes-consumed"] = {order = "e", value = cubes_consumed},
["cubes-consumed-dormant"] = {order = "f", value = cubes_consumed_dormant},
["cubes-consumed-phantom"] = {order = "g", value = cubes_consumed_phantom},
["cubes-consumed-phantom-dormant"] = {order = "h", value = cubes_consumed_phantom_dormant},
["cubes-consumed-total"] = {order = "k", value = cubes_consumed_total},
["matter-created"] = {order = "j", value = matter_created},
}}
-- Ignore-flag some military-oriented stats we don't care about.
stats["miscellaneous"] = {stats = {["total-enemy-kills"] = {ignore = true}}}
stats["player"] = {stats = {["kills"] = {ignore = true}}}
return {by_force = {[force.name] = stats}}
end
local function jetpack_fuels()
-- Modded fuel thrust values are capped at 1.1
return {
-- It would be fun to have the cubes as jetpack fuel, but the jetpack would use the entire 1 GJ
-- of energy before switching to a different fuel, even after the cube leaves the player's inventory
-- The problem above might be fixable with extra scripting [there's a remote call that
-- may help, but I haven't tested yet], but the thrust cap also makes this much less appealling
-- Softlocking wouldn't be an issue though, since the jetpack would put the
-- "burnt result" in the inventory immediately when used
--["cube-ultradense-utility-cube"] = 4
--["cube-ultradense-combustion-cube"] = 4
["cube-condensed-fuel"] = 1.2, --equivalent to rocket fuel, but will be capped to 1.1
["cube-vehicle-fuel"] = 1,
["cube-nuclear-fuel"] = 1.1
}
end
remote.add_interface("Ultracube", {
-- Interfaces for compatibility with certain mods.
["better-victory-screen-statistics"] = better_victory_screen_statistics,
["milestones_presets"] = milestones,
["jetpack_fuels"] = jetpack_fuels,
-- Call cube_info() to get a table with the following fields:
--
-- - position: Current position of the cube. Nil if it could not be found, or there is
-- currently more than one.
-- - min_position: Top-left position of the rectangle containing all cubes. Nil if none could be
-- found.
-- - max_position: Bottom-right position of the rectangle containing all cubes. Nil if none
-- could be found.
-- - distance_delta: Distance travelled by the cube since the last update to this table. Nil if
-- the cube could not be found or there is more than one.
-- - is_working: True if the cube is currently being used in some machine. If there are
-- multiple cubes, this is a random sample of one cube from all of them.
--
-- Note that these values are only updated every 6 ticks, so distance_delta is the distance
-- travelled over 6 ticks. Further, cube position within a belt tile is not tracked, so
-- distance_delta will be 0 most of the time when it is on a belt and 1 when it crosses a tile.
["cube_info"] = remote_cube_info,
-- cube_item_prototypes() returns a set of item prototype names which Ultracube must keep track
-- of at all times, i.e. the ones that must be handled by hint_entity() or the token system below
-- if messed with by other mods.
["cube_item_prototypes"] = remote_cube_item_prototypes,
-- irreplaceable_item_prototypes() returns a set of item prototype names which must never be
-- destroyed by another mod (or else the player will be unable to progress). This includes all
-- items returned by cube_item_prototypes(), as well as all intermediates involved in the
-- Helvetica scenario chain.
["irreplaceable_item_prototypes"] = remote_irreplaceable_item_prototypes,
-- Call hint_entity(entity) if you have teleported or moved an item via script (unless you are
-- certain it is not a cube). Pass the entity into which the item was placed.
["hint_entity"] = remote_hint_entity,
-- The following functions provide a more sophisticated alternative to hint_entity() and allow
-- another mod to temporarily assume responsibility for a cube item which no longer physically
-- exists in the world.
--
-- It can be used as follows:
--
-- (1) Remove exactly N items from the game (e.g. an inventory), and then call
-- token_id = create_ownership_token(item_name, N, timeout_ticks, data), where the parameters
-- are:
-- - item_name: The item prototype name.
-- - N: The count of how many items you removed.
-- - timeout_ticks: A timeout (in number of ticks) after which the token will expire and the
-- item will automatically be placed back into the world at its last known
-- position.
-- - data: A table containing parameters (see below).
-- The function returns an integer token ID which should be stored.
--
-- (2) If necessary, call update_ownership_token(token_id, timeout_ticks, data) periodically to
-- refresh the timeout and/or update parameters (again, see below). timeout_ticks may be nil.
--
-- (3) Call release_ownership_token(token_id) to release the token. This function returns:
-- - nil if the token has already expired due to timeout.
-- - otherwise, a table containing fields 'name' and 'count', which are the item prototype
-- name and count originally passed to create_ownership_token().
-- In the former case, your mod should _not_ attempt to reinsert the item in the world (it has
-- already been recreated by the failsafe). In the latter case, you must recreate exactly one
-- item with the given prototype name, and call hint_entity(entity) for the entity into which
-- it was inserted.
--
-- While a token for a given item is held, Ultracube behaves as if the item existed in the
-- location described, drawing explosion effects (if necessary and not hidden), and treating
-- a single instance of that item as "not missing" for the purposes of reporting cube-lost errors.
--
-- The timeout feature is to protect against irrecoverable cube loss in case of bugs, or in case
-- the player uninstalls your mod while it still holds a token. Try to keep timeouts to reasonable
-- (as small as practical) values. They will be limited to a maximum of 60 seconds anyway.
--
-- The data tables passed to functions (1) and (2) may have any of the following fields:
-- - surface: surface where cube explosion effects should be drawn (defaults to first
-- surface).
-- - position: position where cube explosion effects should be drawn. If nil, explosion
-- effects won't be drawn.
-- - spill_position: position where the item will be dropped if the token expires.
-- - velocity: velocity of the item (used to subtly alter some explosion effects).
-- - height: height of the item (used to subtly alter some explosion effects, but all that
-- matters is whether this is negative, positive, or zero - it's not fully
-- dynamic).
-- Position and velocity should be given as tables with 'x' and 'y' fields.
--
-- Note that this system may be used with any item, not just special cube items. For example,
-- you can use this with items in irreplaceable_item_prototypes() to make sure they don't go
-- missing, even though the special integration / explosions / etc only apply to items in
-- cube_item_prototypes().
["create_ownership_token"] = remote_ownership.create_token,
["update_ownership_token"] = remote_ownership.update_token,
["release_ownership_token"] = remote_ownership.release_token,
-- Special function for Renai compatibility.
["reset_ultralocomotion_fuel"] = cube_fx.reset_ultralocomotion_fuel,
})