From 44c7cae563766d136342b9e567802eb9301a2478 Mon Sep 17 00:00:00 2001 From: PHIDIAS Date: Fri, 13 Oct 2023 23:51:01 +0900 Subject: [PATCH 1/4] deconstruction of tree and rocks when drive over them --- modules/addons/tree-decon.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/addons/tree-decon.lua b/modules/addons/tree-decon.lua index d4a2c206..c3d36920 100644 --- a/modules/addons/tree-decon.lua +++ b/modules/addons/tree-decon.lua @@ -96,4 +96,14 @@ Event.on_nth_tick(300, function() for key, _ in pairs(chache) do chache[key] = nil end -end) \ No newline at end of file +end) + +Event.add(defines.events.on_entity_damaged, function(event) + if not (event.damage_type.name == 'impact' and event.force) then + return + end + + if event.entity.type == 'tree' or event.entity.type == 'simple-entity' then + event.entity.destroy() + end +end) From 193f3e3b88e2aad86ed909df1adcd7a1b8f18d30 Mon Sep 17 00:00:00 2001 From: PHIDIAS Date: Sat, 14 Oct 2023 02:08:53 +0900 Subject: [PATCH 2/4] Update tree-decon.lua --- modules/addons/tree-decon.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/addons/tree-decon.lua b/modules/addons/tree-decon.lua index c3d36920..1f594366 100644 --- a/modules/addons/tree-decon.lua +++ b/modules/addons/tree-decon.lua @@ -99,11 +99,17 @@ Event.on_nth_tick(300, function() end) Event.add(defines.events.on_entity_damaged, function(event) - if not (event.damage_type.name == 'impact' and event.force) then + if not (event.damage_type.name == 'impact' and event.force) then + return + end + + if not (event.entity.type == 'tree' or event.entity.type == 'simple-entity') then + return + end + + if (not event.cause) or (event.cause.type ~= 'car')then return end - if event.entity.type == 'tree' or event.entity.type == 'simple-entity' then - event.entity.destroy() - end + event.entity.destroy() end) From fe1ee200e2ce36488e51dd17ef814101a7c01440 Mon Sep 17 00:00:00 2001 From: PHIDIAS Date: Sat, 14 Oct 2023 02:15:19 +0900 Subject: [PATCH 3/4] Update tree-decon.lua --- modules/addons/tree-decon.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/addons/tree-decon.lua b/modules/addons/tree-decon.lua index 1f594366..84194d7c 100644 --- a/modules/addons/tree-decon.lua +++ b/modules/addons/tree-decon.lua @@ -102,11 +102,11 @@ Event.add(defines.events.on_entity_damaged, function(event) if not (event.damage_type.name == 'impact' and event.force) then return end - + if not (event.entity.type == 'tree' or event.entity.type == 'simple-entity') then return end - + if (not event.cause) or (event.cause.type ~= 'car')then return end From c6d7a24ff595867bbe67503a7365e76c9e3d84d6 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Thu, 21 Dec 2023 20:36:52 +0000 Subject: [PATCH 4/4] Allowed for fast decon toggle --- modules/addons/tree-decon.lua | 44 ++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/modules/addons/tree-decon.lua b/modules/addons/tree-decon.lua index 84194d7c..6e7934d1 100644 --- a/modules/addons/tree-decon.lua +++ b/modules/addons/tree-decon.lua @@ -7,14 +7,24 @@ local Roles = require 'expcore.roles' --- @dep expcore.roles local Gui = require 'expcore.gui' --- @dep expcore.gui local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data --- Global queue used to store trees that need to be removed, also chache for player roles -local chache = {} +-- Global queue used to store trees that need to be removed, also cache for player roles +local cache = {} local tree_queue = { _head=0 } -Global.register({tree_queue, chache}, function(tbl) +Global.register({tree_queue, cache}, function(tbl) tree_queue = tbl[1] - chache = tbl[2] + cache = tbl[2] end) +local function get_permission(player_index) + if cache[player_index] == nil then + local player = game.players[player_index] + if Roles.player_allowed(player, 'fast-tree-decon') then cache[player_index] = 'fast' + elseif Roles.player_allowed(player, 'standard-decon') then cache[player_index] = 'standard' + else cache[player_index] = player.force end + end + + return cache[player_index] +end -- Left menu button to toggle between fast decon and normal decon marking local HasEnabledDecon = PlayerData.Settings:combine('HasEnabledDecon') @@ -36,20 +46,14 @@ Event.add(defines.events.on_marked_for_deconstruction, function(event) -- Check which type of decon a player is allowed local index = event.player_index if not index then return end - if chache[index] == nil then - local player = game.players[index] - if Roles.player_allowed(player, 'fast-tree-decon') then chache[index] = 'fast' - elseif Roles.player_allowed(player, 'standard-decon') then chache[index] = 'standard' - else chache[index] = player.force end - end -- Check what should happen to this entity local entity = event.entity - local allow = chache[index] if not entity or not entity.valid then return end -- Not allowed to decon this entity local last_user = entity.last_user + local allow = get_permission(index) if last_user and allow ~= 'standard' and allow ~= 'fast' then entity.cancel_deconstruction(allow) return @@ -58,7 +62,6 @@ Event.add(defines.events.on_marked_for_deconstruction, function(event) -- Allowed to decon this entity, but not fast if allow ~= 'fast' then return end - local player = game.get_player(index) if not HasEnabledDecon:get(player) then return end @@ -91,13 +94,14 @@ Event.add(defines.events.on_tick, function() tree_queue._head = head end) --- Clear the chache +-- Clear the cache Event.on_nth_tick(300, function() - for key, _ in pairs(chache) do - chache[key] = nil + for key, _ in pairs(cache) do + cache[key] = nil end end) +-- Clear trees when hit with a car Event.add(defines.events.on_entity_damaged, function(event) if not (event.damage_type.name == 'impact' and event.force) then return @@ -111,5 +115,13 @@ Event.add(defines.events.on_entity_damaged, function(event) return end - event.entity.destroy() + local driver = event.cause.get_driver() + if not driver then return end + + local allow = get_permission(driver.player.index) + if allow == "fast" and HasEnabledDecon:get(driver.player) then + event.entity.destroy() + else + event.entity.order_deconstruction(event.force, driver.player) + end end)