NPCObject.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. using Client.MirControls;
  2. using Client.MirGraphics;
  3. using Client.MirScenes;
  4. using S = ServerPackets;
  5. namespace Client.MirObjects
  6. {
  7. public class NPCObject : MapObject
  8. {
  9. public override ObjectType Race
  10. {
  11. get { return ObjectType.Merchant; }
  12. }
  13. public override bool Blocking
  14. {
  15. get { return true; }
  16. }
  17. public FrameSet Frames;
  18. public Frame Frame;
  19. public long QuestTime;
  20. public int BaseIndex, FrameIndex, FrameInterval,
  21. EffectFrameIndex, EffectFrameInterval, QuestIndex;
  22. public ushort Image;
  23. public Color Colour = Color.White;
  24. public QuestIcon QuestIcon = QuestIcon.None;
  25. private bool _canChangeDir = true;
  26. public bool CanChangeDir
  27. {
  28. get { return _canChangeDir; }
  29. set
  30. {
  31. _canChangeDir = value;
  32. if (value == false) Direction = 0;
  33. }
  34. }
  35. public List<ClientQuestInfo> Quests;
  36. public NPCObject(uint objectID) : base(objectID)
  37. {
  38. }
  39. public void Load(S.ObjectNPC info)
  40. {
  41. Name = info.Name;
  42. NameColour = info.NameColour;
  43. CurrentLocation = info.Location;
  44. Direction = info.Direction;
  45. Movement = info.Location;
  46. MapLocation = info.Location;
  47. GameScene.Scene.MapControl.AddObject(this);
  48. Quests = GameScene.QuestInfoList.Where(c => c.NPCIndex == ObjectID).ToList();
  49. Image = info.Image;
  50. Colour = info.Colour;
  51. LoadLibrary();
  52. Frames = BodyLibrary.Frames ?? FrameSet.DefaultNPC;
  53. Light = 10;
  54. BaseIndex = 0;
  55. SetAction(true);
  56. }
  57. public void LoadLibrary()
  58. {
  59. if (Image < Libraries.NPCs.Length)
  60. BodyLibrary = Libraries.NPCs[Image];
  61. else if (Image >= 1000 && Image < 1100)
  62. BodyLibrary = Libraries.Flags[Image - 1000];
  63. }
  64. public override void Process()
  65. {
  66. bool update = CMain.Time >= NextMotion || GameScene.CanMove;
  67. ProcessFrames();
  68. if (update)
  69. {
  70. UpdateBestQuestIcon();
  71. }
  72. if (Frame == null)
  73. {
  74. DrawFrame = 0;
  75. DrawWingFrame = 0;
  76. }
  77. else
  78. {
  79. DrawFrame = Frame.Start + (Frame.OffSet * (byte)Direction) + FrameIndex;
  80. DrawWingFrame = Frame.EffectStart + (Frame.EffectOffSet * (byte)Direction) + EffectFrameIndex;
  81. }
  82. DrawY = CurrentLocation.Y;
  83. DrawLocation = new Point((Movement.X - User.Movement.X + MapControl.OffSetX) * MapControl.CellWidth, (Movement.Y - User.Movement.Y + MapControl.OffSetY) * MapControl.CellHeight);
  84. DrawLocation.Offset(User.OffSetMove);
  85. DrawLocation.Offset(GlobalDisplayLocationOffset);
  86. if (BodyLibrary != null)
  87. FinalDrawLocation = DrawLocation.Add(BodyLibrary.GetOffSet(DrawFrame));
  88. if (BodyLibrary != null && update)
  89. {
  90. FinalDrawLocation = DrawLocation.Add(BodyLibrary.GetOffSet(DrawFrame));
  91. DisplayRectangle = new Rectangle(DrawLocation, BodyLibrary.GetTrueSize(DrawFrame));
  92. }
  93. for (int i = 0; i < Effects.Count; i++)
  94. Effects[i].Process();
  95. Color colour = DrawColour;
  96. switch (Poison)
  97. {
  98. case PoisonType.None:
  99. DrawColour = Color.White;
  100. break;
  101. case PoisonType.Green:
  102. DrawColour = Color.Green;
  103. break;
  104. case PoisonType.Red:
  105. DrawColour = Color.Red;
  106. break;
  107. case PoisonType.Bleeding:
  108. DrawColour = Color.DarkRed;
  109. break;
  110. case PoisonType.Slow:
  111. DrawColour = Color.Purple;
  112. break;
  113. case PoisonType.Stun:
  114. case PoisonType.Dazed:
  115. DrawColour = Color.Yellow;
  116. break;
  117. case PoisonType.Blindness:
  118. DrawColour = Color.MediumVioletRed;
  119. break;
  120. case PoisonType.Frozen:
  121. DrawColour = Color.Blue;
  122. break;
  123. case PoisonType.Paralysis:
  124. case PoisonType.LRParalysis:
  125. //case PoisonType.FlamingMutantWeb:
  126. DrawColour = Color.Gray;
  127. break;
  128. }
  129. if (colour != DrawColour) GameScene.Scene.MapControl.TextureValid = false;
  130. if (CMain.Time > QuestTime)
  131. {
  132. QuestTime = CMain.Time + 500;
  133. if (++QuestIndex > 1) QuestIndex = 0;
  134. }
  135. }
  136. public virtual void ProcessFrames()
  137. {
  138. if (Frame == null) return;
  139. switch (CurrentAction)
  140. {
  141. case MirAction.Standing:
  142. case MirAction.Harvest:
  143. if (CMain.Time >= NextMotion)
  144. {
  145. GameScene.Scene.MapControl.TextureValid = false;
  146. if (SkipFrames) UpdateFrame();
  147. if (UpdateFrame() >= Frame.Count)
  148. {
  149. FrameIndex = Frame.Count - 1;
  150. SetAction();
  151. }
  152. else
  153. {
  154. NextMotion += FrameInterval;
  155. }
  156. }
  157. if(EffectFrameInterval > 0)
  158. if (CMain.Time >= NextMotion2)
  159. {
  160. GameScene.Scene.MapControl.TextureValid = false;
  161. if (SkipFrames) UpdateFrame2();
  162. if (UpdateFrame2() >= Frame.EffectCount)
  163. EffectFrameIndex = Frame.EffectCount - 1;
  164. else
  165. NextMotion2 += EffectFrameInterval;
  166. }
  167. break;
  168. }
  169. }
  170. public int UpdateFrame()
  171. {
  172. if (Frame == null) return 0;
  173. if (Frame.Reverse) return Math.Abs(--FrameIndex);
  174. return ++FrameIndex;
  175. }
  176. public int UpdateFrame2()
  177. {
  178. if (Frame == null) return 0;
  179. if (Frame.Reverse) return Math.Abs(--EffectFrameIndex);
  180. return ++EffectFrameIndex;
  181. }
  182. public virtual void SetAction(bool randomStartFrame = false)
  183. {
  184. if (ActionFeed.Count == 0)
  185. {
  186. if (CMain.Random.Next(2) == 0 && Frames.Count > 1)
  187. CurrentAction = MirAction.Harvest;
  188. else
  189. CurrentAction = MirAction.Standing;
  190. Frames.TryGetValue(CurrentAction, out Frame);
  191. if (randomStartFrame)
  192. {
  193. var frameIndex = new Random().Next(Frame.Count);
  194. FrameIndex = frameIndex;
  195. EffectFrameIndex = Math.Min(Frame.EffectCount, frameIndex);
  196. }
  197. else
  198. {
  199. FrameIndex = 0;
  200. EffectFrameIndex = 0;
  201. }
  202. if (MapLocation != CurrentLocation)
  203. {
  204. GameScene.Scene.MapControl.RemoveObject(this);
  205. MapLocation = CurrentLocation;
  206. GameScene.Scene.MapControl.AddObject(this);
  207. }
  208. if (Frame == null) return;
  209. FrameInterval = Frame.Interval;
  210. EffectFrameInterval = Frame.EffectInterval;
  211. }
  212. else
  213. {
  214. QueuedAction action = ActionFeed[0];
  215. ActionFeed.RemoveAt(0);
  216. CurrentAction = action.Action;
  217. CurrentLocation = action.Location;
  218. //if(CanChangeDir)
  219. // Direction = action.Direction;
  220. FrameIndex = 0;
  221. EffectFrameIndex = 0;
  222. if (Frame == null) return;
  223. FrameInterval = Frame.Interval;
  224. EffectFrameInterval = Frame.EffectInterval;
  225. }
  226. NextMotion = CMain.Time + FrameInterval;
  227. NextMotion2 = CMain.Time + EffectFrameInterval;
  228. GameScene.Scene.MapControl.TextureValid = false;
  229. }
  230. public override void Draw()
  231. {
  232. if (BodyLibrary == null) return;
  233. //BodyLibrary.Draw(DrawFrame, DrawLocation, DrawColour, true);
  234. BodyLibrary.DrawTinted(DrawFrame, DrawLocation, DrawColour, Colour, true);
  235. if (QuestIcon == QuestIcon.None) return;
  236. var offSet = BodyLibrary.GetOffSet(BaseIndex);
  237. var size = BodyLibrary.GetSize(BaseIndex);
  238. int imageIndex = 981 + ((int)QuestIcon * 2) + QuestIndex;
  239. Libraries.Prguse.Draw(imageIndex, DrawLocation.Add(offSet).Add(size.Width / 2 - 28, -40), Color.White, false);
  240. }
  241. public override bool MouseOver(Point p)
  242. {
  243. return MapControl.MapLocation == CurrentLocation || BodyLibrary != null && BodyLibrary.VisiblePixel(DrawFrame, p.Subtract(FinalDrawLocation), false);
  244. }
  245. public override void DrawBehindEffects(bool effectsEnabled)
  246. {
  247. }
  248. public override void DrawEffects(bool effectsEnabled)
  249. {
  250. if (!effectsEnabled) return;
  251. if (BodyLibrary == null) return;
  252. if (DrawWingFrame > 0)
  253. BodyLibrary.DrawBlend(DrawWingFrame, DrawLocation, Color.White, true);
  254. }
  255. public override void DrawName()
  256. {
  257. if (!Name.Contains("_"))
  258. {
  259. base.DrawName();
  260. return;
  261. }
  262. string[] splitName = Name.Split('_');
  263. for (int s = 0; s < splitName.Length; s++)
  264. {
  265. CreateNPCLabel(splitName[s], s);
  266. TempLabel.Location = new Point(DisplayRectangle.X + (48 - TempLabel.Size.Width) / 2, DisplayRectangle.Y - (32 - TempLabel.Size.Height / 2) + (Dead ? 35 : 8) - (((splitName.Length - 1) * 10) / 2) + (s * 12));
  267. TempLabel.Draw();
  268. }
  269. }
  270. public void CreateNPCLabel(string word, int wordOrder)
  271. {
  272. TempLabel = null;
  273. var setForeColour = (wordOrder == 0 ? NameColour : Color.White);
  274. for (int i = 0; i < LabelList.Count; i++)
  275. {
  276. if (LabelList[i].Text != word || LabelList[i].ForeColour != setForeColour) continue;
  277. TempLabel = LabelList[i];
  278. break;
  279. }
  280. if (TempLabel != null && !TempLabel.IsDisposed) return;
  281. TempLabel = new MirLabel
  282. {
  283. AutoSize = true,
  284. BackColour = Color.Transparent,
  285. ForeColour = setForeColour,
  286. OutLine = true,
  287. OutLineColour = Color.Black,
  288. Text = word,
  289. };
  290. TempLabel.Disposing += (o, e) => LabelList.Remove(TempLabel);
  291. LabelList.Add(TempLabel);
  292. }
  293. //Quests
  294. #region Quest System
  295. public void UpdateBestQuestIcon()
  296. {
  297. ClientQuestProgress quests = GetAvailableQuests(true).FirstOrDefault();
  298. QuestIcon bestIcon = QuestIcon.None;
  299. if (quests != null)
  300. {
  301. bestIcon = quests.Icon;
  302. }
  303. QuestIcon = bestIcon;
  304. }
  305. public List<ClientQuestProgress> GetAvailableQuests(bool returnFirst = false)
  306. {
  307. List<ClientQuestProgress> quests = new List<ClientQuestProgress>();
  308. foreach (ClientQuestProgress q in User.CurrentQuests.Where(q => !User.CompletedQuests.Contains(q.QuestInfo.Index)))
  309. {
  310. if (q.QuestInfo.FinishNPCIndex == ObjectID)
  311. {
  312. quests.Add(q);
  313. }
  314. else if (q.QuestInfo.NPCIndex == ObjectID && q.QuestInfo.SameFinishNPC)
  315. {
  316. quests.Add(q);
  317. if (returnFirst) return quests;
  318. }
  319. }
  320. foreach (ClientQuestProgress quest in (
  321. from q in Quests
  322. where !quests.Exists(p => p.QuestInfo.Index == q.Index)
  323. where CanAccept(q)
  324. where !User.CompletedQuests.Contains(q.Index)
  325. select q).Select(
  326. q => User.CurrentQuests.Exists(p => p.QuestInfo.Index == q.Index) ?
  327. new ClientQuestProgress { QuestInfo = q, Taken = true, Completed = false } :
  328. new ClientQuestProgress { QuestInfo = q }))
  329. {
  330. quests.Add(quest);
  331. if (returnFirst) return quests;
  332. }
  333. return quests;
  334. }
  335. public bool CanAccept(ClientQuestInfo quest)
  336. {
  337. if (quest.MinLevelNeeded > User.Level || quest.MaxLevelNeeded < User.Level)
  338. return false;
  339. if (!quest.ClassNeeded.HasFlag(RequiredClass.None))
  340. {
  341. switch (User.Class)
  342. {
  343. case MirClass.Warrior:
  344. if (!quest.ClassNeeded.HasFlag(RequiredClass.Warrior))
  345. return false;
  346. break;
  347. case MirClass.Wizard:
  348. if (!quest.ClassNeeded.HasFlag(RequiredClass.Wizard))
  349. return false;
  350. break;
  351. case MirClass.Taoist:
  352. if (!quest.ClassNeeded.HasFlag(RequiredClass.Taoist))
  353. return false;
  354. break;
  355. case MirClass.Assassin:
  356. if (!quest.ClassNeeded.HasFlag(RequiredClass.Assassin))
  357. return false;
  358. break;
  359. case MirClass.Archer:
  360. if (!quest.ClassNeeded.HasFlag(RequiredClass.Archer))
  361. return false;
  362. break;
  363. }
  364. }
  365. //check against active quest list
  366. return quest.QuestNeeded <= 0 || User.CompletedQuests.Contains(quest.QuestNeeded);
  367. }
  368. #endregion
  369. }
  370. }