Crate twmap

source · []
Expand description

This crate provides a library for safe parsing, editing and saving of Teeworlds and DDNet maps. Goals of this library are:

  • performance
  • reasonable map standards
  • compatibility with all Teeworlds maps (0.7) and DDNet (0.6) maps

In the very center of this library is the TwMap struct. For more information and the complete list of its available methods, check out its documentation page.

Note that library is a aware of the origin of the map (Teeworlds 0.7 or DDNet 0.6). On parsing a map it will store its Version in the version field, which in turn is used to perform the appropriate checks and save in the correct version.


Note that for better readability, some lines of code are left out in the examples, most notably use twmap::*; at the top.

Loading a binary map, accessing its version and image names and saving it again in the binary format:

let mut map = TwMap::parse_file(dm1_path)?;

assert_eq!(map.version, Version::Teeworlds07);
assert_eq!(map.images[0].name(), "bg_cloud1");
assert_eq!(map.images[6].name(), "sun");


Loading a MapDir or binary map, removing unused envelopes, layers, groups, images, sounds and saving it again in the MapDir format:

let mut map = TwMap::parse_path(map_path)?;



Loading a MapDir map, flipping it upside down and saving the map data into a vector:

let mut map = TwMap::parse_dir(map_path)?;
// Usually you would match the Option<TwMap> return values
// -> or wrap it into a function that also returns an Option
// To take up less space, we insert an error that we return in place of the None value
map = map.rotate_right().ok_or(e)?;
map = map.rotate_right().ok_or(e)?;
map = map.mirror().ok_or(e)?;

let mut map_data = Vec::new(); map_data)?;





Default bezier tangent values are all zeroes. This trait implements the constructors for those values. only I32Color needs to implement this manually, since volume (i32) and Position are already all zeroes in their default values.