-
Notifications
You must be signed in to change notification settings - Fork 6
/
Entities.cs
644 lines (586 loc) · 19 KB
/
Entities.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
#if DEBUG
using Keys = System.Windows.Forms.Keys;
#endif
namespace Noxico
{
public enum Direction
{
North, East, South, West,
}
public class Entity
{
public Board ParentBoard { get; set; }
public string ID { get; set; }
#if DEBUG
[System.ComponentModel.Editor(typeof(GlyphSelector), typeof(System.Drawing.Design.UITypeEditor))]
#endif
public int Glyph { get; set; }
public Color ForegroundColor { get; set; }
public Color BackgroundColor { get; set; }
/// <summary>The Entity's position in columns, from left to right.</summary>
public int XPosition { get; set; }
/// <summary>The Entity's position in rows, from top to bottom.</summary>
public int YPosition { get; set; }
///<summary>For Entities, indicates whether a BoardChar can walk into this Entity's Tile. For BoardChars, indicates whether other BoardChars can switch positions into this Tile.</summary>
public bool Blocking { get; set; }
public bool Passive { get; set; }
public int Energy { get; set; }
public Entity()
{
ID = "[null]";
Glyph = '?';
this.Energy = Random.Next(4000, 5000);
}
public virtual void Draw()
{
var localX = this.XPosition - NoxicoGame.CameraX;
var localY = this.YPosition - NoxicoGame.CameraY;
if (localX >= Program.Cols || localY >= Program.Rows || localX < 0 || localY < 0)
return;
var b = ((MainForm)NoxicoGame.HostForm).IsMultiColor ? TileDefinition.Find(this.ParentBoard.Tilemap[this.XPosition, this.YPosition].Index, true).Background : this.BackgroundColor;
if (ParentBoard.IsLit(this.YPosition, this.XPosition))
NoxicoGame.HostForm.SetCell(localY, localX, this.Glyph, this.ForegroundColor, b);
//else
// NoxicoGame.HostForm.SetCell(localY, localX, this.Glyph, this.ForegroundColor.Night(), b.Night());
}
public virtual void Move(Direction targetDirection, SolidityCheck check)
{
var touched = this.CanMove(targetDirection, check);
if (touched is Door)
{
var door = touched as Door;
if (door.Locked)
return;
if (door.Closed)
{
if (this is Player)
NoxicoGame.Sound.PlaySound("set://DoorOpen");
Energy -= 500;
door.Closed = false;
}
}
else if (touched != null)
{
return;
}
if (XPosition >= 0 && YPosition >= 0 && XPosition < this.ParentBoard.Width && YPosition < this.ParentBoard.Height)
this.ParentBoard.DirtySpots.Add(new Point(XPosition, YPosition));
var newX = 0;
var newY = 0;
Toolkit.PredictLocation(XPosition, YPosition, targetDirection, ref newX, ref newY);
XPosition = newX;
YPosition = newY;
}
public virtual void Move(Direction targetDirection)
{
Move(targetDirection, SolidityCheck.Walker);
}
public virtual object CanMove(Board board, int x, int y, SolidityCheck check)
{
if (x < 0 || y < 0 || x >= this.ParentBoard.Width || y >= this.ParentBoard.Height)
return false;
foreach (var entity in board.Entities)
{
if (entity == this)
continue;
if (entity.XPosition == x && entity.YPosition == y)
{
if (entity is Door && ((Door)entity).Closed)
return entity as Door;
if (entity.Blocking)
return entity;
}
}
if (board.IsSolid(y, x, check))
return false;
return null;
}
public virtual object CanMove(Board board, int x, int y)
{
return CanMove(board, x, y, SolidityCheck.Walker);
}
public virtual object CanMove(Direction targetDirection, SolidityCheck check)
{
var newX = this.XPosition;
var newY = this.YPosition;
Toolkit.PredictLocation(newX, newY, targetDirection, ref newX, ref newY);
return CanMove(this.ParentBoard, newX, newY, check);
}
public virtual object CanMove(Direction targetDirection)
{
return CanMove(targetDirection, SolidityCheck.Walker);
}
public virtual void Update()
{
}
public virtual void SaveToFile(BinaryWriter stream)
{
//Program.WriteLine(" * Saving {0} {1}...", this.GetType(), ID ?? "????");
Toolkit.SaveExpectation(stream, "ENTT");
stream.Write(ID ?? "<Null>");
stream.Write((char)Glyph);
BackgroundColor.SaveToFile(stream);
ForegroundColor.SaveToFile(stream);
stream.Write((byte)XPosition);
stream.Write((byte)YPosition);
stream.Write((byte)0); //was Flow
stream.Write(Blocking);
}
public static Entity LoadFromFile(BinaryReader stream)
{
Toolkit.ExpectFromFile(stream, "ENTT", "entity");
var newEntity = new Entity();
newEntity.ID = stream.ReadString();
newEntity.Glyph = stream.ReadChar();
newEntity.BackgroundColor = Toolkit.LoadColorFromFile(stream);
newEntity.ForegroundColor = Toolkit.LoadColorFromFile(stream);
newEntity.XPosition = stream.ReadByte();
newEntity.YPosition = stream.ReadByte();
stream.ReadByte(); //was Flow
newEntity.Blocking = stream.ReadBoolean();
//Program.WriteLine(" * Loaded {0} {1}...", newEntity.GetType(), newEntity.ID ?? "????");
return newEntity;
}
public int DistanceFrom(Entity other)
{
var dX = Math.Abs(this.XPosition - other.XPosition);
var dY = Math.Abs(this.YPosition - other.YPosition);
return (dX < dY) ? dY : dX;
}
public virtual bool CanSee(Entity other)
{
foreach (var point in Toolkit.Line(XPosition, YPosition, other.XPosition, other.YPosition))
//if ((ParentBoard.IsSolid(point.Y, point.X) && !ParentBoard.IsWater(point.Y, point.X)) && ParentBoard.IsLit(point.Y, point.X))
if (ParentBoard.IsSolid(point.Y, point.X, SolidityCheck.Projectile) && ParentBoard.IsLit(point.Y, point.X))
return false;
return true;
}
}
public class Clutter : Entity
{
private static List<Token> clutterDB;
public static Board ParentBoardHack { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Life { get; set; }
public bool CanBurn { get; set; }
public string DBRole { get; private set; }
private bool descriptionFromDB;
public Clutter()
{
this.Glyph = '?';
this.ForegroundColor = Color.Silver;
this.BackgroundColor = Color.Black;
}
public Clutter(char character, Color foreColor, Color backColor, bool blocking = false, string name = "thing", string description = "This is a thing.")
{
this.Glyph = character;
this.ForegroundColor = foreColor;
this.BackgroundColor = backColor;
this.Blocking = blocking;
this.Name = name;
this.Description = description;
}
public override void Update()
{
base.Update();
if (CanBurn && ParentBoard.IsBurning(YPosition, XPosition))
{
ParentBoard.EntitiesToRemove.Add(this);
return;
}
if (Life > 0)
{
Life--;
if (Life == 0)
{
ParentBoard.EntitiesToRemove.Add(this);
return;
}
}
}
public override void Move(Direction targetDirection, SolidityCheck check = SolidityCheck.Walker)
{
Console.WriteLine("Trying to move clutter.");
}
public static bool ResetToKnown(Entity thing)
{
if (!(thing is Clutter) && !(thing is Container))
throw new InvalidCastException("ResetToKnown only takes Clutter and Container objects.");
var name = string.Empty;
if (thing is Clutter) name = ((Clutter)thing).Name;
else if (thing is Container) name = ((Container)thing).Name;
if (clutterDB == null)
clutterDB = Mix.GetTokenTree("clutter.tml", true);
var knownThing = clutterDB.FirstOrDefault(kc =>
thing.Glyph == kc.GetToken("char").IntValue ||
(!name.IsBlank() && name.Equals(kc.Text, StringComparison.InvariantCultureIgnoreCase)) ||
thing.ID.StartsWith(kc.Text, StringComparison.InvariantCultureIgnoreCase));
if (knownThing != null)
{
thing.Glyph = (int)knownThing.GetToken("char").Value;
if (knownThing.HasToken("color"))
{
thing.ForegroundColor = Color.FromName(knownThing.GetToken("color").Text);
thing.BackgroundColor = thing.ForegroundColor.Darken(); //TileDefinition.Find(parentBoardHack.Tilemap[e.XPosition, e.YPosition].Index).Background;
}
if (knownThing.HasToken("background"))
{
if (knownThing.GetToken("background").Text == "inherit")
thing.BackgroundColor = TileDefinition.Find((thing.ParentBoard ?? ParentBoardHack).Tilemap[thing.XPosition, thing.YPosition].Index).Background;
else
thing.BackgroundColor = Color.FromName(knownThing.GetToken("background").Text);
}
if (thing is Clutter) ((Clutter)thing).CanBurn = knownThing.HasToken("canburn");
//else if (thing is Container) ((Container)thing).CanBurn = knownClutter.HasToken("canburn");
thing.Blocking = knownThing.HasToken("blocking");
if (knownThing.HasToken("description"))
{
if (thing is Clutter)
{
((Clutter)thing).Description = knownThing.GetToken("description").Text;
((Clutter)thing).descriptionFromDB = true;
}
else if (thing is Container && !((Container)thing).Token.HasToken("description"))
((Container)thing).Token.AddToken("description", knownThing.GetToken("description").Text);
}
if (knownThing.HasToken("name"))
{
if (thing is Clutter) ((Clutter)thing).Name = knownThing.GetToken("name").Text;
if (thing is Container) ((Container)thing).Name = knownThing.GetToken("name").Text;
}
if (knownThing.HasToken("role") && thing is Clutter) ((Clutter)thing).DBRole = knownThing.GetToken("role").Text;
return true;
}
return false;
}
public override void SaveToFile(BinaryWriter stream)
{
Toolkit.SaveExpectation(stream, "CLUT");
base.SaveToFile(stream);
stream.Write(Name.OrEmpty());
stream.Write(descriptionFromDB ? string.Empty : (Description.OrEmpty()));
stream.Write(CanBurn);
stream.Write(Life);
}
public static new Clutter LoadFromFile(BinaryReader stream)
{
Toolkit.ExpectFromFile(stream, "CLUT", "clutter entity");
var e = Entity.LoadFromFile(stream);
var newClutter = new Clutter()
{
ID = e.ID,
Glyph = e.Glyph,
ForegroundColor = e.ForegroundColor,
BackgroundColor = e.BackgroundColor,
XPosition = e.XPosition,
YPosition = e.YPosition,
Blocking = e.Blocking
};
newClutter.Name = stream.ReadString();
newClutter.Description = stream.ReadString();
newClutter.CanBurn = stream.ReadBoolean();
newClutter.Life = stream.ReadInt32();
Clutter.ResetToKnown(newClutter);
return newClutter;
}
}
public class DroppedItem : Entity
{
public InventoryItem Item { get; set; }
public Token Token { get; set; }
public string Name
{
get
{
if (Item == null)
return "???";
return Item.ToString(Token);
}
}
public DroppedItem(string item)
: this(NoxicoGame.KnownItems.First(i => i.ID == item), null)
{
}
public DroppedItem(InventoryItem item, Token carriedItem)
{
Item = item;
Token = new Token(item.ID);
if (carriedItem != null)
Token.AddSet(carriedItem.Tokens);
this.Glyph = '?';
this.ForegroundColor = Color.Silver;
this.BackgroundColor = Color.Black;
this.Blocking = false;
AdjustView();
}
public void AdjustView()
{
if (Item.HasToken("ascii"))
{
var ascii = Item.GetToken("ascii");
if (ascii.HasToken("char"))
this.Glyph = (char)ascii.GetToken("char").Value;
if (Item.HasToken("colored") && Token.HasToken("color"))
this.ForegroundColor = Color.FromName(Token.GetToken("color").Text);
else if (ascii.HasToken("fore"))
this.ForegroundColor = Color.FromName(ascii.GetToken("fore").Text);
else if (Item.ID == "book" && Token.Tokens.Count > 0)
this.ForegroundColor = Color.FromCGA(Token.GetToken("id").Text.GetHashCode() % 16);
if (ascii.HasToken("back"))
this.BackgroundColor = Color.FromName(ascii.GetToken("back").Tokens[0]);
else
this.BackgroundColor = this.ForegroundColor.Darken();
}
}
public override void Update()
{
if (!Item.HasToken("fireproof") && ParentBoard.IsBurning(YPosition, XPosition))
ParentBoard.EntitiesToRemove.Add(this);
}
public override void Move(Direction targetDirection, SolidityCheck check = SolidityCheck.Walker)
{
Console.WriteLine("Trying to move dropped item.");
}
public override void SaveToFile(BinaryWriter stream)
{
Toolkit.SaveExpectation(stream, "DROP");
base.SaveToFile(stream);
stream.Write(Item.ID);
Token.SaveToFile(stream);
}
public static new DroppedItem LoadFromFile(BinaryReader stream)
{
Toolkit.ExpectFromFile(stream, "DROP", "dropped item entity");
var e = Entity.LoadFromFile(stream);
//Handle broken references (warhammer > war_hammer)
var id = stream.ReadString();
var exists = NoxicoGame.KnownItems.Any(ki => ki.ID == id);
if (!exists)
{
var attempt = NoxicoGame.KnownItems.FirstOrDefault(ki => ki.ID.Replace("_", string.Empty) == id);
if (attempt != null)
id = attempt.ID;
else
id = NoxicoGame.KnownItems[0].ID; //Fallback.
}
var newItem = new DroppedItem(id)
{
ID = e.ID,
Glyph = e.Glyph,
ForegroundColor = e.ForegroundColor,
BackgroundColor = e.BackgroundColor,
XPosition = e.XPosition,
YPosition = e.YPosition,
};
newItem.Token = Token.LoadFromFile(stream);
return newItem;
}
public void Take(Character taker, Board ParentBoard)
{
if (!taker.HasToken("items"))
taker.Tokens.Add(new Token("items"));
taker.GetToken("items").Tokens.Add(Token);
taker.CheckHasteSlow();
ParentBoard.EntitiesToRemove.Add(this);
}
public static List<DroppedItem> GetItemsAt(Board board, int x, int y)
{
return new List<DroppedItem>(board.Entities.OfType<DroppedItem>().Where(drop => drop.XPosition == x && drop.YPosition == y));
}
public static void PickItemsFrom(List<DroppedItem> items)
{
var itemDict = new Dictionary<object, string>();
foreach (var item in items)
{
var name = item.Item.ToString(item.Token, false, false);
name = name.Substring(0, 1).ToUpperInvariant() + name.Substring(1);
itemDict.Add(item, name);
}
itemDict.Add(-2, i18n.GetString("action_pickup_all"));
itemDict.Add(-1, i18n.GetString("action_pickup_cancel")); //"...nothing"
ActionList.Show(i18n.GetString("action_pickup_window") /* "Pick up..." */, items[0].XPosition, items[0].YPosition, itemDict,
() =>
{
var player = NoxicoGame.Me.Player;
if (ActionList.Answer is int && (int)ActionList.Answer == -1)
{
//Cancelled.
return;
}
else if (ActionList.Answer is int && (int)ActionList.Answer == -2)
{
//All
foreach (var d in items)
{
d.Take(player.Character, player.ParentBoard);
player.Energy -= 1000;
}
NoxicoGame.AddMessage(i18n.Format("youpickup_all"));
}
else
{
var drop = ActionList.Answer as DroppedItem;
var item = drop.Item;
var token = drop.Token;
drop.Take(player.Character, player.ParentBoard);
player.Energy -= 1000;
NoxicoGame.AddMessage(i18n.Format("youpickup_x", item.ToString(token, true)));//, drop.ForegroundColor);
}
NoxicoGame.Sound.PlaySound("set://GetItem");
//player.ParentBoard.Redraw();
}
);
}
}
public class Container : Entity
{
public string Description
{
get
{
if (this.Token.HasToken("description") && !this.Token.GetToken("description").Text.IsBlank())
return this.Token.GetToken("description").Text;
return i18n.GetString("generic_description");
}
}
public Token Token { get; set; }
public string Name
{
get
{
return Token.Text.IsBlank("container", Token.Text);
}
set
{
Token.Text = value;
}
}
public Container(string name, List<Token> contents)
{
Token = new Token();
Token.Text = name;
var c = new Token("contents");
if (contents != null)
c.AddSet(contents);
Token.Tokens.Add(c);
Blocking = false;
}
public override void SaveToFile(BinaryWriter stream)
{
Toolkit.SaveExpectation(stream, "CONT");
base.SaveToFile(stream);
Token.SaveToFile(stream);
}
public static new Container LoadFromFile(BinaryReader stream)
{
Toolkit.ExpectFromFile(stream, "CONT", "container entity");
var e = Entity.LoadFromFile(stream);
var newContainer = new Container(string.Empty, null)
{
ID = e.ID,
Glyph = e.Glyph,
ForegroundColor = e.ForegroundColor,
BackgroundColor = e.BackgroundColor,
XPosition = e.XPosition,
YPosition = e.YPosition,
};
newContainer.Token = Token.LoadFromFile(stream);
Clutter.ResetToKnown(newContainer);
return newContainer;
}
public void AdjustView()
{
if (Token.HasToken("ascii"))
{
var ascii = Token.GetToken("ascii");
if (ascii.HasToken("char"))
this.Glyph = (char)ascii.GetToken("char").Value;
if (ascii.HasToken("fore"))
this.ForegroundColor = Color.FromName(ascii.GetToken("fore").Tokens[0]);
if (ascii.HasToken("back"))
this.BackgroundColor = Color.FromName(ascii.GetToken("back").Tokens[0]);
else
this.BackgroundColor = this.ForegroundColor.Darken();
}
}
}
public class Door : Entity
{
public int KeyIndex { get; set; }
public bool Locked { get; set; }
public bool Closed
{
get { return closed; }
set { closed = value; UpdateMapSolidity(); }
}
private bool closed;
private bool horizontal;
private bool dirInited;
private int closeTimer;
private void FindDirection()
{
horizontal = ParentBoard.IsSolid(YPosition, XPosition - 1) && ParentBoard.IsSolid(YPosition, XPosition + 1);
dirInited = true;
}
public override void Draw()
{
if (!dirInited)
FindDirection();
if (closed)
Glyph = horizontal ? 0x152 : 0x154;
else
Glyph = horizontal ? 0x153 : 0x155;
base.Draw();
}
public void UpdateMapSolidity()
{
if (ParentBoard == null)
return;
ParentBoard.Tilemap[XPosition, YPosition].Definition = TileDefinition.Find(closed ? "doorwayClosed" : "doorwayOpened");
}
public override void Update()
{
if (!closed)
{
closeTimer++;
if (closeTimer > 20)
{
Closed = true;
closeTimer = 0;
}
}
}
public override void SaveToFile(BinaryWriter stream)
{
Toolkit.SaveExpectation(stream, "DOOR");
base.SaveToFile(stream);
stream.Write(KeyIndex);
stream.Write(true /* Closed */);
stream.Write(Locked);
}
public static new Door LoadFromFile(BinaryReader stream)
{
Toolkit.ExpectFromFile(stream, "DOOR", "door entity");
var e = Entity.LoadFromFile(stream);
var newDoor = new Door()
{
ID = e.ID,
Glyph = e.Glyph,
ForegroundColor = e.ForegroundColor,
BackgroundColor = e.BackgroundColor,
XPosition = e.XPosition,
YPosition = e.YPosition,
Blocking = e.Blocking
};
newDoor.KeyIndex = stream.ReadInt32();
newDoor.Closed = stream.ReadBoolean();
newDoor.Locked = stream.ReadBoolean();
return newDoor;
}
}
}