Worms4Editor/LibW4M/Data/Teams/TeamData.cs

326 lines
13 KiB
C#

using LibW4M.Data.WeaponFactory;
using LibXom.Data;
using LibXom.Streams;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibW4M.Data.Teams
{
public class TeamData : SaveDataEntry
{
public override string FriendlyName
{
get
{
return Name.Value;
}
}
private WeaponData secretWeapon;
private WeaponData secretWeaponCluster;
public WeaponData SecretWeapon
{
get
{
WeaponData? data = fileBelongs.WeaponFactoryCollective.findWeaponWithContainerUuid(this.secretWeapon.mainContainer.Uuid);
if (data is null) return secretWeapon;
return data;
}
set
{
this.secretWeapon = value;
}
}
public WeaponData SecretWeaponCluster
{
get
{
WeaponData? data = fileBelongs.WeaponFactoryCollective.findWeaponWithContainerUuid(this.secretWeaponCluster.mainContainer.Uuid);
if (data is null) return secretWeaponCluster;
return data;
}
set
{
this.secretWeaponCluster = value;
}
}
public XomString Name;
public XomString[] Worms;
public int TutorialsDone;
public int NewTeam;
public SkillLevel Skill;
public int Grave;
public int SWeapon;
public XomString Flag;
public XomString Speech;
public bool InGame;
public bool AllTutorialsDone;
public bool AllMissionsDone;
public XomString Player;
public XomString CustomWeapon;
public XomString Hat;
public XomString Gloves;
public XomString Glasses;
public XomString Tash;
public int[] StoryMissionsCompleted;
public int StoryChapter;
public TeamData(W4SaveFile fileBelongs, XomContainer mainContainer, bool load=true) : base(fileBelongs, mainContainer, load)
{
}
public override void DeleteEntries()
{
// delete weapon if its not in weapon collective, and this is the last reference to it.
if(fileBelongs.WeaponFactoryCollective.findWeaponWithContainerUuid(secretWeapon.mainContainer.Uuid) is null)
{
bool delete = true;
foreach (TeamData team in fileBelongs.TeamDataColective)
{
if (team.Equals(this)) continue;
if (team.secretWeapon.mainContainer.Uuid.Equals(this.secretWeapon.mainContainer.Uuid, StringComparison.InvariantCultureIgnoreCase)) delete = false;
}
if (delete)
{
this.SecretWeapon.DeleteEntries();
}
}
// delete cluster if its not in weapon collective, and this is the last reference to it.
if (fileBelongs.WeaponFactoryCollective.findWeaponWithContainerUuid(secretWeaponCluster.mainContainer.Uuid) is null)
{
bool delete = true;
foreach (TeamData team in fileBelongs.TeamDataColective)
{
if (team.Equals(this)) continue;
if (team.secretWeaponCluster.mainContainer.Uuid.Equals(this.secretWeaponCluster.mainContainer.Uuid, StringComparison.InvariantCultureIgnoreCase)) delete = false;
}
if (delete)
{
this.SecretWeaponCluster.DeleteEntries();
}
}
base.DeleteEntries();
}
private void setDefaultWeapon()
{
// Search other teams for "FETXT.Factory.Blaster"
bool foundDefault = false;
foreach (TeamData team in fileBelongs.TeamDataColective)
{
if (team.CustomWeapon.Value == "FETXT.Factory.Blaster")
{
this.SecretWeapon = team.SecretWeapon;
this.SecretWeaponCluster = team.SecretWeaponCluster;
foundDefault = true;
break;
}
}
// not found? FIIIIIIIIIIINE ILL CREATE IT MYSELF
if (!foundDefault)
{
WeaponData weapon = new WeaponData(fileBelongs, fileBelongs.CreateContainer("WeaponFactoryContainer"), false);
WeaponData cluster = new WeaponData(fileBelongs, fileBelongs.CreateContainer("WeaponFactoryContainer"), false);
// Set weapon data
weapon.Name = fileBelongs.LookupString("");
weapon.Type = 0;
weapon.DetonationType = DetonationType.Fuse;
weapon.ProjectileLaunchType = ProjectileLaunchType.Thrown;
weapon.RetreatTime = -1;
weapon.FuseTime = -1;
weapon.WormDamageRadius = 0.75f;
weapon.WormDamageMagnitude = 0.75f;
weapon.LandDamageRadius = 0.600000024f;
weapon.Push = 0.600000024f;
weapon.ProjectileMaxPower = 1.0f;
weapon.ProjectileNumClusters = 0;
weapon.ClusterMaxSpeed = 0.300000012f;
weapon.ClusterMaxSpeed = 0.0f;
weapon.PayloadResourceId = 16;
weapon.ProjectileCollisionRadius = 1.0f;
weapon.LaunchFX = fileBelongs.LookupString("");
weapon.ArielFX = fileBelongs.LookupString("");
weapon.DetonationFX = fileBelongs.LookupString("EffectExplosion");
weapon.GraphicalResources = new XomString[4] { fileBelongs.LookupString("Factory.TankGunBody"), fileBelongs.LookupString("Factory.TankGunBarrel"), fileBelongs.LookupString("Factory.TankGunButt"), fileBelongs.LookupString("Factory.TankGunSight") };
weapon.GraphicalLocators = new XomString[4] { fileBelongs.LookupString("TANK_body_locator"), fileBelongs.LookupString("TANK_barrel_root_locator"), fileBelongs.LookupString("TANK_butt_root_locator"), fileBelongs.LookupString("TANK_sight_root_locator") };
weapon.Homing = false;
weapon.HomingAvoidLand = false;
weapon.Poison = false;
weapon.EffectedByWind = false;
weapon.ProjectilePowersUp = true;
weapon.FireOnGround = false;
weapon.Save();
// Set cluster data
cluster.Name = fileBelongs.LookupString("");
cluster.Type = 1;
cluster.DetonationType = DetonationType.Fuse;
cluster.ProjectileLaunchType = ProjectileLaunchType.Thrown;
cluster.RetreatTime = -1;
cluster.FuseTime = -1;
cluster.WormDamageRadius = 0.75f;
cluster.WormDamageMagnitude = 0.75f;
cluster.LandDamageRadius = 0.600000024f;
cluster.Push = 0.600000024f;
cluster.ProjectileMaxPower = 1.0f;
cluster.ProjectileNumClusters = 0;
cluster.ClusterMaxSpeed = 0.300000012f;
cluster.ClusterMaxSpeed = 0.0f;
cluster.PayloadResourceId = 16;
cluster.ProjectileCollisionRadius = 1.0f;
cluster.LaunchFX = fileBelongs.LookupString("");
cluster.ArielFX = fileBelongs.LookupString("");
cluster.DetonationFX = fileBelongs.LookupString("EffectExplosion");
cluster.GraphicalResources = new XomString[1] { fileBelongs.LookupString("Grenade.Weapon") };
cluster.GraphicalLocators = new XomString[0];
cluster.Homing = false;
cluster.HomingAvoidLand = false;
cluster.Poison = false;
cluster.EffectedByWind = false;
cluster.ProjectilePowersUp = true;
cluster.FireOnGround = false;
cluster.Save();
this.SecretWeapon = weapon;
this.SecretWeaponCluster = cluster;
}
}
internal override void loadDefaults()
{
this.Name = fileBelongs.LookupString("Untitled Team");
this.Worms = new XomString[6] { fileBelongs.LookupString("Worm 1"), fileBelongs.LookupString("Worm 2"), fileBelongs.LookupString("Worm 3"), fileBelongs.LookupString("Worm 4"), fileBelongs.LookupString("Worm 5"), fileBelongs.LookupString("Worm 6") };
this.TutorialsDone = 0;
this.NewTeam = 0;
this.Skill = SkillLevel.PlayerControled;
this.Grave = 0;
this.SWeapon = 0;
this.Flag = fileBelongs.LookupString("Flag.UK");
this.Speech = fileBelongs.LookupString("voclassi");
this.InGame = false;
this.AllMissionsDone = false;
this.AllMissionsDone = false;
this.Player = fileBelongs.LookupString("");
// Deal with team weapon
setDefaultWeapon();
this.CustomWeapon = fileBelongs.LookupString("FETXT.Factory.Blaster");
//
this.Hat = fileBelongs.LookupString("");
this.Gloves = fileBelongs.LookupString("");
this.Glasses = fileBelongs.LookupString("");
this.Tash = fileBelongs.LookupString("");
this.StoryMissionsCompleted = new int[5];
this.StoryChapter = 0;
base.loadDefaults();
}
public override void Load()
{
using (XomStreamReader reader = new XomStreamReader(new MemoryStream(this.mainContainer.GetData())))
{
reader.Skip(3);
this.Name = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.Worms = fileBelongs.IntArrayToXomStringArray(reader.ReadCompressedIntArray());
this.TutorialsDone = reader.ReadInt32();
this.NewTeam = reader.ReadInt32();
this.Skill = (SkillLevel)reader.ReadInt32();
this.Grave = reader.ReadInt32();
this.SWeapon = reader.ReadInt32();
this.Flag = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.Speech = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.InGame = reader.ReadBool();
this.AllTutorialsDone = reader.ReadBool();
this.AllMissionsDone = reader.ReadBool();
this.Player = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.secretWeapon = new WeaponData(fileBelongs, fileBelongs.LookupContainerById(reader.ReadCompressedInt()));
this.secretWeaponCluster = new WeaponData(fileBelongs, fileBelongs.LookupContainerById(reader.ReadCompressedInt()));
this.CustomWeapon = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.Hat = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.Gloves = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.Glasses = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.Tash = fileBelongs.LookupStringFromId(reader.ReadCompressedInt());
this.StoryMissionsCompleted = reader.ReadInt32Array();
this.StoryChapter = reader.ReadInt32();
}
}
public override void Save()
{
this.SecretWeapon.Save();
this.SecretWeaponCluster.Save();
using (MemoryStream ms = new MemoryStream())
{
using (XomStreamWriter writer = new XomStreamWriter(ms))
{
writer.Skip(3);
writer.WriteCompressedInt(this.Name.Id);
writer.WriteCompressedIntArray(this.fileBelongs.XomStringArrayToIntArray(this.Worms));
writer.WriteInt32(this.TutorialsDone);
writer.WriteInt32(this.NewTeam);
writer.WriteInt32((int)this.Skill);
writer.WriteInt32(this.Grave);
writer.WriteInt32(this.SWeapon);
writer.WriteCompressedInt(this.Flag.Id);
writer.WriteCompressedInt(this.Speech.Id);
writer.WriteBool(this.InGame);
writer.WriteBool(this.AllTutorialsDone);
writer.WriteBool(this.AllMissionsDone);
writer.WriteCompressedInt(this.Player.Id);
writer.WriteCompressedInt(this.SecretWeapon.containerId);
writer.WriteCompressedInt(this.SecretWeaponCluster.containerId);
writer.WriteCompressedInt(this.CustomWeapon.Id);
writer.WriteCompressedInt(this.Hat.Id);
writer.WriteCompressedInt(this.Gloves.Id);
writer.WriteCompressedInt(this.Glasses.Id);
writer.WriteCompressedInt(this.Tash.Id);
writer.WriteInt32Array(this.StoryMissionsCompleted);
writer.WriteInt32(this.StoryChapter);
ms.Seek(0x00, SeekOrigin.Begin);
mainContainer.SetData(ms.ToArray());
}
}
}
}
}