Skip to content

Commit

Permalink
[Upgrades] Add "reinit" flag to upgrades
Browse files Browse the repository at this point in the history
When set, the object runs its init function whenever it is upgraded or
spawned with an upgrade. Useful when the object caches its tuning value
on creation rather than accessing it directly.
  • Loading branch information
riperiperi committed Jul 28, 2019
1 parent 9653ad0 commit 9ba13a0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions TSOClient/tso.content/Upgrades/Model/ObjectUpgradeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ public class ObjectUpgradeConfig
/// </summary>
public bool? Special;

/// <summary>
/// If true, the object's init function will be run again when upgrading to this level.
/// </summary>
public bool Reinit;

public void Load(int version, BinaryReader reader)
{
GUID = reader.ReadString();
Level = reader.ReadInt32();
Limit = reader.ReadInt32();
if (Limit < 0) Limit = null;
Special = reader.ReadBoolean();
Reinit = reader.ReadBoolean();
}

public void Save(BinaryWriter writer)
Expand All @@ -44,6 +50,7 @@ public void Save(BinaryWriter writer)
writer.Write(Level);
writer.Write(Limit ?? -1);
writer.Write(Special ?? false);
writer.Write(Reinit);
}
}
}
11 changes: 11 additions & 0 deletions TSOClient/tso.content/Upgrades/ObjectUpgradeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public void Init()
return null;
}

public ObjectUpgradeConfig GetUpgradeConfig(string file, uint guid, int level)
{
RuntimeUpgradeFile upgradeFile = null;
if (FileUpgrades.TryGetValue(file, out upgradeFile))
{
upgradeFile.Load(ContentManager);
return upgradeFile.GetConfig(guid);
}
return null;
}

public Dictionary<int, Dictionary<int, short>> GetUpgrade(string file, uint guid, int level)
{
// find upgrade table for this file, find entries for this object, and the specified level.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private bool TryPlace(VM vm, VMAvatar caller)
{
state.UpgradeLevel = TargetUpgradeLevel;
obj.UpdateTuning(vm);
VMNetUpgradeCmd.TryReinit(obj, vm, TargetUpgradeLevel);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,14 @@ private bool TryPlace(VM vm, VMAvatar caller)
var tsostate = (obj.PlatformState as VMTSOObjectState);
if (tsostate != null) {
tsostate.OwnerID = caller.PersistID;
bool reinitRequired = false;
if (Info.UpgradeLevel > tsostate.UpgradeLevel)
{
tsostate.UpgradeLevel = Info.UpgradeLevel;
reinitRequired = true;
}
obj.UpdateTuning(vm);
if (reinitRequired) VMNetUpgradeCmd.TryReinit(obj, vm, tsostate.UpgradeLevel);
}
obj.PersistID = ObjectPID;
((VMGameObject)obj).DisableIfTSOCategoryWrong(vm.Context);
Expand Down
10 changes: 10 additions & 0 deletions TSOClient/tso.simantics/NetPlay/Model/Commands/VMNetUpgradeCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public class VMNetUpgradeCmd : VMNetCommandBodyAbstract
public byte TargetUpgradeLevel;
public int AddedValue;

public static void TryReinit(VMEntity obj, VM vm, int level)
{
var config = Content.Content.Get().Upgrades.GetUpgradeConfig(
obj.Object.Resource.Iff.Filename,
(obj.MasterDefinition ?? obj.Object.OBJ).GUID,
level);
if (config?.Reinit == true) obj.ExecuteEntryPoint(0, vm.Context, true);
}

public override bool Execute(VM vm, VMAvatar caller)
{
var pobj = vm.GetObjectByPersist(ObjectPID);
Expand All @@ -33,6 +42,7 @@ public override bool Execute(VM vm, VMAvatar caller)
state.Wear = 20 * 4;
state.QtrDaysSinceLastRepair = 0;
obj.UpdateTuning(vm);
TryReinit(obj, vm, TargetUpgradeLevel);
}
}

Expand Down

0 comments on commit 9ba13a0

Please sign in to comment.