-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontrol.lua
More file actions
156 lines (137 loc) · 7.8 KB
/
control.lua
File metadata and controls
156 lines (137 loc) · 7.8 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
-- TODO: Split off explorer panel into seperate file.
local gui = require("scripts/gui")
local constants = require("helper-tables/constants")
local top = require("new-lib/graph/consistent-sort")
local function load_dep_graph()
for data, _ in pairs(prototypes.item["propertyrandomizer-graph"].get_entity_type_filters(defines.selection_mode.select)) do
local _, graph = serpent.load(data)
storage.graph = graph
break
end
end
script.on_init(function(event)
storage.printed_change_surface_message = false
storage.player_ind_to_last_return_attempt_ticks = {}
-- Give ability to mine fluid immediately to make things easier
game.forces.player.mining_with_fluid = true
load_dep_graph()
storage.sort_info = top.sort(storage.graph)
end)
script.on_configuration_changed(function(event)
game.print("[img=item.propertyrandomizer-gear] [color=red]exfret's Randomizer:[/color] Mod configuration was changed; keep in mind that updates may break pre-existing runs.\nYou can sync the exact versions of mods by Ctrl + Left Click on the \"Sync mods\" button on the top right when selecting a save to load.\nIf you need any help, message exfret on discord or on the mod's website - mods.factorio.com/mod/propertyrandomizer")
load_dep_graph()
end)
script.on_event("return-to-starting-planet", function(event)
local inventories_to_be_empty = {
defines.inventory.character_main,
defines.inventory.character_ammo,
defines.inventory.character_guns,
defines.inventory.character_trash
}
local all_empty = true
for _, inv in pairs(inventories_to_be_empty) do
-- Was getting nil error so double check this inventory exists
local player = game.players[event.player_index]
local inventory = game.players[event.player_index].get_inventory(inv)
if inventory ~= nil and inventory.valid then
if not inventory.is_empty() then
all_empty = false
end
end
end
if all_empty then
-- 5 seconds to press again
if storage.player_ind_to_last_return_attempt_ticks[event.player_index] == nil or event.tick - storage.player_ind_to_last_return_attempt_ticks[event.player_index] > 5 * 60 then
game.players[event.player_index].print("[img=item.propertyrandomizer-gear] [color=red]exfret's Randomizer:[/color] Respawn key sequence entered. Enter again within 5 seconds to confirm.")
else
game.players[event.player_index].teleport(game.surfaces[constants.starting_planet].find_non_colliding_position("character", {0, 0}, 0, 1), constants.starting_planet)
end
storage.player_ind_to_last_return_attempt_ticks[event.player_index] = event.tick
else
game.players[event.player_index].print("[img=item.propertyrandomizer-gear] [color=red]exfret's Randomizer:[/color] Respawn key sequence entered, but your inventory was not empty. Empty your inventory and try again.")
end
end)
script.on_event(defines.events.on_cargo_pod_finished_descending, function(event)
if event.player_index ~= nil and not storage.printed_change_surface_message then
storage.printed_change_surface_message = true
game.print("[img=item.propertyrandomizer-gear] [color=red]exfret's Randomizer:[/color] To prevent softlocks, you can use the respawn key sequence (by default, CTRL + SHIFT + R) to return home at any time. You must have an empty inventory.")
end
end)
script.on_event(defines.events.on_script_trigger_effect, function(event)
if event.effect_id == "teleport-player" then
local position_to_teleport_to = game.surfaces[event.surface_index].find_non_colliding_position(event.source_entity.name, event.target_position, 5, 0.1)
if position_to_teleport_to ~= nil then
event.source_entity.teleport(position_to_teleport_to)
end
end
end)
script.on_event(defines.events.on_built_entity, function(event)
-- I used to make built biters etc. into enemies but actually I think I prefer them as on the player force
end)
script.on_event(defines.events.on_script_trigger_effect, function(event)
if event.effect_id == "randomizer-follower-robot-created" then
local nearest_player
local nearest_player_dist
for _, player in pairs(game.players) do
if player.character ~= nil then
local player_pos = player.character.position
local offset_x = player_pos.x - event.source_entity.position.x
local offset_y = player_pos.y - event.source_entity.position.y
local player_dist = offset_x * offset_x + offset_y * offset_y
if nearest_player_dist == nil or player_dist < nearest_player_dist then
nearest_player = player
nearest_player_dist = player_dist
end
end
end
storage.combat_robot_entity_to_assign = storage.entity_to_assign or {}
table.insert(storage.combat_robot_entity_to_assign, {event.source_entity, nearest_player.character})
--event.source_entity.combat_robot_owner = nearest_player.character
end
end)
script.on_event(defines.events.on_post_entity_died, function(event)
if string.find(event.prototype.name, "exfret%-unit") ~= nil then
for _, corpse in pairs(event.corpses) do
corpse.force = "player"
end
end
end)
script.on_nth_tick(1, function(event)
if storage.combat_robot_entity_to_assign ~= nil then
for _, spec in pairs(storage.combat_robot_entity_to_assign) do
spec[1].combat_robot_owner = spec[2]
end
storage.combat_robot_entity_to_assign = nil
end
-- Print warnings on 10th tick
if event.tick == 10 then
if settings.startup["propertyrandomizer-seed"].value == 0 then
--game.print("[img=item.propertyrandomizer-gear] [color=red]exfret's Randomizer:[/color] You are on the default seed. If you want things randomized in another way for a new experience, change the \"seed\" setting under mod settings in the menu.")
end
local has_no_graph_randomizations = true
for setting_name, _ in pairs(constants.dep_graph_randomizations) do
if settings.startup[setting_name].value then
has_no_graph_randomizations = false
end
end
-- Also check if seed is changed since if it is that means they did actually look at the settings
if has_no_graph_randomizations and settings.startup["propertyrandomizer-seed"].value == 0 then
game.print("[img=item.propertyrandomizer-gear] [color=red]exfret's Randomizer:[/color] Due to slow load times, recipe and other randomizations are off by default, but highly recommended. See mod settings to turn them on. Also consider turning on prototype caching for faster load times for future game startups (ctrl + shift + click settings, click \"The Rest\", then search for prototype caching).")
end
local table_to_load = prototypes.item["propertyrandomizer-warnings"].get_entity_type_filters(defines.selection_mode.select)
for data, _ in pairs(table_to_load) do
local _, warnings = serpent.load(data)
for _, warning in pairs(warnings) do
if type(warning) == "string" then
storage.there_was_warning = true
game.print(warning)
end
end
end
elseif event.tick >= 60 and game.players[1].controller_type ~= defines.controllers.cutscene and not storage.gave_warning_flying_text and storage.there_was_warning then
storage.gave_warning_flying_text = true
for _, player in pairs(game.players) do
player.create_local_flying_text({text="Hm... I should check randomizer warnings in chat.", position={player.position.x, player.position.y - 1.5}, time_to_live=300, speed = 0.7})
end
end
end)