update DR_rs and post_build script

This commit is contained in:
shenjack 2023-05-14 01:40:34 +08:00
parent 08e2ae9ae2
commit 77e02fa3ca
5 changed files with 324 additions and 220 deletions

View File

@ -7,9 +7,10 @@ import os
import shutil import shutil
import warnings import warnings
import traceback import traceback
from pathlib import Path
package_path = 'Difficult_Rocket_rs' package_path = 'Difficult_Rocket_rs'
lib_path = '../lib' lib_path = Path('../lib').resolve()
build_path = 'build' build_path = 'build'
if not os.path.exists(lib_path): if not os.path.exists(lib_path):
@ -28,7 +29,6 @@ for build_dir in builds:
warnings.warn(f'package not found at {build_path}/{build_dir}') warnings.warn(f'package not found at {build_path}/{build_dir}')
continue continue
for file in os.listdir(os.path.join(build_path, build_dir, package_path)): for file in os.listdir(os.path.join(build_path, build_dir, package_path)):
# file_name = os.path.join(lib_path, file.replace(package_path, f'{package_path}.{DR_runtime.DR_Rust_version}'))
file_name = os.path.join(lib_path, file) file_name = os.path.join(lib_path, file)
shutil.rmtree(file_name, ignore_errors=True) shutil.rmtree(file_name, ignore_errors=True)
try: try:

View File

@ -39,7 +39,7 @@ pub mod data {
#[pyo3(name = "SR1PartList_rs")] #[pyo3(name = "SR1PartList_rs")]
#[pyo3(text_signature = "(file_path = './configs/PartList.xml', list_name = 'NewPartList')")] #[pyo3(text_signature = "(file_path = './configs/PartList.xml', list_name = 'NewPartList')")]
pub struct PySR1PartList { pub struct PySR1PartList {
pub part_list: SR1PartList, pub data: SR1PartList,
} }
#[pymethods] #[pymethods]
@ -47,16 +47,16 @@ pub mod data {
#[new] #[new]
fn new(file_path: String, list_name: String) -> Self { fn new(file_path: String, list_name: String) -> Self {
let raw_part_list: RawPartList = RawPartList::from_file(file_path).unwrap(); let raw_part_list: RawPartList = RawPartList::from_file(file_path).unwrap();
let part_list = raw_part_list.to_sr_part_list(Some(list_name)); let data = raw_part_list.to_sr_part_list(Some(list_name));
Self { part_list } Self { data }
} }
fn as_dict(&self) -> HashMap<String, PySR1PartType> { fn as_dict(&self) -> HashMap<String, PySR1PartType> {
self.part_list.cache.iter().map(|(k, v)| (k.clone(), PySR1PartType::new(v.clone()))).collect() self.data.get_cache().iter().map(|(k, v)| (k.clone(), PySR1PartType::new(v.clone()))).collect()
} }
fn get_part_type(&mut self, name: String) -> Option<PySR1PartType> { fn get_part_type(&self, name: String) -> Option<PySR1PartType> {
let part_type = self.part_list.cache.get(&name); let part_type = self.data.get_part_type(name.clone());
if let Some(part_type) = part_type { if let Some(part_type) = part_type {
Some(PySR1PartType::new(part_type.clone())) Some(PySR1PartType::new(part_type.clone()))
} else { } else {
@ -94,7 +94,7 @@ pub mod data {
// 左下角,右上角 // 左下角,右上角
let mut max_box = get_max_box(&self.ship.parts, &self.part_list); let mut max_box = get_max_box(&self.ship.parts, &self.part_list);
todo!(); todo!();
img_pos // img_pos
} }
fn get_name(&self) -> String { self.ship.name.clone() } fn get_name(&self) -> String { self.ship.name.clone() }
@ -111,12 +111,6 @@ pub mod translate {
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyDict; use pyo3::types::PyDict;
#[derive(Clone)]
pub enum BoolString {
Bool(bool),
String(String),
}
#[pyclass] #[pyclass]
#[pyo3(name = "TranslateConfig_rs")] #[pyo3(name = "TranslateConfig_rs")]
#[pyo3(text_signature = "(language, raise_error = False, replace_normal = False, add_error = False, is_result = False, keep_get = False)")] #[pyo3(text_signature = "(language, raise_error = False, replace_normal = False, add_error = False, is_result = False, keep_get = False)")]

View File

@ -124,7 +124,7 @@ pub mod part_list {
#[inline] #[inline]
pub fn to_damage(&self) -> crate::types::sr1::Damage { pub fn to_damage(&self) -> crate::types::sr1::Damage {
crate::types::sr1::Damage { crate::types::sr1::Damage {
disconnect: self.disconnect, disconnect: self.disconnect.to_owned(),
explode: self.explode.to_owned(), explode: self.explode.to_owned(),
explosion_power: self.explosion_power.unwrap_or(100), explosion_power: self.explosion_power.unwrap_or(100),
explosion_size: self.explosion_size.unwrap_or(100), explosion_size: self.explosion_size.unwrap_or(100),
@ -299,9 +299,9 @@ pub mod part_list {
description: self.description.clone(), description: self.description.clone(),
sprite: self.sprite.clone(), sprite: self.sprite.clone(),
p_type: self.r#type, p_type: self.r#type,
mass: self.mass, mass: self.mass.to_owned(),
width: self.width, width: self.width.to_owned(),
height: self.height, height: self.height.to_owned(),
friction: self.friction.unwrap_or(0.0), friction: self.friction.unwrap_or(0.0),
category: self.category.clone().unwrap_or("".to_string()), category: self.category.clone().unwrap_or("".to_string()),
ignore_editor_intersections: self.ignore_editor_intersections.unwrap_or(false), ignore_editor_intersections: self.ignore_editor_intersections.unwrap_or(false),
@ -419,9 +419,8 @@ pub mod ship {
pub engine: Option<Engine>, pub engine: Option<Engine>,
#[serde(rename = "Pod")] #[serde(rename = "Pod")]
pub pod: Option<Pod>, pub pod: Option<Pod>,
#[serde(rename = "partType")] #[serde(rename = "partType")]
pub part_type: SR1PartTypeEnum, pub part_type_id: String,
pub id: i64, pub id: i64,
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,
@ -519,58 +518,28 @@ pub mod ship {
pub child_part: i64, pub child_part: i64,
} }
impl Part {
/// 根据 Part 的原始数据猜测 Part 的类型
/// jundroo 我日你先人
fn guess_part_type(&self) -> SR1PartTypeEnum { todo!() }
}
impl SR1PartDataTrait for Part { impl SR1PartDataTrait for Part {
fn to_sr_part_data(&self) -> SR1PartData { fn to_sr_part_data(&self) -> SR1PartData {
let attr = match self.part_type { let attr = SR1PartDataAttr::from_raw(&self, None, true);
SR1PartTypeEnum::tank => SR1PartDataAttr::Tank { let part_type = attr.get_part_type();
fuel: if let Some(tank) = &self.tank { tank.fuel } else { 0_f64 },
},
SR1PartTypeEnum::engine => SR1PartDataAttr::Engine {
fuel: if let Some(engine) = &self.engine { engine.fuel } else { 0_f64 },
},
SR1PartTypeEnum::solar => SR1PartDataAttr::Solar {
extension: self.extension.unwrap_or(0_f64),
},
SR1PartTypeEnum::parachute => SR1PartDataAttr::Parachute {
chute_x: self.chute_x.unwrap_or(0_f64),
chute_y: self.chute_y.unwrap_or(0_f64),
chute_angle: self.chute_angle.unwrap_or(0_f64),
chute_height: self.chute_height.unwrap_or(0_f64),
inflate: i8_to_bool(self.inflate.unwrap_or(0_i8)),
inflation: i8_to_bool(self.inflation.unwrap_or(0_i8)),
deployed: i8_to_bool(self.deployed.unwrap_or(0_i8)),
rope: i8_to_bool(self.rope.unwrap_or(0_i8)),
},
SR1PartTypeEnum::pod => {
let pod = self.pod.as_ref().unwrap(); // 一定是有的,别问我为什么
let mut steps = Vec::new();
for step in &pod.stages.steps {
let mut activates = Vec::new();
for active in &step.activates {
activates.push((active.id, i8_to_bool(active.moved)))
}
steps.push(activates)
}
SR1PartDataAttr::Pod {
name: pod.name.clone(),
throttle: pod.throttle,
current_stage: pod.stages.current_stage,
steps,
}
}
_ => SR1PartDataAttr::None,
};
SR1PartData { SR1PartData {
attr, attr,
x: self.x, x: self.x.to_owned(),
y: self.y, y: self.y.to_owned(),
id: self.id, id: self.id.to_owned(),
angle: self.angle, angle: self.angle.to_owned(),
angle_v: self.angle_v, angle_v: self.angle_v.to_owned(),
flip_x: i8_to_bool(self.flip_x.unwrap_or(0_i8)), flip_x: i8_to_bool(self.flip_x.unwrap_or(0_i8)),
flip_y: i8_to_bool(self.flip_y.unwrap_or(0_i8)), flip_y: i8_to_bool(self.flip_y.unwrap_or(0_i8)),
editor_angle: self.editor_angle, editor_angle: self.editor_angle.to_owned(),
part_type: self.part_type, part_type,
part_type_id: self.part_type_id.clone(),
active: i8_to_bool(self.activated.unwrap_or(0_i8)), active: i8_to_bool(self.activated.unwrap_or(0_i8)),
explode: i8_to_bool(self.exploded.unwrap_or(0_i8)), explode: i8_to_bool(self.exploded.unwrap_or(0_i8)),
} }
@ -588,8 +557,6 @@ pub mod ship {
} }
let disconnected = match &self.disconnected { let disconnected = match &self.disconnected {
Some(disconnect) => { Some(disconnect) => {
// let mut disconnect_parts = Vec::new();
let mut disconnect_parts = Vec::new(); let mut disconnect_parts = Vec::new();
for disconnected_part in &disconnect.parts { for disconnected_part in &disconnect.parts {
let mut parts_vec = Vec::new(); let mut parts_vec = Vec::new();
@ -611,8 +578,8 @@ pub mod ship {
description: "".to_string(), description: "".to_string(),
parts, parts,
connections, connections,
lift_off: i8_to_bool(self.lift_off), lift_off: i8_to_bool(self.lift_off.to_owned()),
touch_ground: i8_to_bool(self.touch_ground), touch_ground: i8_to_bool(self.touch_ground.to_owned()),
disconnected, disconnected,
} }
} }

View File

@ -7,8 +7,8 @@
*/ */
pub mod sr1 { pub mod sr1 {
use std::cell::{Cell, RefCell};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs;
use super::math::{Edge, Shape}; use super::math::{Edge, Shape};
use crate::sr1_data::part_list::Damage as RawDamage; use crate::sr1_data::part_list::Damage as RawDamage;
@ -70,6 +70,22 @@ pub mod sr1 {
} }
} }
#[inline]
pub fn option_i8_to_option_bool(i: Option<i8>) -> Option<bool> {
match i {
Some(i) => Some(i8_to_bool(i)),
None => None,
}
}
#[inline]
pub fn option_bool_to_option_i8(b: Option<bool>) -> Option<i8> {
match b {
Some(b) => Some(bool_to_i8(b)),
None => None,
}
}
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum SR1PartTypeAttr { pub enum SR1PartTypeAttr {
Tank { Tank {
@ -122,10 +138,10 @@ pub mod sr1 {
impl Damage { impl Damage {
pub fn to_raw_damage(&self) -> RawDamage { pub fn to_raw_damage(&self) -> RawDamage {
RawDamage { RawDamage {
disconnect: self.disconnect, disconnect: self.disconnect.to_owned(),
explode: self.explode, explode: self.explode.to_owned(),
explosion_power: Some(self.explosion_power), explosion_power: Some(self.explosion_power.to_owned()),
explosion_size: Some(self.explosion_size), explosion_size: Some(self.explosion_size.to_owned()),
} }
} }
} }
@ -181,38 +197,49 @@ pub mod sr1 {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SR1PartList { pub struct SR1PartList {
pub types: Vec<SR1PartType>, pub types: Vec<SR1PartType>,
pub cache: HashMap<String, SR1PartType>, pub cache: RefCell<Option<HashMap<String, SR1PartType>>>,
pub name: String, pub name: String,
} }
impl SR1PartList { impl SR1PartList {
#[inline] #[inline]
pub fn new(name: String, types: Vec<SR1PartType>) -> SR1PartList { pub fn new(name: String, types: Vec<SR1PartType>) -> SR1PartList {
let mut map = HashMap::new(); SR1PartList {
for part in types.iter() { types,
map.insert(part.id.clone(), part.clone()); cache: RefCell::new(None),
name,
} }
SR1PartList { types, cache: map, name }
} }
#[inline] #[inline]
pub fn from_file(file_name: String) -> Option<SR1PartList> { pub fn from_file(file_name: String) -> Option<SR1PartList> {
if let Some(raw_list) = RawPartList::from_file(file_name) { if let Some(raw_list) = RawPartList::from_file(file_name) {
let sr_list = raw_list.to_sr_part_list(None); let sr_list = raw_list.to_sr_part_list(None);
let mut map = HashMap::new(); return Some(sr_list);
for part in sr_list.types.iter() {
map.insert(part.id.clone(), part.clone());
}
} }
None None
} }
#[inline] pub fn get_cache(&self) -> HashMap<String, SR1PartType> {
pub fn get_part_type(self, type_name: String) -> Option<SR1PartType> { let mut cache = self.cache.borrow_mut();
if let Some(part) = self.cache.get(&type_name) { if cache.is_none() {
return Some(part.clone()); let mut map = HashMap::new();
for part in self.types.iter() {
map.insert(part.id.to_owned(), part.to_owned());
}
*cache = Some(map);
self.cache.replace(cache.to_owned());
}
cache.to_owned().unwrap()
}
#[inline]
pub fn get_part_type(&self, type_name: String) -> Option<SR1PartType> {
let cache = self.get_cache();
match cache.get(&type_name) {
Some(part) => Some(part.to_owned()),
None => None,
} }
None
} }
pub fn part_types_new(part_types: Vec<SR1PartType>, name: Option<String>) -> Self { pub fn part_types_new(part_types: Vec<SR1PartType>, name: Option<String>) -> Self {
@ -269,9 +296,9 @@ pub mod sr1 {
let tank: Option<Tank> = match &self.attr { let tank: Option<Tank> = match &self.attr {
Some(attr) => match attr { Some(attr) => match attr {
SR1PartTypeAttr::Tank { fuel, dry_mass, fuel_type } => Some(Tank { SR1PartTypeAttr::Tank { fuel, dry_mass, fuel_type } => Some(Tank {
fuel: *fuel, fuel: fuel.to_owned(),
dry_mass: *dry_mass, dry_mass: dry_mass.to_owned(),
fuel_type: Some(*fuel_type), fuel_type: Some(fuel_type.to_owned()),
}), }),
_ => None, _ => None,
}, },
@ -287,12 +314,12 @@ pub mod sr1 {
fuel_type, fuel_type,
throttle_exponential, throttle_exponential,
} => Some(Engine { } => Some(Engine {
power: *power, power: power.to_owned(),
consumption: *consumption, consumption: consumption.to_owned(),
throttle_exponential: Some(*throttle_exponential), throttle_exponential: Some(throttle_exponential.to_owned()),
size: *size, size: size.to_owned(),
turn: *turn, turn: turn.to_owned(),
fuel_type: Some(*fuel_type), fuel_type: Some(fuel_type.to_owned()),
}), }),
_ => None, _ => None,
}, },
@ -301,9 +328,9 @@ pub mod sr1 {
let rcs: Option<Rcs> = match &self.attr { let rcs: Option<Rcs> = match &self.attr {
Some(attr) => match attr { Some(attr) => match attr {
SR1PartTypeAttr::Rcs { power, consumption, size } => Some(Rcs { SR1PartTypeAttr::Rcs { power, consumption, size } => Some(Rcs {
power: *power, power: power.to_owned(),
consumption: *consumption, consumption: consumption.to_owned(),
size: *size, size: size.to_owned(),
}), }),
_ => None, _ => None,
}, },
@ -311,7 +338,9 @@ pub mod sr1 {
}; };
let solar: Option<Solar> = match &self.attr { let solar: Option<Solar> = match &self.attr {
Some(attr) => match attr { Some(attr) => match attr {
SR1PartTypeAttr::Solar { charge_rate } => Some(Solar { charge_rate: *charge_rate }), SR1PartTypeAttr::Solar { charge_rate } => Some(Solar {
charge_rate: charge_rate.to_owned(),
}),
_ => None, _ => None,
}, },
_ => None, _ => None,
@ -326,12 +355,12 @@ pub mod sr1 {
length_speed, length_speed,
width, width,
} => Some(Lander { } => Some(Lander {
max_angle: *max_angle, max_angle: max_angle.to_owned(),
min_length: *min_length, min_length: min_length.to_owned(),
max_length: *max_length, max_length: max_length.to_owned(),
angle_speed: Some(*angle_speed), angle_speed: Some(angle_speed.to_owned()),
length_speed: Some(*length_speed), length_speed: Some(length_speed.to_owned()),
width: *width, width: width.to_owned(),
}), }),
_ => None, _ => None,
}, },
@ -353,19 +382,19 @@ pub mod sr1 {
description: self.description.clone(), description: self.description.clone(),
sprite: self.sprite.clone(), sprite: self.sprite.clone(),
r#type: self.p_type.clone(), r#type: self.p_type.clone(),
mass: self.mass, mass: self.mass.to_owned(),
width: self.width, width: self.width.to_owned(),
height: self.height, height: self.height.to_owned(),
friction: Some(self.friction), friction: Some(self.friction.to_owned()),
category: Some(self.category.clone()), category: Some(self.category.clone()),
ignore_editor_intersections: Some(self.ignore_editor_intersections), ignore_editor_intersections: Some(self.ignore_editor_intersections.to_owned()),
disable_editor_rotation: Some(self.disable_editor_rotation), disable_editor_rotation: Some(self.disable_editor_rotation.to_owned()),
can_explode: Some(self.can_explode), can_explode: Some(self.can_explode.to_owned()),
cover_height: Some(self.cover_height), cover_height: Some(self.cover_height.to_owned()),
sandbox_only: Some(self.sandbox_only), sandbox_only: Some(self.sandbox_only.to_owned()),
drag: Some(self.drag), drag: Some(self.drag.to_owned()),
hidden: Some(self.hidden), hidden: Some(self.hidden.to_owned()),
buoyancy: Some(self.buoyancy), buoyancy: Some(self.buoyancy.to_owned()),
damage: Some(self.damage.to_raw_damage()), damage: Some(self.damage.to_raw_damage()),
tank, tank,
engine, engine,
@ -384,94 +413,94 @@ pub mod sr1 {
#[inline] #[inline]
fn to_raw_part_data(&self) -> RawPartData { fn to_raw_part_data(&self) -> RawPartData {
let tank = match &self.attr { let (tank, engine) = if let Some(fuel) = &self.attr.fuel {
SR1PartDataAttr::Tank { fuel } => Some(RawTank { fuel: *fuel }), match self.part_type {
_ => None, SR1PartTypeEnum::tank => (Some(RawTank { fuel: fuel.to_owned() }), None),
SR1PartTypeEnum::engine => (None, Some(RawEngine { fuel: fuel.to_owned() })),
_ => (None, None),
}
} else {
(None, None)
}; };
let engine = match &self.attr { // let pod = match &self.attr {
SR1PartDataAttr::Engine { fuel } => Some(RawEngine { fuel: *fuel }), // SR1PartDataAttr::Pod {
_ => None, // name,
}; // throttle,
let pod = match &self.attr { // current_stage,
SR1PartDataAttr::Pod { // steps,
name, // } => Some({
throttle, // let mut actives = Vec::new();
current_stage, // for step in steps {
steps, // let mut steps_ = Vec::new();
} => Some({ // for active in step {
// steps_.push(RawActivate {
// id: active.0,
// moved: bool_to_i8(active.1),
// });
// }
// actives.push(RawStep { activates: steps_ });
// }
// let stages = RawStaging {
// current_stage: *current_stage,
// steps: actives,
// };
// RawPod {
// name: name.clone(),
// throttle: *throttle,
// stages,
// }
// }),
// _ => None,
// };
let pod = match (&self.attr.name, &self.attr.throttle, &self.attr.current_stage, &self.attr.steps) {
(Some(name), Some(throttle), Some(current_stage), Some(steps)) => Some({
let mut actives = Vec::new(); let mut actives = Vec::new();
for step in steps { for step in steps {
let mut steps_ = Vec::new(); let mut steps_ = Vec::new();
for active in step { for active in step {
steps_.push(RawActivate { steps_.push(RawActivate {
id: active.0, id: active.0.to_owned(),
moved: bool_to_i8(active.1), moved: bool_to_i8(active.1.to_owned()),
}); });
} }
actives.push(RawStep { activates: steps_ }); actives.push(RawStep { activates: steps_ });
} }
let stages = RawStaging { let stages = RawStaging {
current_stage: *current_stage, current_stage: current_stage.to_owned(),
steps: actives, steps: actives,
}; };
RawPod { RawPod {
name: name.clone(), name: name.clone(),
throttle: *throttle, throttle: throttle.to_owned(),
stages, stages,
} }
}), }),
_ => None, _ => None,
}; };
let (chute_x, chute_y, chute_angle, chute_height, inflate, inflation, deployed, rope) = match &self.attr {
SR1PartDataAttr::Parachute {
chute_x,
chute_y,
chute_angle,
chute_height,
inflate,
inflation,
deployed,
rope,
} => (
Some(*chute_x),
Some(*chute_y),
Some(*chute_angle),
Some(*chute_height),
Some(bool_to_i8(*inflate)),
Some(bool_to_i8(*inflation)),
Some(bool_to_i8(*deployed)),
Some(bool_to_i8(*rope)),
),
_ => (None, None, None, None, None, None, None, None),
};
let extension = match &self.attr {
SR1PartDataAttr::Solar { extension } => Some(*extension),
_ => None,
};
RawPartData { RawPartData {
tank, tank,
engine, engine,
pod, pod,
part_type: self.part_type, part_type_id: self.part_type_id.clone(),
id: self.id, id: self.id.to_owned(),
x: self.x, x: self.x.to_owned(),
y: self.y, y: self.y.to_owned(),
editor_angle: self.editor_angle, editor_angle: self.editor_angle.to_owned(),
angle: self.angle, angle: self.angle.to_owned(),
angle_v: self.angle_v, angle_v: self.angle_v.to_owned(),
flip_x: Some(bool_to_i8(self.flip_x)), flip_x: Some(bool_to_i8(self.flip_x.to_owned())),
flip_y: Some(bool_to_i8(self.flip_y)), flip_y: Some(bool_to_i8(self.flip_y.to_owned())),
chute_x, chute_x: self.attr.chute_x.to_owned(),
chute_y, chute_y: self.attr.chute_y.to_owned(),
chute_height, chute_height: self.attr.chute_height.to_owned(),
extension, extension: self.attr.extension.to_owned(),
inflate, inflate: option_bool_to_option_i8(self.attr.inflate.to_owned()),
inflation, inflation: option_bool_to_option_i8(self.attr.inflation.to_owned()),
exploded: Some(bool_to_i8(self.explode)), exploded: Some(bool_to_i8(self.explode.to_owned())),
rope, rope: option_bool_to_option_i8(self.attr.rope.to_owned()),
chute_angle, chute_angle: self.attr.chute_angle.to_owned(),
activated: Some(bool_to_i8(self.active)), activated: Some(bool_to_i8(self.active.to_owned())),
deployed, deployed: option_bool_to_option_i8(self.attr.deployed.to_owned()),
} }
} }
} }
@ -488,6 +517,7 @@ pub mod sr1 {
pub angle_v: f64, pub angle_v: f64,
// 状态属性 // 状态属性
pub part_type: SR1PartTypeEnum, pub part_type: SR1PartTypeEnum,
pub part_type_id: String,
pub editor_angle: i32, pub editor_angle: i32,
pub flip_x: bool, pub flip_x: bool,
pub flip_y: bool, pub flip_y: bool,
@ -498,11 +528,11 @@ pub mod sr1 {
impl SR1PartData { impl SR1PartData {
pub fn get_box(&self, part_type: &SR1PartType) -> (f64, f64, f64, f64) { pub fn get_box(&self, part_type: &SR1PartType) -> (f64, f64, f64, f64) {
let width = part_type.width; let width = part_type.width.to_owned();
let height = part_type.height; let height = part_type.height.to_owned();
let radius = self.angle; let radius = self.angle.to_owned();
let mut shape = Shape::new_width_height(width as f64, height as f64, Some(radius)); let mut shape = Shape::new_width_height(width as f64, height as f64, Some(radius));
shape.move_xy(Some(self.x), Some(self.y)); shape.move_xy(Some(self.x.to_owned()), Some(self.y.to_owned()));
let mut pos_box = (0_f64, 0_f64, 0_f64, 0_f64); let mut pos_box = (0_f64, 0_f64, 0_f64, 0_f64);
match shape.bounds[0] { match shape.bounds[0] {
Edge::OneTimeLine(line) => { Edge::OneTimeLine(line) => {
@ -523,33 +553,145 @@ pub mod sr1 {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SR1PartDataAttr { pub struct SR1PartDataAttr {
Tank { // Tank | Engine
fuel: f64, pub fuel: Option<f64>,
}, // Pod
Engine { pub name: Option<String>,
fuel: f64, pub throttle: Option<f64>,
}, pub current_stage: Option<u32>,
Pod { pub steps: Option<Vec<Vec<(i64, bool)>>>,
name: String, // Solar
throttle: f64, pub extension: Option<f64>,
current_stage: u32, // Parachute
steps: Vec<Vec<(i64, bool)>>, pub chute_x: Option<f64>,
}, pub chute_y: Option<f64>,
Solar { pub chute_height: Option<f64>,
extension: f64, pub chute_angle: Option<f64>,
}, pub inflate: Option<bool>,
Parachute { pub inflation: Option<bool>,
chute_x: f64, pub deployed: Option<bool>,
chute_y: f64, pub rope: Option<bool>,
chute_angle: f64, // part_type
chute_height: f64, pub part_type: Cell<Option<SR1PartTypeEnum>>,
inflate: bool, }
inflation: bool,
deployed: bool, impl SR1PartDataAttr {
rope: bool, pub fn guess_type(&self) -> SR1PartTypeEnum {
}, if let Some(part_type) = self.part_type.get() {
None, return part_type;
}
if self.fuel.is_some() {
self.part_type.set(Some(SR1PartTypeEnum::tank));
return self.part_type.get().unwrap();
}
if self.name.is_some() {
self.part_type.set(Some(SR1PartTypeEnum::pod));
return self.part_type.get().unwrap();
}
if self.extension.is_some() {
self.part_type.set(Some(SR1PartTypeEnum::solar));
return self.part_type.get().unwrap();
}
if self.chute_x.is_some() {
self.part_type.set(Some(SR1PartTypeEnum::parachute));
return self.part_type.get().unwrap();
}
SR1PartTypeEnum::strut // 默认为 Strut 开摆
}
pub fn get_part_type(&self) -> SR1PartTypeEnum {
if let Some(part_type) = self.part_type.get() {
return part_type;
}
self.guess_type()
}
pub fn new(
fuel: Option<f64>,
name: Option<String>,
throttle: Option<f64>,
current_stage: Option<u32>,
steps: Option<Vec<Vec<(i64, bool)>>>,
extension: Option<f64>,
chute_x: Option<f64>,
chute_y: Option<f64>,
chute_height: Option<f64>,
chute_angle: Option<f64>,
inflate: Option<bool>,
inflation: Option<bool>,
deployed: Option<bool>,
rope: Option<bool>,
part_type: Option<SR1PartTypeEnum>,
) -> Self {
SR1PartDataAttr {
fuel,
name,
throttle,
current_stage,
steps,
extension,
chute_x,
chute_y,
chute_height,
chute_angle,
inflate,
inflation,
deployed,
rope,
part_type: Cell::new(part_type),
}
}
pub fn from_raw(raw_data: &RawPartData, part_type: Option<SR1PartTypeEnum>, guess: bool) -> Self {
let fuel = if let Some(tank) = &raw_data.tank {
Some(tank.fuel.to_owned())
} else if let Some(engine) = &raw_data.engine {
Some(engine.fuel.to_owned())
} else {
None
};
let (name, throttle, current_stage, steps) = if let Some(pod) = &raw_data.pod {
(
Some(pod.name.to_owned()),
Some(pod.throttle.to_owned()),
Some(pod.stages.current_stage.to_owned()),
Some({
let mut steps = Vec::new();
for step in &pod.stages.steps {
let mut step_vec = Vec::new();
for act in &step.activates {
step_vec.push((act.id.to_owned(), i8_to_bool(act.moved.to_owned())));
}
steps.push(step_vec);
}
steps
}),
)
} else {
(None, None, None, None)
};
let results = SR1PartDataAttr {
fuel,
name,
throttle,
current_stage,
steps,
extension: raw_data.extension.to_owned(),
chute_x: raw_data.chute_x.to_owned(),
chute_y: raw_data.chute_y.to_owned(),
chute_height: raw_data.chute_height.to_owned(),
chute_angle: raw_data.chute_angle.to_owned(),
inflate: option_i8_to_option_bool(raw_data.inflate.to_owned()),
inflation: option_i8_to_option_bool(raw_data.inflation.to_owned()),
deployed: option_i8_to_option_bool(raw_data.deployed.to_owned()),
rope: option_i8_to_option_bool(raw_data.rope.to_owned()),
part_type: Cell::new(part_type),
};
if guess & results.part_type.get().is_none() {
results.guess_type();
}
results
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -619,8 +761,8 @@ pub mod sr1 {
parts: RawParts { parts }, parts: RawParts { parts },
connects: connections, connects: connections,
version: 1, version: 1,
lift_off: bool_to_i8(self.lift_off), lift_off: bool_to_i8(self.lift_off.to_owned()),
touch_ground: bool_to_i8(self.touch_ground), touch_ground: bool_to_i8(self.touch_ground.to_owned()),
disconnected, disconnected,
} }
} }
@ -658,8 +800,8 @@ pub mod math {
#[inline] #[inline]
pub fn distance(&self, other: &Point2D) -> f64 { pub fn distance(&self, other: &Point2D) -> f64 {
let dx = (other.x - self.x).powf(2.0); let dx = (other.x.to_owned() - self.x.to_owned()).powf(2.0);
let dy = (other.y - self.y).powf(2.0); let dy = (other.y.to_owned() - self.y.to_owned()).powf(2.0);
(dx + dy).powf(0.5) (dx + dy).powf(0.5)
} }

View File

@ -20,6 +20,7 @@ DR_rust_version = Version("0.2.7.0") # DR_mod 的 Rust 编写部分的兼容版
class _DR_mod_runtime(Options): class _DR_mod_runtime(Options):
name = 'DR mod runtime'
use_DR_rust: bool = True use_DR_rust: bool = True
DR_rust_available: bool = False DR_rust_available: bool = False