1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

building fixes

This commit is contained in:
Robin Appelman 2022-08-27 14:31:15 +02:00
commit cf58a0609a

View file

@ -14,6 +14,7 @@ use crate::demo::sendprop::{SendProp, SendPropIdentifier, SendPropValue};
use crate::demo::vector::{Vector, VectorXY}; use crate::demo::vector::{Vector, VectorXY};
use crate::{MessageType, ParserState, ReadResult, Stream}; use crate::{MessageType, ParserState, ReadResult, Stream};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::str::FromStr; use std::str::FromStr;
@ -256,7 +257,7 @@ impl Kill {
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)] #[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
pub struct GameState { pub struct GameState {
pub players: Vec<Player>, pub players: Vec<Player>,
pub buildings: Vec<Building>, pub buildings: BTreeMap<EntityId, Building>,
pub world: Option<World>, pub world: Option<World>,
pub kills: Vec<Kill>, pub kills: Vec<Kill>,
pub tick: u32, pub tick: u32,
@ -289,37 +290,13 @@ impl GameState {
entity_id: EntityId, entity_id: EntityId,
class: BuildingClass, class: BuildingClass,
) -> &mut Building { ) -> &mut Building {
let index = match self self.buildings
.buildings .entry(entity_id)
.iter() .or_insert_with(|| Building::new(entity_id, class))
.enumerate()
.find(|(_index, building)| building.entity_id() == entity_id)
.map(|(index, _)| index)
{
Some(index) => index,
None => {
let index = self.buildings.len();
self.buildings.push(Building::new(entity_id, class));
index
}
};
&mut self.buildings[index]
} }
pub fn remove_building(&mut self, entity_id: EntityId) { pub fn remove_building(&mut self, entity_id: EntityId) {
match self self.buildings.remove(&entity_id);
.buildings
.iter()
.enumerate()
.find(|(_index, building)| building.entity_id() == entity_id)
.map(|(index, _)| index)
{
Some(index) => {
self.buildings.remove(index);
}
_ => {}
};
} }
} }
@ -354,6 +331,9 @@ impl MessageHandler for GameStateAnalyser {
GameEvent::RoundStart(_) => { GameEvent::RoundStart(_) => {
self.state.buildings.clear(); self.state.buildings.clear();
} }
GameEvent::TeamPlayRoundStart(_) => {
self.state.buildings.clear();
}
GameEvent::ObjectDestroyed(ObjectDestroyedEvent { index, .. }) => { GameEvent::ObjectDestroyed(ObjectDestroyedEvent { index, .. }) => {
self.state.remove_building((*index as u32).into()); self.state.remove_building((*index as u32).into());
} }