Skip to content

Commit

Permalink
Merge pull request #44 from corwinkuiper/pre-compiled-items
Browse files Browse the repository at this point in the history
Pre compiled items
  • Loading branch information
corwinkuiper authored Sep 8, 2024
2 parents 00b97e5 + f86b993 commit 75b34bd
Show file tree
Hide file tree
Showing 30 changed files with 720 additions and 555 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.files.excludeDirs": [
"dofus_items/src/data"
]
}
31 changes: 30 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[workspace]
resolver = "2"
members = ["bench", "dofus_set", "server", "wasm"]
members = ["bench", "dofus_characteristics", "dofus_items", "dofus_set", "server", "wasm"]

[profile.release]
opt-level = 3
debug = true
lto = true
lto = "fat"
2 changes: 2 additions & 0 deletions bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
dofus_characteristics = { path = "../dofus_characteristics" }
dofus_set = { path = "../dofus_set" }
dofus_items = { path = "../dofus_items" }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.6"
12 changes: 6 additions & 6 deletions bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![deny(clippy::all)]

use dofus_characteristics::Stat;
use dofus_items::ITEMS;
use dofus_set::config;
use dofus_set::dofus_set::Optimiser;
use dofus_set::items::Items;
use dofus_set::stats::Stat;

fn main() {
let items = Items::new();
let items = &ITEMS;

let mut weights = [0.0; 51];
weights[Stat::Power as usize] = 1.0;
Expand All @@ -30,11 +30,11 @@ fn main() {
let initial_set: [Option<_>; 16] = [None; 16];

for _ in 0..10 {
let optimiser = Optimiser::new(&config, initial_set, &items).unwrap();
let optimiser = Optimiser::new(&config, initial_set, items).unwrap();

let final_state = optimiser.optimise().unwrap();
let sets = final_state.sets(&items);
println!("Set Energy: {}", -final_state.energy(&config, &sets));
let sets = final_state.sets(items);
println!("Set Energy: {}", -final_state.energy(&config, items, &sets));
}
}

Expand Down
11 changes: 11 additions & 0 deletions dofus_characteristics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "dofus_characteristics"
version = "0.1.0"
edition = "2021"

[dependencies]
strum = { version = "0.26", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
thiserror = "1"
quote = "1"
proc-macro2 = "1"
60 changes: 41 additions & 19 deletions dofus_set/src/stats.rs → dofus_characteristics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ use std::{
ops::{AddAssign, Index, IndexMut, SubAssign},
};

use proc_macro2::TokenStream;
use quote::{quote, ToTokens, TokenStreamExt};
use serde::Serialize;
use thiserror::Error;

#[derive(Clone, Debug)]
pub struct Characteristic([i32; 51]);

impl ToTokens for Characteristic {
fn to_tokens(&self, tokens: &mut TokenStream) {
let values = &self.0;
tokens.append_all(quote! {
Characteristic::new_from_raw([#(#values),*])
})
}
}

impl Serialize for Characteristic {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -33,7 +44,7 @@ impl IndexMut<Stat> for Characteristic {
}

pub trait Restriction: Debug {
fn accepts(&self, characteristics: &Characteristic, set_bonus: i32, leniency: i32) -> bool;
fn accepts(&self, characteristics: &Characteristic, set_bonus: i32) -> i32;
}

#[derive(Debug)]
Expand All @@ -45,20 +56,23 @@ pub enum BooleanOperator {
#[derive(Debug)]
pub struct RestrictionSet {
pub operator: BooleanOperator,
pub restrictions: Vec<Box<dyn Restriction + Sync + Send>>,
pub restrictions: &'static [&'static (dyn Restriction + Sync + Send)],
}

impl Restriction for RestrictionSet {
fn accepts(&self, characteristics: &Characteristic, set_bonus: i32, leniency: i32) -> bool {
fn accepts(&self, characteristics: &Characteristic, set_bonus: i32) -> i32 {
match self.operator {
BooleanOperator::And => self
.restrictions
.iter()
.all(|restriction| restriction.accepts(characteristics, set_bonus, leniency)),
.map(|restriction| restriction.accepts(characteristics, set_bonus))
.sum(),
BooleanOperator::Or => self
.restrictions
.iter()
.any(|restriction| restriction.accepts(characteristics, set_bonus, leniency)),
.map(|restriction| restriction.accepts(characteristics, set_bonus))
.max()
.unwrap(),
}
}
}
Expand All @@ -77,14 +91,17 @@ pub struct RestrictionLeaf {
}

impl Restriction for RestrictionLeaf {
fn accepts(&self, characteristics: &Characteristic, _set_bonus: i32, leniency: i32) -> bool {
fn accepts(&self, characteristics: &Characteristic, _set_bonus: i32) -> i32 {
let value = characteristics[self.stat];
let lenient = !(self.stat == Stat::AP || self.stat == Stat::MP);
let leniency = if lenient { leniency } else { 0 };
match self.operator {
Operator::GreaterThan => value + leniency > self.value,
Operator::LessThan => value - leniency < self.value,
}
let extra_strict = self.stat == Stat::AP || self.stat == Stat::MP;
let difference = match self.operator {
Operator::GreaterThan => (self.value - value).max(0),
Operator::LessThan => (value - self.value).max(0),
};

let multiplier = if extra_strict { 100 } else { 1 };

difference * multiplier
}
}

Expand All @@ -95,20 +112,21 @@ pub struct SetBonusRestriction {
}

impl Restriction for SetBonusRestriction {
fn accepts(&self, _characteristics: &Characteristic, set_bonus: i32, _leniency: i32) -> bool {
match self.operator {
Operator::GreaterThan => set_bonus > self.value,
Operator::LessThan => set_bonus < self.value,
}
fn accepts(&self, _characteristics: &Characteristic, set_bonus: i32) -> i32 {
let difference = match self.operator {
Operator::GreaterThan => (self.value - set_bonus).max(0),
Operator::LessThan => (set_bonus - self.value).max(0),
};
difference * 100
}
}

#[derive(Debug)]
pub struct NullRestriction;

impl Restriction for NullRestriction {
fn accepts(&self, _characteristics: &Characteristic, _set_bonus: i32, _leniency: i32) -> bool {
true
fn accepts(&self, _characteristics: &Characteristic, _set_bonus: i32) -> i32 {
0
}
}

Expand All @@ -120,6 +138,10 @@ impl Characteristic {
pub fn iter(&self) -> core::slice::Iter<'_, i32> {
self.0.iter()
}

pub const fn new_from_raw(raw: [i32; 51]) -> Self {
Self(raw)
}
}

impl Default for Characteristic {
Expand Down
18 changes: 18 additions & 0 deletions dofus_items/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "dofus_items"
version = "0.1.0"
edition = "2021"



[dependencies]
dofus_characteristics = { path = "../dofus_characteristics" }
serde = { version = "1", features = ["derive"] }


[build-dependencies]
dofus_characteristics = { path = "../dofus_characteristics" }
proc-macro2 = "1"
quote = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Loading

0 comments on commit 75b34bd

Please sign in to comment.