KBuySell.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #include "KEngine.h"
  2. #include "KCore.h"
  3. #include "KTabFile.h"
  4. #include "KPlayer.h"
  5. #include "KNpc.h"
  6. #include "KItemGenerator.h"
  7. #include "KSubWorldSet.h"
  8. #include "KItemSet.h"
  9. #include "KBuySell.h"
  10. #ifndef _STANDALONE
  11. #include "CoreShell.h"
  12. #include "crtdbg.h"
  13. #endif
  14. #include "CoreUseNameDef.h"
  15. #ifdef _SERVER
  16. //#include "../MultiServer/Heaven/Interface/iServer.h"
  17. #endif
  18. #define SHOP_BOX_WIDTH 6
  19. #define SHOP_BOX_HEIGHT 10
  20. KBuySell BuySell;
  21. KBuySell::KBuySell()
  22. {
  23. #ifndef _SERVER
  24. m_pShopRoom = NULL;
  25. #endif
  26. m_Item = NULL;
  27. m_SellItem = NULL;
  28. m_Width = 0;
  29. m_Height = 0;
  30. m_MaxItem = 0;
  31. }
  32. KBuySell::~KBuySell()
  33. {
  34. #ifndef _SERVER
  35. if (m_pShopRoom)
  36. {
  37. delete m_pShopRoom;
  38. m_pShopRoom = NULL;
  39. }
  40. #endif
  41. if (m_Item)
  42. {
  43. delete [] m_Item;
  44. m_Item = NULL;
  45. }
  46. if (m_SellItem)
  47. {
  48. for (int i = 0; i < m_Height; i++)
  49. {
  50. if (m_SellItem[i])
  51. {
  52. delete m_SellItem[i];
  53. m_SellItem[i] = NULL;
  54. }
  55. }
  56. delete m_SellItem;
  57. m_SellItem = NULL;
  58. }
  59. m_Width = 0;
  60. m_Height = 0;
  61. m_MaxItem = 0;
  62. }
  63. BOOL KBuySell::Init()
  64. {
  65. KTabFile GoodsFile;
  66. KTabFile BuySellFile;
  67. // g_SetFilePath("\");
  68. if (!BuySellFile.Load(BUYSELL_FILE) || !GoodsFile.Load(GOODS_FILE))
  69. return FALSE;
  70. int nHeight = GoodsFile.GetHeight() - 1;
  71. int nWidth = GoodsFile.GetWidth() - 1;
  72. if (nWidth == 0 || nHeight == 0)
  73. return FALSE;
  74. m_Item = (KItem *)new KItem[nHeight];
  75. if (!m_Item)
  76. return FALSE;
  77. ItemGenerate ItemGenerator;
  78. for (int k = 0; k < nHeight; k++)
  79. {
  80. GoodsFile.GetInteger(k + 2, 1, -1, &ItemGenerator.nGenre);
  81. GoodsFile.GetInteger(k + 2, 2, -1, &ItemGenerator.nDetailType);
  82. GoodsFile.GetInteger(k + 2, 3, -1, &ItemGenerator.nParticularType);
  83. GoodsFile.GetInteger(k + 2, 4, -1, &ItemGenerator.nSeriesReq);
  84. GoodsFile.GetInteger(k + 2, 5, -1, &ItemGenerator.nLevel);
  85. switch(ItemGenerator.nGenre)
  86. {
  87. case item_equip:
  88. ItemGen.Gen_Equipment(ItemGenerator.nDetailType,
  89. ItemGenerator.nParticularType,
  90. ItemGenerator.nSeriesReq,
  91. ItemGenerator.nLevel,
  92. NULL,
  93. 0,
  94. g_SubWorldSet.GetGameVersion(),
  95. &m_Item[k]);
  96. break;
  97. case item_medicine:
  98. ItemGen.Gen_Medicine(ItemGenerator.nDetailType, 
  99. ItemGenerator.nLevel, 
  100. g_SubWorldSet.GetGameVersion(),
  101. &m_Item[k]);
  102. break;
  103. case item_townportal:
  104. ItemGen.Gen_TownPortal(&m_Item[k]);
  105. break;
  106. default:
  107. break;
  108. }
  109. m_MaxItem++;
  110. }
  111. m_Height = BuySellFile.GetHeight() - 1;
  112. m_Width  = BuySellFile.GetWidth();
  113. if (m_Width == 0 || m_Height == 0)
  114. return FALSE;
  115. m_SellItem = (int **)new int*[m_Height];
  116. if (!m_SellItem)
  117. return FALSE;
  118. for (int i = 0; i < m_Height; i++)
  119. {
  120. m_SellItem[i] = NULL;
  121. m_SellItem[i] = (int *)new int[m_Width];
  122. if (!m_SellItem[i])
  123. return FALSE;
  124. for (int j = 0; j < m_Width; j++)
  125. {
  126. BuySellFile.GetInteger(i + 2, j + 1, -1, &m_SellItem[i][j]);
  127. if (m_SellItem[i][j] == -1)
  128. {
  129. continue;
  130. }
  131. _ASSERT(m_SellItem[i][j] > 0); // 策划是从1开始的
  132. if (m_SellItem[i][j] > 0)
  133. m_SellItem[i][j] -= 1; // 为了策划从1开始填表
  134. }
  135. }
  136. #ifndef _SERVER
  137. if (!m_pShopRoom)
  138. {
  139. m_pShopRoom = new KInventory;
  140. m_pShopRoom->Init(SHOP_BOX_WIDTH, SHOP_BOX_HEIGHT);
  141. }
  142. #endif
  143. return TRUE;
  144. }
  145. KItem* KBuySell::GetItem(int nIndex)
  146. {
  147. if (nIndex < 0 || nIndex >= m_MaxItem || !m_Item)
  148. return NULL;
  149. return &m_Item[nIndex];
  150. }
  151. int KBuySell::GetItemIndex(int nShop, int nIndex)
  152. {
  153. if (!m_SellItem || nShop < 0 || nShop >= m_Height || nIndex < 0 || nIndex >= m_Width)
  154. return -1;
  155. if (!m_SellItem[nShop])
  156. return -1;
  157. return m_SellItem[nShop][nIndex];
  158. }
  159. #ifdef _SERVER
  160. BOOL KBuySell::Buy(int nPlayerIdx, int nBuy, int nBuyIdx, int nPlace, int nX, int nY)
  161. {
  162. KASSERT(nPlayerIdx >= 0 && nPlayerIdx < MAX_PLAYER);
  163. if (nBuy != Player[nPlayerIdx].m_BuyInfo.m_nBuyIdx)
  164. {
  165. g_DebugLog("BuySell: %s buy idx error!", Npc[Player[nPlayerIdx].m_nIndex].Name);
  166. return FALSE;
  167. }
  168. if (nBuyIdx > m_Width)
  169. return FALSE;
  170. if (m_SellItem[nBuy][nBuyIdx] < 0 || m_SellItem[nBuy][nBuyIdx] >= m_MaxItem)
  171. return FALSE;
  172. int nIdx = m_SellItem[nBuy][nBuyIdx];
  173. if (Player[nPlayerIdx].m_ItemList.GetEquipmentMoney() < m_Item[nIdx].GetPrice())
  174. return FALSE;
  175. int nItemIdx = ItemSet.Add(&m_Item[nIdx]);
  176. if (!nItemIdx)
  177. return FALSE;
  178. Player[nPlayerIdx].Pay(m_Item[nIdx].GetPrice());
  179. Player[nPlayerIdx].m_ItemList.Add(nItemIdx, nPlace, nX, nY);
  180. return TRUE;
  181. }
  182. /*******************************************************************************
  183. 参数 nIdx 指游戏里Item数组的编号
  184. *******************************************************************************/
  185. BOOL KBuySell::Sell(int nPlayerIdx, int nBuy, int nIdx)
  186. {
  187. KASSERT(nPlayerIdx >= 0 && nPlayerIdx < MAX_PLAYER);
  188. KASSERT(nIdx >= 0 && nIdx < MAX_ITEM);
  189. if (Item[nIdx].GetGenre() == item_task)
  190. return FALSE;
  191. if (nBuy != Player[nPlayerIdx].m_BuyInfo.m_nBuyIdx)
  192. {
  193. g_DebugLog("BuySell: %s buy idx error!", Npc[Player[nPlayerIdx].m_nIndex].Name);
  194. return FALSE;
  195. }
  196. int nMoney = Item[nIdx].GetPrice();
  197. nMoney /= BUY_SELL_SCALE;
  198. if (nMoney <= 0)
  199. nMoney = 1;
  200. Player[nPlayerIdx].Earn(nMoney);
  201. Player[nPlayerIdx].m_ItemList.Remove(nIdx);
  202. ItemSet.Remove(nIdx);
  203. return TRUE;
  204. }
  205. #endif
  206. #ifndef _SERVER
  207. void KBuySell::PaintItem(int nIdx, int nX, int nY)
  208. {
  209. int nShop = Player[CLIENT_PLAYER_INDEX].m_BuyInfo.m_nBuyIdx;
  210. if (nShop < 0 || nShop >= m_Height)
  211. return;
  212. int nItemIdx = GetItemIndex(nShop, nIdx);
  213. int x = nX;
  214. int y = nY;
  215. KItem* pItem = GetItem(nItemIdx);
  216. if (pItem)
  217. {
  218. pItem->Paint(x, y);
  219. }
  220. }
  221. void KBuySell::OpenSale(int nShop)
  222. {
  223. if (nShop < 0 || nShop >= m_Height)
  224. return;
  225. Player[CLIENT_PLAYER_INDEX].m_BuyInfo.m_nBuyIdx = nShop;
  226. CoreDataChanged(GDCNI_NPC_TRADE, NULL, TRUE);
  227. }
  228. #endif
  229. #ifdef _SERVER
  230. void KBuySell::OpenSale(int nPlayerIdx, int nShop)
  231. {
  232. if (nPlayerIdx <= 0 || nPlayerIdx > MAX_PLAYER)
  233. {
  234. return;
  235. }
  236. Player[nPlayerIdx].m_BuyInfo.m_nBuyIdx = nShop;
  237. Player[nPlayerIdx].m_BuyInfo.m_SubWorldID = Npc[Player[nPlayerIdx].m_nIndex].m_SubWorldIndex;
  238. Npc[Player[nPlayerIdx].m_nIndex].GetMpsPos(
  239. &Player[nPlayerIdx].m_BuyInfo.m_nMpsX,
  240. &Player[nPlayerIdx].m_BuyInfo.m_nMpsY);
  241. SALE_BOX_SYNC saleSync;
  242. saleSync.ProtocolType = s2c_opensalebox;
  243. saleSync.nShopIndex = nShop;
  244. g_pServer->PackDataToClient(Player[nPlayerIdx].m_nNetConnectIdx, &saleSync, sizeof(SALE_BOX_SYNC));
  245. }
  246. #endif