-
Notifications
You must be signed in to change notification settings - Fork 135
/
BountyTarget.cs
184 lines (161 loc) · 6.43 KB
/
BountyTarget.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
using System;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;
namespace EpicLoot.Adventure
{
[RequireComponent(typeof(Character))]
public class BountyTarget : MonoBehaviour
{
public const string BountyIDKey = "BountyID";
public const string BountyDataKey = "BountyData";
public const string MonsterIDKey = "MonsterID";
public const string IsAddKey = "IsAdd";
public const string BountyTargetNameKey = "BountyTargetName";
private BountyInfo _bountyInfo;
private string _monsterID;
private bool _isAdd;
private Character _character;
private ZDO _zdo;
[UsedImplicitly]
public void Awake()
{
_character = GetComponent<Character>();
_character.m_onDeath += OnDeath;
_zdo = _character.m_nview.GetZDO();
if (HasBeenSetup())
{
Reinitialize();
}
}
[UsedImplicitly]
public void OnDestroy()
{
if (_character != null)
{
_character.m_onDeath -= OnDeath;
}
}
private bool HasBeenSetup()
{
var bountyID = _zdo.GetString(BountyIDKey);
return !string.IsNullOrEmpty(bountyID);
}
private void OnDeath()
{
EpicLoot.LogWarning("BountyTarget.OnDeath");
if (ZNet.instance.IsServer() || !ZNet.instance.IsServer() && !ZNet.instance.IsDedicated())
{
var pkg = new ZPackage();
_bountyInfo.ToPackage(pkg);
EpicLoot.LogWarning($"SENDING -> RPC_SlayBountyTarget: {_monsterID} ({(_isAdd ? "minion" : "target")})");
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "SlayBountyTarget", pkg, _monsterID, _isAdd);
}
else
{
var bountyID = _zdo.GetString(BountyIDKey);
EpicLoot.LogWarning($"SENDING -> RPC_SlayBountyTargetFromBountyId: (bountyID={bountyID}) {_monsterID} ({(_isAdd ? "minion" : "target")})");
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "SlayBountyIDTarget", _monsterID, _isAdd, bountyID);
}
}
public void Initialize(BountyInfo bounty, string monsterID, bool isAdd)
{
_zdo.Set(BountyIDKey, bounty.ID);
if (ZNet.instance.IsServer() || !ZNet.instance.IsServer() && !ZNet.instance.IsDedicated())
{
var pkg = new ZPackage();
bounty.ToPackage(pkg);
pkg.SetPos(0);
_zdo.Set(BountyDataKey, pkg.GetBase64());
}
_zdo.Set(MonsterIDKey, monsterID);
_zdo.Set(IsAddKey, isAdd);
_zdo.Set(BountyTargetNameKey, GetTargetName(_character.m_name, isAdd, bounty.TargetName));
_character.SetLevel(GetTargetLevel(bounty, monsterID, isAdd));
_character.SetMaxHealth(GetModifiedMaxHealth(_character, bounty, isAdd));
_character.m_baseAI.SetPatrolPoint();
Reinitialize();
}
public void Reinitialize()
{
if (ZNet.instance.IsServer() || !ZNet.instance.IsServer() && !ZNet.instance.IsDedicated())
{
var pkgString = _zdo.GetString(BountyDataKey);
var pkg = new ZPackage(pkgString);
try
{
_bountyInfo = BountyInfo.FromPackage(pkg);
}
catch (Exception)
{
Debug.LogError($"[EpicLoot] Error loading bounty info on creature ({name})! Possibly old or outdated bounty target, destroying creature.\nBountyData:\n{pkgString}");
_zdo.Set("BountyTarget", "");
_zdo.Set(BountyDataKey, "");
_character.m_nview.Destroy();
return;
}
}
_monsterID = _zdo.GetString(MonsterIDKey);
_isAdd = _zdo.GetBool(IsAddKey);
_character.m_name = _zdo.GetString(BountyTargetNameKey);
_character.m_boss = !_zdo.GetBool(IsAddKey);
}
private static float GetModifiedMaxHealth(Character character, BountyInfo bounty, bool isAdd)
{
if (isAdd)
{
return character.GetMaxHealth() * AdventureDataManager.Config.Bounties.AddsHealthMultiplier;
}
if (bounty.RewardGold > 0)
{
return character.GetMaxHealth() * AdventureDataManager.Config.Bounties.GoldHealthMultiplier;
}
return character.GetMaxHealth() * AdventureDataManager.Config.Bounties.IronHealthMultiplier;
}
private static string GetTargetName(string originalName, bool isAdd, string targetName)
{
return isAdd ?
Localization.instance.Localize("$mod_epicloot_bounties_minionname", originalName)
: (string.IsNullOrEmpty(targetName) ? originalName : targetName);
}
private static int GetTargetLevel(BountyInfo bounty, string monsterID, bool isAdd)
{
if (isAdd)
{
foreach (var targetInfo in bounty.Adds)
{
if (targetInfo.MonsterID == monsterID)
{
return targetInfo.Level;
}
}
return 1;
}
return bounty.Target.Level;
}
}
[HarmonyPatch(typeof(Character), nameof(Character.Start))]
public static class Character_Start_Patch
{
public static void Postfix(Character __instance)
{
var zdo = __instance.m_nview?.GetZDO();
if (zdo != null && zdo.IsValid())
{
var old = !string.IsNullOrEmpty(zdo.GetString("BountyTarget"));
if (old)
{
EpicLoot.LogWarning($"Destroying old bounty target: {__instance.name}");
zdo.Set("BountyTarget", "");
__instance.m_nview.Destroy();
return;
}
if (!string.IsNullOrEmpty(zdo.GetString(BountyTarget.BountyIDKey)))
{
var bountyTarget = __instance.gameObject.AddComponent<BountyTarget>();
bountyTarget.Reinitialize();
}
}
}
}
}