Skip to content

Commit

Permalink
zzre: Add changeDatabase Dialog instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Jan 27, 2024
1 parent 4b77525 commit 1fbcc0b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
10 changes: 5 additions & 5 deletions zzre/game/Zanzarah.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public Zanzarah(ITagContainer diContainer, IZanzarahContainer zanzarahContainer)
tagContainer.AddTag(UI = new UI(this));
this.zanzarahContainer = zanzarahContainer;

var savegame = new zzio.Savegame() { sceneId = 3600 };
using (var fileStream = new System.IO.FileStream(@"C:\dev\zanzarah\Save\_0004.dat", System.IO.FileMode.Open, System.IO.FileAccess.Read))
using (var reader = new System.IO.BinaryReader(fileStream))
savegame = zzio.Savegame.ReadNew(reader);
savegame.entryId = -1;
var savegame = new zzio.Savegame() { sceneId = 2800 };
//using (var fileStream = new System.IO.FileStream(@"C:\dev\zanzarah\Save\_0004.dat", System.IO.FileMode.Open, System.IO.FileAccess.Read))
//using (var reader = new System.IO.BinaryReader(fileStream))
// savegame = zzio.Savegame.ReadNew(reader);
//savegame.entryId = -1;
CurrentGame = new Game(this, savegame);
tagContainer.AddTag(CurrentGame);
}
Expand Down
2 changes: 1 addition & 1 deletion zzre/game/messages/ExecuteNPCScript.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace zzre.game.messages;

public readonly struct ExecuteNPCScript { }
public readonly record struct ExecuteNPCScript(DefaultEcs.Entity? OnlyFor = null);
15 changes: 13 additions & 2 deletions zzre/game/systems/dialog/DialogScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,19 @@ private void Fight(DefaultEcs.Entity entity, int stage, bool canFlee)

private void ChangeDatabase(DefaultEcs.Entity entity, UID uid)
{
var curMethod = System.Reflection.MethodBase.GetCurrentMethod();
Console.WriteLine($"Warning: unimplemented dialog instruction \"{curMethod!.Name}\"");
var npcEntity = NPCEntity;
var dbRow = db.GetNpc(uid);
World.Publish(new GSModChangeNPCState(npcEntity.Get<Trigger>().idx, uid));
npcEntity.Set(dbRow);
if (dbRow.InitScript.Length > 0)
{
npcEntity.Set(new components.ScriptExecution(dbRow.InitScript));
World.Publish(new messages.ExecuteNPCScript(OnlyFor: npcEntity));
}
if (dbRow.UpdateScript.Length > 0)
npcEntity.Set(new components.ScriptExecution(dbRow.UpdateScript));
else
npcEntity.Remove<components.ScriptExecution>();
}

private void RemoveNpc(DefaultEcs.Entity entity)
Expand Down
5 changes: 4 additions & 1 deletion zzre/game/systems/npc/NPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Numerics;
using DefaultEcs.System;
using zzio;
using zzio.scn;

namespace zzre.game.systems;
Expand All @@ -13,6 +14,7 @@ public partial class NPC : AEntitySetSystem<float>

private readonly IDisposable sceneChangingSubscription;
private readonly IDisposable sceneLoadSubscription;
private readonly IDisposable changeNpcStateSubscription;
private readonly IDisposable setNpcModifierSubscription;
private readonly zzio.db.MappedDB mappedDB;

Expand All @@ -21,14 +23,15 @@ public NPC(ITagContainer diContainer) : base(diContainer.GetTag<DefaultEcs.World
mappedDB = diContainer.GetTag<zzio.db.MappedDB>();
sceneChangingSubscription = World.Subscribe<messages.SceneChanging>(HandleSceneChanging);
sceneLoadSubscription = World.Subscribe<messages.SceneLoaded>(HandleSceneLoaded);
setNpcModifierSubscription = World.Subscribe<zzio.GSModSetNPCModifier>(HandleSetNpcModifier);
setNpcModifierSubscription = World.Subscribe<GSModSetNPCModifier>(HandleSetNpcModifier);
}

public override void Dispose()
{
base.Dispose();
sceneChangingSubscription.Dispose();
sceneLoadSubscription.Dispose();
changeNpcStateSubscription.Dispose();
setNpcModifierSubscription.Dispose();
}

Expand Down
4 changes: 2 additions & 2 deletions zzre/game/systems/npc/NPCMovementByState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ private void HandleMoveSystem(in messages.NPCMoveSystem msg)
if (nextWaypoint == null)
return;

if (move.CurWaypointId >= 0)
waypointByIdx[move.CurWaypointId].ii3 = 0;
if (waypointByIdx.TryGetValue(move.CurWaypointId, out var curWaypointTrigger))
curWaypointTrigger.ii3 = 0;
nextWaypoint.ii3 = 1; // reserving this waypoint
move.NextWaypointId = (int)nextWaypoint.idx;
move.TargetPos = nextWaypoint.pos;
Expand Down
22 changes: 15 additions & 7 deletions zzre/game/systems/npc/NPCScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ public override void Dispose()

private void HandleExecuteNPCScript(in messages.ExecuteNPCScript message)
{
var executionPool = World.GetComponents<components.ScriptExecution>();
foreach (var entity in Set.GetEntities())
Update(entity, ref executionPool[entity]);
if (message.OnlyFor == null)
{
var executionPool = World.GetComponents<components.ScriptExecution>();
foreach (var entity in Set.GetEntities())
Update(entity, ref executionPool[entity]);
}
else
Update(message.OnlyFor.Value, ref message.OnlyFor.Value.Get<components.ScriptExecution>());
}

[Update]
Expand All @@ -63,10 +68,13 @@ private void Update(in DefaultEcs.Entity entity, ref components.ScriptExecution

private void SetModel(DefaultEcs.Entity entity, string name)
{
// not required by Zanzarah and a bit too much effort to clean up old actor parts&resources
if (entity.Has<components.ActorParts>())
throw new InvalidOperationException("NPC already has a model");
entity.Set(ManagedResource<zzio.ActorExDescription>.Create(name));
if (entity.TryGet<components.ActorParts>(out var oldActorParts))
{
oldActorParts.Body.Dispose();
oldActorParts.Wings?.Dispose();
}

entity.Set(ManagedResource<ActorExDescription>.Create(name));

var actorParts = entity.Get<components.ActorParts>();
var bodyClump = actorParts.Body.Get<ClumpMesh>();
Expand Down
6 changes: 5 additions & 1 deletion zzre/game/systems/player/Savegame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ private void HandlePlayerEntered(in messages.PlayerEntered message)
private void HandleSetTrigger(in GSModSetTrigger message) => HandleGSModForThisScene(message);
private void HandleSetNpcModifier(in GSModSetNPCModifier message) => HandleGSModForThisScene(message);

private void HandleGSModForThisScene(IGameStateMod gsMod) => savegame.Add($"sc_{CurSceneID}", gsMod);
private void HandleGSModForThisScene(IGameStateMod gsMod)
{
if (IsEnabled)
savegame.Add($"sc_{CurSceneID}", gsMod);
}

private void HandleGSModForScene(in messages.GSModForScene message)
{
Expand Down

0 comments on commit 1fbcc0b

Please sign in to comment.