Skip to content

Commit

Permalink
Merge pull request #8 from sebosp/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
sebosp authored Jun 23, 2023
2 parents 1a49a64 + 7d39949 commit c1c549d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = "Visualizer experiments for Starcraft II - Replay data"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rerun = "0.5.1"
rerun = {version = "0.7.0", features = ["native_viewer"] }
s2protocol = "1.1.1"
nom-mpq = "0.1.1"
colored = "2.0.0"
Expand Down
109 changes: 82 additions & 27 deletions src/game_events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
use super::*;
use rerun::components::Arrow3D;
use rerun::transform::TranslationRotationScale3D;
use rerun::{
components::{Box3D, Quaternion, Radius, Rigid3, Transform, Vec3D},
components::{Box3D, Radius, Transform3D, Vec3D},
MsgSender,
};
use s2protocol::game_events::*;
Expand All @@ -15,21 +16,26 @@ pub fn register_camera_update(
game_loop: i64,
user_id: i64,
camera_update: &CameraUpdateEvent,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
if let Some(target) = &camera_update.m_target {
MsgSender::new(format!("Unit/999{}/Player", user_id))
.with_time(sc2_rerun.timeline, game_loop)
.with_splat(Box3D::new(3.0, 3.0, 0.0))?
.with_splat(Transform::Rigid3(Rigid3 {
rotation: Quaternion::new(0., 0., 0., 0.),
translation: Vec3D::new(
(target.x as f32 / 250f32) - 1.5,
(-1. * target.y as f32 / 250f32) - 1.5,
0.,
.with_splat(Transform3D::new(TranslationRotationScale3D {
translation: Some(
[
(target.x as f32 / 250f32) - 1.5,
(-1. * target.y as f32 / 250f32) - 1.5,
0.,
]
.into(),
),
rotation: None,
scale: None,
}))?
.with_splat(user_color(user_id))?
.send(&sc2_rerun.rerun_session)?;
.send(recording_stream)?;
}
Ok(())
}
Expand All @@ -39,13 +45,20 @@ pub fn register_cmd(
game_loop: i64,
user_id: i64,
game_cmd: &GameSCmdEvent,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
match &game_cmd.m_data {
GameSCmdData::TargetPoint(target) => {
register_update_target_point(sc2_rerun, game_loop, user_id, target)?;
register_update_target_point(sc2_rerun, game_loop, user_id, target, recording_stream)?;
}
GameSCmdData::TargetUnit(target_unit) => {
register_update_target_unit(sc2_rerun, game_loop, user_id, target_unit)?;
register_update_target_unit(
sc2_rerun,
game_loop,
user_id,
target_unit,
recording_stream,
)?;
}
GameSCmdData::Data(data) => {
tracing::info!("GameSCmdData: {}", data);
Expand All @@ -60,6 +73,7 @@ pub fn register_update_target_point(
game_loop: i64,
user_id: i64,
target_point: &GameSMapCoord3D,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
let unit_target_pos = Vec3D::new(
target_point.x as f32 / GAME_EVENT_POS_RATIO,
Expand All @@ -85,7 +99,7 @@ pub fn register_update_target_point(
),
})?
.with_splat(user_color(user_id))?
.send(&sc2_rerun.rerun_session)?;
.send(recording_stream)?;
}
}
Ok(())
Expand All @@ -96,6 +110,7 @@ pub fn register_update_target_unit(
game_loop: i64,
user_id: i64,
target_unit: &GameSCmdDataTargetUnit,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
let unit_target_pos = Vec3D::new(
target_unit.m_snapshot_point.x as f32 / GAME_EVENT_POS_RATIO,
Expand All @@ -121,7 +136,7 @@ pub fn register_update_target_unit(
),
})?
.with_splat(user_color(user_id))?
.send(&sc2_rerun.rerun_session)?;
.send(recording_stream)?;
}
}
Ok(())
Expand All @@ -132,6 +147,7 @@ pub fn unmark_previously_selected_units(
sc2_rerun: &SC2Rerun,
game_loop: i64,
user_id: i64,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
if let Some(state) = sc2_rerun.sc2_state.user_state.get(&user_id) {
for prev_unit in &state.control_groups[ACTIVE_UNITS_GROUP_IDX] {
Expand All @@ -143,7 +159,7 @@ pub fn unmark_previously_selected_units(
MsgSender::new(format!("Unit/{}/Born", unit_index))
.with_time(sc2_rerun.timeline, game_loop)
.with_splat(Radius(unit.radius))?
.send(&sc2_rerun.rerun_session)?;
.send(recording_stream)?;
}
}
}
Expand All @@ -156,6 +172,7 @@ pub fn mark_selected_units(
game_loop: i64,
_user_id: i64,
selected_units: &[u32],
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
for new_selected_unit in selected_units {
let unit_index = unit_tag_index(*new_selected_unit as i64);
Expand All @@ -166,7 +183,7 @@ pub fn mark_selected_units(
MsgSender::new(format!("Unit/{}/Born", unit_index))
.with_time(sc2_rerun.timeline, game_loop)
.with_splat(Radius(unit.radius))?
.send(&sc2_rerun.rerun_session)?;
.send(recording_stream)?;
}
}
Ok(())
Expand All @@ -185,14 +202,16 @@ pub fn register_selection_delta(
game_loop: i64,
user_id: i64,
selection_delta: &GameSSelectionDeltaEvent,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
if selection_delta.m_control_group_id == ACTIVE_UNITS_GROUP_IDX as u8 {
unmark_previously_selected_units(sc2_rerun, game_loop, user_id)?;
unmark_previously_selected_units(sc2_rerun, game_loop, user_id, recording_stream)?;
mark_selected_units(
sc2_rerun,
game_loop,
user_id,
&selection_delta.m_delta.m_add_unit_tags,
recording_stream,
)?;
}
Ok(())
Expand All @@ -206,13 +225,17 @@ pub fn update_control_group(
user_id: i64,
ctrl_group_evt: &GameSControlGroupUpdateEvent,
updated_units: Vec<u32>,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
unmark_previously_selected_units(sc2_rerun, game_loop, user_id)?;
match ctrl_group_evt.m_control_group_update {
GameEControlGroupUpdate::ERecall => {
mark_selected_units(sc2_rerun, game_loop, user_id, &updated_units)?;
}
_ => {}
unmark_previously_selected_units(sc2_rerun, game_loop, user_id, recording_stream)?;
if ctrl_group_evt.m_control_group_update == GameEControlGroupUpdate::ERecall {
mark_selected_units(
sc2_rerun,
game_loop,
user_id,
&updated_units,
recording_stream,
)?;
}
Ok(())
}
Expand All @@ -224,25 +247,57 @@ pub fn add_game_event(
user_id: i64,
evt: &ReplayGameEvent,
updated_units: Vec<u32>,
recording_stream: &RecordingStream,
) -> Result<(), SwarmyError> {
match &evt {
ReplayGameEvent::CameraUpdate(camera_update) => {
register_camera_update(&sc2_rerun, game_loop, user_id, camera_update)?;
register_camera_update(
sc2_rerun,
game_loop,
user_id,
camera_update,
recording_stream,
)?;
}
ReplayGameEvent::Cmd(game_cmd) => {
register_cmd(sc2_rerun, game_loop, user_id, game_cmd)?;
register_cmd(sc2_rerun, game_loop, user_id, game_cmd, recording_stream)?;
}
ReplayGameEvent::CmdUpdateTargetPoint(target_point) => {
register_update_target_point(sc2_rerun, game_loop, user_id, &target_point.m_target)?;
register_update_target_point(
sc2_rerun,
game_loop,
user_id,
&target_point.m_target,
recording_stream,
)?;
}
ReplayGameEvent::CmdUpdateTargetUnit(target_unit) => {
register_update_target_unit(sc2_rerun, game_loop, user_id, &target_unit.m_target)?;
register_update_target_unit(
sc2_rerun,
game_loop,
user_id,
&target_unit.m_target,
recording_stream,
)?;
}
ReplayGameEvent::SelectionDelta(selection_delta) => {
register_selection_delta(sc2_rerun, game_loop, user_id, &selection_delta)?;
register_selection_delta(
sc2_rerun,
game_loop,
user_id,
selection_delta,
recording_stream,
)?;
}
ReplayGameEvent::ControlGroupUpdate(ctrl_group) => {
update_control_group(sc2_rerun, game_loop, user_id, &ctrl_group, updated_units)?;
update_control_group(
sc2_rerun,
game_loop,
user_id,
ctrl_group,
updated_units,
recording_stream,
)?;
}
_ => {}
}
Expand Down
36 changes: 24 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rerun::components::{ColorRGBA, Vec3D};
use rerun::external::re_log_types::DataTableError;
use rerun::external::re_viewer::external::eframe::Error as eframe_Error;
use rerun::time::Timeline;
use rerun::Session;
use rerun::RecordingStream;
use rerun::{time, MsgSenderError};
use s2protocol::{S2ProtocolError, SC2EventType, SC2ReplayFilters, SC2ReplayState};
pub use tracker_events::*;
Expand Down Expand Up @@ -55,11 +55,11 @@ pub struct SC2Rerun {
/// The absolute GameEvevnt loop timeline, the tracker loop should be relative to it.
pub timeline: Timeline,

/// The rerun session to display data.
pub rerun_session: Session,

/// The SC2 replay state as it steps through game loops.
pub sc2_state: SC2ReplayState,

/// The file path containing the SC2 Replay
pub file_path: String,
}

impl SC2Rerun {
Expand All @@ -68,38 +68,50 @@ impl SC2Rerun {
filters: SC2ReplayFilters,
include_stats: bool,
) -> Result<Self, SwarmyError> {
let rerun_session = rerun::SessionBuilder::new(file_path).buffered();
let timeline = rerun::time::Timeline::new("game_timeline", time::TimeType::Sequence);
let sc2_state = SC2ReplayState::new(file_path, filters, include_stats)?;
Ok(Self {
timeline,
rerun_session,
sc2_state,
file_path: file_path.to_string(),
})
}

pub fn add_events(&mut self) -> Result<(), SwarmyError> {
pub fn add_events(&mut self, recording_stream: &RecordingStream) -> Result<(), SwarmyError> {
while let Some((event, updated_units)) = self.sc2_state.transduce() {
match event {
SC2EventType::Tracker {
tracker_loop,
event,
} => add_tracker_event(&self, tracker_loop, &event, updated_units)?,
} => {
add_tracker_event(self, tracker_loop, &event, updated_units, recording_stream)?
}
SC2EventType::Game {
game_loop,
user_id,
event,
} => add_game_event(&self, game_loop, user_id, &event, updated_units)?,
} => add_game_event(
self,
game_loop,
user_id,
&event,
updated_units,
recording_stream,
)?,
}
}
Ok(())
}

pub fn show(&self) -> Result<(), SwarmyError> {
Ok(rerun::native_viewer::show(&self.rerun_session)?)
pub fn show(mut self) -> Result<(), SwarmyError> {
let recording_info = rerun::new_recording_info(self.file_path.clone());
rerun::native_viewer::spawn(recording_info, Default::default(), move |rec_stream| {
self.add_events(&rec_stream).unwrap();
})?;
Ok(())
}
}

pub fn from_vec3d(source: s2protocol::Vec3D) -> Vec3D {
Vec3D::new(source.0[0] as f32, source.0[1] as f32, source.0[2] as f32)
Vec3D::new(source.0[0], source.0[1], source.0[2])
}
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::Parser;
use s2protocol::SC2ReplayFilters;
use swarmy::*;
use tracing_subscriber;

use rerun::external::re_memory::AccountingAllocator;

Expand Down Expand Up @@ -66,8 +65,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
max_events: cli.filter_max_events,
};
tracing::info!("Filters: {:?}", filters);
let mut sc2_rerun = SC2Rerun::new(&cli.source, filters, cli.include_stats)?;
sc2_rerun.add_events()?;
let sc2_rerun = SC2Rerun::new(&cli.source, filters, cli.include_stats)?;
sc2_rerun.show()?;
Ok(())
}
Loading

0 comments on commit c1c549d

Please sign in to comment.