/
DaggerfallCourtWindow.cs
525 lines (473 loc) · 23.7 KB
/
DaggerfallCourtWindow.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
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Allofich
// Contributors:
//
// Notes:
//
using UnityEngine;
using System;
using DaggerfallWorkshop.Game.UserInterface;
using DaggerfallWorkshop.Utility.AssetInjection;
using DaggerfallConnect.Utility;
using DaggerfallWorkshop.Utility;
using DaggerfallConnect;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Game.Utility;
using DaggerfallWorkshop.Game.Guilds;
namespace DaggerfallWorkshop.Game.UserInterfaceWindows
{
/// <summary>
/// Implements popup window for when player is in court for a crime.
/// </summary>
public class DaggerfallCourtWindow : DaggerfallPopupWindow
{
protected const string nativeImgName = "CORT01I0.IMG";
protected const string nativeImgName2 = "PRIS00I0.IMG";
protected const int courtTextTG = 550;
protected const int courtTextDB = 551;
protected const int courtTextStart = 8050;
protected const int courtTextFoundGuilty = 8055;
protected const int courtTextExecuted = 8060;
protected const int courtTextFreeToGo = 8062;
protected const int courtTextBanished = 8063;
protected const int courtTextHowConvince = 8064;
protected Texture2D nativeTexture;
protected Panel courtPanel = new Panel();
protected Entity.PlayerEntity playerEntity;
protected TextLabel daysUntilFreedomLabel;
protected int regionIndex;
protected int punishmentType;
protected int fine;
protected int daysInPrison;
protected int daysInPrisonLeft;
protected int state;
protected bool repositionPlayer;
protected float prisonUpdateTimer = 0f;
protected float prisonUpdateInterval = 0.3f; // Approximated to classic based on measuring a video recording.
// From FALL.EXE offset 0x1B34E0
// Vanilla unused crime values adjusted below by adding reasonable values for any zeros and a column for loan default crime.
// Mnemonics TryB&E Tressp B&E Assault Murder TaxEv CrimCon Vagrant Smuggle Pirate HiTre PickP Theft Treason LoanD
protected byte[] PenaltyPerLegalRepPoint = { 0x05, 0x05, 0x06, 0x06, 0x0A, 0x05, 0x05, 0x03, 0x08, 0x08, 0x09, 0x06, 0x00, 0x08, 0x00 };
protected short[] BasePenaltyAmounts = { 0x12C, 0xC8, 0x258, 0x3E8, 0x2710, 0xC8, 0x1F4, 0x64, 0x1F4, 0x1F4, 0x4B0, 0xC8, 0xC8, 0x3E8, 0x64 };
protected short[] MinimumPenaltyAmounts = { 0x32, 0x0A, 0x50, 0x0A, 0x2328, 0x0A, 0x0A, 0x02, 0x0A, 0x0A, 0xA0, 0x05, 0x05, 0x0A, 0x04 };
protected short[] MaximumPenaltyAmounts = { 0x3E8, 0x320, 0x4B0, 0x5DC, 0x2EE0, 0x2EE0, 0x5DC, 0x2BC, 0x5DC, 0x5DC, 0x7D0, 0x3E8, 0x3E8, 0x5DC, 0x2BC };
public int PunishmentType { get { return punishmentType; } }
public int Fine { get { return fine; } }
public int DaysInPrison { get { return daysInPrison; } }
public DaggerfallCourtWindow(IUserInterfaceManager uiManager, IUserInterfaceWindow previousWindow = null)
: base(uiManager, previousWindow)
{
}
protected override void Setup()
{
// Load native texture
nativeTexture = DaggerfallUI.GetTextureFromImg(nativeImgName);
if (!nativeTexture)
throw new Exception("DaggerfallCourtWindow: Could not load native texture.");
// Native court panel
courtPanel.HorizontalAlignment = HorizontalAlignment.Center;
courtPanel.Size = TextureReplacement.GetSize(nativeTexture, nativeImgName);
courtPanel.BackgroundTexture = nativeTexture;
NativePanel.Components.Add(courtPanel);
// Cancel any camera recoil
RaiseOnCourtScreenEvent();
// Add days until freedom label
daysUntilFreedomLabel = DaggerfallUI.AddTextLabel(DaggerfallUI.DefaultFont, new Vector2(156, 165), string.Empty, NativePanel);
daysUntilFreedomLabel.TextColor = DaggerfallUI.DaggerfallPrisonDaysUntilFreedomColor;
daysUntilFreedomLabel.ShadowColor = DaggerfallUI.DaggerfallPrisonDaysUntilFreedomShadowColor;
daysUntilFreedomLabel.HorizontalAlignment = HorizontalAlignment.Center;
playerEntity = GameManager.Instance.PlayerEntity;
AllowCancel = false;
}
public override void Update()
{
base.Update();
HandleCourtLogic();
}
protected virtual void HandleCourtLogic()
{
// Close immediately if no crime assigned to player
if (playerEntity.CrimeCommitted == Entity.PlayerEntity.Crimes.None)
{
CloseWindow();
return;
}
if (state == 0) // Starting
{
regionIndex = GameManager.Instance.PlayerGPS.CurrentRegionIndex;
int crimeType = (int)playerEntity.CrimeCommitted - 1;
int legalRep = playerEntity.RegionData[regionIndex].LegalRep;
// Get whether punishment is normal (fine/prison) or a severe punishment (banishment/execution)
int threshold1 = 0;
int threshold2 = 0;
if (legalRep < 0)
{
threshold1 = -legalRep;
if (threshold1 > 75)
threshold1 = 75;
threshold2 = -legalRep / 2;
if (threshold2 > 75)
threshold2 = 75;
}
if (Dice100.FailedRoll(threshold2) && Dice100.FailedRoll(threshold1))
punishmentType = 2; // fine/prison
else
punishmentType = 0; // banishment or execution
// Calculate penalty amount
int penaltyAmount = 0;
if (legalRep >= 0)
penaltyAmount = PenaltyPerLegalRepPoint[crimeType] * legalRep + BasePenaltyAmounts[crimeType];
else
penaltyAmount = BasePenaltyAmounts[crimeType] - PenaltyPerLegalRepPoint[crimeType] * legalRep;
if (penaltyAmount > MaximumPenaltyAmounts[crimeType])
penaltyAmount = MaximumPenaltyAmounts[crimeType];
else if (penaltyAmount < MinimumPenaltyAmounts[crimeType])
penaltyAmount = MinimumPenaltyAmounts[crimeType];
penaltyAmount /= 40;
// Calculate days of prison and fine
daysInPrison = 0;
fine = 0;
for (int i = 0; i < penaltyAmount; i++)
{
if ((DFRandom.rand() & 1) != 0)
fine += 40;
else
daysInPrison += 3;
}
// If player can't pay fine, limit fine and add to prison sentence
int playerGold = playerEntity.GetGoldAmount();
if (playerGold < fine)
{
daysInPrison += (fine - playerGold) / 40;
fine = playerGold;
}
DaggerfallMessageBox messageBox;
if (crimeType == 4 || crimeType == 3) // Assault or murder
{
// If player is a member of the Dark Brotherhood, they may be rescued for a violent crime
IGuild guild = GameManager.Instance.GuildManager.GetGuild((int)FactionFile.FactionIDs.The_Dark_Brotherhood);
if (guild.IsMember())
{
if (guild.Rank >= UnityEngine.Random.Range(0, 20))
{
messageBox = new DaggerfallMessageBox(uiManager, this, false, 149);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextDB));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.ClickAnywhereToClose = true;
uiManager.PushWindow(messageBox);
playerEntity.FillVitalSigns();
playerEntity.RaiseReputationForDoingSentence();
state = 100;
return;
}
}
}
if (crimeType <= 2 || crimeType == 11) // Attempted breaking and entering, trespassing, breaking and entering, pickpocketing
{
// If player is a member of the Thieves Guild, they may be rescued for a thieving crime
IGuild guild = GameManager.Instance.GuildManager.GetGuild((int)FactionFile.FactionIDs.The_Thieves_Guild);
if (guild.IsMember())
{
if (guild.Rank >= UnityEngine.Random.Range(0, 20))
{
messageBox = new DaggerfallMessageBox(uiManager, this, false, 149);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextTG));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.ClickAnywhereToClose = true;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
playerEntity.FillVitalSigns();
playerEntity.RaiseReputationForDoingSentence();
state = 100;
return;
}
}
}
messageBox = new DaggerfallMessageBox(uiManager, this, false, 105);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextStart));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.AddButton(DaggerfallMessageBox.MessageBoxButtons.Guilty);
messageBox.AddButton(DaggerfallMessageBox.MessageBoxButtons.NotGuilty);
messageBox.OnButtonClick += GuiltyNotGuilty_OnButtonClick;
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
state = 1; // Done with initial message
}
else if (state == 2) // Found guilty
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this, false, 149);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextFoundGuilty));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.ClickAnywhereToClose = true;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
if (daysInPrison > 0)
state = 3;
else
{
// Give the reputation raise here if no prison time will be served.
playerEntity.RaiseReputationForDoingSentence();
repositionPlayer = true;
playerEntity.FillVitalSigns();
ReleaseFromPrison();
state = 100;
}
}
else if (state == 3) // Serve prison sentence
{
playerEntity.InPrison = true;
SwitchToPrisonScreen();
daysInPrisonLeft = daysInPrison;
playerEntity.RaiseReputationForDoingSentence();
repositionPlayer = true;
state = 100;
}
else if (state == 4) // Banished
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this, false, 149);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextBanished));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.ClickAnywhereToClose = true;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
playerEntity.RegionData[regionIndex].SeverePunishmentFlags |= 1;
repositionPlayer = true;
// Refill player vitals after banishment, otherwise player left with 1HP outside city gates
playerEntity.FillVitalSigns();
state = 100;
}
// Note: Seems like an execution sentence can't be given in classic. It can't be given here, either.
else if (state == 5) // Execution
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this, false, 149);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextExecuted));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.ClickAnywhereToClose = true;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
playerEntity.RegionData[regionIndex].SeverePunishmentFlags |= 2;
state = 6;
}
else if (state == 6) // Reposition player at entrance
{
repositionPlayer = true;
state = 100;
}
else if (state == 100) // Done
{
if (playerEntity.InPrison)
{
if (InputManager.Instance.GetBackButton()) // Speed up prison day countdown. Not in classic.
prisonUpdateInterval = 0.001f;
else
prisonUpdateInterval = 0.3f;
if (prisonUpdateTimer == 0)
prisonUpdateTimer = Time.realtimeSinceStartup;
if (Time.realtimeSinceStartup < prisonUpdateTimer + prisonUpdateInterval)
return;
else
{
prisonUpdateTimer = Time.realtimeSinceStartup;
UpdatePrisonScreen();
}
}
else
ReleaseFromPrison();
}
}
protected virtual void GuiltyNotGuilty_OnButtonClick(DaggerfallMessageBox sender, DaggerfallMessageBox.MessageBoxButtons messageBoxButton)
{
sender.CloseWindow();
if (messageBoxButton == DaggerfallMessageBox.MessageBoxButtons.Guilty)
{
if (punishmentType != 0)
{
if (punishmentType == 1)
state = 5;
else
{
fine >>= 1;
daysInPrison >>= 1;
playerEntity.DeductGoldAmount(fine);
// Classic gives a legal reputation raise here, probably a bug since it means you get a separate raise
// for paying the fine and for serving prison time.
if (daysInPrison > 0)
state = 3;
else
{
// Give the reputation raise here if no prison time will be served.
playerEntity.RaiseReputationForDoingSentence();
repositionPlayer = true;
// Oversight in classic: Does not refill vital signs when releasing in this case, so player is left with 1 health.
playerEntity.FillVitalSigns();
ReleaseFromPrison();
}
}
}
else
state = 4;
}
else // Pleading not guilty
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this, false, 127);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextHowConvince));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.AddButton(DaggerfallMessageBox.MessageBoxButtons.Debate);
messageBox.AddButton(DaggerfallMessageBox.MessageBoxButtons.Lie);
messageBox.OnButtonClick += DebateLie_OnButtonClick;
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
}
Update();
}
protected virtual void DebateLie_OnButtonClick(DaggerfallMessageBox sender, DaggerfallMessageBox.MessageBoxButtons messageBoxButton)
{
sender.CloseWindow();
int playerSkill = 0;
if (messageBoxButton == DaggerfallMessageBox.MessageBoxButtons.Debate)
{
playerSkill = playerEntity.Skills.GetLiveSkillValue(DFCareer.Skills.Etiquette);
playerEntity.TallySkill(DFCareer.Skills.Etiquette, 1);
}
else
{
playerSkill = playerEntity.Skills.GetLiveSkillValue(DFCareer.Skills.Streetwise);
playerEntity.TallySkill(DFCareer.Skills.Streetwise, 1);
}
int chanceToGoFree = playerEntity.RegionData[regionIndex].LegalRep +
(playerSkill + playerEntity.Stats.GetLiveStatValue(DFCareer.Stats.Personality)) / 2;
if (chanceToGoFree > 95)
chanceToGoFree = 95;
else if (chanceToGoFree < 5)
chanceToGoFree = 5;
if (Dice100.FailedRoll(chanceToGoFree))
{
// Banishment
if (punishmentType == 0)
state = 4;
// Execution
else if (punishmentType == 1)
state = 5;
// Prison/Fine
else
{
int roll = playerEntity.RegionData[regionIndex].LegalRep + Dice100.Roll();
if (roll < 25)
fine *= 2;
else if (roll > 75)
fine >>= 1;
state = 2;
}
}
else
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this, false, 149);
messageBox.SetTextTokens(DaggerfallUnity.Instance.TextProvider.GetRSCTokens(courtTextFreeToGo));
messageBox.ScreenDimColor = new Color32(0, 0, 0, 0);
messageBox.ParentPanel.VerticalAlignment = VerticalAlignment.Bottom;
messageBox.ClickAnywhereToClose = true;
messageBox.AllowCancel = false;
uiManager.PushWindow(messageBox);
// Oversight in classic: Does not refill vital signs when releasing in this case, so player is left with 1 health.
// Also does not repair reputation.
playerEntity.FillVitalSigns();
playerEntity.RaiseReputationForDoingSentence();
state = 6;
}
Update();
}
public override void OnPop()
{
// Reset variables
GameManager.Instance.PlayerEntity.Arrested = false;
state = 0;
prisonUpdateTimer = 0f;
GameManager.Instance.PlayerEntity.InPrison = false;
repositionPlayer = false;
daysUntilFreedomLabel.Text = string.Empty;
// Return native texture to court screen
nativeTexture = DaggerfallUI.GetTextureFromImg(nativeImgName);
if (!nativeTexture)
throw new Exception("DaggerfallCourtWindow: Could not load native texture.");
courtPanel.Size = TextureReplacement.GetSize(nativeTexture, nativeImgName);
courtPanel.BackgroundTexture = nativeTexture;
base.CancelWindow();
}
public void PositionPlayerAtLocationEntrance()
{
DFPosition mapPixel = GameManager.Instance.PlayerGPS.CurrentMapPixel;
ContentReader.MapSummary mapSummary;
if (DaggerfallUnity.Instance.ContentReader.HasLocation(mapPixel.X, mapPixel.Y, out mapSummary))
{
StreamingWorld world = GameManager.Instance.StreamingWorld;
StreamingWorld.RepositionMethods reposition = StreamingWorld.RepositionMethods.None;
reposition = StreamingWorld.RepositionMethods.RandomStartMarker;
world.TeleportToCoordinates(mapPixel.X, mapPixel.Y, reposition);
}
}
public void UpdatePrisonScreen()
{
daysInPrisonLeft--;
daysUntilFreedomLabel.Text = TextManager.Instance.GetLocalizedText("daysUntilFreedom");
daysUntilFreedomLabel.Text = daysUntilFreedomLabel.Text.Replace("%d", daysInPrisonLeft.ToString());
if (daysInPrisonLeft == 0)
{
playerEntity.PreventEnemySpawns = true;
playerEntity.PreventNormalizingReputations = true;
DaggerfallUnity.WorldTime.DaggerfallDateTime.RaiseTime(daysInPrison * 1440 * 60);
RaiseOnEndPrisonTimeEvent();
playerEntity.InPrison = false;
playerEntity.FillVitalSigns();
}
}
public void ReleaseFromPrison()
{
playerEntity.PreventEnemySpawns = true;
DaggerfallUnity.WorldTime.DaggerfallDateTime.RaiseTime(240 * 60);
playerEntity.CrimeCommitted = Entity.PlayerEntity.Crimes.None;
if (repositionPlayer)
PositionPlayerAtLocationEntrance();
GameManager.Instance.ClearEnemies();
CancelWindow();
}
// OnCourtSceen. Used for resetting camera recoil.
public delegate void OnCourtScreenEventHandler();
public static event OnCourtScreenEventHandler OnCourtScreen;
protected virtual void RaiseOnCourtScreenEvent()
{
if (OnCourtScreen != null)
OnCourtScreen();
}
// OnEndPrisonTime
public delegate void OnEndPrisonTimeEventHandler();
public static event OnCourtScreenEventHandler OnEndPrisonTime;
protected virtual void RaiseOnEndPrisonTimeEvent()
{
if (OnEndPrisonTime != null)
OnEndPrisonTime();
}
protected virtual void SwitchToPrisonScreen()
{
// Load native texture
nativeTexture = DaggerfallUI.GetTextureFromImg(nativeImgName2);
if (!nativeTexture)
throw new Exception("DaggerfallCourtWindow: Could not load native texture.");
// Native court panel
courtPanel.Size = TextureReplacement.GetSize(nativeTexture, nativeImgName2);
courtPanel.BackgroundTexture = nativeTexture;
daysUntilFreedomLabel.Text = TextManager.Instance.GetLocalizedText("daysUntilFreedom");
daysUntilFreedomLabel.Text = daysUntilFreedomLabel.Text.Replace("%d", daysInPrison.ToString());
}
}
}