Skip to content

Commit 3f6b1a8

Browse files
committed
Some cleanup and serf state fixes
1 parent 8400d50 commit 3f6b1a8

File tree

7 files changed

+71
-22
lines changed

7 files changed

+71
-22
lines changed

Freeserf.Core/Game.cs

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ internal Game(Map map)
154154

155155
if (map != null)
156156
Map = new Map(map.Geometry, null, map);
157+
158+
InitKnights();
157159
}
158160

159161
internal Game(Render.IRenderView renderView, IAudioInterface audioInterface)

Freeserf.Core/Serf.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
using System;
2424
using System.Collections.Generic;
25+
using System.Diagnostics;
2526
using System.Runtime.CompilerServices;
2627

2728
namespace Freeserf
@@ -176,6 +177,14 @@ private set
176177
}
177178
}
178179

180+
public void Update()
181+
{
182+
var temp = StateDataBase.Create(this) as StateDataBase;
183+
184+
if (Data == null || temp == null || temp.GetType() != Data.GetType())
185+
Data = temp;
186+
}
187+
179188
private readonly Serf serf;
180189

181190
static StateData()
@@ -222,7 +231,7 @@ protected StateDataBase(StateData parent)
222231
this.parent = parent;
223232
}
224233

225-
private static object Create(object parent)
234+
internal static object Create(object parent)
226235
{
227236
if (!(parent is StateData parentStateData))
228237
throw new ExceptionFreeserf($"Parent of {nameof(StateDataBase)} is no {nameof(StateData)}.");
@@ -341,6 +350,7 @@ public uint InventoryIndex
341350
}
342351
}
343352
}
353+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
344354
public StateDataIdleInStock IdleInStock
345355
{
346356
get
@@ -444,6 +454,7 @@ public int WaitCounter
444454
}
445455
}
446456
}
457+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
447458
public StateDataWalking Walking
448459
{
449460
get
@@ -503,6 +514,7 @@ public int SlopeLength
503514
}
504515
}
505516
}
517+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
506518
public StateDataEnteringBuilding EnteringBuilding
507519
{
508520
get
@@ -605,6 +617,7 @@ public State NextState
605617
}
606618
}
607619
}
620+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
608621
public StateDataLeavingBuilding LeavingBuilding
609622
{
610623
get
@@ -648,6 +661,7 @@ public int FieldB
648661
}
649662
}
650663
}
664+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
651665
public StateDataReadyToEnter ReadyToEnter
652666
{
653667
get
@@ -740,6 +754,7 @@ public int Substate
740754
}
741755
}
742756
}
757+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
743758
public StateDataDigging Digging
744759
{
745760
get
@@ -828,6 +843,7 @@ public uint Counter
828843
}
829844
}
830845
}
846+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
831847
public StateDataBuilding Building
832848
{
833849
get
@@ -869,6 +885,7 @@ public uint InventoryIndex
869885
}
870886
}
871887
}
888+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
872889
public StateDataBuildingCastle BuildingCastle
873890
{
874891
get
@@ -941,6 +958,7 @@ public State NextState
941958
}
942959
}
943960
}
961+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
944962
public StateDataMoveResourceOut MoveResourceOut
945963
{
946964
get
@@ -1015,6 +1033,7 @@ public uint InventoryIndex
10151033
}
10161034
}
10171035
}
1036+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
10181037
public StateDataReadyToLeaveInventory ReadyToLeaveInventory
10191038
{
10201039
get
@@ -1119,6 +1138,7 @@ public int Flags
11191138
}
11201139
}
11211140
}
1141+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
11221142
public StateDataFreeWalking FreeWalking
11231143
{
11241144
get
@@ -1174,6 +1194,7 @@ public int Mode
11741194
}
11751195
}
11761196
}
1197+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
11771198
public StateDataSawing Sawing
11781199
{
11791200
get
@@ -1215,6 +1236,7 @@ public int FieldB
12151236
}
12161237
}
12171238
}
1239+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
12181240
public StateDataLost Lost
12191241
{
12201242
get
@@ -1286,6 +1308,7 @@ public Map.Minerals Deposit
12861308
}
12871309
}
12881310
}
1311+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
12891312
public StateDataMining Mining
12901313
{
12911314
get
@@ -1358,6 +1381,7 @@ public int Type
13581381
}
13591382
}
13601383
}
1384+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
13611385
public StateDataSmelting Smelting
13621386
{
13631387
get
@@ -1401,6 +1425,7 @@ public int Mode
14011425
}
14021426
}
14031427
}
1428+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
14041429
public StateDataMilling Milling
14051430
{
14061431
get
@@ -1442,6 +1467,7 @@ public int Mode
14421467
}
14431468
}
14441469
}
1470+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
14451471
public StateDataBaking Baking
14461472
{
14471473
get
@@ -1483,6 +1509,7 @@ public int Mode
14831509
}
14841510
}
14851511
}
1512+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
14861513
public StateDataPigFarming PigFarming
14871514
{
14881515
get
@@ -1524,6 +1551,7 @@ public int Mode
15241551
}
15251552
}
15261553
}
1554+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
15271555
public StateDataButchering Butchering
15281556
{
15291557
get
@@ -1565,6 +1593,7 @@ public int Mode
15651593
}
15661594
}
15671595
}
1596+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
15681597
public StateDataMakingWeapon MakingWeapon
15691598
{
15701599
get
@@ -1606,6 +1635,7 @@ public int Mode
16061635
}
16071636
}
16081637
}
1638+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
16091639
public StateDataMakingTool MakingTool
16101640
{
16111641
get
@@ -1647,6 +1677,7 @@ public int Mode
16471677
}
16481678
}
16491679
}
1680+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
16501681
public StateDataBuildingBoat BuildingBoat
16511682
{
16521683
get
@@ -1738,6 +1769,7 @@ public int DefenderIndex
17381769
}
17391770
}
17401771
}
1772+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
17411773
public StateDataAttacking Attacking
17421774
{
17431775
get
@@ -1832,6 +1864,7 @@ public int DefenderIndex
18321864
}
18331865
}
18341866
}
1867+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
18351868
public StateDataAttackingVictoryFree AttackingVictoryFree
18361869
{
18371870
get
@@ -1934,6 +1967,7 @@ public int OtherDistanceRow
19341967
}
19351968
}
19361969
}
1970+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
19371971
public StateDataDefendingFree DefendingFree
19381972
{
19391973
get
@@ -2038,6 +2072,7 @@ public State NextState
20382072
}
20392073
}
20402074
}
2075+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
20412076
public StateDataLeaveForWalkToFight LeaveForWalkToFight
20422077
{
20432078
get
@@ -2111,6 +2146,7 @@ public Direction ReverseDirection
21112146
}
21122147
}
21132148
}
2149+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
21142150
public StateDataIdleOnPath IdleOnPath
21152151
{
21162152
get
@@ -2159,6 +2195,7 @@ public uint NextKnight
21592195
}
21602196
}
21612197
}
2198+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
21622199
public StateDataDefending Defending
21632200
{
21642201
get

Freeserf.Core/Serialize/StateSerializer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ private static void DeserializeWithoutHeaderInto(object targetObject, BinaryRead
388388
if (value == null && customTypeCreators.ContainsKey(field.FieldType))
389389
value = customTypeCreators[field.FieldType]?.Invoke(targetObject);
390390

391+
// The serf state data needs some special care.
392+
if (value is Serf.StateData serfStateData)
393+
serfStateData.Update();
394+
391395
field.SetValue(targetObject, DeserializePropertyValue(reader, field.FieldType, value));
392396
}
393397
}
@@ -413,7 +417,9 @@ private static void DeserializePropertyValueInto(object targetObject, BinaryRead
413417
value = DeserializePropertyValue(reader, property.PropertyType, value);
414418
}
415419
else
420+
{
416421
value = DeserializePropertyValue(reader, property.PropertyType, value);
422+
}
417423

418424
if (wasNull)
419425
property.SetValue(targetObject, value);

Freeserf.Core/UI/ListBox.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ namespace Freeserf.UI
2929
{
3030
internal class ListBox<T> : GuiObject
3131
{
32-
Render.Color colorFocus = new Render.Color(0x60, 0x70, 0x60);//new Color(0x00, 0x8b, 0x47);
33-
Render.Color colorText = Render.Color.Green;
34-
Render.Color colorBackground = Render.Color.Black;
32+
static readonly Render.Color colorFocus = new Render.Color(0x60, 0x70, 0x60);//new Color(0x00, 0x8b, 0x47);
33+
static readonly Render.Color colorBackground = Render.Color.Black;
3534

3635
int firstVisibleItem = 0;
3736
int selectedItem = -1;
3837
Action<T> selectionHandler = null;
3938
protected readonly List<T> items = new List<T>();
40-
TextRenderType renderType = TextRenderType.NewUI;
39+
readonly TextRenderType renderType = TextRenderType.NewUI;
4140

4241
readonly IColoredRect background = null;
4342
readonly IColoredRect selectionBackground = null;

Freeserf.Core/UI/ListSavedFiles.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace Freeserf.UI
2323
{
2424
internal class ListSavedFiles : ListBox<GameStore.SaveInfo>
2525
{
26-
GameStore saveGame = null;
27-
Interface interf = null;
26+
readonly GameStore saveGame = null;
27+
readonly Interface interf = null;
2828

2929
public ListSavedFiles(Interface interf)
3030
: base(interf, Render.TextRenderType.NewUI)

Freeserf.Network/Client.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,20 @@ void handleReceivedData(IRemote source, INetworkData data, ResponseHandler respo
207207

208208
if (Game != null && client != null && client.Connected)
209209
{
210-
var gameTime = Game.GameTime;
211-
212-
// Save game state from time to time to avoid huge syncs.
213-
if (gameTime - lastSavedGameStateGameTime >= SavedGameState.SyncDelay && SavedGameState.TimeToSync(Game))
210+
lock (Game)
214211
{
215-
Log.Verbose.Write(ErrorSystemType.Network, $"Saving game state with game time {Misc.SecondsToTime(gameTime)}.");
212+
var gameTime = Game.GameTime;
213+
214+
// Save game state from time to time to avoid huge syncs.
215+
if (gameTime - lastSavedGameStateGameTime >= SavedGameState.SyncDelay && SavedGameState.TimeToSync(Game))
216+
{
217+
Log.Verbose.Write(ErrorSystemType.Network, $"Saving game state with game time {Misc.SecondsToTime(gameTime)}.");
216218

217-
lastSavedGameStateGameTime = gameTime;
218-
lastSavedGameStates.Add(gameTime, SavedGameState.FromGame(Game));
219+
lastSavedGameStateGameTime = gameTime;
220+
lastSavedGameStates.Add(gameTime, SavedGameState.FromGame(Game));
219221

220-
Log.Verbose.Write(ErrorSystemType.Network, $"Finished saving game state with game time {Misc.SecondsToTime(gameTime)}.");
222+
Log.Verbose.Write(ErrorSystemType.Network, $"Finished saving game state with game time {Misc.SecondsToTime(gameTime)}.");
223+
}
221224
}
222225
}
223226
}
@@ -324,11 +327,14 @@ void ProcessData(IRemoteServer server, INetworkData networkData, ResponseHandler
324327
var stopWatch = System.Diagnostics.Stopwatch.StartNew();
325328
Log.Verbose.Write(ErrorSystemType.Network, "Processing sync ... ");
326329
#endif
327-
lastSavedGameStates.Clear();
328-
bool full = syncData.Full && lastVerifiedSavedGameState != null; // first sync should not be handled as full
329-
if (lastVerifiedSavedGameState == null)
330-
lastVerifiedSavedGameState = SavedGameState.FromGame(Game);
331-
lastVerifiedSavedGameState = SavedGameState.UpdateGameAndLastState(Game, lastVerifiedSavedGameState, syncData.SerializedData, full);
330+
lock (Game)
331+
{
332+
lastSavedGameStates.Clear();
333+
bool full = syncData.Full && lastVerifiedSavedGameState != null; // first sync should not be handled as full
334+
if (lastVerifiedSavedGameState == null)
335+
lastVerifiedSavedGameState = SavedGameState.FromGame(Game);
336+
lastVerifiedSavedGameState = SavedGameState.UpdateGameAndLastState(Game, lastVerifiedSavedGameState, syncData.SerializedData, full);
337+
}
332338

333339
#if DEBUG
334340
Log.Verbose.Write(ErrorSystemType.Network, $"Processing sync done in {stopWatch.ElapsedMilliseconds / 1000.0} seconds");
@@ -344,7 +350,7 @@ void ProcessData(IRemoteServer server, INetworkData networkData, ResponseHandler
344350
break;
345351
}
346352
default:
347-
// Should be handled by Server_DataReceived already.
353+
// Should have been handled by Server_DataReceived already.
348354
break;
349355
}
350356
}

Issues.md

-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123

124124
## Nice to have
125125

126-
- Possibility to show possible buildings as spectator?
127126
- Possibility to copy the map seed number
128127
- The list of savegames should also allow subdirs with savegames in them. But then there must be a possibility to go up and down the directory hierarchy.
129128
Maybe just add a directory called ".." in every directory inside the savegame list which performs a "up" operation.

0 commit comments

Comments
 (0)