Advertisement
Guest User

Untitled

a guest
May 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include <vector>
  2.  
  3. #include "Unit.h"
  4. #include "Constants.h"
  5. #include "D2Helpers.h"
  6. #include "D2BS.h"
  7.  
  8. #define HAS_BIT(value, bit) ((((value) >> (bit)) & 0x1) == 0x1)
  9.  
  10. using namespace std;
  11.  
  12. UnitAny* GetUnit(char* szName, DWORD dwClassId, DWORD dwType, DWORD dwMode, DWORD dwUnitId)
  13. {
  14. if(ClientState() != ClientStateInGame)
  15. return NULL;
  16.  
  17. EnterCriticalSection(&Vars.cUnitListSection);
  18.  
  19. UnitAny* result = NULL;
  20. for(vector<pair<DWORD, DWORD> >::iterator it = Vars.vUnitList.begin(); it != Vars.vUnitList.end(); it++)
  21. {
  22. UnitAny* unit = D2CLIENT_FindUnit(it->first, it->second);
  23. if(unit != NULL && CheckUnit(unit, szName, dwClassId, dwType, dwMode, dwUnitId))
  24. {
  25. result = unit;
  26. break;
  27. }
  28. }
  29.  
  30. LeaveCriticalSection(&Vars.cUnitListSection);
  31.  
  32. return result;
  33.  
  34. // First off, check for near units
  35. /* UnitAny* player = D2CLIENT_GetPlayerUnit();
  36. if(player && player->pPath && player->pPath->pRoom1 && player->pPath->pRoom1->pRoom2 &&
  37. player->pPath->pRoom1->pRoom2->pLevel)
  38. {
  39. Room2* ptRoomOther = player->pPath->pRoom1->pRoom2->pLevel->pRoom2First;
  40.  
  41. for(;ptRoomOther; ptRoomOther = ptRoomOther->pRoom2Next)
  42. {
  43. if(!ptRoomOther->pRoom1)
  44. continue;
  45. for(UnitAny* lpUnit = ptRoomOther->pRoom1->pUnitFirst; lpUnit; lpUnit = lpUnit->pListNext)
  46. {
  47. if(CheckUnit(lpUnit, szName, dwClassId, dwType, dwMode, dwUnitId))
  48. return lpUnit;
  49. }
  50. }
  51. }
  52.  
  53. return NULL;*/
  54. }
  55.  
  56. UnitAny* GetNextUnit(UnitAny* pUnit, char* szName, DWORD dwClassId, DWORD dwType, DWORD dwMode)
  57. {
  58. if(ClientState() != ClientStateInGame)
  59. return NULL;
  60.  
  61. if(!pUnit)
  62. return NULL;
  63.  
  64. EnterCriticalSection(&Vars.cUnitListSection);
  65.  
  66. UnitAny* result = NULL;
  67. // find where we left off
  68. vector<pair<DWORD, DWORD> >::iterator it = Vars.vUnitList.begin();
  69. for(; it != Vars.vUnitList.end (); it++)
  70. {
  71. if(it->first == pUnit->dwUnitId && it->second == pUnit->dwType)
  72. // this is where we left off
  73. break;
  74. }
  75.  
  76. if(it != Vars.vUnitList.end())
  77. {
  78. it++;
  79.  
  80. for(; it != Vars.vUnitList.end(); it++)
  81. {
  82. UnitAny* unit = D2CLIENT_FindUnit(it->first, it->second);
  83. if(unit != NULL && CheckUnit(unit, szName, dwClassId, dwType, dwMode, (DWORD)-1))
  84. {
  85. result = unit;
  86. break;
  87. }
  88. }
  89. }
  90.  
  91. LeaveCriticalSection(&Vars.cUnitListSection);
  92.  
  93. return result;
  94.  
  95. /* UnitAny* lpUnit = pUnit->pListNext;
  96. Room1* ptRoom = D2COMMON_GetRoomFromUnit(pUnit);
  97. Room2* ptRoomOther = NULL;
  98.  
  99. if(ptRoom)
  100. {
  101. ptRoomOther = ptRoom->pRoom2;
  102.  
  103. if(!lpUnit && ptRoomOther)
  104. ptRoomOther = ptRoomOther->pRoom2Next;
  105.  
  106. for(; ptRoomOther; ptRoomOther = ptRoomOther->pRoom2Next)
  107. {
  108. if(ptRoomOther->pRoom1)
  109. {
  110. if(!lpUnit)
  111. lpUnit = ptRoomOther->pRoom1->pUnitFirst;
  112.  
  113. for(; lpUnit; lpUnit = lpUnit->pListNext)
  114. {
  115. if(CheckUnit(lpUnit, szName, dwClassId, dwType, dwMode, (DWORD)-1))
  116. return lpUnit;
  117. }
  118. }
  119. }
  120. }
  121.  
  122. return NULL;*/
  123. }
  124.  
  125. UnitAny* GetInvUnit(UnitAny* pOwner, char* szName, DWORD dwClassId, DWORD dwMode, DWORD dwUnitId)
  126. {
  127. for(UnitAny* pItem = D2COMMON_GetItemFromInventory(pOwner->pInventory); pItem; pItem = D2COMMON_GetNextItemFromInventory(pItem))
  128. {
  129. if(CheckUnit(pItem, szName, dwClassId, 4, dwMode, dwUnitId))
  130. return pItem;
  131. }
  132.  
  133. return NULL;
  134. }
  135.  
  136. UnitAny* GetInvNextUnit(UnitAny* pUnit, UnitAny* pOwner, char* szName, DWORD dwClassId, DWORD dwMode)
  137. {
  138. if(pUnit->dwType == UNIT_ITEM)
  139. {
  140. // Check first if it belongs to a person
  141. if(pUnit->pItemData && pUnit->pItemData->pOwnerInventory && pUnit->pItemData->pOwnerInventory == pOwner->pInventory)
  142. {
  143. // Get the next matching unit from the owner's inventory
  144. for(UnitAny* pItem = D2COMMON_GetNextItemFromInventory(pUnit); pItem; pItem = D2COMMON_GetNextItemFromInventory(pItem))
  145. {
  146. if(CheckUnit(pItem, szName, dwClassId, 4, dwMode, (DWORD)-1))
  147. return pItem;
  148. }
  149.  
  150. }
  151. }
  152.  
  153. return NULL;
  154. }
  155.  
  156. BOOL CheckUnit(UnitAny* pUnit, char* szName, DWORD dwClassId, DWORD dwType, DWORD dwMode, DWORD dwUnitId)
  157. {
  158. if((dwUnitId != -1 && pUnit->dwUnitId != dwUnitId) ||
  159. (dwType != -1 && pUnit->dwType != dwType) ||
  160. (dwClassId != -1 && pUnit->dwTxtFileNo != dwClassId))
  161. return FALSE;
  162.  
  163. if(dwMode != -1)
  164. {
  165. if(dwMode >= 100 && pUnit->dwType == UNIT_ITEM)
  166. {
  167. if(pUnit->pItemData && dwMode-100 != pUnit->pItemData->ItemLocation)
  168. return FALSE;
  169. }
  170. else
  171. {
  172. if(HAS_BIT(dwMode, 29))
  173. {
  174. bool result = false;
  175. // mode is a mask
  176. for(unsigned int i = 0; i < 28; i++)
  177. if(HAS_BIT(dwMode, i) && pUnit->dwMode == i)
  178. result = true;
  179. if(!result)
  180. return FALSE;
  181. }
  182. else if(pUnit->dwMode != dwMode)
  183. return FALSE;
  184. }
  185. }
  186.  
  187. if(szName && szName[0])
  188. {
  189. char szBuf[512] = "";
  190.  
  191. if(dwType == UNIT_ITEM)
  192. GetItemCode(pUnit, szBuf);
  193. else
  194. GetUnitName(pUnit, szBuf, 512);
  195. if(!!_stricmp(szBuf, szName))
  196. return FALSE;
  197. }
  198.  
  199. return TRUE;
  200. }
  201.  
  202. int GetUnitHP(UnitAny* pUnit)
  203. {
  204. return (int)(D2COMMON_GetUnitStat(pUnit, STAT_HP, 0) >> 8);
  205. }
  206.  
  207. int GetUnitMP(UnitAny* pUnit)
  208. {
  209. return (int)(D2COMMON_GetUnitStat(pUnit, STAT_MANA, 0) >> 8);
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement