-
Notifications
You must be signed in to change notification settings - Fork 6
/
InventoryItem.cs
833 lines (771 loc) · 26.5 KB
/
InventoryItem.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Noxico
{
public class InventoryItem : TokenCarrier
{
public string ID { get; private set; }
public string Name { get; private set; }
public string UnknownName { get; set; }
public bool IsProperNamed { get; set; }
public string Indefinite { get; set; }
public string Definite { get; set; }
public string OnUse { get; private set; }
public string OnEquip { get; private set; }
public string OnUnequip { get; private set; }
public string OnTimer { get; private set; }
/// <summary>
/// To help bind an InventoryItem to a Character's actual inventory. Key is supposed to be a character's ID.
/// </summary>
/// <remarks>
/// Don't forget to clear out characters that don't matter anymore.
/// You *really* don't want two of the same type of item equipped by the same character, too.
/// </remarks>
public Dictionary<string, Token> CarriedToken { get; private set; }
public override string ToString()
{
return ToString(null);
}
public string ToString(Token token, bool the = false, bool a = true)
{
if (ID == "book" && token != null && token.HasToken("id") && NoxicoGame.BookTitles.ContainsKey(token.GetToken("id").Text))
return string.Format("\"{0}\"", NoxicoGame.BookTitles[token.GetToken("id").Text][0]);
var canBeIdentified = !UnknownName.IsBlank();
var isIdentified = canBeIdentified ? NoxicoGame.Identifications.Contains(ID) : true;
var name = isIdentified ? Name : UnknownName;
var color = (token != null && token.HasToken("color")) ? Color.NameColor(token.GetToken("color").Text) : string.Empty;
var reps = new Dictionary<string, string>()
{
{ "[color]", color },
{ "[, color]", ", " + color },
{ "[color ]", color + " " },
{ "[color, ]", color + ", " },
};
if (color.Length == 0)
{
foreach (var key in reps.Keys)
name = name.Replace(key, string.Empty);
}
else
{
foreach (var item in reps)
name = name.Replace(item.Key, item.Value);
}
if (token != null && token.HasToken("bonus"))
name = string.Format("{0} +{1}", name, token.GetToken("bonus").Value);
var proper = IsProperNamed && isIdentified;
if (proper || !a)
{
if (the && !string.IsNullOrEmpty(Definite))
return Definite + ' ' + name;
return name;
}
if (HasToken("charge") && token != null)
{
var charge = 0;
var limit = "inf";
if (GetToken("charge").HasToken("limit"))
limit = Path("charge/limit").Value.ToString();
if (token.HasToken("charge") && token.GetToken("charge").Value > 0)
{
charge = (int)token.GetToken("charge").Value;
var collective = Path("charge/collectivename");
if (collective != null)
name = collective.Text;
}
return string.Format("{0} {1} ({2}/{3})", the ? Definite : i18n.GetArticle(name), name, charge, limit);
}
if (isIdentified)
return string.Format("{0} {1}", the ? Definite : Indefinite, name).Trim();
return string.Format("{0} {1}", the ? Definite : i18n.GetArticle(UnknownName), name).Trim();
}
public string GetDescription(Token token)
{
var canBeIdentified = !UnknownName.IsBlank();
var isIdentified = canBeIdentified ? NoxicoGame.Identifications.Contains(ID) : true;
if (this.ID == "book" && token != null && token.HasToken("id") && NoxicoGame.BookTitles.ContainsKey(token.GetToken("id").Text))
return i18n.Format("book_description", NoxicoGame.BookTitles[token.GetToken("id").Text][0], NoxicoGame.BookTitles[token.GetToken("id").Text][1]);
var a = string.Empty;
var description = isIdentified ? "description" : "unknown";
if (this.HasToken(description))
{
var ret = GetToken(description).Text;
var color = (token != null && token.HasToken("color")) ? Color.NameColor(token.GetToken("color").Text) : string.Empty;
var reps = new Dictionary<string, string>()
{
{ "[color]", color },
{ "[, color]", ", " + color },
{ "[color ]", color + " " },
{ "[color, ]", color + ", " },
};
if (color.Length == 0)
{
foreach (var key in reps.Keys)
ret = ret.Replace(key, string.Empty);
}
else
{
foreach (var item in reps)
ret = ret.Replace(item.Key, item.Value);
}
return ret;
}
else
{
a = Toolkit.StartsWithVowel(this.UnknownName) ? "an " : "a ";
}
return i18n.Format("inventory_thisis_x", this.ToString(token)); //"This is " + this.ToString(token) + ".";
}
public static InventoryItem FromToken(Token item)
{
var ni = new InventoryItem();
ni.ID = item.Text.Trim();
if (item.HasToken("_n"))
ni.Name = item.GetToken("_n").Text;
else
ni.Name = ni.ID.Replace('_', ' ');
if (item.HasToken("_u"))
ni.UnknownName = item.GetToken("_u").Text;
if (item.HasToken("_ia"))
ni.Indefinite = item.GetToken("_ia").Text;
else
ni.Indefinite = ni.Name.StartsWithVowel() ? i18n.GetString("an") : i18n.GetString("a");
ni.IsProperNamed = char.IsUpper(ni.Name[0]);
if (item.HasToken("_da"))
ni.Definite = item.GetToken("_da").Text;
else
ni.Definite = i18n.GetString("the");
ni.OnUse = null;
foreach (var script in item.Tokens.Where(t => t.Name == "script"))
{
switch (script.Text)
{
case "equip":
ni.OnEquip = script.GetToken("#text").Text;
break;
case "unequip":
ni.OnUnequip = script.GetToken("#text").Text;
break;
case "timer":
ni.OnTimer = script.GetToken("#text").Text;
break;
default:
ni.OnUse = script.GetToken("#text").Text;
break;
}
}
ni.Tokens.Clear();
ni.Tokens.AddRange(item.Tokens);
ni.RemoveAll("_n");
ni.RemoveAll("_u");
ni.RemoveAll("_ia");
ni.RemoveAll("_da");
ni.RemoveAll("script");
ni.CarriedToken = new Dictionary<string, Token>();
return ni;
}
public void CheckHands(Character character, string slot)
{
var max = slot == "ring" ? 8 : 2;
if (character.HasToken("monoceros"))
max = slot == "ring" ? 10 : 3;
if (character.HasToken("quadruped"))
{
if (slot == "hand")
max = character.HasToken("monoceros") ? 2 : 1;
else
max = character.HasToken("monoceros") ? 2 : 0;
}
/* therefore:
* normal pony unicorn humancorn
* rings 8 0 2 10
* weapons 2 1 2 3
*/
var worn = 0;
var items = character.GetToken("items");
foreach (var carriedItem in items.Tokens)
{
if (!carriedItem.HasToken("equipped"))
continue;
var find = NoxicoGame.KnownItems.Find(x => x.ID == carriedItem.Name);
if (find == null)
continue;
var equip = find.GetToken("equipable");
if (equip.HasToken(slot))
worn++;
}
if (worn >= max)
{
var error = "yourhandsarefull";
if (max == 1 && slot == "hand")
error = "yourmouthisfull";
else if (max == 2 && slot == "ring")
error = "yourhornisfull";
throw new ItemException(i18n.GetString(error));
}
}
public void CheckPants(Character character, Token item)
{
if (!(character.HasToken("taur") || (character.HasToken("quadruped"))))
return;
if ((item.HasToken("underpants") && item.HasToken("undershirt")) ||
(item.HasToken("pants") && item.HasToken("shirt")))
return; //allow bodysuits
throw new ItemException(i18n.GetString("cannot_equip_incompatible_body")); //"Your body is not made for this sort of clothing.");
}
public bool CanSeeThrough(Character who)
{
if (!HasToken("equipable"))
throw new ItemException("Tried to check translucency on something not equipable.");
if (CarriedToken.ContainsKey(who.ID) && (CarriedToken[who.ID].HasToken("torn") || CarriedToken[who.ID].HasToken("wet")))
return true;
return this.GetToken("equipable").HasToken("translucent");
}
public bool CanReachThrough(Character who, string part = null)
{
if (!HasToken("equipable"))
throw new ItemException("Tried to check reach on something not equipable.");
if (CarriedToken.ContainsKey(who.ID) && CarriedToken[who.ID].HasToken("torn"))
return true;
if (part.IsBlank())
return this.GetToken("equipable").HasToken("reach");
else if (this.GetToken("equipable").HasToken("reach"))
{
if (this.GetToken("equipable").GetToken("reach").Count() == 0)
return true;
return this.GetToken("equipable").GetToken("reach").HasToken(part);
}
return false;
}
public bool Equip(Character character, Token item)
{
/*
if rings and character is quadruped, error out.
if required slots have covering slots
check for target slot's reachability.
if unreachable, try to temp-remove items in covering slots, recursively.
if still unreachable, error out.
if required slots are taken
try to unequip the items in those slots, recursively.
if required slots are still taken, error out;
else, mark the item as equipped.
replace each temp-removed item whose required slots are still free.
*/
var equip = this.GetToken("equipable");
var tempRemove = new Stack<Token>();
//var items = character.GetToken("items");
//TODO: make full quadrupeds equip weapons in their mouth instead of the hands they don't have.
//This means they can carry only ONE weapon at a time, and maybe not be able to converse until unequipped.
if ((equip.HasToken("hands") || equip.HasToken("ring")) && (character.HasToken("quadruped")))
throw new ItemException(i18n.Format("cannot_equip_no_hands", this.ToString(item, true, false)));
if (equip.HasToken("hand"))
CheckHands(character, "hand");
else if (equip.HasToken("ring"))
CheckHands(character, "ring");
if (equip.HasToken("pants") || equip.HasToken("underpants") || equip.HasToken("shoes") || equip.HasToken("socks"))
CheckPants(character, equip);
if (character.HasToken("snaketail") && (equip.HasToken("pants") || equip.HasToken("underpants")))
throw new ItemException(i18n.Format("cannot_equip_no_legs", this.ToString(item, true, false)));
//lol
foreach (var nonLayeredSlot in new[] { "socks", "hat", "mask", "goggles", "neck" })
{
if (equip.HasToken(nonLayeredSlot))
{
var currentNonLayeredItem = character.GetEquippedItemBySlot(nonLayeredSlot);
if (currentNonLayeredItem != null)
currentNonLayeredItem.Unequip(character);
}
}
foreach (var t in equip.Tokens)
{
if (t.Name == "underpants" && (!TempRemove(character, tempRemove, "pants") || !TempRemove(character, tempRemove, "underpants")))
return false;
else if (t.Name == "undershirt" && (!TempRemove(character, tempRemove, "shirt") || !TempRemove(character, tempRemove, "undershirt")))
return false;
else if (t.Name == "shirt" && (!TempRemove(character, tempRemove, "shirt") || !TempRemove(character, tempRemove, "jacket")))
return false;
else if (t.Name == "jacket" && (!TempRemove(character, tempRemove, "cloak") || !TempRemove(character, tempRemove, "jacket")))
return false;
else if (t.Name == "socks" && (!TempRemove(character, tempRemove, "shoes") || !TempRemove(character, tempRemove, "socks")))
return false;
}
var succeed = true;
if (!this.OnEquip.IsBlank())
succeed = Convert.ToBoolean(RunScript(item, this.OnEquip, character, null, null));
if (succeed)
item.AddToken("equipped");
if (this.HasToken("timer") && !this.OnTimer.IsBlank() && !item.HasToken("timer"))
{
item.AddToken("timer").Value = (this.GetToken("timer").IntValue == 0) ? 60 : this.GetToken("timer").Value;
item.GetToken("timer").Text = NoxicoGame.InGameTime.ToBinary().ToString();
}
character.RecalculateStatBonuses();
character.CheckHasteSlow();
//Difficult bit: gotta re-equip tempremovals without removing the target item all over. THAT WOULD BE QUITE BAD.
return succeed;
}
public bool Unequip(Character character)
{
/*
if item's slots have covering slots
check for target slot's reachability.
if unreachable, try to temp-remove items in covering slots, recursively.
if still unreachable, error out.
if item is cursed, error out
mark item as unequipped.
*/
var item = this.CarriedToken.ContainsKey(character.ID) ? this.CarriedToken[character.ID] : null;
if (item != null && item.HasToken("cursed") && item.GetToken("cursed").HasToken("known"))
throw new ItemException(item.GetToken("cursed").Text.IsBlank(i18n.Format("cannot_remove_sticky", this.ToString(item, true)), item.GetToken("cursed").Text));
var equip = this.GetToken("equipable");
var tempRemove = new Stack<Token>();
var items = character.GetToken("items");
foreach (var t in equip.Tokens)
{
if (t.Name == "underpants")
TempRemove(character, tempRemove, "pants");
else if (t.Name == "undershirt")
TempRemove(character, tempRemove, "shirt");
else if (t.Name == "shirt")
TempRemove(character, tempRemove, "jacket");
else if (t.Name == "jacket")
TempRemove(character, tempRemove, "cloak");
else if (t.Name == "socks")
TempRemove(character, tempRemove, "shoes");
}
if (item == null)
item = items.Tokens.Find(x => x.Name == this.ID);
if (item.HasToken("cursed"))
{
item.GetToken("cursed").Tokens.Add(new Token("known"));
throw new ItemException(i18n.Format("surprise_its_sticky", this.ToString(item, true)));
}
var succeed = true;
if (!this.OnUnequip.IsBlank())
succeed = Convert.ToBoolean(RunScript(item, this.OnUnequip, character, null, null));
if (succeed)
item.RemoveToken("equipped");
if (!this.OnTimer.IsBlank() && this.Path("timer/evenunequipped") == null)
item.RemoveToken("timer");
//Not sure about automatically putting pants back on after taking them off to take off underpants...
//while (tempRemove.Count > 0)
// tempRemove.Pop().Tokens.Add(new Token("equipped"));
character.RecalculateStatBonuses();
character.CheckHasteSlow();
return succeed;
}
private bool TempRemove(Character character, Stack<Token> list, string slot)
{
foreach (var carriedItem in character.GetToken("items").Tokens)
{
if (!carriedItem.HasToken("equipped"))
continue;
var find = NoxicoGame.KnownItems.Find(x => x.ID == carriedItem.Name);
var equip = find.GetToken("equipable");
if (equip == null)
{
System.Windows.Forms.MessageBox.Show("Item " + carriedItem.Name + " is marked as equipped, but " + find.Name + " is not equippable.");
carriedItem.RemoveToken("equipped");
continue;
}
if (equip.HasToken(slot))
{
if (equip.HasToken("reach"))
return true;
var success = find.Unequip(character);
if (success)
list.Push(carriedItem);
return success;
}
}
return true;
}
public void Drop(BoardChar boardChar, Token item)
{
//Find a spot to drop the item
int lives = 1000, x = 0, y = 0;
while (lives > 0)
{
x = boardChar.XPosition + Random.Next(-1, 2);
y = boardChar.YPosition + Random.Next(-1, 2);
if (!boardChar.ParentBoard.IsSolid(y, x) && !boardChar.ParentBoard.IsBurning(y, x))
break;
lives--;
}
var tile = boardChar.ParentBoard.Tilemap[x, y];
if (tile.Fluid != Fluids.Dry || tile.Definition.Cliff)
NoxicoGame.AddMessage(i18n.Format(tile.Definition.Cliff ? "x_dropped_y_inthedepths" : ("x_dropped_y_inthe_" + tile.Fluid.ToString().ToLowerInvariant()), this.ToString(item, true, false)).Viewpoint(boardChar.Character));
if (lives == 0)
{
boardChar.Character.GetToken("items").Tokens.Remove(item);
boardChar.Character.CheckHasteSlow();
return;
}
var droppedItem = new DroppedItem(this, item)
{
XPosition = x,
YPosition = y,
ParentBoard = boardChar.ParentBoard,
};
droppedItem.AdjustView();
droppedItem.ParentBoard.EntitiesToAdd.Add(droppedItem);
boardChar.Character.GetToken("items").Tokens.Remove(item);
boardChar.Character.CheckHasteSlow();
}
public void Use(Character character, Token item, bool noConfirm = false)
{
var boardchar = NoxicoGame.Me.CurrentBoard.Entities.OfType<BoardChar>().First(x => x.Character == character);
var runningDesc = string.Empty;
Action<string> showDesc = new Action<string>(d =>
{
NoxicoGame.DrawStatus();
if (d.Contains('\n'))
MessageBox.Notice(runningDesc.Viewpoint(boardchar.Character, null));
else
NoxicoGame.AddMessage(runningDesc.Viewpoint(boardchar.Character, null));
});
#region Books
if (this.ID == "book")
{
TextScroller.ReadBook(item.GetToken("id").Text);
return;
}
#endregion
#region Equipment
if (this.HasToken("equipable"))
{
if (item == null)
{
var items = character.GetToken("items");
item = items.Tokens.Find(x => x.Name == this.ID);
}
if (!item.HasToken("equipped"))
{
//TODO: only ask if it's the player?
//Not wearing it
MessageBox.Ask(runningDesc + i18n.Format("inventory_equip_x", this.ToString(item, true)), () =>
{
try
{
if (this.Equip(character, item))
{
runningDesc += i18n.Format("x_equiped_y", this.ToString(item, true));
}
}
catch (ItemException c)
{
runningDesc += c.Message;
}
if (!runningDesc.IsBlank())
showDesc(runningDesc.Viewpoint(boardchar.Character));
return;
},
null);
}
else
{
//Wearing/wielding it
if (item.HasToken("cursed") && item.GetToken("cursed").HasToken("known"))
{
runningDesc += item.GetToken("cursed").Text.IsBlank(i18n.Format("inventory_cursed_" + (this.HasToken("plural") ? "plural" : "singular"), this.ToString(item, true)), item.GetToken("cursed").Text);
showDesc(runningDesc.Viewpoint(boardchar.Character));
return;
}
MessageBox.Ask(i18n.Format("inventory_unequip_x", this.ToString(item, true)), () =>
{
try
{
if (this.Unequip(character))
{
runningDesc += i18n.Format("x_unequiped_y", this.ToString(item, true));
}
}
catch (ItemException x)
{
runningDesc += x.Message;
}
if (!runningDesc.IsBlank())
showDesc(runningDesc.Viewpoint(boardchar.Character));
return;
},
null);
}
return;
}
#endregion
if (this.HasToken("ammo"))
{
MessageBox.Notice(i18n.GetString("thisisammo"));
return;
}
if (this.HasToken("quest") || this.HasToken("nouse"))
{
if (this.HasToken("description"))
runningDesc = this.GetToken("description").Text + "\n\n";
showDesc(runningDesc + i18n.GetString("noeffect"));
return;
}
//Confirm use of potentially hazardous items
if (!noConfirm)
{
var name = new StringBuilder();
if (this.IsProperNamed)
name.Append(this.Definite.IsBlank(string.Empty, this.Definite + ' '));
else
name.Append(this.Indefinite.IsBlank(string.Empty, this.Indefinite + ' '));
name.Append(this.Name);
if (item.HasToken("unidentified") && !this.UnknownName.IsBlank())
{
runningDesc = i18n.GetString("unidentified_warning");
}
else
{
if (this.HasToken("description"))
{
//No need to check for "worn" or "examined" here...
runningDesc = this.GetDescription(item) + "\n\n"; //this.GetToken("description").Text + "\n\n";
}
runningDesc += i18n.Format("use_x_confirm", this.ToString(item, true));
}
MessageBox.Ask(runningDesc, () => { this.Use(character, item, true); }, null);
return;
}
var statBonus = this.GetToken("statbonus");
if (statBonus != null)
{
foreach (var bonus in statBonus.Tokens)
{
if (bonus.Name == "health")
{
character.Health += bonus.Value;
if (character.Health > character.MaximumHealth)
character.Health = character.MaximumHealth;
}
}
}
var food = this.GetToken("food");
if (food != null)
Eat(character, food);
if (!this.OnUse.IsBlank())
RunScript(item, this.OnUse, character, boardchar, (x => runningDesc += x));
else
this.Consume(character, item);
if (!runningDesc.IsBlank())
showDesc(runningDesc.Viewpoint(boardchar.Character));
}
public void Consume(Character carrier, Token carriedItem)
{
if (this.HasToken("charge"))
{
var charge = carriedItem.Path("charge");
if (charge == null && carriedItem.Name == "charge")
charge = carriedItem;
if (charge == null || charge.IntValue == 1)
{
if (HasToken("revert"))
{
carriedItem.Name = GetToken("revert").Text;
carriedItem.Tokens.Clear();
}
else
{
carrier.GetToken("items").Tokens.Remove(carriedItem);
carrier.CheckHasteSlow();
}
}
else
charge.Value--;
}
else
{
if (HasToken("revert"))
{
carriedItem.Name = GetToken("revert").Text;
carriedItem.Tokens.Clear();
}
else
{
carrier.GetToken("items").Tokens.Remove(carriedItem);
carrier.CheckHasteSlow();
}
}
}
public object RunScript(Token item, string script, Character character, BoardChar boardchar, Action<string> running)
{
var env = Lua.Environment;
env.user = character;
env.thisItem = this;
env.thisToken = item;
env.Consume = new Action<string>(x => this.Consume(character, item) /* character.GetToken("items").Tokens.Remove(item) */);
env.print = new Action<string, bool>((x, y) =>
{
var paused = true;
MessageBox.ScriptPauseHandler = () =>
{
paused = false;
};
MessageBox.Notice((y ? x : x.Viewpoint(character)), true);
while (paused)
{
NoxicoGame.Me.Update();
System.Windows.Forms.Application.DoEvents();
}
});
env.ReportSet = new Action<List<string>>(x =>
{
foreach (var result in x)
if (!result.IsBlank() && result[0] != '\uE2FC')
NoxicoGame.AddMessage(result.Viewpoint(character));
});
env.Identify = new Action<string>(x =>
{
if (character.GetStat("mind") < 10)
{
//Dumb characters can't identify as well.
if (Random.NextDouble() < 0.5)
return;
}
//Random potion identification
/*
if (this.HasToken("randomized"))
{
var rid = (int)this.GetToken("randomized").Value;
if (this.Path("equipable/ring") != null && rid < 128)
rid += 128;
var rdesc = NoxicoGame.Me.Potions[rid];
if (rdesc[0] != '!')
{
//Still unidentified. Let's rock.
rdesc = '!' + rdesc;
NoxicoGame.Me.Potions[rid] = rdesc;
this.UnknownName = null;
}
//Random potions and rings are un-unidentified by taking away their UnknownName, but we clear the unidentified state anyway.
//item.RemoveToken("unidentified";
//runningDesc += "You have identified this as " + this.ToString(item, true) + ".";
//return;
}
*/
//Regular item identification
if (!this.UnknownName.IsBlank() && !NoxicoGame.Identifications.Contains(this.ID))
{
NoxicoGame.Identifications.Add(this.ID);
if (running != null)
running(i18n.Format("inventory_identify_as_x", this.ToString(item, true)));
}
});
//var ret = env.DoChunk(script, "lol.lua");
return Lua.Run(script, env);
}
#region PillowShout's additions
public bool IsSuit()
{
var eq = this.GetToken("equipable");
if (eq != null)
return eq.HasToken("pants") && eq.HasToken("shirt");
return false;
}
#endregion
public List<string> GetModifiers(Token token)
{
var info = new List<string>();
if (token != null && token.HasToken("torn"))
info.Add(i18n.GetString("sigil_torn"));
//if (HasToken("equipable"))
{
if (HasToken("weapon"))
{
var damage = Path("weapon/damage").Value;
if (token != null && token.HasToken("bonus"))
damage = (float)Math.Ceiling(damage * ((token.GetToken("bonus").Value + 1) * 0.75f));
info.Add(damage + " dmg");
if (new[] { "throwing", "small_firearm", "large_firearm", "huge_firearm" }.Contains(Path("weapon/skill").Text))
info.Add(i18n.GetString("sigil_ranged"));
else
info.Add(i18n.GetString("sigil_melee"));
}
if (HasToken("statbonus"))
{
foreach (var bonus in GetToken("statbonus").Tokens)
{
if (bonus.Name == "health")
info.Add(bonus.Value + " HP");
else
info.Add(bonus.Value + " " + bonus.Name.Remove(3).ToUpperInvariant());
}
}
}
return info;
}
public string ToLongString(Token token)
{
var info = GetModifiers(token);
if (info.Count == 0)
return ToString(token);
return string.Format("{0} ({1})", ToString(token), info.Join());
}
public void Eat(Character gourmand, Token item)
{
if (item.HasToken("fat"))
{
var hwa = Random.Flip() ? "hips" : Random.Flip() ? "waist" : "ass/size";
if (gourmand.Path(hwa) == null)
return;
var change = Random.NextDouble() * 0.25;
if (change > 0)
{
gourmand.Path(hwa).Value += (float)change;
if (!gourmand.HasToken("player"))
return;
if (hwa == "ass/size") hwa = "butt";
NoxicoGame.AddMessage(i18n.GetString("eat_toyour" + hwa).Viewpoint(gourmand));
}
}
}
//YOU ARE TEARING ME APAAAAHT LISA!!!
public static bool TearApart(InventoryItem equip, Character who, bool force = false)
{
if (!equip.CarriedToken.ContainsKey(who.ID))
return false; //no actual item to tear
if (!force && equip.HasToken("sturdy"))
return false; //failed, too sturdy
equip.CarriedToken[who.ID].AddToken("torn");
return true;
/*
var slot = "pants";
if (equip.HasToken("pants") && equip.HasToken("shirt"))
slot = "over";
else if (equip.HasToken("underpants"))
{
slot = "underpants";
if (equip.HasToken("undershirt"))
slot = "under";
}
carriedItem.Name = "tatteredshreds_" + slot;
carriedItem.Tokens.Clear();
*/
}
}
[Serializable]
public class ItemException : Exception
{
public ItemException()
: base()
{
}
public ItemException(string message)
: base(message)
{
}
public ItemException(string message, Exception exception)
: base(message, exception)
{
}
protected ItemException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
}
}