arrowmanagerplayer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. class ArrowManagerPlayer : ArrowManagerBase
  2. {
  3. private static ref map<int,typename> m_TypeHashTable;
  4. void ArrowManagerPlayer(PlayerBase player)
  5. {
  6. if (!m_TypeHashTable)
  7. {
  8. InitializeHash();
  9. }
  10. }
  11. private static void InitializeHash()
  12. {
  13. m_TypeHashTable = new map<int,typename>();
  14. AddArrowTypeToHash("Ammo_HuntingBolt");
  15. AddArrowTypeToHash("Ammo_ImprovisedBolt_1");
  16. AddArrowTypeToHash("Ammo_ImprovisedBolt_2");
  17. AddArrowTypeToHash("Ammo_ImprovisedBolt_3");
  18. }
  19. private static void AddArrowTypeToHash(string ArrowType)
  20. {
  21. m_TypeHashTable.Insert(ArrowType.Hash(), ArrowType.ToType());
  22. }
  23. private static typename GetArrowTypeFromHash(int hash)
  24. {
  25. return m_TypeHashTable.Get(hash);
  26. }
  27. bool Save(ParamsWriteContext ctx)
  28. {
  29. ctx.Write(VERSION);
  30. int count = m_Arrows.Count();
  31. int i;
  32. //TODO MW Delete after find why sometimes arrow missing - most likely Life span
  33. for (i = count - 1; i >= 0; i--)
  34. {
  35. if (!m_Arrows.Get(i))
  36. {
  37. m_Arrows.Remove(i);
  38. }
  39. }
  40. count = m_Arrows.Count();
  41. ctx.Write(count);
  42. for (i = 0; i < count; i++)
  43. {
  44. EntityAI arrow = m_Arrows.Get(i);
  45. string type = arrow.GetType();
  46. ctx.Write(type.Hash());
  47. vector angle = arrow.GetLocalYawPitchRoll();
  48. vector pos = arrow.GetLocalPosition();
  49. ctx.Write(angle[0]);
  50. ctx.Write(angle[1]);
  51. ctx.Write(angle[2]);
  52. ctx.Write(pos[0]);
  53. ctx.Write(pos[1]);
  54. ctx.Write(pos[2]);
  55. int pivot = arrow.GetHierarchyPivot();
  56. ctx.Write(pivot);
  57. }
  58. return true;
  59. }
  60. bool Load(ParamsReadContext ctx)
  61. {
  62. int version;
  63. if (!ctx.Read(version))
  64. {
  65. return false;
  66. }
  67. int count;
  68. if (!ctx.Read(count))
  69. {
  70. return false;
  71. }
  72. for (int i = 0; i < count; i++)
  73. {
  74. if (version >= 1)
  75. {
  76. int hash;
  77. if (!ctx.Read(hash))
  78. {
  79. return false;
  80. }
  81. float angleF[3];
  82. float posF[3];
  83. float value;
  84. if (!ctx.Read(value))
  85. {
  86. return false;
  87. }
  88. angleF[0] = value;
  89. if (!ctx.Read(value))
  90. {
  91. return false;
  92. }
  93. angleF[1] = value;
  94. if (!ctx.Read(value))
  95. {
  96. return false;
  97. }
  98. angleF[2] = value;
  99. if (!ctx.Read(value))
  100. {
  101. return false;
  102. }
  103. posF[0] = value;
  104. if (!ctx.Read(value))
  105. {
  106. return false;
  107. }
  108. posF[1] = value;
  109. if (!ctx.Read(value))
  110. {
  111. return false;
  112. }
  113. posF[2] = value;
  114. vector angle = "0 0 0";
  115. vector pos = "0 0 0";
  116. angle = vector.ArrayToVec(angleF);
  117. pos = vector.ArrayToVec(posF);
  118. int pivot;
  119. if (!ctx.Read(pivot))
  120. {
  121. return false;
  122. }
  123. if (version >= 2)
  124. {
  125. #ifdef SERVER
  126. int spawnFlags = ECE_KEEPHEIGHT|ECE_DYNAMIC_PERSISTENCY;
  127. #else
  128. int spawnFlags = ECE_LOCAL|ECE_KEEPHEIGHT|ECE_DYNAMIC_PERSISTENCY;
  129. #endif
  130. typename arrowType = GetArrowTypeFromHash(hash);
  131. EntityAI arrow = EntityAI.Cast(GetGame().CreateObjectEx(arrowType.ToString(), pos, spawnFlags));
  132. if (arrow)
  133. {
  134. arrow.SetQuantityToMinimum();
  135. arrow.SetYawPitchRoll(angle);
  136. m_Owner.AddChild(arrow, pivot);
  137. }
  138. }
  139. }
  140. }
  141. return true;
  142. }
  143. }