Skip to main content

terra_wld/
lib.rs

1use std::ops::RangeInclusive;
2mod banner;
3mod entity;
4pub mod error;
5pub mod tile;
6mod world;
7mod world_manifest;
8
9pub use banner::Banner;
10pub use entity::{Entity, EntityType};
11pub use terra_types::{Evil, MoonPhase, Rain};
12pub use world::{
13	AnglerQuest, BestiaryEntry, Chest, Difficulty, Home, Invasion, InvasionType, ItemDyeRow,
14	LanternNight, LogicCheck, MoonType, Party, Sandstorm, Sign, TeamSpawns, TileEntity,
15	TileEntityType, World, default, read, write,
16};
17pub use world_manifest::WorldManifest;
18
19pub const SUPPORTED_VERSIONS: RangeInclusive<i32> = 0..=318;
20const MAGIC_NUMBER: &[u8; 7] = b"relogic";
21
22fn round_division_i64(dividend: i64, divisor: i64) -> i64 {
23	let double_remainder = (dividend % divisor) * 2;
24	dividend / divisor
25		+ if double_remainder.abs() >= divisor.abs() {
26			if dividend.is_negative() == divisor.is_negative() {
27				1
28			} else {
29				-1
30			}
31		} else {
32			0
33		}
34}
35
36const fn array_len<const SIZE: usize, T>(_: &[T; SIZE]) -> usize {
37	SIZE
38}
39
40fn round_division_i128(dividend: i128, divisor: i128) -> i128 {
41	let double_remainder = (dividend % divisor) * 2;
42	dividend / divisor
43		+ if double_remainder.abs() >= divisor.abs() {
44			if dividend.is_negative() == divisor.is_negative() {
45				1
46			} else {
47				-1
48			}
49		} else {
50			0
51		}
52}
53
54fn remap(v: f32, x: RangeInclusive<f32>, y: RangeInclusive<f32>) -> f32 {
55	if v <= *x.start() {
56		return *y.start();
57	}
58
59	if v >= *x.end() {
60		return *y.end();
61	}
62
63	let a = (*y.end() - *y.start()) / (*x.end() - *x.start());
64	v * a + *y.start() - a * *x.start()
65}