mirror of
https://github.com/RYDE-WORK/ballistica.git
synced 2026-01-27 09:23:12 +08:00
Merge pull request #11 from Dmitry450/master
Bots AI fixes, and some game modes editings
This commit is contained in:
commit
327880d1e7
@ -69,7 +69,7 @@ from ba._appdelegate import AppDelegate
|
||||
from ba._apputils import is_browser_likely_available
|
||||
from ba._campaign import Campaign
|
||||
from ba._gameutils import (animate, animate_array, show_damage_count,
|
||||
sharedobj, timestring, cameraflash)
|
||||
sharedobj, timestring, cameraflash, distance)
|
||||
from ba._general import WeakCall, Call
|
||||
from ba._level import Level
|
||||
from ba._lobby import Lobby, Chooser
|
||||
|
||||
@ -22,12 +22,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
import math
|
||||
|
||||
import _ba
|
||||
from ba._enums import TimeType, TimeFormat, SpecialChar
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any, Dict, Sequence
|
||||
from typing import Any, Dict, Sequence, Tuple
|
||||
import ba
|
||||
|
||||
TROPHY_CHARS = {
|
||||
@ -46,6 +47,14 @@ def get_trophy_string(trophy_id: str) -> str:
|
||||
return _ba.charstr(TROPHY_CHARS[trophy_id])
|
||||
return '?'
|
||||
|
||||
def distance(vec1: Tuple[float, float, float],
|
||||
vec2: Tuple[float, float, float]) -> float:
|
||||
"""Find distance between two positions"""
|
||||
xlen = vec1[0] - vec2[0]
|
||||
ylen = vec1[1] - vec2[1]
|
||||
zlen = vec1[2] - vec2[2]
|
||||
xylen = math.sqrt(xlen**2 + ylen**2)
|
||||
return math.sqrt(xylen**2 + zlen**2)
|
||||
|
||||
def sharedobj(name: str) -> Any:
|
||||
"""Return a predefined object for the current Activity, creating if needed.
|
||||
|
||||
@ -594,7 +594,8 @@ class Blast(ba.Actor):
|
||||
ba.timer(0.4, _extra_debris_sound)
|
||||
|
||||
def handlemessage(self, msg: Any) -> Any:
|
||||
self._handlemessage_sanity_check()
|
||||
if __debug__:
|
||||
self._handlemessage_sanity_check()
|
||||
|
||||
if isinstance(msg, ba.DieMessage):
|
||||
if self.node:
|
||||
|
||||
@ -333,7 +333,9 @@ class Flag(ba.Actor):
|
||||
1.0, ba.WeakCall(self._hide_score_text))
|
||||
|
||||
def handlemessage(self, msg: Any) -> Any:
|
||||
self._handlemessage_sanity_check()
|
||||
# pylint: disable=too-many-branches
|
||||
if __debug__:
|
||||
self._handlemessage_sanity_check()
|
||||
if isinstance(msg, ba.DieMessage):
|
||||
if self.node:
|
||||
self.node.delete()
|
||||
|
||||
@ -221,7 +221,8 @@ class PlayerSpaz(Spaz):
|
||||
# pylint: disable=too-many-branches
|
||||
# pylint: disable=too-many-statements
|
||||
# pylint: disable=too-many-nested-blocks
|
||||
self._handlemessage_sanity_check()
|
||||
if __debug__:
|
||||
self._handlemessage_sanity_check()
|
||||
|
||||
# Keep track of if we're being held and by who most recently.
|
||||
if isinstance(msg, ba.PickedUpMessage):
|
||||
|
||||
@ -289,7 +289,8 @@ class PowerupBox(ba.Actor):
|
||||
|
||||
def handlemessage(self, msg: Any) -> Any:
|
||||
# pylint: disable=too-many-branches
|
||||
self._handlemessage_sanity_check()
|
||||
if __debug__:
|
||||
self._handlemessage_sanity_check()
|
||||
|
||||
if isinstance(msg, ba.PowerupAcceptMessage):
|
||||
factory = get_factory()
|
||||
|
||||
@ -28,6 +28,7 @@ import weakref
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import ba
|
||||
from ba import distance
|
||||
from bastd.actor import spaz as basespaz
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -41,7 +42,6 @@ DEFAULT_BOT_HIGHLIGHT = (0.1, 0.3, 0.1)
|
||||
PRO_BOT_COLOR = (1.0, 0.2, 0.1)
|
||||
PRO_BOT_HIGHLIGHT = (0.6, 0.1, 0.05)
|
||||
|
||||
|
||||
class SpazBotPunchedMessage:
|
||||
"""A message saying a ba.SpazBot got punched.
|
||||
|
||||
@ -192,8 +192,9 @@ class SpazBot(basespaz.Spaz):
|
||||
closest = None
|
||||
assert self._player_pts is not None
|
||||
for plpt, plvel in self._player_pts:
|
||||
dist = (plpt - botpt).length()
|
||||
|
||||
dist = distance(
|
||||
(plpt.x, plpt.y, plpt.z),
|
||||
(botpt.x, botpt.y, botpt.z))
|
||||
# Ignore player-points that are significantly below the bot
|
||||
# (keeps bots from following players off cliffs).
|
||||
if (closest_dist is None
|
||||
@ -256,7 +257,9 @@ class SpazBot(basespaz.Spaz):
|
||||
target_pt_raw = ba.Vec3(*self.target_flag.node.position)
|
||||
diff = (target_pt_raw - our_pos)
|
||||
diff = ba.Vec3(diff[0], 0, diff[2]) # don't care about y
|
||||
dist = diff.length()
|
||||
dist = distance(
|
||||
(target_pt_raw.x, 0, target_pt_raw.z),
|
||||
(our_pos.x, 0, our_pos.z))
|
||||
to_target = diff.normalized()
|
||||
|
||||
# If we're holding some non-flag item, drop it.
|
||||
@ -324,7 +327,9 @@ class SpazBot(basespaz.Spaz):
|
||||
target_vel * dist_raw * 0.3 * self._lead_amount)
|
||||
|
||||
diff = (target_pt - our_pos)
|
||||
dist = diff.length()
|
||||
dist = distance(
|
||||
(target_pt.x, target_pt.y, target_pt.z),
|
||||
(our_pos.x, our_pos.y, our_pos.z))
|
||||
to_target = diff.normalized()
|
||||
|
||||
if self._mode == 'throw':
|
||||
@ -513,7 +518,8 @@ class SpazBot(basespaz.Spaz):
|
||||
|
||||
def handlemessage(self, msg: Any) -> Any:
|
||||
# pylint: disable=too-many-branches
|
||||
self._handlemessage_sanity_check()
|
||||
if __debug__:
|
||||
self._handlemessage_sanity_check()
|
||||
|
||||
# Keep track of if we're being held and by who most recently.
|
||||
if isinstance(msg, ba.PickedUpMessage):
|
||||
|
||||
@ -176,7 +176,7 @@ class AssaultGame(ba.TeamGameActivity):
|
||||
return
|
||||
|
||||
# If its another team's player, they scored.
|
||||
player_team = player.get_team()
|
||||
player_team = player.team
|
||||
if player_team is not team:
|
||||
|
||||
# Prevent multiple simultaneous scores.
|
||||
|
||||
@ -63,7 +63,8 @@ class CTFFlag(stdflag.Flag):
|
||||
self.touch_return_time = float(
|
||||
self.activity.settings['Flag Touch Return Time'])
|
||||
|
||||
def get_team(self) -> ba.Team:
|
||||
@property
|
||||
def team(self) -> ba.Team:
|
||||
"""return the flag's team."""
|
||||
return self._team
|
||||
|
||||
@ -224,7 +225,7 @@ class CaptureTheFlagGame(ba.TeamGameActivity):
|
||||
flag = ba.get_collision_info("opposing_node").getdelegate()
|
||||
assert isinstance(flag, CTFFlag)
|
||||
|
||||
if flag.get_team() is team:
|
||||
if flag.team is team:
|
||||
team.gamedata['home_flag_at_base'] = True
|
||||
|
||||
# If the enemy flag is already here, score!
|
||||
@ -331,7 +332,7 @@ class CaptureTheFlagGame(ba.TeamGameActivity):
|
||||
except Exception:
|
||||
return # Can happen when we kill a flag.
|
||||
|
||||
if flag.get_team() is team:
|
||||
if flag.team is team:
|
||||
|
||||
# Check times here to prevent too much flashing.
|
||||
if ('last_flag_leave_time' not in team.gamedata
|
||||
@ -487,7 +488,7 @@ class CaptureTheFlagGame(ba.TeamGameActivity):
|
||||
elif isinstance(msg, stdflag.FlagDeathMessage):
|
||||
assert isinstance(msg.flag, CTFFlag)
|
||||
ba.timer(0.1,
|
||||
ba.Call(self._spawn_flag_for_team, msg.flag.get_team()))
|
||||
ba.Call(self._spawn_flag_for_team, msg.flag.team))
|
||||
elif isinstance(msg, stdflag.FlagPickedUpMessage):
|
||||
# Store the last player to hold the flag for scoring purposes.
|
||||
assert isinstance(msg.flag, CTFFlag)
|
||||
|
||||
@ -228,10 +228,10 @@ class ConquestGame(ba.TeamGameActivity):
|
||||
except Exception:
|
||||
return # Player may have left and his body hit the flag.
|
||||
|
||||
if flag.get_team() is not player.get_team():
|
||||
flag.set_team(player.get_team())
|
||||
flag.light.color = player.get_team().color
|
||||
flag.node.color = player.get_team().color
|
||||
if flag.team is not player.team:
|
||||
flag.set_team(player.team)
|
||||
flag.light.color = player.team.color
|
||||
flag.node.color = player.team.color
|
||||
self.stats.player_scored(player, 10, screenmessage=False)
|
||||
ba.playsound(self._swipsound)
|
||||
self._flash_flag(flag)
|
||||
|
||||
@ -255,7 +255,7 @@ class HockeyGame(ba.TeamGameActivity):
|
||||
except Exception:
|
||||
player = puck = None
|
||||
if player and puck:
|
||||
puck.last_players_to_touch[player.get_team().get_id()] = player
|
||||
puck.last_players_to_touch[player.team.get_id()] = player
|
||||
|
||||
def _kill_puck(self) -> None:
|
||||
self._puck = None
|
||||
|
||||
@ -241,7 +241,7 @@ class RaceGame(ba.TeamGameActivity):
|
||||
|
||||
player.gamedata['last_region'] = this_region
|
||||
if last_region >= len(self._regions) - 2 and this_region == 0:
|
||||
team = player.get_team()
|
||||
team = player.team
|
||||
player.gamedata['lap'] = min(self.settings['Laps'],
|
||||
player.gamedata['lap'] + 1)
|
||||
|
||||
@ -283,10 +283,10 @@ class RaceGame(ba.TeamGameActivity):
|
||||
# If the whole team has finished the race.
|
||||
if team.gamedata['lap'] == self.settings['Laps']:
|
||||
ba.playsound(self._score_sound)
|
||||
player.get_team().gamedata['finished'] = True
|
||||
player.team.gamedata['finished'] = True
|
||||
assert self._timer is not None
|
||||
self._last_team_time = (
|
||||
player.get_team().gamedata['time']) = (
|
||||
player.team.gamedata['time']) = (
|
||||
ba.time() - self._timer.getstarttime())
|
||||
self._check_end_game()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user