Add support for LEV files.

This commit is contained in:
Ray Koopa 2017-04-26 14:56:54 +02:00
parent feb2ed72f7
commit fb16f94cf6
7 changed files with 288 additions and 6 deletions

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Collections.Generic;
using Syroot.Worms.Gen2;
using Syroot.Worms.Gen2.WorldParty;
using Syroot.Worms.Gen2.Armageddon;
namespace Syroot.Worms.Test
{
@ -19,9 +20,7 @@ namespace Syroot.Worms.Test
private static void Main(string[] args)
{
Image image = new Image(@"D:\Archive\Games\Worms\Worms Armageddon\3.7.2.1\custom.img");
image.Save(@"D:\Pictures\test.img");
image.Load(@"D:\Pictures\test.img");
GeneratedMap genMap = new GeneratedMap(@"D:\Archive\Games\Worms\Worms Armageddon\3.7.2.1\User\SavedLevels\test.LEV");
Console.WriteLine("Done.");
Console.ReadLine();

View File

@ -0,0 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Syroot.Worms.Gen2.Armageddon;
using Syroot.Worms.UnitTest.Common;
namespace Syroot.Worms.UnitTest.Gen2.Armageddon
{
/// <summary>
/// Represents a collection of tests for the <see cref="GeneratedMap"/> class.
/// </summary>
[TestCategory("GeneratedMap")]
[TestClass]
public class GeneratedMapTests
{
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
/// <summary>
/// Loads all files found in any game directories.
/// </summary>
[TestMethod]
public void LoadGeneratedMaps()
{
TestHelpers.LoadFiles<GeneratedMap>(Game.Armageddon, "*.lev");
}
}
}

View File

@ -0,0 +1,99 @@
using System.IO;
using System.Text;
using Syroot.IO;
using Syroot.Worms.Core;
namespace Syroot.Worms.Gen2.Armageddon
{
/// <summary>
/// Represents <see cref="MapGeneratorSettings"/> stored in a LEV file.
/// Used by WA and WWP. S. https://worms2d.info/Monochrome_map_(.bit,_.lev).
/// </summary>
public class GeneratedMap : ILoadableFile, ISaveableFile
{
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="GeneratedMap"/> class.
/// </summary>
public GeneratedMap()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GeneratedMap"/> class, loading the data from the given
/// <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to load the data from.</param>
public GeneratedMap(Stream stream)
{
Load(stream);
}
/// <summary>
/// Initializes a new instance of the <see cref="GeneratedMap"/> class, loading the data from the given file.
/// </summary>
/// <param name="fileName">The name of the file to load the data from.</param>
public GeneratedMap(string fileName)
{
Load(fileName);
}
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
/// <summary>
/// Gets or sets the <see cref="MapGeneratorSettings"/>.
/// </summary>
public MapGeneratorSettings Settings { get; set; }
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
/// <summary>
/// Loads the data from the given <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to load the data from.</param>
public void Load(Stream stream)
{
using (BinaryDataReader reader = new BinaryDataReader(stream, Encoding.ASCII, true))
{
Settings = reader.ReadStruct<MapGeneratorSettings>();
}
}
/// <summary>
/// Loads the data from the given file.
/// </summary>
/// <param name="fileName">The name of the file to load the data from.</param>
public void Load(string fileName)
{
using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Load(stream);
}
}
/// <summary>
/// Saves the data into the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream)
{
using (BinaryDataWriter writer = new BinaryDataWriter(stream, Encoding.ASCII))
{
writer.Write(Settings);
}
}
/// <summary>
/// Saves the data in the given file.
/// </summary>
/// <param name="fileName">The name of the file to save the data in.</param>
public void Save(string fileName)
{
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
Save(stream);
}
}
}
}

View File

@ -8,7 +8,7 @@ namespace Syroot.Worms.Gen2.Armageddon
{
/// <summary>
/// Represents map configuration stored by the land generator in LAND.DAT files.
/// Used by WA and WWP. S. https://worms2d.info/Land_Data_file.
/// Used by WA. S. https://worms2d.info/Land_Data_file.
/// </summary>
public class LandData : ILoadableFile, ISaveableFile
{

View File

@ -0,0 +1,159 @@
using System.Runtime.InteropServices;
namespace Syroot.Worms.Gen2.Armageddon
{
/// <summary>
/// Represents the required configuration for the land generator to create a map. This structure appears in LEV
/// files (being their only content), in BIT files (being the header) and in PNG maps stored by WA (being the data
/// of the w2lv or waLV chunks).
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MapGeneratorSettings
{
// ---- MEMBERS ------------------------------------------------------------------------------------------------
/// <summary>
/// The random seed to generate the land shape (no effect in BIT files).
/// </summary>
public int LandSeed;
/// <summary>
/// The random seed to generate object placements.
/// </summary>
public int ObjectSeed;
/// <summary>
/// <c>true</c> treats the generated map as a cavern, otherwise as an island, depending on <see cref="Style"/>.
/// </summary>
[MarshalAs(UnmanagedType.Bool)]
public bool Cavern;
/// <summary>
/// The style of the land generated, dependending on <see cref="Cavern"/>
/// </summary>
public MapGeneratorStyle Style;
/// <summary>
/// <c>true</c> to disable an indestructible border being placed around the map.
/// </summary>
[MarshalAs(UnmanagedType.Bool)]
public bool NoIndestructibleBorders;
/// <summary>
/// The probability percentage of map objects to appear on the land, from 0-100.
/// </summary>
public int ObjectPercentage;
/// <summary>
/// The probability percentage of bridges to appear on the land, from 0-100.
/// </summary>
public int BridgePercentage;
/// <summary>
/// The height of the water in percent, from 0-99.
/// </summary>
public int WaterLevel;
/// <summary>
/// The version of the structure, determining the available <see cref="SoilTextureIndex"/> values in Worms
/// Armageddon.
/// </summary>
public MapGeneratorVersion Version;
/// <summary>
/// Represents the texture index determining the style of the generated map.
/// </summary>
public MapGeneratorSoil SoilTextureIndex;
/// <summary>
/// Represents the water color used for the map (deprecated, used only in Worms Armageddon 1.0).
/// </summary>
public int WaterColor;
}
/// <summary>
/// Represents the possible land styles.
/// </summary>
public enum MapGeneratorStyle : int
{
/// <summary>
/// Represents a single island or a cavern.
/// </summary>
OneIslandOrCavern,
/// <summary>
/// Represents two islands or a double-layer cavern.
/// </summary>
TwoIslandsOrCaverns,
/// <summary>
/// Represents one flat island or a cavern open at the bottom.
/// </summary>
OneFlatIslandOrCavernOpenBottom,
/// <summary>
/// Represents two flat islands or a cavern open at the left or right.
/// </summary>
TwoFlatIslandsOrCavernOpenLeftRight
}
/// <summary>
/// Represents the possible versions which can be stored in the <see cref="MapGeneratorSettings.Version"/> field.
/// </summary>
public enum MapGeneratorVersion : short
{
/// <summary>
/// Game version 3.0. Special soil texture indices are 26 = Tribal, 27 = Tribal, 28 = Urban. Additionally,
/// floor and ceiling trimming is more severe.
/// </summary>
Version_3_0_0_0 = -1,
/// <summary>
/// Game version 3.5. Special soil texture indices are 27 = Urban, 28 = undefined.
/// </summary>
Version_3_5_0_0 = 0,
/// <summary>
/// Game version 3.6.26.4. Special soil texture indices are 26 = Tools, 27 = Tribal, Urban = 28 (same as in
/// WWP).
/// </summary>
Version_3_6_26_4 = 1
}
/// <summary>
/// Represents the possible soil styles. In case of Worms Armageddon, this list matches
/// <see cref="MapGeneratorVersion.Version_3_6_26_4"/>.
/// </summary>
public enum MapGeneratorSoil : short
{
ClassicBeach,
ClassicDesert,
ClassicFarm,
ClassicForest,
ClassicHell,
Art,
Cheese,
Construction,
Desert,
Dungeon,
Easter,
Forest,
Fruit,
Gulf,
Hell,
Hospital,
Jungle,
Manhattan,
Medieval,
Music,
Pirate,
Snow,
Space,
Sports,
Tentacle,
Time,
Tools,
Tribal,
Urban
}
}

View File

@ -107,7 +107,7 @@ namespace Syroot.Worms.Gen2.WorldParty
{
throw new InvalidDataException("Invalid LND file signature.");
}
int fileLength = reader.ReadInt32();
int fileSize = reader.ReadInt32();
// Read the data.
Size = reader.ReadStruct<Vector2>();

View File

@ -111,7 +111,7 @@ namespace Syroot.Worms.Gen2.Worms2
{
throw new InvalidDataException("Invalid LND file signature.");
}
int fileLength = reader.ReadInt32();
int fileSize = reader.ReadInt32();
// Read the data.
Size = reader.ReadStruct<Vector2>();