-
Notifications
You must be signed in to change notification settings - Fork 6
/
Board.cs
1646 lines (1509 loc) · 54.1 KB
/
Board.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
namespace Noxico
{
/// <summary>
/// Determines which subscreen to run when UserMode is set to Subscreen.
/// </summary>
public delegate void SubscreenFunc();
/// <summary>
/// Stores a set of four integers that represent the location and size of a rectangle.
/// Basically a version of <see cref="System.Drawing.Rectangle"/> that replaces the extra stuff with a single feature.
/// </summary>
public struct Rectangle : IEquatable<Rectangle>
{
/// <summary>
/// Gets the x-coordinate of the left edge of this <see cref="Noxico.Rectangle"/> structure.
/// </summary>
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
/// <summary>
/// Creates and returns a <see cref="Noxico.Point"/> that is the centerpoint of this <c>Noxico.Rectangle</c>.
/// </summary>
/// <returns></returns>
public Point GetCenter()
{
return new Point(Left + ((Right - Left) / 2), Top + ((Bottom - Top) / 2));
}
public void SaveToFile(BinaryWriter stream)
{
stream.Write(Left);
stream.Write(Top);
stream.Write(Right);
stream.Write(Bottom);
}
public static Rectangle LoadFromFile(BinaryReader stream)
{
var l = stream.ReadInt32();
var t = stream.ReadInt32();
var r = stream.ReadInt32();
var b = stream.ReadInt32();
return new Rectangle { Left = l, Top = t, Right = r, Bottom = b };
}
public bool Equals(Rectangle other)
{
return other.Top == Top && other.Left == Left && other.Bottom == Bottom && other.Right == Right;
}
}
/// <summary>
/// Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.
/// Basically a version of <see cref="System.Drawing.Point"/> without the extra stuff.
/// </summary>
public struct Point : IEquatable<Point>
{
public int X { get; set; }
public int Y { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Noxico.Point"/> class with the specified coordinates.
/// </summary>
/// <param name="x">The horizontal position of the point.</param>
/// <param name="y">The vertical position of the point.</param>
public Point(int x, int y) : this()
{
X = x;
Y = y;
}
/// <summary>
/// Compares two <see cref="Noxico.Point"/> objects.
/// The result specifies whether the values of the <see cref="Noxico.Point.X"/> and <see cref="Noxico.Point.Y"/> properties of the two <see cref="Noxico.Point"/> objects are equal.
/// </summary>
/// <param name="l">A <see cref="Noxico.Point"/> to compare.</param>
/// <param name="r">A <see cref="Noxico.Point"/> to compare.</param>
/// <returns>true if the <see cref="Noxico.Point.X"/> and <see cref="Noxico.Point.Y"/> values of left and right are equal; otherwise, false.</returns>
public static bool operator ==(Point l, Point r)
{
return l.X == r.X && l.Y == r.Y;
}
/// <summary>
/// Compares two <see cref="Noxico.Point"/> objects.
/// The result specifies whether the values of the <see cref="Noxico.Point.X"/> and <see cref="Noxico.Point.Y"/> properties of the two <see cref="Noxico.Point"/> objects are unequal.
/// </summary>
/// <param name="l">A <see cref="Noxico.Point"/> to compare.</param>
/// <param name="r">A <see cref="Noxico.Point"/> to compare.</param>
/// <returns>true if the <see cref="Noxico.Point.X"/> and <see cref="Noxico.Point.Y"/> values of left and right differ; otherwise, false.</returns>
public static bool operator !=(Point l, Point r)
{
return !(l == r);
}
/// <summary>
/// Specifies whether this <see cref="Noxico.Point"/> contains the same coordinates as the specified <c>System.Object</c>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to test.</param>
/// <returns>true if <paramref name="obj"/> is a <see cref="Noxico.Point"/> and has the same coordinates as this <see cref="Noxico.Point"/>.</returns>
public override bool Equals(object obj)
{
if (obj == null || !(obj is Point))
return false;
var opt = (Point)obj;
return opt.X == X && opt.Y == Y;
}
/// <summary>
/// Returns a hash code for this <see cref="Noxico.Point"/>.
/// </summary>
/// <returns>An integer value that specifies a hash value for this <see cref="Noxico.Point"/>.</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
/// <summary>
/// Converts this <see cref="Noxico.Point"/> to a human-readable string.
/// </summary>
/// <returns>A string that represents this <see cref="Noxico.Point"/>.</returns>
public override string ToString()
{
return string.Format("{0}x{1}", X, Y);
}
public bool Equals(Point other)
{
return other.X == X && other.Y == Y;
}
}
/// <summary>
/// The current operation state of the game.
/// </summary>
public enum UserMode
{
Walkabout, Aiming, Subscreen
}
public enum SolidityCheck
{
Walker, DryWalker, Flyer, Projectile
}
public enum Fluids
{
Dry, Water, KoolAid, Slime, Blood, Semen, Reserved1, Reserved2
}
public class TileDefinition
{
public string Name { get; private set; }
public int Index { get; private set; }
public int Glyph { get; private set; }
public Color Foreground { get; private set; }
public Color Background { get; private set; }
public Color MultiForeground { get; private set; }
public bool Wall { get; private set; }
public bool Ceiling { get; private set; }
public bool Cliff { get; private set; }
public bool Fence { get; private set; }
public bool Grate { get; private set; }
public bool CanBurn { get; private set; }
public string FriendlyName { get; private set; }
public string Description { get; private set; }
public Token Variants { get; private set; }
public int VariableWall { get; private set; }
public bool IsVariableWall { get { return VariableWall > 0; } }
public bool SolidToWalker { get { return Wall || Fence || Cliff; } }
public bool SolidToFlyer { get { return Ceiling || Wall; } }
public bool SolidToProjectile { get { return (Wall && !Grate); } }
private static Dictionary<int, TileDefinition> defs;
static TileDefinition()
{
defs = new Dictionary<int, TileDefinition>();
var tml = Mix.GetTokenTree("tiles.tml", true);
foreach (var tile in tml.Where(t => t.Name == "tile"))
{
var i = (int)tile.Value;
var def = new TileDefinition
{
Index = i,
Name = tile.GetToken("id").Text,
Glyph = (int)tile.GetToken("char").Value,
Background = Color.FromName(tile.GetToken("back").Text),
Foreground = tile.HasToken("fore") ? Color.FromName(tile.GetToken("fore").Text) : Color.FromName(tile.GetToken("back").Text).Darken(),
Wall = tile.HasToken("wall"),
Ceiling = tile.HasToken("ceiling"),
Cliff = tile.HasToken("cliff"),
Fence = tile.HasToken("fence"),
Grate = tile.HasToken("grate"),
CanBurn = tile.HasToken("canburn"),
FriendlyName = tile.HasToken("_n") ? tile.GetToken("_n").Text : null,
Description = tile.HasToken("description") ? tile.GetToken("description").Text : null,
Variants = tile.HasToken("variants") ? tile.GetToken("variants") : new Token("variants"),
VariableWall = tile.HasToken("varwall") ? (int)tile.GetToken("varwall").Value : 0,
};
def.MultiForeground = tile.HasToken("mult") ? Color.FromName(tile.GetToken("mult").Text) : def.Foreground;
defs.Add(i, def);
}
}
public static TileDefinition Find(string tileName, bool noVariants)
{
var def = defs.FirstOrDefault(t => t.Value.Name.Equals(tileName, StringComparison.InvariantCultureIgnoreCase)).Value;
if (def == null)
return defs[0];
if (!noVariants && def.Variants.Tokens.Count > 0)
{
var iant = def.Variants.Tokens.PickOne();
if (Random.NextDouble() > iant.Value)
def = TileDefinition.Find(iant.Name, false);
}
return def;
}
public static TileDefinition Find(string tileName)
{
return Find(tileName, false);
}
public static TileDefinition Find(int index, bool noVariants)
{
if (defs.ContainsKey(index))
{
var def = defs[index];
if (!noVariants && def.Variants.Tokens.Count > 0)
{
var iant = def.Variants.Tokens.PickOne();
if (Random.NextDouble() > iant.Value)
def = TileDefinition.Find(iant.Name);
}
return def;
}
else
return null;
}
public static TileDefinition Find(int index)
{
return Find(index, false);
}
public override string ToString()
{
return Name;
}
}
/// <summary>
/// A single tile on a board.
/// </summary>
public class Tile
{
public int Index { get; set; }
public Fluids Fluid { get; set; }
public bool Shallow { get; set; }
public int BurnTimer { get; set; }
public bool Seen { get; set; }
public Color SlimeColor { get; set; }
public int InherentLight { get; set; }
public bool SolidToWalker { get { return Definition.SolidToWalker; } }
public bool SolidToDryWalker { get { return Definition.SolidToWalker || (Fluid != Fluids.Dry && !Shallow); } }
public bool SolidToFlyer { get { return Definition.SolidToFlyer; } }
public bool SolidToProjectile { get { return Definition.SolidToProjectile; } }
public TileDefinition Definition
{
get
{
return TileDefinition.Find(Index, true);
}
set
{
Index = value.Index;
}
}
public void SaveToFile(BinaryWriter stream)
{
stream.Write((UInt16)Index);
var bits = new BitVector32();
bits[32] = Shallow;
bits[64] = (InherentLight > 0);
bits[128] = Seen;
stream.Write((byte)((byte)bits.Data | (byte)Fluid));
stream.Write((byte)BurnTimer);
if (SlimeColor == null)
SlimeColor = Color.Transparent;
SlimeColor.SaveToFile(stream);
if (InherentLight > 0)
stream.Write((byte)InherentLight);
}
public void LoadFromFile(BinaryReader stream)
{
Index = stream.ReadUInt16();
var set = stream.ReadByte();
var bits = new BitVector32(set);
Shallow = bits[32];
Seen = bits[128];
BurnTimer = stream.ReadByte();
Fluid = (Fluids)(set & 7);
SlimeColor = Toolkit.LoadColorFromFile(stream);
if (bits[64])
InherentLight = stream.ReadByte();
}
public Tile Clone()
{
return new Tile
{
Index = this.Index,
Fluid = this.Fluid,
Shallow = this.Shallow,
BurnTimer = this.BurnTimer,
Seen = this.Seen,
SlimeColor = this.SlimeColor,
InherentLight = this.InherentLight
};
}
public override string ToString()
{
if (Fluid != Fluids.Dry)
return string.Format("{0} - {1}", Definition, Fluid);
return Definition.ToString();
}
}
/// <summary>
/// An exit of some sort. Activate them by standing on them and pressing Enter.
/// </summary>
public class Warp
{
public static int GeneratorCount = 0;
public string ID { get; set; }
public int XPosition { get; set; }
public int YPosition { get; set; }
public int TargetBoard { get; set; }
public string TargetWarpID { get; set; }
public Warp()
{
ID = GeneratorCount.ToString();
GeneratorCount++;
}
public void SaveToFile(BinaryWriter stream)
{
stream.Write(ID);
stream.Write((byte)XPosition);
stream.Write((byte)YPosition);
stream.Write(TargetBoard);
stream.Write(TargetWarpID.OrEmpty());
}
public static Warp LoadFromFile(BinaryReader stream)
{
var newWarp = new Warp();
newWarp.ID = stream.ReadString();
newWarp.XPosition = stream.ReadByte();
newWarp.YPosition = stream.ReadByte();
newWarp.TargetBoard = stream.ReadInt32();
newWarp.TargetWarpID = stream.ReadString();
return newWarp;
}
}
public enum BoardType
{
Wild, Town, Dungeon, Special
}
public enum Realms
{
Nox, Seradevari,
}
public partial class Board : TokenCarrier
{
public static int GeneratorCount;
public static string HackishBoardTypeThing = "wild";
public int BoardNum { get; set; }
public int Width { get { return (int)GetToken("width").Value; } set { GetToken("width").Value = value; } }
public int Height { get { return (int)GetToken("height").Value; } set { GetToken("height").Value = value; } }
public int Lifetime { get; set; }
public string Name { get { return GetToken("name").Text; } set { GetToken("name").Text = value; } }
public string ID { get { return GetToken("id").Text; } set { GetToken("id").Text = value; } }
public string Music { get { return GetToken("music").Text; } set { GetToken("music").Text = value; } }
public BoardType BoardType { get { return (BoardType)GetToken("type").IntValue; } set { GetToken("type").IntValue = (int)value; } }
public int ToNorth { get { return (int)GetToken("north").Value; } set { GetToken("north").Value = value; } }
public int ToSouth { get { return (int)GetToken("south").Value; } set { GetToken("south").Value = value; } }
public int ToEast { get { return (int)GetToken("east").Value; } set { GetToken("east").Value = value; } }
public int ToWest { get { return (int)GetToken("west").Value; } set { GetToken("west").Value = value; } }
public bool AllowTravel { get { return !HasToken("noTravel"); } set { RemoveToken("noTravel"); if (!value) AddToken("noTravel"); } }
public int Stock
{
//Why does encounters/stock disappear?
get
{
return Path("encounters/stock") != null ? (int)Path("encounters/stock").Value : 0;
}
set
{
var stock = Path("encounters/stock");
if (stock == null)
{
AddToken("encounters");
GetToken("encounters").AddToken("stock");
stock = Path("encounters/stock");
}
stock.Value = value;
}
}
public Realms Realm { get { return (Realms)GetToken("realm").Value; } set { GetToken("realm").Value = (float)value; } }
public Point Coordinate
{
get
{
var coordinate = GetToken("coordinate");
if (coordinate != null)
return new Point((int)coordinate.GetToken("x").Value, (int)coordinate.GetToken("y").Value);
return default(Point);
}
set
{
var coordinate = GetToken("coordinate");
if (coordinate == null)
{
coordinate = AddToken("coordinate");
coordinate.AddToken("x", value.X);
coordinate.AddToken("y", value.Y);
return;
}
coordinate.GetToken("x").Value = value.X;
coordinate.GetToken("y").Value = value.Y;
}
}
public List<Entity> Entities { get; private set; }
public List<Warp> Warps { get; private set; }
public List<Point> DirtySpots { get; private set; }
public List<Entity> EntitiesToRemove { get; private set; }
public List<Entity> EntitiesToAdd { get; private set; }
public Tile[,] Tilemap;
public bool[,] Lightmap;
public Dictionary<string, Rectangle> Sectors { get; private set; }
public List<Point> ExitPossibilities { get; private set; }
public override string ToString()
{
return string.Format("#{0} {1} - \"{2}\" ({3})", BoardNum, ID, Name, BoardType);
}
public Board(int width, int height)
{
foreach (var t in new[] { "name", "id", "type", "music", "realm", "biome", "encounters" })
this.AddToken(t);
this.AddToken("culture", 0, "human");
this.GetToken("encounters").AddToken("stock", 0);
foreach (var t in new[] { "north", "south", "east", "west" })
this.AddToken(t, -1, string.Empty);
this.AddToken("width", width);
this.AddToken("height", height);
this.Entities = new List<Entity>();
this.EntitiesToRemove = new List<Entity>();
this.EntitiesToAdd = new List<Entity>();
this.Warps = new List<Warp>();
this.Sectors = new Dictionary<string, Rectangle>();
this.DirtySpots = new List<Point>();
this.Tilemap = new Tile[this.Width, this.Height];
this.Lightmap = new bool[this.Width, this.Height];
for (int row = 0; row < Height; row++)
for (int col = 0; col < Width; col++)
this.Tilemap[col, row] = new Tile();
}
public void Flush()
{
Program.WriteLine("Flushing board {0}.", ID);
var me = NoxicoGame.Me.Boards.FindIndex(x => x == this);
CleanUpSlimeTrails();
SaveToFile(me);
NoxicoGame.Me.Boards[me] = null;
}
public void SaveToFile(int index)
{
if (NoxicoGame.WorldName == "<Testing Arena>")
return;
var realm = System.IO.Path.Combine(NoxicoGame.SavePath, NoxicoGame.WorldName, "boards");
if (!Directory.Exists(realm))
Directory.CreateDirectory(realm);
//Program.WriteLine(" * Saving board {0}...", Name);
using (var stream = new BinaryWriter(File.Open(System.IO.Path.Combine(realm, "Board" + index + ".brd"), FileMode.Create)))
{
Toolkit.SaveExpectation(stream, "BORD");
Toolkit.SaveExpectation(stream, "TOKS");
stream.Write(Tokens.Count);
Tokens.ForEach(x => x.SaveToFile(stream));
Toolkit.SaveExpectation(stream, "AMNT");
stream.Write(Sectors.Count);
stream.Write(Entities.OfType<BoardChar>().Count() - Entities.OfType<Player>().Count());
stream.Write(Entities.OfType<DroppedItem>().Count());
stream.Write(Entities.OfType<Clutter>().Count());
stream.Write(Entities.OfType<Container>().Count());
stream.Write(Entities.OfType<Door>().Count());
stream.Write(Warps.Count);
Toolkit.SaveExpectation(stream, "TMAP");
for (int row = 0; row < Height; row++)
for (int col = 0; col < Width; col++)
Tilemap[col, row].SaveToFile(stream);
Toolkit.SaveExpectation(stream, "SECT");
foreach (var sector in Sectors)
{
stream.Write(sector.Key);
sector.Value.SaveToFile(stream);
}
Toolkit.SaveExpectation(stream, "ENTT");
foreach (var e in Entities.OfType<BoardChar>())
if (e is Player)
continue;
else
e.SaveToFile(stream);
foreach (var e in Entities.OfType<DroppedItem>())
e.SaveToFile(stream);
foreach (var e in Entities.OfType<Clutter>())
e.SaveToFile(stream);
foreach (var e in Entities.OfType<Container>())
e.SaveToFile(stream);
foreach (var e in Entities.OfType<Door>())
e.SaveToFile(stream);
Toolkit.SaveExpectation(stream, "WARP");
Warps.ForEach(x => x.SaveToFile(stream));
}
}
public static Board LoadFromFile(int index)
{
var realm = System.IO.Path.Combine(NoxicoGame.SavePath, NoxicoGame.WorldName, "boards");
var file = System.IO.Path.Combine(realm, "Board" + index + ".brd");
if (!File.Exists(file))
throw new FileNotFoundException("Board #" + index + " not found!");
var newBoard = new Board(1, 1);
using (var stream = new BinaryReader(File.Open(file, FileMode.Open)))
{
Toolkit.ExpectFromFile(stream, "BORD", "board description");
Toolkit.ExpectFromFile(stream, "TOKS", "board token tree");
var numTokens = stream.ReadInt32();
newBoard.Tokens.Clear();
for (var i = 0; i < numTokens; i++)
newBoard.Tokens.Add(Token.LoadFromFile(stream));
if (newBoard.HasToken("width"))
newBoard.Width = (int)newBoard.GetToken("width").Value;
else
newBoard.AddToken("width", WorldMapGenerator.TileWidth);
if (newBoard.HasToken("height"))
newBoard.Height = (int)newBoard.GetToken("height").Value;
else
newBoard.AddToken("height", WorldMapGenerator.TileHeight);
newBoard.Tilemap = new Tile[newBoard.Width, newBoard.Height];
newBoard.Lightmap = new bool[newBoard.Width, newBoard.Height];
newBoard.Name = newBoard.GetToken("name").Text;
newBoard.BoardType = (BoardType)newBoard.GetToken("type").Value;
if (newBoard.HasToken("music") && newBoard.GetToken("music").Text == "-")
newBoard.RemoveToken("music");
if (!newBoard.HasToken("music"))
{
var music = BiomeData.Biomes[(int)newBoard.GetToken("biome").Value].Music;
if (newBoard.BoardType == BoardType.Town)
music = "set://Town";
else if (newBoard.BoardType == BoardType.Dungeon)
music = "set://Dungeon";
newBoard.AddToken("music", music);
}
newBoard.Music = newBoard.GetToken("music").Text;
Toolkit.ExpectFromFile(stream, "AMNT", "board part amounts");
var secCt = stream.ReadInt32();
//var botCt = stream.ReadInt32();
var chrCt = stream.ReadInt32();
var drpCt = stream.ReadInt32();
var cltCt = stream.ReadInt32();
var conCt = stream.ReadInt32();
var dorCt = stream.ReadInt32();
var wrpCt = stream.ReadInt32();
Toolkit.ExpectFromFile(stream, "TMAP", "tile map");
for (int row = 0; row < newBoard.Height; row++)
{
for (int col = 0; col < newBoard.Width; col++)
{
newBoard.Tilemap[col, row] = new Tile();
newBoard.Tilemap[col, row].LoadFromFile(stream);
}
}
Toolkit.ExpectFromFile(stream, "SECT", "sector");
for (int i = 0; i < secCt; i++)
{
var secName = stream.ReadString();
newBoard.Sectors.Add(secName, Rectangle.LoadFromFile(stream));
}
Toolkit.ExpectFromFile(stream, "ENTT", "board entity");
//Unlike in SaveToFile, there's no need to worry about the player because that one's handled on the world level.
Clutter.ParentBoardHack = newBoard;
for (int i = 0; i < chrCt; i++)
newBoard.Entities.Add(BoardChar.LoadFromFile(stream));
for (int i = 0; i < drpCt; i++)
newBoard.Entities.Add(DroppedItem.LoadFromFile(stream));
for (int i = 0; i < cltCt; i++)
newBoard.Entities.Add(Clutter.LoadFromFile(stream));
for (int i = 0; i < conCt; i++)
newBoard.Entities.Add(Container.LoadFromFile(stream));
for (int i = 0; i < dorCt; i++)
newBoard.Entities.Add(Door.LoadFromFile(stream));
Toolkit.ExpectFromFile(stream, "WARP", "board warp");
for (int i = 0; i < wrpCt; i++)
newBoard.Warps.Add(Warp.LoadFromFile(stream));
//Quick hack, doesn't really warrant another version bump.
if (newBoard.Path("encounters/stock") == null)
newBoard.GetToken("encounters").AddToken("stock", newBoard.GetToken("encounters").Value * 2);
newBoard.RespawnEncounters();
newBoard.CleanUpSlimeTrails();
newBoard.CleanUpCorpses();
newBoard.BindEntities();
foreach (var e in newBoard.Entities.OfType<BoardChar>())
e.RunScript(e.OnLoad);
foreach (var e in newBoard.Entities.OfType<Door>())
e.UpdateMapSolidity();
newBoard.UpdateLightmap(null, true);
newBoard.CheckCombatStart();
//Program.WriteLine(" * Loaded board {0}...", newBoard.Name);
}
return newBoard;
}
private void CleanUpCorpses()
{
foreach (var corpse in Entities.OfType<Container>().Where(x => x.Token.HasToken("corpse")))
if (Random.NextDouble() > 0.7)
this.EntitiesToRemove.Add(corpse);
}
public void ClampCoord(ref int row, ref int col)
{
if (col >= Width)
col = Width - 1;
if (col < 0)
col = 0;
if (row >= Height)
row = Height - 1;
if (row < 0)
row = 0;
}
public bool IsSolid(int row, int col, SolidityCheck check = SolidityCheck.Walker)
{
ClampCoord(ref row, ref col);
if (check == SolidityCheck.Walker && Tilemap[col, row].SolidToWalker)
return true;
else if (check == SolidityCheck.DryWalker && Tilemap[col, row].SolidToDryWalker)
return true;
else if (check == SolidityCheck.Flyer && Tilemap[col, row].SolidToFlyer)
return true;
else if (check == SolidityCheck.Projectile && Tilemap[col, row].SolidToProjectile)
return true;
return Tilemap[col, row].Definition.Wall;
}
public bool IsBurning(int row, int col)
{
ClampCoord(ref row, ref col);
if (Tilemap[col, row].Fluid != Fluids.Dry)
return false;
if (!Tilemap[col, row].Definition.CanBurn)
return false;
return Tilemap[col, row].BurnTimer > 0;
}
public bool IsWater(int row, int col)
{
ClampCoord(ref row, ref col);
return Tilemap[col, row].Fluid != Fluids.Dry && !Tilemap[col, row].Shallow;
}
public bool IsLit(int row, int col)
{
ClampCoord(ref row, ref col);
return Lightmap[col, row];
}
public bool IsSeen(int row, int col)
{
ClampCoord(ref row, ref col);
return Tilemap[col, row].Seen;
}
public string GetDescription(int row, int col)
{
ClampCoord(ref row, ref col);
var tileDef = Tilemap[col, row].Definition;
if (tileDef.IsVariableWall)
return TileDefinition.Find(tileDef.VariableWall, true).Description;
return Tilemap[col, row].Definition.Description;
}
public string GetName(int row, int col)
{
ClampCoord(ref row, ref col);
return Tilemap[col, row].Definition.Name;
}
public void SetTile(int row, int col, int index)
{
Tilemap[col, row].Index = index;
DirtySpots.Add(new Point(col, row));
}
public void SetTile(int row, int col, string tileName)
{
SetTile(row, col, TileDefinition.Find(tileName).Index);
}
public void SetTile(int row, int col, TileDefinition def)
{
if (def == null)
return;
SetTile(row, col, def.Index);
}
public void Immolate(int row, int col)
{
ClampCoord(ref row, ref col);
var tile = Tilemap[col, row];
if (tile.Definition.CanBurn && tile.Fluid == Fluids.Dry)
{
tile.BurnTimer = Random.Next(20, 23) * 3;
DirtySpots.Add(new Point(col, row));
}
}
public void TrailSlime(int row, int col, Color color)
{
ClampCoord(ref row, ref col);
var tile = Tilemap[col, row];
var newColor = color.Darken(1.4);
if (tile.Fluid == Fluids.Dry)
{
tile.Fluid = Fluids.Slime;
tile.SlimeColor = newColor;
tile.Shallow = true;
tile.BurnTimer = (Random.Next(0, 4) * 10) + 100;
DirtySpots.Add(new Point(row, col));
}
else if (tile.Fluid == Fluids.Slime)
{
tile.SlimeColor = Toolkit.Lerp(tile.SlimeColor, newColor, 0.5);
tile.BurnTimer += (Random.Next(0, 4) * 10) + 100;
DirtySpots.Add(new Point(row, col));
}
}
public void BindEntities()
{
foreach (var entity in this.Entities)
entity.ParentBoard = this;
}
public void Burn(bool spread)
{
for (int row = 0; row < Height; row++)
{
for (int col = 0; col < Width; col++)
{
if (Tilemap[col, row].BurnTimer > 0)
{
if (Tilemap[col, row].Fluid == Fluids.Dry)
{
DirtySpots.Add(new Point(col, row));
if (!spread)
continue;
Tilemap[col, row].BurnTimer--;
if (Tilemap[col, row].BurnTimer == 0)
{
Tilemap[col, row].Definition = TileDefinition.Find("ash");
DirtySpots.Add(new Point(col, row));
}
else if (Tilemap[col, row].BurnTimer == 10 && Random.Flip())
{
Immolate(row - 1, col);
Immolate(row, col - 1);
Immolate(row + 1, col);
Immolate(row, col + 1);
if (Random.Flip())
{
Immolate(row - 1, col - 1);
Immolate(row - 1, col + 1);
Immolate(row + 1, col - 1);
Immolate(row + 1, col + 1);
}
}
}
else if (Tilemap[col, row].Fluid == Fluids.Slime && Tilemap[col, row].Shallow)
{
if (spread)
Tilemap[col, row].BurnTimer--;
if (Tilemap[col, row].BurnTimer == 0)
{
Tilemap[col, row].Fluid = Fluids.Dry;
DirtySpots.Add(new Point(col, row));
}
}
}
}
}
}
public void Update(bool active = false, bool surrounding = false)
{
Lifetime = 0;
if (active)
{
foreach (var entity in this.Entities.Where(x => !x.Passive && !(x is Player)))
{
entity.Update();
if (NoxicoGame.Me.Player.Character.Health <= 0)
return;
}
if (!surrounding && BoardType != BoardType.Dungeon)
UpdateSurroundings();
Burn(true);
return;
}
Burn(false);
foreach (var entity in this.Entities.Where(x => x.Passive))
entity.Update();
if (NoxicoGame.Me.CurrentBoard == this)
NoxicoGame.Me.Player.Update();
if (EntitiesToRemove.Count > 0)
{
EntitiesToRemove.ForEach(x => { if (x is BoardChar) { ((BoardChar)x).Character.ResetEquipmentCarries(); } });
EntitiesToRemove.ForEach(x => { Entities.Remove(x); this.DirtySpots.Add(new Point(x.XPosition, x.YPosition)); });
EntitiesToRemove.Clear();
}
if (EntitiesToAdd.Count > 0)
{
foreach (var entity in EntitiesToAdd)
{
entity.ParentBoard = this;
Entities.Add(entity);
}
//Entities.AddRange(EntitiesToAdd);
EntitiesToAdd.Clear();
}
}
public void UpdateSurroundings()
{
var nox = NoxicoGame.Me;
if (this != nox.CurrentBoard)
return;
if (this.ToNorth > -1)
nox.GetBoard(this.ToNorth).Update(true, true);
if (this.ToSouth > -1)
nox.GetBoard(this.ToSouth).Update(true, true);
if (this.ToEast > -1)
nox.GetBoard(this.ToEast).Update(true, true);
if (this.ToWest > -1)
nox.GetBoard(this.ToWest).Update(true, true);
//Handle the NorthWest corner through either North or West.
if (this.ToNorth > -1 || this.ToWest > -1)
{
if (this.ToNorth > -1 && nox.GetBoard(this.ToNorth).ToWest > -1)
nox.GetBoard(nox.GetBoard(this.ToNorth).ToWest).Update(true, true);
else if (this.ToWest > -1 && nox.GetBoard(this.ToWest).ToNorth > -1)
nox.GetBoard(nox.GetBoard(this.ToWest).ToNorth).Update(true, true);
}
//Similar for other corners
if (this.ToNorth > -1 || this.ToEast > -1)
{
if (this.ToNorth > -1 && nox.GetBoard(this.ToNorth).ToEast > -1)
nox.GetBoard(nox.GetBoard(this.ToNorth).ToEast).Update(true, true);
else if (this.ToEast > -1 && nox.GetBoard(this.ToEast).ToNorth > -1)
nox.GetBoard(nox.GetBoard(this.ToEast).ToNorth).Update(true, true);
}
if (this.ToSouth > -1 || this.ToWest > -1)
{
if (this.ToSouth > -1 && nox.GetBoard(this.ToSouth).ToWest > -1)
nox.GetBoard(nox.GetBoard(this.ToSouth).ToWest).Update(true, true);
else if (this.ToWest > -1 && nox.GetBoard(this.ToWest).ToSouth > -1)
nox.GetBoard(nox.GetBoard(this.ToWest).ToSouth).Update(true, true);
}
if (this.ToSouth > -1 || this.ToEast > -1)
{
if (this.ToSouth > -1 && nox.GetBoard(this.ToSouth).ToEast > -1)
nox.GetBoard(nox.GetBoard(this.ToSouth).ToEast).Update(true, true);
else if (this.ToEast > -1 && nox.GetBoard(this.ToEast).ToSouth > -1)
nox.GetBoard(nox.GetBoard(this.ToEast).ToSouth).Update(true, true);
}
}
public void AimCamera()
{
AimCamera(NoxicoGame.Me.Player.XPosition, NoxicoGame.Me.Player.YPosition);
}
public void AimCamera(int x, int y)
{
var oldCamX = NoxicoGame.CameraX;
if (this.Width == Program.Cols)
NoxicoGame.CameraX = 0;
else if (this.Width > Program.Cols)
{
NoxicoGame.CameraX = x - (Program.Cols / 2);
if (NoxicoGame.CameraX < 0)
NoxicoGame.CameraX = 0;
if (NoxicoGame.CameraX > this.Width - Program.Cols)
NoxicoGame.CameraX = this.Width - Program.Cols;
}
else
NoxicoGame.CameraX = -((Program.Cols / 2) - (this.Width / 2));
var oldCamY = NoxicoGame.CameraY;
if (this.Height == Program.Rows)
NoxicoGame.CameraY = 0;
else if (this.Height > Program.Rows)
{
NoxicoGame.CameraY = y - (Program.Rows / 2);
if (NoxicoGame.CameraY < 0)
NoxicoGame.CameraY = 0;
if (NoxicoGame.CameraY > this.Height - Program.Rows)
NoxicoGame.CameraY = this.Height - Program.Rows;
}
else
NoxicoGame.CameraY = -((Program.Rows / 2) - (this.Height / 2));
if (oldCamX != NoxicoGame.CameraX || oldCamY != NoxicoGame.CameraY)
Redraw();
}
public void Redraw()
{
for (int row = 0; row < Height; row++)
for (int col = 0; col < Width; col++)
DirtySpots.Add(new Point(col, row));
}
public void Draw(bool force = false)
{
if (Height < Program.Rows || Width < Program.Cols)
{
/*
for (var r = 0; r < Program.Rows; r++)
for (var c = 0; c < Program.Cols; c++)
NoxicoGame.HostForm.SetCell(r, c, (char)0xB0, Color.DarkGray, Color.Black);
*/
for (var r = 0; r < Program.Rows; r++)
{
if ((r < -NoxicoGame.CameraY - 1) || (r > -NoxicoGame.CameraY + Height))