basic nav height query

This commit is contained in:
Robin Appelman 2020-05-02 16:55:41 +02:00
commit 94d7095a06
5 changed files with 158 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
Cargo.lock
*.smx

View file

@ -4,6 +4,10 @@ version = "0.1.0"
authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
name = "sourcenav"
[dependencies]
sm-ext = { version = "0.3.0", git = "https://github.com/asherkin/sm-ext-rs" }
sourcenav = "0.2.0"

29
include/sourcenav.inc Normal file
View file

@ -0,0 +1,29 @@
#if defined _sourcenav_included
#endinput
#endif
#define _sourcenav_included
methodmap SourceNav < Handle {
public native SourceNav(char[] path);
public native float query(float x, float y, float z_guess);
}
public Extension __ext_sourcenav =
{
name = "sourcenav",
file = "sourcenav.ext",
autoload = 1,
#if defined REQUIRE_EXTENSIONS
required = 1,
#else
required = 0,
#endif
};
#if !defined REQUIRE_EXTENSIONS
public void __ext_sourcenav_SetNTVOptional()
{
}
#endif

31
plugin/example.sp Normal file
View file

@ -0,0 +1,31 @@
#include <sourcenav>
new SourceNav:sourceNav = null;
public void OnPluginStart() {
new String:map[128];
GetCurrentMap(map, sizeof(map));
new String:nav[128];
Format(nav, sizeof(nav), "tf/maps/%s.nav", map);
PrintToServer("Loading %s", nav);
sourceNav = new SourceNav(nav);
RegServerCmd("sm_nav_query", Query, "Query the nav file at x/y point");
}
public Action:Query(args) {
new String:x_str[128];
GetCmdArg(1, x_str, sizeof(x_str));
new String:y_str[128];
GetCmdArg(2, y_str, sizeof(y_str));
float x = StringToFloat(x_str);
float y = StringToFloat(y_str);
PrintToServer("x: %f, y: %f", x, y);
float z = sourceNav.query(x, y, 0.0);
PrintToServer("Z: %f", z);
return Plugin_Handled;
}

View file

@ -1,7 +1,94 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
use sm_ext::{
cell_t, native, register_natives, HandleError, HandleId, HandleType, IExtension,
IExtensionInterface, IHandleSys, IPluginContext, IShareSys, SMExtension, TryIntoPlugin,
};
use sourcenav::{get_quad_tree, NavQuadTree};
use std::cell::RefCell;
use std::error::Error;
use std::ffi::CStr;
use std::fs::read;
use std::rc::Rc;
struct NavTree(NavQuadTree);
impl<'ctx> TryIntoPlugin<'ctx> for NavTree {
type Error = HandleError;
fn try_into_plugin(self, ctx: &'ctx IPluginContext) -> Result<cell_t, Self::Error> {
let object = Rc::new(RefCell::new(self));
let handle = MyExtension::handle_type().create_handle(object, ctx.get_identity(), None)?;
Ok(handle.into())
}
}
#[native]
fn native_obj_new(_ctx: &IPluginContext, path: &CStr) -> NavTree {
println!("path: {}", path.to_str().unwrap());
println!(
"pwd: {}",
std::env::current_dir().unwrap().to_str().unwrap()
);
let data = read(path.to_str().unwrap()).unwrap();
NavTree(get_quad_tree(data).unwrap())
}
#[native]
fn native_obj_query(
ctx: &IPluginContext,
this: HandleId,
x: f32,
y: f32,
z_guess: f32,
) -> Result<f32, Box<dyn Error>> {
let this = MyExtension::handle_type().read_handle(this, ctx.get_identity())?;
let this = this.try_borrow()?;
Ok(this.0.find_best_height(x, y, z_guess))
}
#[derive(Default, SMExtension)]
#[extension(name = "sourcenav", description = ".nav parser")]
pub struct MyExtension {
handle_type: Option<HandleType<RefCell<NavTree>>>,
}
impl MyExtension {
/// Helper to get the extension singleton from the global provided by sm-ext.
/// This is implemented here rather than by the SMExtension derive to aid code completion.
fn get() -> &'static Self {
EXTENSION_GLOBAL.with(|ext| unsafe { &(*ext.borrow().unwrap()).delegate })
}
fn handle_type() -> &'static HandleType<RefCell<NavTree>> {
Self::get().handle_type.as_ref().unwrap()
}
}
impl IExtensionInterface for MyExtension {
fn on_extension_load(
&mut self,
myself: IExtension,
sys: IShareSys,
_late: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let handlesys: IHandleSys = sys.request_interface(&myself)?;
self.handle_type = Some(handlesys.create_type("NavTree", None, myself.get_identity())?);
register_natives!(
&sys,
&myself,
[
("SourceNav.SourceNav", native_obj_new),
("SourceNav.query", native_obj_query),
]
);
Ok(())
}
fn on_extension_unload(&mut self) {
self.handle_type = None;
}
}