Guest User

shortwave.patch

a guest
Apr 26th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. diff --git a/control.lua b/control.lua
  2. index 7254d59..d4c0307 100644
  3. --- a/control.lua
  4. +++ b/control.lua
  5. @@ -15,7 +15,7 @@ end
  6. -- Returns the channel string corresponding with the current combinator state
  7. local function get_channel_string(radio, toggle, showlog)
  8. local cb = radio.get_control_behavior()
  9. -
  10. +
  11. -- Make sure there is always exactly one section
  12. if cb.sections_count == 0 then
  13. log("Adding logistic section for "..tostring(radio))
  14. @@ -26,35 +26,35 @@ local function get_channel_string(radio, toggle, showlog)
  15. cb.remove_section(2)
  16. end
  17. end
  18. -
  19. +
  20. -- If "toggle" is true, it means that the toggle-entity keybind is *about to* change the enable state of this combinator.
  21. -- It doesn't actually happen until after the linked custom input has executed, so we have to assume that it will toggle in the future.
  22. if (not cb.enabled and not toggle) or (cb.enabled and toggle) then
  23. return
  24. end
  25. -
  26. +
  27. -- Make sure that the first section was created correctly
  28. local section = cb.get_section(1)
  29. if not section then
  30. return
  31. end
  32. -
  33. +
  34. -- Make sure the first section always stays active. Disable the radio with the constant combinator enable switch.
  35. if not section.active then
  36. log("Setting logistic section active for "..tostring(radio))
  37. section.active = true
  38. end
  39. -
  40. +
  41. -- If the first section is named (in a group), use the group name as the channel instead of the icons
  42. - if section.group and section.group ~= "" then
  43. + if settings.global["shortwave-use-logistic-group-name"].value and section.group and section.group ~= "" then
  44. return "group."..section.group
  45. end
  46. -
  47. +
  48. -- Make sure that at least one signal entry is in the section
  49. if section.filters_count == 0 then
  50. return
  51. end
  52. -
  53. +
  54. -- Assemble the channel string using all specified signals and empty slots
  55. local channel_string = ""
  56. for i=1,section.filters_count do
  57. @@ -124,7 +124,7 @@ local function radio_port(radio)
  58. end
  59.  
  60. port.operable = false
  61. -
  62. +
  63. return port
  64. end
  65.  
  66. @@ -132,7 +132,7 @@ end
  67. local function get_existing_channel(link)
  68. local team = link.force.index
  69. if not storage.teams[team] then return end
  70. -
  71. +
  72. -- Find the central link this radio-link is connected to
  73. local target_relay
  74. local link_red = link.get_wire_connector(defines.wire_connector_id.circuit_red)
  75. @@ -142,7 +142,7 @@ local function get_existing_channel(link)
  76. break
  77. end
  78. end
  79. -
  80. +
  81. -- Find the channel this central link is associated with
  82. if not target_relay then return end
  83. for channel, relay in pairs(storage.teams[team]) do
  84. @@ -157,20 +157,20 @@ local function radio_tune(radio, toggle, showlog)
  85. local team = radio.force.index
  86. local link = radio_link(radio)
  87. local port = radio_port(radio)
  88. -
  89. +
  90. -- Check if the channel name changed and log if it did
  91. local old_channel = get_existing_channel(link)
  92. local channel = get_channel_string(radio, toggle)
  93. -
  94. +
  95. if (old_channel and channel and old_channel == channel) or (not old_channel and not channel) then
  96. -- New and old channel are identical, nothing to do here
  97. return false
  98. end
  99. -
  100. +
  101. if showlog then
  102. log("Retuning "..tostring(radio)..": \""..(old_channel and (game.forces[team] and game.forces[team].name or tostring(team)).."::"..old_channel or "<invalid>").."\" became \""..(channel and (game.forces[team] and game.forces[team].name or tostring(team)).."::"..channel or "<disabled>").."\"")
  103. end
  104. -
  105. +
  106. -- Channel changed, remove old wires if any
  107. local link_red = link.get_wire_connector(defines.wire_connector_id.circuit_red)
  108. local link_green = link.get_wire_connector(defines.wire_connector_id.circuit_green)
  109. @@ -180,7 +180,7 @@ local function radio_tune(radio, toggle, showlog)
  110. link_red.disconnect_from(connection.target, defines.wire_origin.script)
  111. end
  112. end
  113. -
  114. +
  115. for _, connection in pairs(link_green.connections) do
  116. if connection.target.owner.name == "shortwave-link" then
  117. link_green.disconnect_from(connection.target, defines.wire_origin.script)
  118. @@ -217,7 +217,7 @@ local function radio_tune(radio, toggle, showlog)
  119.  
  120. local port_red = port.get_wire_connector(defines.wire_connector_id.combinator_input_red)
  121. local port_green = port.get_wire_connector(defines.wire_connector_id.combinator_input_green)
  122. -
  123. +
  124. link_red.connect_to(port_red, false, defines.wire_origin.script)
  125. link_green.connect_to(port_green, false, defines.wire_origin.script)
  126.  
  127. @@ -227,13 +227,13 @@ end
  128.  
  129. local function retune_all(showlog)
  130. local warning = false
  131. -
  132. +
  133. for _,surface in pairs(game.surfaces) do
  134. for _,entity in pairs(surface.find_entities_filtered{name="shortwave-radio"}) do
  135. warning = radio_tune(entity, false, showlog) or warning
  136. end
  137. end
  138. -
  139. +
  140. return warning
  141. end
  142.  
  143. @@ -259,7 +259,7 @@ function check_channels(second_time)
  144. end
  145. end
  146. end
  147. -
  148. +
  149. -- If any link was deleted because it was invalid, retune all radios to recreate the link and check again.
  150. if retune then
  151. assert(not second_time, "Invalid shortwave radio link could not be corrected.")
  152. @@ -293,7 +293,7 @@ local function OnEntityCreated(event)
  153. entity.destroy()
  154. return
  155. end
  156. -
  157. +
  158. elseif entity.name == "shortwave-radio" then
  159. check_state(entity.force)
  160. radio_tune(entity)
  161. @@ -329,7 +329,7 @@ local function OnEntityRemoved(event)
  162. for _, connection in pairs(link_green.connections) do
  163. link_green.disconnect_from(connection.target, defines.wire_origin.script)
  164. end
  165. -
  166. +
  167. -- We removed it from the table so it won't show up as invalid in the consistency check. We still have to retune everything that was connected to it.
  168. retune_all(true)
  169. break
  170. @@ -367,6 +367,13 @@ local function OnEntitySettingChanged(event)
  171. end
  172. end
  173.  
  174. +local function OnLogisticGroupChanged(event)
  175. +
  176. + if settings.global["shortwave-use-logistic-group-name"].value then
  177. + return
  178. + end
  179. +end
  180. +
  181.  
  182. remote.add_interface('shortwave', {
  183. get_channel_merged_signals = function(force, channel)
  184. @@ -432,6 +439,9 @@ script.on_event(defines.events.on_gui_closed, OnEntitySettingChanged)
  185. script.on_event(defines.events.on_entity_settings_pasted, OnEntitySettingChanged)
  186. script.on_event("shortwave-toggle", OnEntitySettingChanged)
  187.  
  188. +-- When logistic group changes
  189. +script.on_event(defines.events.on_entity_logistic_slot_changed, retune_all)
  190. +
  191. -- When player pipettes radio
  192. script.on_event(defines.events.on_player_pipette, function(event)
  193. local player = game.players[event.player_index]
  194. @@ -452,12 +462,12 @@ end)
  195. -- On configuration changed, need to recheck that all the channel signals still exist
  196. script.on_configuration_changed(function()
  197. local old_storage = serpent.block(storage)
  198. -
  199. +
  200. -- First retune all channels and make new central links if necessary. Remember to print a warning if any channels changed.
  201. local warning = retune_all(true)
  202. -- Then purge unused and invalid links. This might trigger another retune_all() and another check_channels() but nothing will have changed.
  203. check_channels()
  204. -
  205. +
  206. if warning then
  207. game.print{"shortwave-message.migration-changed"}
  208. log{"shortwave-message.migration-log",old_storage,serpent.block(storage)}
  209. diff --git a/locale/en/locale.cfg b/locale/en/locale.cfg
  210. index 3d9b6a0..a176a89 100644
  211. --- a/locale/en/locale.cfg
  212. +++ b/locale/en/locale.cfg
  213. @@ -4,6 +4,12 @@ shortwave=Shortwave
  214. [mod-description]
  215. shortwave=Connect circuit networks wirelessly via shortwave radio channels.
  216.  
  217. +[mod-setting-name]
  218. +shortwave-use-logistic-group-name=Use Logistic Group Name
  219. +
  220. +[mod-setting-description]
  221. +shortwave-use-logistic-group-name=Whether the Logistic Group Name overrides the signal-based channel or is ignored
  222. +
  223. [item-name]
  224. shortwave-radio=Shortwave Radio
  225. shortwave-port=Shortwave I/O
  226. diff --git a/settings.lua b/settings.lua
  227. new file mode 100644
  228. index 0000000..a8e5e01
  229. --- /dev/null
  230. +++ b/settings.lua
  231. @@ -0,0 +1,9 @@
  232. +data:extend{
  233. +-- Use labels - Map setting
  234. +{
  235. +type = "bool-setting",
  236. +setting_type = "runtime-global",
  237. +name = "shortwave-use-logistic-group-name",
  238. +default_value = true
  239. +},
  240. +}
  241.  
Add Comment
Please, Sign In to add comment