terra-wld 0.3.0

Terraria world parser
Documentation
use std::ops::RangeInclusive;
mod banner;
mod entity;
pub mod error;
pub mod tile;
mod world;
mod world_manifest;

pub use banner::Banner;
pub use entity::{Entity, EntityType};
pub use terra_types::{Evil, MoonPhase, Rain};
pub use world::{
	AnglerQuest, BestiaryEntry, Chest, Difficulty, Home, Invasion, InvasionType, ItemDyeRow,
	LanternNight, LogicCheck, MoonType, Party, Sandstorm, Sign, TeamSpawns, TileEntity,
	TileEntityType, World, default, read, write,
};
pub use world_manifest::WorldManifest;

pub const SUPPORTED_VERSIONS: RangeInclusive<i32> = 0..=318;
const MAGIC_NUMBER: &[u8; 7] = b"relogic";

fn round_division_i64(dividend: i64, divisor: i64) -> i64 {
	let double_remainder = (dividend % divisor) * 2;
	dividend / divisor
		+ if double_remainder.abs() >= divisor.abs() {
			if dividend.is_negative() == divisor.is_negative() {
				1
			} else {
				-1
			}
		} else {
			0
		}
}

const fn array_len<const SIZE: usize, T>(_: &[T; SIZE]) -> usize {
	SIZE
}

fn round_division_i128(dividend: i128, divisor: i128) -> i128 {
	let double_remainder = (dividend % divisor) * 2;
	dividend / divisor
		+ if double_remainder.abs() >= divisor.abs() {
			if dividend.is_negative() == divisor.is_negative() {
				1
			} else {
				-1
			}
		} else {
			0
		}
}

fn remap(v: f32, x: RangeInclusive<f32>, y: RangeInclusive<f32>) -> f32 {
	if v <= *x.start() {
		return *y.start();
	}

	if v >= *x.end() {
		return *y.end();
	}

	let a = (*y.end() - *y.start()) / (*x.end() - *x.start());
	v * a + *y.start() - a * *x.start()
}