bombsquad-plugin-manager/plugins/utilities/disable_friendly_fire.py

111 lines
4 KiB
Python
Raw Permalink Normal View History

# ba_meta require api 9
2024-01-24 14:17:05 +03:00
from __future__ import annotations
from typing import TYPE_CHECKING
import babase
import bascenev1 as bs
import bascenev1lib
from bascenev1lib.gameutils import SharedObjects
if TYPE_CHECKING:
pass
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
class BombPickupMessage:
""" message says that someone pick up the dropped bomb """
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# for bs.FreezeMessage
freeze: bool = True
2025-06-24 00:55:09 +05:30
# ba_meta export babase.Plugin
2025-06-23 19:27:10 +00:00
2024-01-24 14:17:05 +03:00
class Plugin(babase.Plugin):
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# there are two ways to ignore our team player hits
# either change playerspaz handlemessage or change spaz handlemessage
def playerspaz_new_handlemessage(func: fuction) -> fuction:
def wrapper(*args, **kwargs):
global freeze
2024-01-24 11:26:19 +00:00
# only run if session is dual team
2024-01-24 14:17:05 +03:00
if isinstance(args[0].activity.session, bs.DualTeamSession):
# when spaz got hurt by any reason this statement is runs.
if isinstance(args[1], bs.HitMessage):
our_team_players: list[type(args[0]._player)]
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# source_player
attacker = args[1].get_source_player(type(args[0]._player))
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# our team payers
our_team_players = args[0]._player.team.players.copy()
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
if len(our_team_players) > 0:
2024-01-24 11:26:19 +00:00
# removing our self
our_team_players.remove(args[0]._player)
# if we honding teammate or if we have a shield, do hit.
for player in our_team_players:
if player.actor.exists() and args[0]._player.actor.exists():
if args[0]._player.actor.node.hold_node == player.actor.node or args[0]._player.actor.shield:
our_team_players.remove(player)
break
if attacker in our_team_players:
freeze = False
return None
else:
freeze = True
2024-01-24 14:17:05 +03:00
# if ice_bomb blast hits any spaz this statement runs.
elif isinstance(args[1], bs.FreezeMessage):
if not freeze:
2024-01-24 11:26:19 +00:00
freeze = True # use it and reset it
2024-01-24 14:17:05 +03:00
return None
# orignal unchanged code goes here
func(*args, **kwargs)
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
return wrapper
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# replace original fuction to modified function
bascenev1lib.actor.playerspaz.PlayerSpaz.handlemessage = playerspaz_new_handlemessage(
bascenev1lib.actor.playerspaz.PlayerSpaz.handlemessage)
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# let's add a message when bomb is pick by player
def bombfact_new_init(func: function) -> function:
def wrapper(*args):
2024-01-24 11:26:19 +00:00
func(*args) # original code
2024-01-24 14:17:05 +03:00
args[0].bomb_material.add_actions(
conditions=('they_have_material', SharedObjects.get().pickup_material),
actions=('message', 'our_node', 'at_connect', BombPickupMessage()),
)
return wrapper
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
# you get the idea
bascenev1lib.actor.bomb.BombFactory.__init__ = bombfact_new_init(
bascenev1lib.actor.bomb.BombFactory.__init__)
2024-01-24 11:26:19 +00:00
2024-01-24 14:17:05 +03:00
def bomb_new_handlemessage(func: function) -> function:
def wrapper(*args, **kwargs):
2024-01-24 11:26:19 +00:00
# only run if session is dual team
2024-01-24 14:17:05 +03:00
if isinstance(args[0].activity.session, bs.DualTeamSession):
if isinstance(args[1], BombPickupMessage):
# get the pickuper and assign the pickuper to the source_player(attacker) of bomb blast
for player in args[0].activity.players:
if player.actor.exists():
if player.actor.node.hold_node == args[0].node:
args[0]._source_player = player
break
2024-01-24 11:26:19 +00:00
func(*args, **kwargs) # original
2024-01-24 14:17:05 +03:00
return wrapper
bascenev1lib.actor.bomb.Bomb.handlemessage = bomb_new_handlemessage(
2024-01-24 11:26:19 +00:00
bascenev1lib.actor.bomb.Bomb.handlemessage)