PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/Ultima/World/Entities/Mobiles/Animations/ActionTranslator.cs

https://gitlab.com/swak/UltimaXNA
C# | 323 lines | 292 code | 22 blank | 9 comment | 43 complexity | b57b6eef2731ce327a3d97fbd0918218 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UltimaXNA.Ultima.Data;
  6. using UltimaXNA.Core.Diagnostics.Tracing;
  7. using UltimaXNA.Ultima.World.Entities.Items.Containers;
  8. namespace UltimaXNA.Ultima.World.Entities.Mobiles.Animations
  9. {
  10. static class ActionTranslator
  11. {
  12. public static int GetActionIndex(AEntity entity, MobileAction action)
  13. {
  14. return GetActionIndex(entity, action, -1);
  15. }
  16. public static int GetActionIndex(AEntity entity, MobileAction action, int index)
  17. {
  18. Body body = 0;
  19. bool isMounted = false, isWarMode = false, isSitting = false, dieForwards = false;
  20. int lightSourceBodyID = 0;
  21. if (entity is Corpse)
  22. {
  23. body = (entity as Corpse).Body;
  24. dieForwards = (entity as Corpse).DieForwards;
  25. }
  26. else if (entity is Mobile)
  27. {
  28. Mobile mobile = entity as Mobile;
  29. body = mobile.Body;
  30. isMounted = mobile.IsMounted;
  31. isWarMode = mobile.Flags.IsWarMode;
  32. isSitting = mobile.IsSitting;
  33. lightSourceBodyID = mobile.LightSourceBodyID;
  34. }
  35. else
  36. {
  37. Tracer.Critical("Entity of type {0} cannot get an action index.", entity.ToString());
  38. }
  39. if (body.IsHumanoid)
  40. {
  41. switch (action)
  42. {
  43. case MobileAction.None:
  44. // this will never be called.
  45. return GetActionIndex(entity, MobileAction.Stand, index);
  46. case MobileAction.Walk:
  47. if (isMounted)
  48. return (int)ActionIndexHumanoid.Mounted_RideSlow;
  49. else
  50. if (isWarMode)
  51. return (int)ActionIndexHumanoid.Walk_Warmode;
  52. else
  53. {
  54. // if carrying a light source, return Walk_Armed.
  55. if (lightSourceBodyID != 0)
  56. return (int)ActionIndexHumanoid.Walk_Armed;
  57. else
  58. return (int)ActionIndexHumanoid.Walk;
  59. }
  60. case MobileAction.Run:
  61. if (isMounted)
  62. return (int)ActionIndexHumanoid.Mounted_RideFast;
  63. else
  64. {
  65. // if carrying a light source, return Run_Armed.
  66. if (lightSourceBodyID!= 0)
  67. return (int)ActionIndexHumanoid.Run_Armed;
  68. else
  69. return (int)ActionIndexHumanoid.Run;
  70. }
  71. case MobileAction.Stand:
  72. if (isMounted)
  73. {
  74. return (int)ActionIndexHumanoid.Mounted_Stand;
  75. }
  76. else
  77. {
  78. if (isSitting)
  79. {
  80. return (int)ActionIndexHumanoid.Sit;
  81. }
  82. else if (isWarMode)
  83. {
  84. // TODO: Also check if weapon type is 2h. Can be 1H or 2H
  85. return (int)ActionIndexHumanoid.Stand_Warmode1H;
  86. }
  87. else
  88. {
  89. return (int)ActionIndexHumanoid.Stand;
  90. }
  91. }
  92. case MobileAction.Death:
  93. if (dieForwards)
  94. return (int)ActionIndexHumanoid.Die_Backwards;
  95. else
  96. return (int)ActionIndexHumanoid.Die_Forwards;
  97. case MobileAction.Attack:
  98. if (isMounted)
  99. {
  100. // check weapon type. Can be 1H, Bow, or XBow
  101. return (int)ActionIndexHumanoid.Mounted_Attack_1H;
  102. }
  103. else
  104. {
  105. // check weapon type. Can be 1H, 2H across, 2H down, 2H jab, bow, xbow, or unarmed.
  106. return (int)ActionIndexHumanoid.Attack_1H;
  107. }
  108. case MobileAction.Cast_Directed:
  109. if (isMounted)
  110. return GetActionIndex(entity, MobileAction.Stand, index);
  111. else
  112. return (int)ActionIndexHumanoid.Cast_Directed;
  113. case MobileAction.Cast_Area:
  114. if (isMounted)
  115. return GetActionIndex(entity, MobileAction.Stand, index);
  116. else
  117. return (int)ActionIndexHumanoid.Cast_Area;
  118. case MobileAction.GetHit:
  119. if (isMounted)
  120. return GetActionIndex(entity, MobileAction.Stand, index);
  121. else
  122. return (int)ActionIndexHumanoid.Hit;
  123. case MobileAction.Block:
  124. if (isMounted)
  125. return GetActionIndex(entity, MobileAction.Stand, index);
  126. else
  127. return (int)ActionIndexHumanoid.Block_WithShield;
  128. case MobileAction.Emote_Fidget_1:
  129. if (isMounted)
  130. return GetActionIndex(entity, MobileAction.Stand, index);
  131. else
  132. return (int)ActionIndexHumanoid.Fidget_1;
  133. case MobileAction.Emote_Fidget_2:
  134. if (isMounted)
  135. return GetActionIndex(entity, MobileAction.Stand, index);
  136. else
  137. return (int)ActionIndexHumanoid.Fidget_2;
  138. case MobileAction.Emote_Bow:
  139. if (isMounted)
  140. return GetActionIndex(entity, MobileAction.Stand, index);
  141. else
  142. return (int)ActionIndexHumanoid.Emote_Bow;
  143. case MobileAction.Emote_Salute:
  144. if (isMounted)
  145. return GetActionIndex(entity, MobileAction.Stand, index);
  146. else
  147. return (int)ActionIndexHumanoid.Emote_Salute;
  148. case MobileAction.Emote_Eat:
  149. if (isMounted)
  150. return GetActionIndex(entity, MobileAction.Stand, index);
  151. else
  152. return (int)ActionIndexHumanoid.Emote_Eat;
  153. default:
  154. return (int)-1;
  155. }
  156. }
  157. else if (body.IsAnimal)
  158. {
  159. switch (action)
  160. {
  161. case MobileAction.None:
  162. return GetActionIndex(entity, MobileAction.Stand, index);
  163. case MobileAction.Walk:
  164. return (int)ActionIndexAnimal.Walk;
  165. case MobileAction.Run:
  166. return (int)ActionIndexAnimal.Run;
  167. case MobileAction.Stand:
  168. return (int)ActionIndexAnimal.Stand;
  169. case MobileAction.Death:
  170. if (dieForwards)
  171. return (int)ActionIndexAnimal.Die_Backwards;
  172. else
  173. return (int)ActionIndexAnimal.Die_Forwards;
  174. case MobileAction.MonsterAction:
  175. return index;
  176. default:
  177. return (int)-1;
  178. }
  179. }
  180. else if (body.IsMonster)
  181. {
  182. switch (action)
  183. {
  184. case MobileAction.None:
  185. return GetActionIndex(entity, MobileAction.Stand, index);
  186. case MobileAction.Walk:
  187. return (int)ActionIndexMonster.Walk;
  188. case MobileAction.Run:
  189. return (int)ActionIndexMonster.Walk;
  190. case MobileAction.Stand:
  191. return (int)ActionIndexMonster.Stand;
  192. case MobileAction.Death:
  193. if (dieForwards)
  194. return (int)ActionIndexMonster.Die_Backwards;
  195. else
  196. return (int)ActionIndexMonster.Die_Forwards;
  197. case MobileAction.MonsterAction:
  198. return index;
  199. default:
  200. return (int)-1;
  201. }
  202. }
  203. return -1;
  204. }
  205. public static MobileAction GetActionFromIndex(Body body, int index)
  206. {
  207. if (body.IsHumanoid)
  208. {
  209. switch ((ActionIndexHumanoid)index)
  210. {
  211. case ActionIndexHumanoid.Walk:
  212. case ActionIndexHumanoid.Walk_Armed:
  213. case ActionIndexHumanoid.Walk_Warmode:
  214. case ActionIndexHumanoid.Mounted_RideSlow:
  215. return MobileAction.Walk;
  216. case ActionIndexHumanoid.Mounted_RideFast:
  217. case ActionIndexHumanoid.Run:
  218. case ActionIndexHumanoid.Run_Armed:
  219. return MobileAction.Run;
  220. case ActionIndexHumanoid.Stand:
  221. case ActionIndexHumanoid.Stand_Warmode1H:
  222. case ActionIndexHumanoid.Stand_Warmode2H:
  223. case ActionIndexHumanoid.Mounted_Stand:
  224. return MobileAction.Stand;
  225. case ActionIndexHumanoid.Fidget_1:
  226. return MobileAction.Emote_Fidget_1;
  227. case ActionIndexHumanoid.Fidget_2:
  228. return MobileAction.Emote_Fidget_2;
  229. case ActionIndexHumanoid.Attack_1H:
  230. case ActionIndexHumanoid.Attack_Unarmed1:
  231. case ActionIndexHumanoid.Attack_Unarmed2:
  232. case ActionIndexHumanoid.Attack_2H_Down:
  233. case ActionIndexHumanoid.Attack_2H_Across:
  234. case ActionIndexHumanoid.Attack_2H_Jab:
  235. case ActionIndexHumanoid.Attack_Bow:
  236. case ActionIndexHumanoid.Attack_BowX:
  237. case ActionIndexHumanoid.Mounted_Attack_1H:
  238. case ActionIndexHumanoid.Mounted_Attack_Bow:
  239. case ActionIndexHumanoid.Mounted_Attack_BowX:
  240. case ActionIndexHumanoid.Attack_Unarmed3:
  241. return MobileAction.Attack;
  242. case ActionIndexHumanoid.Cast_Directed:
  243. return MobileAction.Cast_Directed;
  244. case ActionIndexHumanoid.Cast_Area:
  245. return MobileAction.Cast_Area;
  246. case ActionIndexHumanoid.Hit:
  247. return MobileAction.GetHit;
  248. case ActionIndexHumanoid.Die_Backwards:
  249. case ActionIndexHumanoid.Die_Forwards:
  250. return MobileAction.Death;
  251. case ActionIndexHumanoid.Mounted_SlapHorse: // not coded or used?
  252. return MobileAction.Stand;
  253. case ActionIndexHumanoid.Block_WithShield:
  254. return MobileAction.Block;
  255. case ActionIndexHumanoid.Emote_Bow:
  256. return MobileAction.Emote_Bow;
  257. case ActionIndexHumanoid.Emote_Salute:
  258. return MobileAction.Emote_Salute;
  259. case ActionIndexHumanoid.Emote_Eat:
  260. return MobileAction.Emote_Eat;
  261. }
  262. // special case animations. When casting a spell, the server will send animation indexes over 200,
  263. // which all seem to correspond to Cast_Directed. Example indexes are:
  264. // 200, 201, 203, 206, 209, 212, 215, 218, 221, 227, 230, 239, 245, 260, 266 and doubtless others.
  265. if (index >= 200)
  266. return MobileAction.Cast_Directed;
  267. Tracer.Warn("Unknown action index {0}", index);
  268. return MobileAction.None;
  269. }
  270. else if (body.IsAnimal)
  271. {
  272. switch ((ActionIndexAnimal)index)
  273. {
  274. case ActionIndexAnimal.Stand:
  275. return MobileAction.Stand;
  276. case ActionIndexAnimal.Walk:
  277. return MobileAction.Walk;
  278. case ActionIndexAnimal.Run:
  279. return MobileAction.Run;
  280. default:
  281. return MobileAction.MonsterAction;
  282. }
  283. }
  284. else if (body.IsMonster)
  285. {
  286. switch ((ActionIndexMonster)index)
  287. {
  288. case ActionIndexMonster.Stand:
  289. return MobileAction.Stand;
  290. case ActionIndexMonster.Walk:
  291. return MobileAction.Walk;
  292. case ActionIndexMonster.Run:
  293. return MobileAction.Run;
  294. default:
  295. return MobileAction.MonsterAction;
  296. }
  297. }
  298. return MobileAction.None;
  299. }
  300. }
  301. }