-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheLunaticWorld.cs
137 lines (107 loc) · 3.94 KB
/
TheLunaticWorld.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using HamstarHelpers.Helpers.Debug;
using System;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using TheLunatic.Logic;
namespace TheLunatic {
class TheLunaticWorld : ModWorld {
public string ID { get; private set; }
public bool HasCorrectID { get; private set; } // Workaround for tml bug?
public GameLogic GameLogic { get; private set; }
public MaskLogic MaskLogic { get; private set; }
////////////////
public override void Initialize() {
var mymod = (TheLunaticMod)this.mod;
this.ID = Guid.NewGuid().ToString( "D" );
this.HasCorrectID = false; // 'Load()' decides if no pre-existing one is found
this.GameLogic = new GameLogic();
this.MaskLogic = new MaskLogic();
if( mymod.Config.DebugModeInfo ) {
LogHelpers.Log( "DEBUG World created; logics (re)created." );
}
}
////////////////
public override void Load( TagCompound tag ) {
var mymod = (TheLunaticMod)this.mod;
bool hasArrived = false, hasQuit = false, hasEnd = false, hasWon = false, isSafe = false;
int time = 0;
int[] masks = new int[0];
int customMaskCount = 0;
string[] customMasks = new string[0];
if( tag.ContainsKey("world_id") ) {
this.ID = tag.GetString( "world_id" );
hasArrived = tag.GetBool( "has_loony_arrived" );
hasQuit = tag.GetBool( "has_loony_quit" );
hasEnd = tag.GetBool( "has_game_ended" );
hasWon = tag.GetBool( "has_won" );
isSafe = tag.GetBool( "is_safe" );
time = tag.GetInt( "half_days_elapsed_" + this.ID );
masks = tag.GetIntArray( "masks_given_" + this.ID );
customMaskCount = tag.GetInt( "custom_masks_given_" + this.ID );
customMasks = new string[customMaskCount];
for( int i = 0; i < customMaskCount; i++ ) {
customMasks[i] = tag.GetString( "custom_mask_" + this.ID + "_" + i );
}
}
this.HasCorrectID = true;
this.GameLogic.LoadOnce( hasArrived, hasQuit, hasEnd, hasWon, isSafe, time );
this.MaskLogic.LoadOnce( masks, customMasks );
}
public override TagCompound Save() {
var mymod = (TheLunaticMod)this.mod;
var tag = new TagCompound {
{ "world_id", this.ID },
{ "has_loony_arrived", (bool)this.GameLogic.HasLoonyArrived },
{ "has_loony_quit", (bool)this.GameLogic.HasLoonyQuit },
{ "has_game_ended", (bool)this.GameLogic.HasGameEnded },
{ "has_won", (bool)this.GameLogic.HasWon },
{ "is_safe", (bool)this.GameLogic.IsSafe },
{ "half_days_elapsed_" + this.ID, (int)this.GameLogic.HalfDaysElapsed },
{ "masks_given_" + this.ID, (int[])this.MaskLogic.GivenVanillaMasksByType.ToArray() },
{ "custom_masks_given_" + this.ID, (int)this.MaskLogic.GivenCustomMasksByBossUid.Count }
};
int i = 0;
foreach( string maskId in this.MaskLogic.GivenCustomMasksByBossUid ) {
tag.Set( "custom_mask_" + this.ID + "_" + i, (string)maskId );
i++;
}
if( mymod.Config.DebugModeInfo ) {
LogHelpers.Log( "DEBUG Saving world. " + this.ID + ", "
+ this.GameLogic.HasLoonyArrived + ", "
+ this.GameLogic.HasLoonyQuit + ", "
+ this.GameLogic.HasGameEnded + ", "
+ this.GameLogic.HasWon + ", "
+ this.GameLogic.IsSafe + ", "
+ this.GameLogic.HalfDaysElapsed + ", "
+ "[" + String.Join( ",", this.MaskLogic.GivenVanillaMasksByType.ToArray() ) + "], " );
}
return tag;
}
////////////////
public override void NetSend( BinaryWriter writer ) {
writer.Write( this.HasCorrectID );
writer.Write( this.ID );
}
public override void NetReceive( BinaryReader reader ) {
bool hasCorrectId = reader.ReadBoolean();
string id = reader.ReadString();
if( hasCorrectId ) {
this.ID = id;
this.HasCorrectID = true;
}
}
////////////////
public override void PreUpdate() {
var mymod = (TheLunaticMod)this.mod;
if( !mymod.Config.Enabled ) { return; }
if( Main.netMode == 2 ) { // Server only
if( this.HasCorrectID && this.GameLogic != null ) {
this.GameLogic.Update();
}
}
}
}
}