Skip to content

Commit f169502

Browse files
committed
Add debug_navpoint_matrix mod
1 parent 5ce54e2 commit f169502

11 files changed

Lines changed: 123 additions & 5 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
- run: cargo build --release
1515
- run: |
1616
mkdir mods
17+
mv target/i686-pc-windows-msvc/release/debug_navpoint_matrix.dll ./mods/
1718
mv target/i686-pc-windows-msvc/release/fix_bath_house_bribe_blunders.dll ./mods/
1819
mv target/i686-pc-windows-msvc/release/fix_damage_to_offside_ship_artillery.dll ./mods/
1920
mv target/i686-pc-windows-msvc/release/fix_invulnerable_ship_artillery_slots.dll ./mods/

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ resolver = "2"
44
members = [
55
"aimcli",
66
"cprcli",
7+
"mod-debug-navpoint-matrix",
78
"mod-fix-bath-house-bribe-blunders",
89
"mod-fix-damage-to-offside-ship-artillery",
910
"mod-fix-invulnerable-ship-artillery-slots",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "mod-debug-navpoint-matrix"
3+
edition = "2021"
4+
version.workspace = true
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
name="debug_navpoint_matrix"
9+
10+
[dependencies]
11+
log = { workspace = true }
12+
p3-api = { path = "../p3-api" }
13+
hooklet = { workspace = true }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::{
2+
mem,
3+
sync::atomic::{AtomicPtr, Ordering},
4+
};
5+
6+
use hooklet::windows::x86::{hook_call_rel32, CallRel32Hook};
7+
use log::debug;
8+
use p3_api::{class35::Class35Ptr, data::ddraw_set_constant_color, mods, ui::draw_geometry_abs};
9+
10+
static DRAW_NAVIGATION_LINE_HOOK_PTR: AtomicPtr<CallRel32Hook> = AtomicPtr::new(std::ptr::null_mut());
11+
const STROKE_LENGTH: i32 = 4;
12+
13+
#[no_mangle]
14+
unsafe extern "C" fn start() -> u32 {
15+
mods::init_mod();
16+
17+
debug!("Deploying draw_navigation_line hook");
18+
match hook_call_rel32(0x4AE38, draw_line_hook as usize as u32) {
19+
Ok(hook) => {
20+
DRAW_NAVIGATION_LINE_HOOK_PTR.store(Box::into_raw(Box::new(hook)), Ordering::SeqCst);
21+
}
22+
Err(_) => return 1,
23+
}
24+
25+
0
26+
}
27+
28+
#[no_mangle]
29+
unsafe extern "thiscall" fn draw_line_hook(this: u32, screen_rectangle: u32) {
30+
let class35 = Class35Ptr::new();
31+
ddraw_set_constant_color(0xffcc0000);
32+
for i in 0..class35.get_nav_vec_count() {
33+
let point = class35.get_nav_vec_entry(i as _);
34+
let x = nav_vec_coord_to_game_coord(point.0 as _);
35+
let y = nav_vec_coord_to_game_coord(point.1 as _);
36+
draw_marker(x, y);
37+
}
38+
39+
let orig_address = (*DRAW_NAVIGATION_LINE_HOOK_PTR.load(Ordering::SeqCst)).old_absolute;
40+
let orig: extern "thiscall" fn(this: u32, screen_rectangle: u32) = unsafe { mem::transmute(orig_address) };
41+
orig(this, screen_rectangle)
42+
}
43+
44+
unsafe fn draw_marker(x: i32, y: i32) {
45+
draw_geometry_abs(x - STROKE_LENGTH, y - STROKE_LENGTH, x + STROKE_LENGTH, y + STROKE_LENGTH);
46+
draw_geometry_abs(x - STROKE_LENGTH, y + STROKE_LENGTH, x + STROKE_LENGTH, y - STROKE_LENGTH);
47+
}
48+
49+
fn nav_vec_coord_to_game_coord(i: i32) -> i32 {
50+
(26 * i + 487) * 17 / 125
51+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod ffi;

navpointmatrixcli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Build Connected Nodes
44
- `cargo run --bin navpointmatrixcli -- connected-nodes-from-navpoint-matrix --input "C:\Users\Benni\Patrician 3_workbench\navdata\matrix_int.dat" --output ./connected_nodes.navpointmatrix.dat`
5-
- `cargo run --bin navpointmatrixcli -- connected-nodes-from-navigation-data --navigation-matrix "C:\Users\Benni\Patrician 3_workbench\navdata\nav_matrix.dat" --navigation-vector "C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat" --output ./connected_nodes.navpointdata.dat`
5+
- `cargo run --bin navpointmatrixcli -- connected-nodes-from-navigation-data --navigation-matrix "C:\Users\Benni\Patrician 3_workbench\navdata\nav_matrix.dat" --navigation-vector "C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat" --calculation-mode bresenham-line --output ./connected_nodes.navpointdata.dat`
66

77
## Build Navpoint Matrix
88
- `cargo run --release --bin navpointmatrixcli -- navpoint-matrix --navigation-vector "C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat" --connected-nodes .\connected_nodes.navpointmatrix.dat --output ./matrix_int.dat`

p3-api/src/class35.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ impl Class35Ptr {
9797
Self { address: *CLASS35_PTR_ADDRESS }
9898
}
9999

100+
pub unsafe fn get_nav_vec_entry(&self, offset: usize) -> (i16, i16) {
101+
let ptr: *const i16 = self.get(0x0c);
102+
(*ptr.add(2 * offset), *ptr.add(2 * offset + 1))
103+
}
104+
105+
pub unsafe fn get_nav_vec_count(&self) -> i16 {
106+
self.get(0x1c)
107+
}
108+
100109
pub unsafe fn calculate_town_route(&self, source: TownId, destination: TownId) -> Option<ShipRoutePtr> {
101110
let source_data = StaticTownDataPtr::new(source);
102111
let destination_data = StaticTownDataPtr::new(destination);

p3-api/src/missions/alderman_missions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl FoundTownPtr {
7777
let mut wares = vec![];
7878
let raw = self.get_production_effective_raw();
7979
for i in 0..17 {
80-
if raw & 1 << i != 0 {
80+
if raw & (1 << i) != 0 {
8181
wares.push(FacilityId::from_usize(i + 4).unwrap());
8282
}
8383
}

p3-api/src/ui/class37.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ impl Class37Ptr {
2020
}
2121
}
2222

23-
pub fn get_x(&self) -> i16 {
23+
pub fn get_x(&self) -> i32 {
24+
unsafe { self.get(0x14) }
25+
}
26+
27+
pub fn get_y(&self) -> i32 {
28+
unsafe { self.get(0x18) }
29+
}
30+
31+
pub fn get_offset_x(&self) -> i16 {
2432
unsafe { self.get(0x29e4) }
2533
}
2634

27-
pub fn get_y(&self) -> i16 {
35+
pub fn get_offset_y(&self) -> i16 {
2836
unsafe { self.get(0x29e6) }
2937
}
3038
}

p3-api/src/ui/ddraw.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::mem;
2+
3+
pub fn ddraw_sgl_draw_line(x1: f32, y1: f32, x2: f32, y2: f32) {
4+
let function: extern "cdecl" fn(x1: f32, y1: f32, x2: f32, y2: f32) = unsafe { mem::transmute(0x004BB390) };
5+
function(x1, y1, x2, y2)
6+
}

0 commit comments

Comments
 (0)