-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPCFollowers.lua
896 lines (722 loc) · 21.2 KB
/
NPCFollowers.lua
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
NPCFollowers = {}
local OpenFlwPnlBtn, FollowersBG, ShiftFollowersUp, ShiftFollowersDw
local FollowerBtns = {}
local NPCPanelOpen = false
local Width, Height = 640, 480
local FolowersScreenShift = 0
local InGame = 0x71ef8d
local OpenPanelIconUp, OpenPanelIconDown = "NPCOpenUp", "NPCOpenDw"
local LayoutActive = false
-- Service functions for NPC panel.
local function SpeakWithFollower(ButtonPos)
local CurNPC = vars.NPCFollowers[ButtonPos+FolowersScreenShift]
if CurNPC then
evt.SpeakNPC{CurNPC}
end
end
local function ShowFollowerName(ButtonPos)
local CurNPC = vars.NPCFollowers[ButtonPos+FolowersScreenShift]
if CurNPC then
Game.ShowStatusText(Game.NPC[CurNPC].Name)
end
end
local function StdCond()
return NPCPanelOpen and mem.u4[InGame] == 1
end
function BtnCond()
return NPCPanelOpen or (mem.u4[InGame] == 1 and vars and vars.NPCFollowers and table.maxn(vars.NPCFollowers) > 0)
end
local function UpdatePos()
local layout = Game.PatchOptions.UILayoutActive()
if layout ~= LayoutActive then
LayoutActive = layout
local dy = layout and -30 or 30
local dx = dy*3
for i, t in ipairs({FollowersBG, ShiftFollowersUp, ShiftFollowersDw, OpenFlwPnlBtn}) do
t.X = t.X + dx
t.Y = t.Y + dy
end
for i, t in ipairs(FollowerBtns) do
t.X = t.X + dx
t.Y = t.Y + dy
end
end
end
local function ToggleNPCPanel(State)
if type(State) == "boolean" then
NPCPanelOpen = State
else
NPCPanelOpen = not NPCPanelOpen
end
if NPCPanelOpen then
OpenFlwPnlBtn.IUpSrc = OpenPanelIconDown
OpenFlwPnlBtn.IDwSrc = OpenPanelIconUp
else
OpenFlwPnlBtn.IUpSrc = OpenPanelIconUp
OpenFlwPnlBtn.IDwSrc = OpenPanelIconDown
end
Game.NeedRedraw = true
end
local function RebuildFollowersTable()
local NewT = {}
for k,v in pairs(vars.NPCFollowers) do
if v then
table.insert(NewT, v)
end
vars.NPCFollowers = NewT
end
collectgarbage("collect")
end
NPCFollowers.Rebuild = RebuildFollowersTable
local function RemoveNPCFollower(i)
local NPCf = table.find(vars.NPCFollowers, i)
if NPCf then
table.remove(vars.NPCFollowers, NPCf)
Game.NPC[i].Hired = false
return true
end
return false
end
NPCFollowers.Remove = RemoveNPCFollower
local function AddNPCFollower(i)
local NPCf = table.find(vars.NPCFollowers, i)
if not NPCf then
table.insert(vars.NPCFollowers, i)
Game.NPC[i].Hired = true
return true
end
return false
end
NPCFollowers.Add = AddNPCFollower
local function NPCInGroup(i)
if vars.NPCFollowers then
return table.find(vars.NPCFollowers, i)
end
end
NPCFollowers.NPCInGroup = NPCInGroup
-- Service function for NPC topics.
NPCFollowers.DismissNPCTopic = 1511
NPCFollowers.HireNPCTopic = 1512
NPCFollowers.HireState = false
function events.ExitNPC()
NPCFollowers.HireState = false
end
local function FindFreeEvent(NPC, Replace)
if type(Replace) == "table" then
for i = 0, 5 do
local Event = NPC.Events[i]
if Event == 0 or table.find(Replace, Event) then
return i
end
end
else
for i = 0, 5 do
local Event = NPC.Events[i]
if Event == 0 or Event == Replace then
return i
end
end
end
return nil
end
NPCFollowers.FindFreeEvent = FindFreeEvent
local function ClearEvents(NPC, Events)
if Events then
for k,v in NPC.Events do
if table.find(Events, v) then
NPC.Events[k] = 0
end
end
else
for k,v in NPC.Events do
NPC.Events[k] = 0
end
end
end
NPCFollowers.ClearEvents = ClearEvents
local function DismissNPC()
local NPCId = GetCurrentNPC()
if not NPCId then return end
if NPCFollowers.HireState then
if NPCFollowers.Remove(NPCId) then
for i = 0, 5 do
if Game.NPC[NPCId].Events[i] == NPCFollowers.HireNPCTopic then
Game.NPC[NPCId].Events[i] = HireNPCTopic
end
end
evt.MoveNPC{NPCId, Game.NPCDataTxt[NPCId].House}
ExitCurrentScreen(Game.CurrentScreen == 13)
local CurNPC = Game.NPC[NPCId]
ClearEvents(CurNPC, {NPCFollowers.HirePeasantEvent, NPCFollowers.HireNPCTopic, NPCFollowers.ShowNPCDescrEvent, NPCFollowers.DismissNPCTopic})
local ProfSet = Game.NPCProf[CurNPC.Profession]
if ProfSet and ProfSet.Topic > 0 then
ClearEvents(CurNPC, {ProfSet.Topic})
end
end
NPCFollowers.HireState = false
else
Message(Game.NPCText[1674] .. Game.NPC[NPCId].Name .. ".")
NPCFollowers.HireState = true
end
end
NPCFollowers.DismissNPC = DismissNPC
local function HaveProfession(Prof)
for k,v in pairs(vars.NPCFollowers) do
if Game.NPC[v].Profession == Prof then
return v
end
end
return nil
end
NPCFollowers.HaveProfession = HaveProfession
local function GetTotalFee(Amount)
local Total = 0
for k,v in pairs(vars.NPCFollowers) do
local ProfSet = Game.NPCProf[Game.NPC[v].Profession]
if ProfSet then
Total = Total + ProfSet.Cost/100
end
end
return Amount and math.floor(Amount*Total/100) or Total
end
NPCFollowers.GetTotalFee = GetTotalFee
local function HireNPC()
local NPCId = GetCurrentNPC()
if not NPCId then return end
local cProf = Game.NPC[NPCId].Profession
local ProfSet = Game.NPCProf and Game.NPCProf[cProf]
if NPCFollowers.HireState then
NPCFollowers.HireState = false
if #vars.NPCFollowers >= 4 or GetTotalFee() >= 100 then
Message(Game.GlobalTxt[533])
return
end
if ProfSet then
if Party.Gold < ProfSet.Cost then
Message(Game.GlobalTxt[155])
return
else
evt.ForPlayer("Current").Subtract{"Gold", ProfSet.Cost}
end
end
if NPCFollowers.Add(NPCId) then
evt.MoveNPC{NPCId, 0}
if mapvars.MapNPCNews and mapvars.MapNPCNews[NPCId] then
Map.Monsters[mapvars.MapNPCNews[NPCId].Mid].AIState = 19
else
for i,v in Map.Monsters do
if v.NPC_ID == NPCId then
v.AIState = 19
end
end
end
for i = 0, 5 do
if Game.NPC[NPCId].Events[i] == NPCFollowers.HireNPCTopic then
Game.NPC[NPCId].Events[i] = DismissNPCTopic
end
end
ExitCurrentScreen(Game.CurrentScreen == 13)
end
else
if ProfSet then
local NPCName = Game.NPC[NPCId].Name
local cText = ProfSet.Text .. "\n" .. "(" .. ProfSet.Description .. " " .. Game.NPCText[1675] .. NPCName .. ".)"
cText = string.replace(cText, "%17", tostring(ProfSet.Cost/100))
cText = string.replace(cText, "%01", NPCName)
Message(cText)
else
Message(Game.NPCText[1675] .. Game.NPC[NPCId].Name .. ".")
end
NPCFollowers.HireState = true
end
end
NPCFollowers.HireNPC = HireNPC
local function HireRecruit()
local npcId = GetCurrentNPC()
local npcInfo = mapvars.MapNPCNews[npcId]
if npcInfo and Party.count < 5 then
if NPCFollowers.HireState then
local Pl = Party.PlayersArray[npcInfo.RecruitId]
GenerateMercenary{RosterId = npcInfo.RecruitId, Class = const.Class.Peasant, Level = 1, Face = npcInfo.Face}
Pl.Name = Game.NPC[npcId].Name
Pl.Biography = Pl.Name .. " - " .. Game.ClassNames[Pl.Class]
ExitCurrentScreen()
Map.Monsters[npcInfo.Mid].AIState = 19
HireCharacter(npcInfo.RecruitId)
else
NPCFollowers.HireState = true
local ProfSet = Game.NPCProf[npcInfo.Profession]
local cText = ProfSet.Text
cText = string.replace(cText, "%17", tostring(ProfSet.Cost/100))
cText = string.replace(cText, "%01", Game.NPC[npcId].Name)
Message(cText)
end
else
Message(Game.GlobalTxt[533])
end
end
NPCFollowers.HireRecruit = HireRecruit
---- Initialization ----
function events.GameInitialized2()
-- Interface
FollowersBG = CustomUI.CreateIcon{
Icon = "NPCPnl",
X = Width - 120,
Y = Height - 379,
Condition = StdCond,
Layer = 1,
Screen = 0}
FollowerBtns[1] = CustomUI.CreateButton{
IconUp = "evtnpc-b",
X = FollowersBG.X + 10,
Y = FollowersBG.Y + 20,
Action = function() SpeakWithFollower(1) end,
MouseOverAction = function() ShowFollowerName(1) end,
Condition = StdCond,
Layer = 0,
Screen = 0}
FollowerBtns[2] = CustomUI.CreateButton{
IconUp = "evtnpc-b",
X = FollowersBG.X + 10,
Y = FollowersBG.Y + 105,
Action = function() SpeakWithFollower(2) end,
MouseOverAction = function() ShowFollowerName(2) end,
Condition = StdCond,
Layer = 0,
Screen = 0}
FollowerBtns[3] = CustomUI.CreateButton{
IconUp = "evtnpc-b",
X = FollowersBG.X + 10,
Y = FollowersBG.Y + 190,
Action = function() SpeakWithFollower(3) end,
MouseOverAction = function() ShowFollowerName(3) end,
Condition = StdCond,
Layer = 0,
Screen = 0}
ShiftFollowersUp = CustomUI.CreateButton{
IconUp = "NPCUpA",
IconDown = "NPCUpB",
X = FollowersBG.X + 7,
Y = FollowersBG.Y + 5,
Action = function() if FolowersScreenShift > 0 then
FolowersScreenShift = FolowersScreenShift - 1
end
end,
Condition = StdCond,
Layer = 0,
Screen = 0}
ShiftFollowersDw = CustomUI.CreateButton{
IconUp = "NPCDwA",
IconDown = "NPCDwB",
X = FollowersBG.X + 7,
Y = FollowersBG.Y + 275,
Action = function() if FolowersScreenShift + 2 < table.maxn(vars.NPCFollowers) then
FolowersScreenShift = FolowersScreenShift + 1
end
end,
Condition = StdCond,
Layer = 0,
Screen = 0}
OpenFlwPnlBtn = CustomUI.CreateButton{
IconUp = OpenPanelIconUp,
IconDown = OpenPanelIconDw,
X = Width - 26,
Y = Height - 139,
Action = ToggleNPCPanel,
Condition = BtnCond,
Layer = 2,
Screen = 0}
function events.FGInterfaceUpd()
UpdatePos()
if StdCond() and Game.CurrentScreen == 0 and not Game.LoadingScreen then
for i = 1, 3 do
CurNPC = vars.NPCFollowers[i+FolowersScreenShift]
if CurNPC then
CurPic = tostring(Game.NPC[CurNPC].Pic)
CurPic = "npc" .. string.sub("0000", string.len(CurPic)+1) .. CurPic
CustomUI.ShowIcon(CurPic, FollowerBtns[i].X + 4, FollowerBtns[i].Y + 4)
end
end
end
end
-- NPC won't follow party underwater.
function events.AfterLoadMap()
local IsUnderwater = Game.MapStats[Map.MapStatsIndex].EaxEnvironments == 22
OpenFlwPnlBtn.Active = not IsUnderwater
end
function events.Action(t)
if Game.CurrentScreen == 0 and not Game.LoadingScreen and (StdCond() and CustomUI.MouseInBox(FollowersBG.X, FollowersBG.Y, FollowersBG.Wt, FollowersBG.Ht) or BtnCond() and CustomUI.MouseInBox(OpenFlwPnlBtn.X, OpenFlwPnlBtn.Y, OpenFlwPnlBtn.Wt, OpenFlwPnlBtn.Ht)) then
t.Handled = true
if t.Action == 107 then
ToggleNPCPanel()
end
end
end
-- Topics
function events.LoadMap(WasInGame)
local LastTopic
if not WasInGame then
function events.EvtGlobal(i)
NPCFollowers.HireState = LastTopic == i
LastTopic = i
end
vars.NPCFollowers = vars.NPCFollowers or {}
Game.GlobalEvtLines:RemoveEvent(NPCFollowers.DismissNPCTopic)
Game.GlobalEvtLines:RemoveEvent(NPCFollowers.HireNPCTopic)
Game.GlobalEvtLines:RemoveEvent(NPCFollowers.HirePeasantEvent)
evt.Global[NPCFollowers.DismissNPCTopic] = DismissNPC
evt.Global[NPCFollowers.HireNPCTopic] = HireNPC
evt.Global[NPCFollowers.HirePeasantEvent] = HireRecruit
Game.NPCTopic[NPCFollowers.DismissNPCTopic] = Game.NPCTopic[38]
Game.NPCTopic[NPCFollowers.HireNPCTopic] = Game.NPCTopic[100]
Game.NPCTopic[NPCFollowers.HirePeasantEvent] = Game.NPCTopic[100]
evt.Global[NPCFollowers.ShowNPCDescrEvent] = function()
local cNPC = Game.NPC[GetCurrentNPC()]
local ProfSet = Game.NPCProf[cNPC.Profession]
if ProfSet then
local cText = ProfSet.Description
cText = string.replace(cText, "%17", tostring(ProfSet.Cost/100))
cText = string.replace(cText, "%01", cNPC.Name)
Message(cText)
end
end
Game.NPCTopic[NPCFollowers.ShowNPCDescrEvent] = Game.GlobalTxt[407]
end
ToggleNPCPanel(false)
end
function events.LeaveGame()
ToggleNPCPanel(false)
end
end
--------------------------------
---- NPC Prof hireligs ----
--------------------------------
NPCFollowers.BegTopic = 1766
NPCFollowers.ThreatTopic = 1767
NPCFollowers.BribeTopic = 1768
local function NPCWillSpeak(npc)
if Game.CurrentScreen == 13 or NPCInGroup(npc) or not NPCFollowers.IsRandomNPC(npc) then
return true
end
local NPCExtra = mapvars.MapNPCNews[npc]
if not NPCExtra then
return
end
if NPCExtra.ThreatSuccess then
return true
end
local ContSet = Game.ContinentSettings[TownPortalControls.MapOfContinent(Map.MapStatsIndex)]
if not ContSet.RepNPC then
return true
end
local PersSet = Game.NPCPersonalities[Game.NPCProf[Game.NPC[npc].Profession].Personality]
local cRep = NPCFollowers.GetPartyReputation()
local cFame = NPCFollowers.GetPartyFame()
local IsGoodNPC = NPCExtra.Alignment == "G"
NPCExtra.Alignment = NPCExtra.Alignment or (math.random(1,2) == 1 and "G" or "E")
if (IsGoodNPC and cRep > 0) or (not IsGoodNPC and cRep < -10) then
return false, "Rep"
end
if math.abs(cRep) < PersSet.ReqRep then
return false, "LowRep"
end
if PersSet.ReqFame > cFame then
return false, "NoFame"
end
return true
end
NPCFollowers.NPCWillSpeak = NPCWillSpeak
local function SetHireTopic(npc)
local CurNPC = Game.NPC[npc]
local ProfSet = Game.NPCProf[CurNPC.Profession]
-- all outdoor npcs can join if profession allows. Only signed indoor npc can join.
if ProfSet and (CurNPC.Joins > 0 or ProfSet.Joins and Game.CurrentScreen ~= 13) then
local RndNPCInfo = mapvars.MapNPCNews and mapvars.MapNPCNews[npc]
NPCFollowers.HireState = false
ClearEvents(CurNPC, {NPCFollowers.HirePeasantEvent, NPCFollowers.HireNPCTopic, NPCFollowers.ShowNPCDescrEvent, NPCFollowers.DismissNPCTopic, ProfSet.Topic})
if ProfSet.Recruit and RndNPCInfo and RndNPCInfo.Race ~= -1 then
local cHave, cNPC, cRosterId = NPCFollowers.HaveFreeMerc()
if cHave then
local FreeEvent = FindFreeEvent(CurNPC)
if FreeEvent then
mapvars.MapNPCNews[npc].RecruitId = cRosterId
CurNPC.Events[FreeEvent] = NPCFollowers.HirePeasantEvent
end
else
ClearEvents(CurNPC, {NPCFollowers.HirePeasantEvent})
end
else
local FreeEvent
if NPCInGroup(npc) then
ClearEvents(CurNPC, {1031, 1032, 1033}) -- Remove extra news topics.
if ProfSet.Topic > 0 then
FreeEvent = FindFreeEvent(CurNPC)
if FreeEvent then
CurNPC.Events[FreeEvent] = ProfSet.Topic
end
end
if string.len(ProfSet.Description) > 0 then
FreeEvent = FindFreeEvent(CurNPC, NPCFollowers.ShowNPCDescrEvent)
if FreeEvent then
CurNPC.Events[FreeEvent] = NPCFollowers.ShowNPCDescrEvent
end
end
FreeEvent = FindFreeEvent(CurNPC, NPCFollowers.DismissNPCTopic)
if FreeEvent then
CurNPC.Events[FreeEvent] = NPCFollowers.DismissNPCTopic
end
else
FreeEvent = FindFreeEvent(CurNPC, NPCFollowers.HireNPCTopic)
if FreeEvent then
CurNPC.Events[FreeEvent] = NPCFollowers.HireNPCTopic
end
end
end
end
end
NPCFollowers.SetHireTopic = SetHireTopic
local function SetBTBTopics(npc)
local cNPC = Game.NPC[npc]
ClearEvents(cNPC)
cNPC.EventA = NPCFollowers.BegTopic
cNPC.EventB = NPCFollowers.ThreatTopic
cNPC.EventC = NPCFollowers.BribeTopic
end
NPCFollowers.SetBTBTopics = SetBTBTopics
-- Make NPCProf table
NPCFollowers.HirePeasantEvent = 1510
NPCFollowers.ShowNPCDescrEvent = 1509
local function ProcessNPCProf()
local function SetProfNamesHook()
local function SetProfName(d)
d.eax = mem.topointer(Game.NPCProfessions[d.eax])
end
for i,v in pairs({0x41dfc1, 0x442198, 0x4426a7, 0x4b1328}) do
mem.asmpatch(v, "push eax")
mem.autohook(v, SetProfName)
end
end
local TxtTable = io.open("Data/Tables/NPC professions.txt", "r")
if not TxtTable then
if Game.NPCProfessions then
SetProfNamesHook()
end
return false
end
Game.NPCProfessions = Game.NPCProfessions or {}
SetProfNamesHook()
local NPCProf = {}
const.NPCPersonality = {
Peasant = 1,
Monster = 2,
Thief = 3,
Merchant = 4,
Sorcerer = 5,
Scholar = 6,
Adventurer = 7,
Priest = 8,
Official = 9,
Guard = 10,
Fanatic = 11,
Paladin = 12,
Noble = 13
}
local LineIt = TxtTable:lines()
LineIt() -- skip header
local cnt = 0
for line in LineIt do
local Words = string.split(line, "\9")
local ProfName = tonumber(Words[3])
Game.NPCProfessions[cnt] = ProfName and Game.GlobalTxt[ProfName] or Words[2]
NPCProf[cnt] = {
Rarity = tonumber(Words[4]) or 10,
Cost = tonumber(Words[5]) or 100,
Personality = const.NPCPersonality[Words[6]] or tonumber(Words[6]) or 1,
Topic = tonumber(Words[7]) or 0,
Joins = Words[8] == "x",
Recruit = Words[9] == "x",
Text = tonumber(Words[10]) and Game.NPCText[tonumber(Words[10])] or Words[10],
Description = tonumber(Words[11]) and Game.NPCText[tonumber(Words[11])] or Words[11]
}
cnt = cnt + 1
end
io.close(TxtTable)
Game.NPCProf = NPCProf
return true
end
local function ProcessBTBTxt()
local TxtTable = io.open("Data/Tables/NPC BTB.txt", "r")
if not TxtTable then
return
end
local T = {}
local LineIt = TxtTable:lines()
LineIt() -- skip header
local function GetBTBText(str)
local num = tonumber(str)
return num and Game.NPCText[num] or str
end
for line in LineIt do
local Words = string.split(line, "\9")
local Num = const.NPCPersonality[Words[1]] or tonumber(Words[1])
if Num then
T[Num] = {
AcceptBeg = Words[2] == "x",
AcceptBribe = Words[3] == "x",
AcceptThreat = Words[4] == "x",
Creed = const.Bolster.Creed[Words[5]] or tonumber(Words[5]) or 0,
ReqFame = tonumber(Words[6]) or 0,
ReqRep = tonumber(Words[7]) or 0,
RepOk1 = GetBTBText(Words[8]),
RepOk2 = GetBTBText(Words[9]),
BegRet = GetBTBText(Words[10]),
BribeRet = GetBTBText(Words[11]),
ThreatRet = GetBTBText(Words[12]),
NoFame = GetBTBText(Words[13]),
RepNotoriousG = GetBTBText(Words[14]),
RepNotoriousE = GetBTBText(Words[15]),
RepSaintlyG = GetBTBText(Words[16]),
RepSaintlyE = GetBTBText(Words[17]),
Rep1E = GetBTBText(Words[19]),
Rep2E = GetBTBText(Words[23]),
Rep1G = GetBTBText(Words[18]),
Rep2G = GetBTBText(Words[22]),
LowRep1G = GetBTBText(Words[20]),
LowRep2G = GetBTBText(Words[24]),
LowRep1E = GetBTBText(Words[21]),
LowRep2E = GetBTBText(Words[25]),
BegSuccess = GetBTBText(Words[26]),
BegFail = GetBTBText(Words[27]),
BribeSuccess = GetBTBText(Words[28]),
BribeFail = GetBTBText(Words[29]),
ThreatSuccess = GetBTBText(Words[30]),
ThreatFail = GetBTBText(Words[31])
}
end
end
io.close(TxtTable)
Game.NPCPersonalities = T
end
local function GenerateFace(Gender, Race)
Gender = (Gender == "M" and 0 or Gender == "F" and 1) or 0
local tmpT = {}
for i,v in Game.CharacterPortraits do
if v.DefSex == Gender and v.Race == Race then
table.insert(tmpT, i)
end
end
if #tmpT > 0 then
return tmpT[math.random(1, #tmpT)]
end
return nil
end
function events.GameInitialized2()
if not ProcessNPCProf() then
return
end
ProcessBTBTxt()
-- process outdoor NPCs
function events.AfterLoadMap()
local ContSet = Game.ContinentSettings[TownPortalControls.MapOfContinent(Map.MapStatsIndex)]
if not ContSet.NPCFollowers then
return
end
if not mapvars.MapNPCNews then
return
end
if not NPCFollowers.HaveFreeMerc() then
return
end
for k,v in pairs(mapvars.MapNPCNews) do
local ProfSet = Game.NPCProf[v.Profession]
if not NPCInGroup(k) and ProfSet and ProfSet.Joins and ProfSet.Recruit then
local CurNPC = Game.NPC[k]
if v.Race == nil then
local cMonApp = NPCFollowers.GetMonAppearance(Map.Monsters[v.Mid].Id)
v.Race = cMonApp.Race
end
if v.Race > -1 then
v.Face = v.Face or GenerateFace(v.Gender, v.Race)
if v.Face then
Game.NPC[k].Pic = Game.CharacterPortraits[v.Face].NPCPic
end
end
end
Game.NPC[k].EventB = 0
end
end
-- set "Hire" topic
function events.EnterNPC(npc)
if NPCWillSpeak(npc) then
SetHireTopic(npc)
end
end
-- Disable original "Join" topic
mem.asmpatch(0x4b2e83, "jmp absolute 0x4b2eaf")
mem.asmpatch(0x41bd27, "jmp absolute 0x41bd57")
-- Show amoutn of gold taken by followers
local GoldTakenLine = Game.GlobalTxt[467]
local NewCode = mem.asmproc([[
nop
nop
nop
nop
nop
push esi
push eax
jmp absolute 0x42015a]])
mem.asmpatch(0x420153, "jmp absolute " .. NewCode)
NPCFollowers.LastGoldTaken = 0
mem.hook(NewCode, function(d)
local Fee = NPCFollowers.LastGoldTaken
if Fee > 0 then
GoldTakenLine = Game.GlobalTxt[466]
GoldTakenLine = string.replace(GoldTakenLine, "%lu)!", tostring(Fee) .. ")!")
else
GoldTakenLine = Game.GlobalTxt[467]
end
d.eax = mem.topointer(GoldTakenLine)
end)
end
local function getNpcID(i)
for k,v in pairs(vars.NPCFollowers) do
if (k == i) then
return v
end
end
print("not found!")
return 0
end
function getNpcType(i)
local id = getNpcID(i)
if (id > 0) then
print("NPC ID:", id)
print(" type:", Game.NPC[id].Profession)
end
end
-- 魔法大師=19,兵器大師=16
function setNpcType(i, t)
local id = getNpcID(i)
if (id > 0) then
print("NPC ID:", id)
print("type changed to:", t)
Game.NPC[id].Profession = t
end
end
function getNpcFace(i)
local id = getNpcID(i)
if (id > 0) then
print("NPC ID:", id)
print(" face:", Game.NPC[id].Pic)
end
end
-- 部分較為好看的臉:1231、1244、193、198、213、267?、822、844、851、852(翡翠島琴姐)、853(翡翠島嚮導女)。七代精靈臉641-671
function setNpcFace(i, face)
local id = getNpcID(i)
if (id > 0) then
print("NPC ID:", id)
print("face changed to:", face)
Game.NPC[id].Pic = face
end
end
--1231,198,851,853