NPCObject.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using System.Drawing;
  2. using Server.MirDatabase;
  3. using Server.MirEnvir;
  4. using S = ServerPackets;
  5. namespace Server.MirObjects
  6. {
  7. public sealed class NPCObject : MapObject
  8. {
  9. public override ObjectType Race
  10. {
  11. get { return ObjectType.Merchant; }
  12. }
  13. public static NPCObject Get(uint objectID)
  14. {
  15. if (objectID == 0) return null;
  16. var obj = Envir.NPCs.SingleOrDefault(x => x.ObjectID == objectID);
  17. if (obj != null && obj is NPCObject)
  18. {
  19. return obj as NPCObject;
  20. }
  21. return null;
  22. }
  23. public int ScriptID { get; set; }
  24. public NPCInfo Info;
  25. private const long TurnDelay = 10000, SpeechDelay = 5000;
  26. public long TurnTime, UsedGoodsTime, VisTime, SpeechTime;
  27. public bool Visible = true;
  28. public Dictionary<int, bool> VisibleLog = new Dictionary<int, bool>();
  29. public ConquestObject Conq;
  30. public List<QuestInfo> Quests = new List<QuestInfo>();
  31. public List<NPCSpeech> Speech = new List<NPCSpeech>();
  32. public List<UserItem> UsedGoods = new List<UserItem>();
  33. public Dictionary<string, List<UserItem>> BuyBack = new Dictionary<string, List<UserItem>>();
  34. public bool NeedSave;
  35. public NPCObject(NPCInfo info)
  36. {
  37. Info = info;
  38. NameColour = Color.Lime;
  39. Direction = (MirDirection)Envir.Random.Next(3);
  40. TurnTime = Envir.Time + Envir.Random.Next(100);
  41. Envir.NPCs.Add(this);
  42. Spawned();
  43. LoadScript();
  44. }
  45. private void LoadScript()
  46. {
  47. var script = NPCScript.GetOrAdd(ObjectID, Info.FileName, NPCScriptType.Normal);
  48. ScriptID = script.ScriptID;
  49. }
  50. public void ProcessGoods(bool clear = false)
  51. {
  52. if (!Settings.GoodsOn) return;
  53. var script = NPCScript.Get(ScriptID);
  54. List<UserItem> deleteList = new List<UserItem>();
  55. List<string> emptyBuyBackKeys = null;
  56. foreach (var playerGoods in BuyBack)
  57. {
  58. List<UserItem> items = playerGoods.Value;
  59. deleteList.Clear();
  60. for (int i = 0; i < items.Count; i++)
  61. {
  62. UserItem item = items[i];
  63. if (DateTime.Compare(item.BuybackExpiryDate.AddMinutes(Settings.GoodsBuyBackTime), Envir.Now) <= 0 || clear)
  64. {
  65. deleteList.Add(item);
  66. if (script.UsedTypes.Count != 0 && !script.UsedTypes.Contains(item.Info.Type))
  67. {
  68. continue;
  69. }
  70. var multiCount = UsedGoods.Count(x => x.Info.Index == item.Info.Index);
  71. if (multiCount >= Settings.GoodsMaxStored)
  72. {
  73. UserItem nonAddedItem = UsedGoods.FirstOrDefault(e => e.IsAdded == false);
  74. if (nonAddedItem != null)
  75. {
  76. UsedGoods.Remove(nonAddedItem);
  77. }
  78. else
  79. {
  80. UsedGoods.RemoveAt(0);
  81. }
  82. }
  83. UsedGoods.Add(item);
  84. NeedSave = true;
  85. }
  86. }
  87. for (int i = 0; i < deleteList.Count; i++)
  88. {
  89. items.Remove(deleteList[i]);
  90. }
  91. if (items.Count == 0)
  92. {
  93. (emptyBuyBackKeys ??= new List<string>()).Add(playerGoods.Key);
  94. }
  95. }
  96. if (emptyBuyBackKeys != null)
  97. {
  98. for (int i = 0; i < emptyBuyBackKeys.Count; i++)
  99. {
  100. BuyBack.Remove(emptyBuyBackKeys[i]);
  101. }
  102. }
  103. }
  104. #region Overrides
  105. public override void Process(DelayedAction action)
  106. {
  107. throw new NotSupportedException();
  108. }
  109. public override bool IsAttackTarget(HumanObject attacker)
  110. {
  111. return false;
  112. }
  113. public override bool IsFriendlyTarget(HumanObject ally)
  114. {
  115. throw new NotSupportedException();
  116. }
  117. public override bool IsFriendlyTarget(MonsterObject ally)
  118. {
  119. throw new NotSupportedException();
  120. }
  121. public override bool IsAttackTarget(MonsterObject attacker)
  122. {
  123. return false;
  124. }
  125. public override Buff AddBuff(BuffType type, MapObject owner, int duration, Stats stats, bool refreshStats = true, bool updateOnly = false, params int[] values)
  126. {
  127. throw new NotSupportedException();
  128. }
  129. public override int Attacked(HumanObject attacker, int damage, DefenceType type = DefenceType.ACAgility, bool damageWeapon = true)
  130. {
  131. throw new NotSupportedException();
  132. }
  133. public override int Attacked(MonsterObject attacker, int damage, DefenceType type = DefenceType.ACAgility)
  134. {
  135. throw new NotSupportedException();
  136. }
  137. public override int Struck(int damage, DefenceType type = DefenceType.ACAgility)
  138. {
  139. throw new NotSupportedException();
  140. }
  141. public override void SendHealth(HumanObject player)
  142. {
  143. throw new NotSupportedException();
  144. }
  145. public override void Die()
  146. {
  147. throw new NotSupportedException();
  148. }
  149. public override int Pushed(MapObject pusher, MirDirection dir, int distance)
  150. {
  151. throw new NotSupportedException();
  152. }
  153. public override ushort Level
  154. {
  155. get { throw new NotSupportedException(); }
  156. set { throw new NotSupportedException(); }
  157. }
  158. public override void ReceiveChat(string text, ChatType type)
  159. {
  160. throw new NotSupportedException();
  161. }
  162. public void Turn(MirDirection dir)
  163. {
  164. Direction = dir;
  165. Broadcast(new S.ObjectTurn { ObjectID = ObjectID, Direction = Direction, Location = CurrentLocation });
  166. }
  167. public override void Process()
  168. {
  169. base.Process();
  170. if (Envir.Time > TurnTime)
  171. {
  172. TurnTime = Envir.Time + TurnDelay;
  173. Turn((MirDirection)Envir.Random.Next(3));
  174. }
  175. if (Envir.Time > UsedGoodsTime)
  176. {
  177. UsedGoodsTime = Envir.Time + (Settings.Minute * Settings.GoodsBuyBackTime);
  178. ProcessGoods();
  179. }
  180. if (Envir.Time > VisTime)
  181. {
  182. VisTime = Envir.Time + (Settings.Minute);
  183. if (Info.DayofWeek != "" && Info.DayofWeek != Envir.Now.DayOfWeek.ToString())
  184. {
  185. if (Visible) Hide();
  186. }
  187. else
  188. {
  189. int StartTime = ((Info.HourStart * 60) + Info.MinuteStart);
  190. int FinishTime = ((Info.HourEnd * 60) + Info.MinuteEnd);
  191. int CurrentTime = ((Envir.Now.Hour * 60) + Envir.Now.Minute);
  192. if (Info.TimeVisible)
  193. {
  194. if (StartTime > CurrentTime || FinishTime <= CurrentTime)
  195. {
  196. if (Visible) Hide();
  197. }
  198. else if (StartTime <= CurrentTime && FinishTime > CurrentTime)
  199. {
  200. if (!Visible) Show();
  201. }
  202. }
  203. }
  204. }
  205. if (Speech.Count > 0 && Envir.Time > SpeechTime)
  206. {
  207. var nearby = FindNearby(4);
  208. SpeechTime = Envir.Time + (SpeechDelay * (nearby ? Envir.Random.Next(1, 13) : 1));
  209. if (nearby)
  210. {
  211. var maxWeight = Speech.Max(x => x.Weight);
  212. var speech = Speech.OrderBy(x => x.GetWeight(Envir.Random, maxWeight)).Last();
  213. Broadcast(new S.ObjectChat { ObjectID = this.ObjectID, Text = $"{Info.Name.Split('_')[0]}:{speech.Message}", Type = ChatType.Normal });
  214. }
  215. }
  216. }
  217. public override void SetOperateTime()
  218. {
  219. long time = Envir.Time + 2000;
  220. if (TurnTime < time && TurnTime > Envir.Time)
  221. time = TurnTime;
  222. if (OwnerTime < time && OwnerTime > Envir.Time)
  223. time = OwnerTime;
  224. if (ExpireTime < time && ExpireTime > Envir.Time)
  225. time = ExpireTime;
  226. if (PKPointTime < time && PKPointTime > Envir.Time)
  227. time = PKPointTime;
  228. if (LastHitTime < time && LastHitTime > Envir.Time)
  229. time = LastHitTime;
  230. if (EXPOwnerTime < time && EXPOwnerTime > Envir.Time)
  231. time = EXPOwnerTime;
  232. if (BrownTime < time && BrownTime > Envir.Time)
  233. time = BrownTime;
  234. for (int i = 0; i < ActionList.Count; i++)
  235. {
  236. if (ActionList[i].Time >= time && ActionList[i].Time > Envir.Time) continue;
  237. time = ActionList[i].Time;
  238. }
  239. for (int i = 0; i < PoisonList.Count; i++)
  240. {
  241. if (PoisonList[i].TickTime >= time && PoisonList[i].TickTime > Envir.Time) continue;
  242. time = PoisonList[i].TickTime;
  243. }
  244. for (int i = 0; i < Buffs.Count; i++)
  245. {
  246. if (Buffs[i].NextTime >= time && Buffs[i].NextTime > Envir.Time) continue;
  247. time = Buffs[i].NextTime;
  248. }
  249. if (OperateTime <= Envir.Time || time < OperateTime)
  250. OperateTime = time;
  251. }
  252. public void Hide()
  253. {
  254. CurrentMap.Broadcast(new S.ObjectRemove { ObjectID = ObjectID }, CurrentLocation);
  255. Visible = false;
  256. }
  257. public void Show()
  258. {
  259. Visible = true;
  260. for (int i = CurrentMap.Players.Count - 1; i >= 0; i--)
  261. {
  262. PlayerObject player = CurrentMap.Players[i];
  263. if (Functions.InRange(CurrentLocation, player.CurrentLocation, Globals.DataRange))
  264. {
  265. CheckVisible(player, true);
  266. if (player.CheckStacked())
  267. {
  268. player.StackingTime = Envir.Time + 1000;
  269. player.Stacking = true;
  270. }
  271. }
  272. }
  273. }
  274. public bool FindNearby(int distance)
  275. {
  276. for (int d = 0; d <= distance; d++)
  277. {
  278. for (int y = CurrentLocation.Y - d; y <= CurrentLocation.Y + d; y++)
  279. {
  280. if (y < 0) continue;
  281. if (y >= CurrentMap.Height) break;
  282. for (int x = CurrentLocation.X - d; x <= CurrentLocation.X + d; x += Math.Abs(y - CurrentLocation.Y) == d ? 1 : d * 2)
  283. {
  284. if (x < 0) continue;
  285. if (x >= CurrentMap.Width) break;
  286. if (!CurrentMap.ValidPoint(x, y)) continue;
  287. Cell cell = CurrentMap.GetCell(x, y);
  288. if (cell.Objects == null) continue;
  289. for (int i = 0; i < cell.Objects.Count; i++)
  290. {
  291. MapObject ob = cell.Objects[i];
  292. switch (ob.Race)
  293. {
  294. case ObjectType.Player:
  295. if (ob == this || ob.Dead) continue;
  296. return true;
  297. default:
  298. continue;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. return false;
  305. }
  306. public override Packet GetInfo()
  307. {
  308. return new S.ObjectNPC
  309. {
  310. ObjectID = ObjectID,
  311. Name = Name,
  312. NameColour = NameColour,
  313. Image = Info.Image,
  314. Colour = Info.Colour,
  315. Location = CurrentLocation,
  316. Direction = Direction,
  317. QuestIDs = (from q in Quests
  318. select q.Index).ToList()
  319. };
  320. }
  321. public Packet GetUpdateInfo()
  322. {
  323. return new S.NPCImageUpdate
  324. {
  325. ObjectID = ObjectID,
  326. Image = Info.Image,
  327. Colour = Info.Colour
  328. };
  329. }
  330. public override void ApplyPoison(Poison p, MapObject Caster = null, bool NoResist = false, bool ignoreDefence = true)
  331. {
  332. throw new NotSupportedException();
  333. }
  334. public override string Name
  335. {
  336. get { return Info.Name; }
  337. set { throw new NotSupportedException(); }
  338. }
  339. public override bool Blocking
  340. {
  341. get { return Visible; }
  342. }
  343. public void CheckVisible(PlayerObject Player, bool Force = false)
  344. {
  345. VisibleLog.TryGetValue(Player.Info.Index, out bool canSee);
  346. if (Conq != null &&
  347. Conq.WarIsOn &&
  348. !Info.ConquestVisible)
  349. {
  350. if (canSee) CurrentMap.Broadcast(new S.ObjectRemove { ObjectID = ObjectID }, CurrentLocation, Player);
  351. VisibleLog[Player.Info.Index] = false;
  352. return;
  353. }
  354. if (Info.FlagNeeded != 0 && !Player.Info.Flags[Info.FlagNeeded])
  355. {
  356. if (canSee) CurrentMap.Broadcast(new S.ObjectRemove { ObjectID = ObjectID }, CurrentLocation, Player);
  357. VisibleLog[Player.Info.Index] = false;
  358. return;
  359. }
  360. if (Info.MinLev != 0 && Player.Level < Info.MinLev || Info.MaxLev != 0 && Player.Level > Info.MaxLev)
  361. {
  362. if (canSee) CurrentMap.Broadcast(new S.ObjectRemove { ObjectID = ObjectID }, CurrentLocation, Player);
  363. VisibleLog[Player.Info.Index] = false;
  364. return;
  365. }
  366. if (Info.ClassRequired != "" && Player.Class.ToString() != Info.ClassRequired)
  367. {
  368. if (canSee) CurrentMap.Broadcast(new S.ObjectRemove { ObjectID = ObjectID }, CurrentLocation, Player);
  369. VisibleLog[Player.Info.Index] = false;
  370. return;
  371. }
  372. if (Visible && !canSee) CurrentMap.Broadcast(GetInfo(), CurrentLocation, Player);
  373. else if (Force && Visible) CurrentMap.Broadcast(GetInfo(), CurrentLocation, Player);
  374. VisibleLog[Player.Info.Index] = true;
  375. }
  376. public override int CurrentMapIndex { get; set; }
  377. public override Point CurrentLocation
  378. {
  379. get { return Info.Location; }
  380. set { throw new NotSupportedException(); }
  381. }
  382. public override MirDirection Direction { get; set; }
  383. public override int Health
  384. {
  385. get { throw new NotSupportedException(); }
  386. }
  387. public override int MaxHealth
  388. {
  389. get { throw new NotSupportedException(); }
  390. }
  391. #endregion
  392. }
  393. public class NPCSpeech
  394. {
  395. public int Weight { get; set; }
  396. public string Message { get; set; }
  397. public int GetWeight(RandomProvider rnd, int max)
  398. {
  399. return rnd.Next(Weight, max + 100);
  400. }
  401. }
  402. }