Created
October 17, 2019 11:44
-
-
Save ManuelBlanc/695aa4560c28976b68d5132990cce0b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- [SERVER] The attack generation. | |
local _attack_gen = 0 | |
-- [SERVER] When the attack animation is initiated, also request an attack ACK. | |
mod:hook_safe(BTAttackAction, "enter", function(self, unit, bb, t) | |
if bb.action.unblockable then return end | |
local mob_id = Managers.state.unit_storage:go_id(unit) | |
local atk_id = _attack_gen | |
mod:network_send("request_attack_ack", mob_id, atk_id) | |
bb._csb_needs_ack, bb._csb_mob_id, bb._csb_atk_id = true, mob_id, atk_id | |
_attack_gen = (atk_id+1) % 0x10000 | |
end | |
-- [CLIENT] Attacks that require an acknowledgement. | |
local _client_ack_attacks = {} | |
-- [CLIENT] Keep track of attacks that should be confirmed. | |
mod:network_register("request_attack_ack", function(_, mob_id, atk_id, fatigue_type, attack_direction) | |
_client_ack_attacks[mob_id] = { atk_id, fatigue_type, attack_direction } | |
end) | |
-- [SERVER] Buffer attacks that connect and require an ACK. | |
mod:hook(AnimationCallbackTemplates.server, "anim_cb_damage", function(func, unit, param) | |
local bb = BLACKBOARDS[unit] | |
local needs_ack = bb._csb_needs_ack | |
if needs_ack then mod:hook_enable(DamageUtils, "add_damage_network") end | |
func(unit, param) | |
if needs_ack then mod:hook_disable(DamageUtils, "add_damage_network") end | |
bb._csb_needs_ack = false | |
end | |
-- [CLIENT] Confirm damage at the damage point on clients. | |
AnimationCallbackTemplates.client.anim_cb_damage = function(unit, param) | |
if Managers.player.is_server then return end | |
local unit_storage = Managers.state.unit_storage | |
local mob_id = unit_storage:go_id(unit) | |
local atk_data = _client_ack_attacks[mob_id] | |
local target_unit = Managers.player:local_player().player_unit | |
if atk_id and not DamageUtils.check_block(unit, target_unit, action.fatigue_type, attack_direction) then | |
mod:network_send("attack_confirm_damage", mob_id, atk_id) | |
end | |
_client_ack_attacks[mob_id] = nil | |
end | |
-- [SERVER] Damage buffering handling. | |
mod:hook_origin(DamageUtils, "add_damage_network", function(target_unit, attacker_unit, ...) | |
BLACKBOARDS[attacker_unit]._csb_dmg_data = { | |
n = select("#", ...), target_unit, attacker_unit, ... | |
} | |
end | |
mod:hook_disable(DamageUtils, "add_damage_network") | |
-- [SERVER] Apply the buffered damage on a confirmation. | |
mod:network_register("attack_confirm_damage", function(_, mob_id, atk_id) | |
local mob = Managers.state.unit_storage:unit(mob_id) | |
local bb = BLACKBOARDS[mob] | |
local data = bb._csb_dmg_data | |
if data and data.atk_id == atk_id then | |
DamageUtils.add_damage_network(unpack(data, 1, data.n)) | |
end | |
_attacks[mob_id] = nil | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment