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

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 2002 by Kingsoft
  3. //
  4. // File: KPlayerTeam.cpp
  5. // Date: 2002.01.06
  6. // Code: 边城浪子
  7. // Desc: Team Class
  8. //---------------------------------------------------------------------------
  9. #include "KCore.h"
  10. #include "KNpc.h"
  11. #ifdef _SERVER
  12. //#include "KNetServer.h"
  13. //#include "../../Headers/IServer.h"
  14. #else
  15. //#include "KNetClient.h"
  16. #include "../../Headers/IClient.h"
  17. #include "CoreShell.h"
  18. #endif
  19. #include "KPlayer.h"
  20. #include "KPlayerSet.h"
  21. #include "KPlayerTeam.h"
  22. //#include "MyAssert.h"
  23. enum
  24. {
  25. Team_S_Close = 0,
  26. Team_S_Open,
  27. };
  28. KTeam g_Team[MAX_TEAM];
  29. #ifdef _SERVER
  30. KTeamSet g_TeamSet;
  31. #endif
  32. #ifndef _SERVER
  33. KPlayerTeam::KPlayerTeam()
  34. {
  35. Release();
  36. }
  37. #endif
  38. #ifndef _SERVER
  39. void KPlayerTeam::Release()
  40. {
  41. m_nFlag = 0;
  42. m_nFigure = TEAM_CAPTAIN;
  43. m_nApplyCaptainID = -1;
  44. m_nApplyCaptainID = 0;
  45. m_dwApplyTimer = 0;
  46. m_bAutoRefuseInviteFlag = FALSE;  // TRUE 自动拒绝   FALSE 手动
  47. ReleaseList();
  48. }
  49. #endif
  50. #ifndef _SERVER
  51. void KPlayerTeam::ReleaseList()
  52. {
  53. for (int i = 0; i < MAX_TEAM_APPLY_LIST; i++)
  54. m_sApplyList[i].Release();
  55. }
  56. #endif
  57. #ifndef _SERVER
  58. BOOL KPlayerTeam::ApplyCreate()//char *lpszTeamName)
  59. {
  60. // if (!lpszTeamName || !lpszTeamName[0])
  61. // return FALSE;
  62. // if (strlen(lpszTeamName) >= 32)
  63. // return FALSE;
  64. if (m_nFlag)
  65. return FALSE;
  66. PLAYER_APPLY_CREATE_TEAM sCreateTeam;
  67. sCreateTeam.ProtocolType = c2s_teamapplycreate;
  68. // memset(sCreateTeam.m_szTeamName, 0, sizeof(sCreateTeam.m_szTeamName));
  69. // strcpy(sCreateTeam.m_szTeamName, lpszTeamName);
  70. if (g_pClient)
  71. g_pClient->SendPackToServer(&sCreateTeam, sizeof(PLAYER_APPLY_CREATE_TEAM));
  72. return TRUE;
  73. }
  74. #endif
  75. #ifndef _SERVER
  76. //---------------------------------------------------------------------------
  77. // 功能:邀请加入队伍
  78. //---------------------------------------------------------------------------
  79. void KPlayerTeam::InviteAdd(DWORD dwNpcID)
  80. {
  81. // if ( !this->m_nFlag || this->m_nFigure != TEAM_CAPTAIN || g_Team[0].m_nState != Team_S_Open)
  82. // return;
  83. TEAM_INVITE_ADD_COMMAND sAdd;
  84. sAdd.ProtocolType = c2s_teaminviteadd;
  85. sAdd.m_dwNpcID = dwNpcID;
  86. if (g_pClient)
  87. g_pClient->SendPackToServer(&sAdd, sizeof(TEAM_INVITE_ADD_COMMAND));
  88. }
  89. #endif
  90. #ifndef _SERVER
  91. //---------------------------------------------------------------------------
  92. // 功能:收到邀请
  93. //---------------------------------------------------------------------------
  94. void KPlayerTeam::ReceiveInvite(TEAM_INVITE_ADD_SYNC *pInvite)
  95. {
  96. if ( !pInvite )
  97. return;
  98. char szName[32];
  99. int nIdx;
  100. memset(szName, 0, sizeof(szName));
  101. memcpy(szName, pInvite->m_szName, sizeof(pInvite->m_szName) - (sizeof(TEAM_INVITE_ADD_SYNC) - pInvite->m_wLength - 1));
  102. nIdx = pInvite->m_nIdx;
  103. if (m_bAutoRefuseInviteFlag)
  104. {
  105. ReplyInvite(nIdx, 0);
  106. }
  107. else
  108. {
  109. // 通知界面有人邀请玩家加入某个队伍
  110. KUiPlayerItem sPlayer;
  111. KSystemMessage sMsg;
  112. strcpy(sPlayer.Name, szName);
  113. sPlayer.nIndex = pInvite->m_nIdx;
  114. sPlayer.uId = 0;
  115. sPlayer.nData = 0;
  116. sprintf(sMsg.szMessage, MSG_TEAM_GET_INVITE, szName);
  117. sMsg.eType = SMT_TEAM;
  118. sMsg.byConfirmType = SMCT_UI_TEAM_INVITE;
  119. sMsg.byPriority = 3;
  120. sMsg.byParamSize = sizeof(KUiPlayerItem);
  121. CoreDataChanged(GDCNI_SYSTEM_MESSAGE, (unsigned int)&sMsg, (int)&sPlayer);
  122. }
  123. }
  124. #endif
  125. #ifndef _SERVER
  126. //---------------------------------------------------------------------------
  127. // 功能:回复邀请
  128. // 参数:nResult  if == 0 拒绝  if == 1 接受
  129. //---------------------------------------------------------------------------
  130. void KPlayerTeam::ReplyInvite(int nIdx, int nResult)
  131. {
  132. if (nIdx < 0 || nResult < 0 || nResult > 1)
  133. return;
  134. TEAM_REPLY_INVITE_COMMAND sReply;
  135. sReply.ProtocolType = c2s_teamreplyinvite;
  136. sReply.m_nIndex = nIdx;
  137. sReply.m_btResult = nResult;
  138. if (g_pClient)
  139. g_pClient->SendPackToServer(&sReply, sizeof(TEAM_REPLY_INVITE_COMMAND));
  140. }
  141. #endif
  142. #ifndef _SERVER
  143. //---------------------------------------------------------------------------
  144. // 功能:设定是否自动拒绝别人的加入队伍的邀请
  145. //---------------------------------------------------------------------------
  146. void KPlayerTeam::SetAutoRefuseInvite(BOOL bFlag)
  147. {
  148. KSystemMessage sMsg;
  149. sMsg.eType = SMT_NORMAL;
  150. sMsg.byConfirmType = SMCT_NONE;
  151. sMsg.byPriority = 0;
  152. sMsg.byParamSize = 0;
  153. if (bFlag)
  154. {
  155. m_bAutoRefuseInviteFlag = TRUE;
  156. sprintf(sMsg.szMessage, MSG_TEAM_AUTO_REFUSE_INVITE);
  157. }
  158. else
  159. {
  160. m_bAutoRefuseInviteFlag = FALSE;
  161. sprintf(sMsg.szMessage, MSG_TEAM_NOT_AUTO_REFUSE_INVITE);
  162. }
  163. CoreDataChanged(GDCNI_SYSTEM_MESSAGE, (unsigned int)&sMsg, 0);
  164. }
  165. #endif
  166. #ifndef _SERVER
  167. //---------------------------------------------------------------------------
  168. // 功能:获得是否自动拒绝别人的加入队伍的邀请状态
  169. //---------------------------------------------------------------------------
  170. BOOL KPlayerTeam::GetAutoRefuseState()
  171. {
  172. return m_bAutoRefuseInviteFlag;
  173. }
  174. #endif
  175. #ifndef _SERVER
  176. //---------------------------------------------------------------------------
  177. // 功能:获得自身队伍信息(给界面)
  178. //---------------------------------------------------------------------------
  179. int KPlayerTeam::GetInfo(KUiPlayerTeam *pTeam)
  180. {
  181. if (!pTeam)
  182. return 0;
  183. pTeam->nCaptainPower = g_Team[0].CalcCaptainPower();
  184. if (m_nFlag == 0)
  185. return 0;
  186. if (m_nFigure == TEAM_CAPTAIN)
  187. {
  188. pTeam->bTeamLeader = true;
  189. int nNo = 0;
  190. for (int i = 0; i < MAX_TEAM_APPLY_LIST; i++)
  191. {
  192. if (this->m_sApplyList[i].m_dwNpcID > 0)
  193. nNo++;
  194. }
  195. pTeam->cNumTojoin = nNo;
  196. }
  197. else
  198. {
  199. pTeam->bTeamLeader = false;
  200. pTeam->cNumTojoin = 0;
  201. }
  202. pTeam->nTeamServerID = g_Team[0].m_nTeamServerID;
  203. pTeam->cNumMember = g_Team[0].m_nMemNum + 1;
  204. pTeam->bOpened = 0;
  205. if (g_Team[0].m_nState == Team_S_Open)
  206. pTeam->bOpened = 1;
  207. return 1;
  208. }
  209. #endif
  210. #ifndef _SERVER
  211. //---------------------------------------------------------------------------
  212. // 功能:更新界面显示
  213. //---------------------------------------------------------------------------
  214. void KPlayerTeam::UpdateInterface()
  215. {
  216. KUiPlayerTeam sTeam;
  217. if (GetInfo(&sTeam) == 0)
  218. {
  219. CoreDataChanged(GDCNI_TEAM, NULL, 0);
  220. }
  221. else
  222. {
  223. CoreDataChanged(GDCNI_TEAM, (unsigned int)&sTeam, 0);
  224. }
  225. }
  226. #endif
  227. #ifndef _SERVER
  228. //---------------------------------------------------------------------------
  229. // 功能:从申请人列表中删除某个申请人
  230. //---------------------------------------------------------------------------
  231. void KPlayerTeam::DeleteOneFromApplyList(DWORD dwNpcID)
  232. {
  233. for (int i = 0; i < MAX_TEAM_APPLY_LIST; i++)
  234. {
  235. if (m_sApplyList[i].m_dwNpcID == dwNpcID)
  236. {
  237. m_sApplyList[i].Release();
  238. return;
  239. }
  240. }
  241. }
  242. #endif
  243. #ifdef _SERVER
  244. //---------------------------------------------------------------------------
  245. // 功能:清空
  246. //---------------------------------------------------------------------------
  247. void KPlayerTeam::Release()
  248. {
  249. m_nFlag = 0;
  250. m_nID = -1;
  251. m_nFigure = 0;
  252. m_nApplyCaptainID = 0;
  253. memset(m_nInviteList, 0, sizeof(m_nInviteList));
  254. m_nListPos = 0;
  255. }
  256. #endif
  257. #ifdef _SERVER
  258. BOOL KPlayerTeam::CreateTeam(int nIdx, PLAYER_APPLY_CREATE_TEAM *pCreateTeam)
  259. {
  260. _ASSERT(pCreateTeam);
  261. // 当前处于不能组队状态
  262. if (!m_bCanTeamFlag)
  263. {
  264. PLAYER_SEND_CREATE_TEAM_FALSE sCreateFalse;
  265. sCreateFalse.ProtocolType = s2c_teamcreatefalse;
  266. sCreateFalse.m_btErrorID = Team_Create_Error_CannotCreate;
  267. g_pServer->PackDataToClient(Player[nIdx].m_nNetConnectIdx, (BYTE*)&sCreateFalse, sizeof(PLAYER_SEND_CREATE_TEAM_FALSE));
  268. return FALSE;
  269. }
  270. if (m_nFlag) // 队伍创建失败:已在队伍中
  271. {
  272. PLAYER_SEND_CREATE_TEAM_FALSE sCreateFalse;
  273. sCreateFalse.ProtocolType = s2c_teamcreatefalse;
  274. sCreateFalse.m_btErrorID = Team_Create_Error_InTeam;
  275. g_pServer->PackDataToClient(Player[nIdx].m_nNetConnectIdx, (BYTE*)&sCreateFalse, sizeof(PLAYER_SEND_CREATE_TEAM_FALSE));
  276. return FALSE;
  277. }
  278. // char szTeamName[32];
  279. // memcpy(szTeamName, pCreateTeam->m_szTeamName, sizeof(szTeamName));
  280. // szTeamName[31] = 0;
  281. // if ( !szTeamName[0] )
  282. // {
  283. // PLAYER_SEND_CREATE_TEAM_FALSE sCreateFalse;
  284. // sCreateFalse.ProtocolType = s2c_teamcreatefalse;
  285. // sCreateFalse.m_btErrorID = Team_Create_Error_Name;
  286. // SendToClient(Player[nIdx].m_nNetConnectIdx, (BYTE*)&sCreateFalse, sizeof(PLAYER_SEND_CREATE_TEAM_FALSE));
  287. // return FALSE;
  288. // }
  289. // 创建队伍
  290. m_nID = g_TeamSet.CreateTeam(nIdx);//, szTeamName);
  291. if (m_nID >= 0) // 队伍创建成功
  292. {
  293. m_nFlag = 1;
  294. m_nFigure = TEAM_CAPTAIN;
  295. m_nApplyCaptainID = 0;
  296. Npc[Player[nIdx].m_nIndex].RestoreCurrentCamp();
  297. if (Player[nIdx].m_cMenuState.m_nState == PLAYER_MENU_STATE_TRADEOPEN)
  298. Player[nIdx].m_cMenuState.SetState(nIdx, PLAYER_MENU_STATE_NORMAL);
  299. PLAYER_SEND_CREATE_TEAM_SUCCESS sCreateSuccess;
  300. sCreateSuccess.ProtocolType = s2c_teamcreatesuccess;
  301. sCreateSuccess.nTeamServerID = m_nID;
  302. g_pServer->PackDataToClient(Player[nIdx].m_nNetConnectIdx, (BYTE*)&sCreateSuccess, sizeof(PLAYER_SEND_CREATE_TEAM_SUCCESS));
  303. g_Team[m_nID].SetTeamOpen();
  304. return TRUE;
  305. }
  306. // else if (m_nID == -1) // 队伍创建失败:同名
  307. // {
  308. // PLAYER_SEND_CREATE_TEAM_FALSE sCreateFalse;
  309. // sCreateFalse.ProtocolType = s2c_teamcreatefalse;
  310. // sCreateFalse.m_btErrorID = Team_Create_Error_SameName;
  311. // SendToClient(Player[nIdx].m_nNetConnectIdx, (BYTE*)&sCreateFalse, sizeof(PLAYER_SEND_CREATE_TEAM_FALSE));
  312. // return FALSE;
  313. // }
  314. else if (m_nID == -2)
  315. {
  316. PLAYER_SEND_CREATE_TEAM_FALSE sCreateFalse;
  317. sCreateFalse.ProtocolType = s2c_teamcreatefalse;
  318. sCreateFalse.m_btErrorID = Team_Create_Error_TeamFull;
  319. g_pServer->PackDataToClient(Player[nIdx].m_nNetConnectIdx, (BYTE*)&sCreateFalse, sizeof(PLAYER_SEND_CREATE_TEAM_FALSE));
  320. return FALSE;
  321. }
  322. else // 其他错误
  323. {
  324. return FALSE;
  325. }
  326. return TRUE;
  327. }
  328. #endif
  329. #ifdef _SERVER
  330. //---------------------------------------------------------------------------
  331. // 功能:转发邀请加入队伍
  332. //---------------------------------------------------------------------------
  333. void KPlayerTeam::InviteAdd(int nIdx, TEAM_INVITE_ADD_COMMAND *pAdd)
  334. {
  335. if (!pAdd)
  336. return;
  337. // 如果不在队伍中或者不是队长或者队伍未开放
  338. if ( !this->m_nFlag || this->m_nFigure != TEAM_CAPTAIN || !g_Team[this->m_nID].IsOpen() )
  339. return;
  340. int nTargetIdx = Player[nIdx].FindAroundPlayer(pAdd->m_dwNpcID);
  341. if (nTargetIdx == -1)
  342. return;
  343. if (!Player[nTargetIdx].m_cTeam.m_bCanTeamFlag)
  344. {
  345. SHOW_MSG_SYNC sMsg;
  346. sMsg.ProtocolType = s2c_msgshow;
  347. sMsg.m_wMsgID = enumMSG_ID_TARGET_CANNOT_ADD_TEAM;
  348. sMsg.m_wLength = sizeof(SHOW_MSG_SYNC) - 1 - sizeof(LPVOID);
  349. g_pServer->PackDataToClient(Player[nIdx].m_nNetConnectIdx, &sMsg, sMsg.m_wLength + 1);
  350. return;
  351. }
  352. this->m_nInviteList[this->m_nListPos] = nTargetIdx;
  353. this->m_nListPos++;
  354. this->m_nListPos %= MAX_TEAM_MEMBER;
  355. TEAM_INVITE_ADD_SYNC sAdd;
  356. sAdd.ProtocolType = s2c_teaminviteadd;
  357. sAdd.m_nIdx = nIdx;
  358. memset(sAdd.m_szName, 0, sizeof(sAdd.m_szName));
  359. strcpy(sAdd.m_szName, Npc[Player[nIdx].m_nIndex].Name);
  360. sAdd.m_wLength = sizeof(TEAM_INVITE_ADD_SYNC) - 1 - sizeof(sAdd.m_szName) + strlen(sAdd.m_szName);
  361. g_pServer->PackDataToClient(Player[nTargetIdx].m_nNetConnectIdx, (BYTE*)&sAdd, sAdd.m_wLength + 1);
  362. }
  363. #endif
  364. #ifdef _SERVER
  365. //---------------------------------------------------------------------------
  366. // 功能:收到邀请加入队伍的回复
  367. //---------------------------------------------------------------------------
  368. void KPlayerTeam::GetInviteReply(int nSelfIdx, int nTargetIdx, int nResult)
  369. {
  370. // 状态对不对
  371. if (!m_nFlag || m_nFigure != TEAM_CAPTAIN || !g_Team[m_nID].IsOpen())
  372. return;
  373. // 有没有邀请
  374. int i;
  375. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  376. {
  377. if (m_nInviteList[i] == nTargetIdx)
  378. break;
  379. }
  380. if (i >= MAX_TEAM_MEMBER)
  381. return;
  382. if (Player[nTargetIdx].m_nIndex == 0)
  383. return;
  384. // 对方拒绝邀请
  385. if (nResult == 0)
  386. {
  387. // 需要向队长发消息通知对方拒绝
  388. SHOW_MSG_SYNC sMsg;
  389. sMsg.ProtocolType = s2c_msgshow;
  390. sMsg.m_wMsgID = enumMSG_ID_TEAM_REFUSE_INVITE;
  391. sMsg.m_wLength = sizeof(SHOW_MSG_SYNC) - 1 - sizeof(LPVOID) + sizeof(Npc[Player[nTargetIdx].m_nIndex].Name);
  392. sMsg.m_lpBuf = new BYTE[sMsg.m_wLength + 1];
  393. memcpy(sMsg.m_lpBuf, &sMsg, sizeof(SHOW_MSG_SYNC) - sizeof(LPVOID));
  394. memcpy((char*)sMsg.m_lpBuf + sizeof(SHOW_MSG_SYNC) - sizeof(LPVOID), Npc[Player[nTargetIdx].m_nIndex].Name, sizeof(Npc[Player[nTargetIdx].m_nIndex].Name));
  395. g_pServer->PackDataToClient(Player[nSelfIdx].m_nNetConnectIdx, sMsg.m_lpBuf, sMsg.m_wLength + 1);
  396. return;
  397. }
  398. if (g_Team[m_nID].m_nMemNum >= MAX_TEAM_MEMBER ||
  399. g_Team[m_nID].m_nMemNum >= g_Team[m_nID].CalcCaptainPower())
  400. return;
  401. // 自动离队
  402. if (Player[nTargetIdx].m_cTeam.m_nFlag)
  403. {
  404. PLAYER_APPLY_LEAVE_TEAM sLeave;
  405. sLeave.ProtocolType = c2s_teamapplyleave;
  406. Player[nTargetIdx].LeaveTeam((BYTE*)&sLeave);
  407. }
  408. // 队伍添加成员
  409. if ( !g_Team[m_nID].AddMember(nTargetIdx) )
  410. return;
  411. // 改变队伍状态
  412. if (g_Team[m_nID].m_nMemNum >= MAX_TEAM_MEMBER || g_Team[m_nID].CheckFull())
  413. {
  414. g_Team[m_nID].SetTeamClose();
  415. }
  416. // 被接受队员队伍数据处理
  417. Player[nTargetIdx].m_cTeam.Release();
  418. Player[nTargetIdx].m_cTeam.m_nFlag = 1;
  419. Player[nTargetIdx].m_cTeam.m_nFigure = TEAM_MEMBER;
  420. Player[nTargetIdx].m_cTeam.m_nID = m_nID;
  421. Npc[Player[nTargetIdx].m_nIndex].SetCurrentCamp(Npc[Player[nSelfIdx].m_nIndex].m_Camp);
  422. // 给各个队员发消息
  423. PLAYER_TEAM_ADD_MEMBER sAddMem;
  424. memset(sAddMem.m_szName, 0, sizeof(sAddMem.m_szName));
  425. sAddMem.ProtocolType = s2c_teamaddmember;
  426. sAddMem.m_dwNpcID = Npc[Player[nTargetIdx].m_nIndex].m_dwID;
  427. sAddMem.m_btLevel = (DWORD)Npc[Player[nTargetIdx].m_nIndex].m_Level;
  428. strcpy(sAddMem.m_szName, Npc[Player[nTargetIdx].m_nIndex].Name);
  429. // 给队长发消息
  430. g_pServer->PackDataToClient(Player[nSelfIdx].m_nNetConnectIdx, (BYTE*)&sAddMem, sizeof(PLAYER_TEAM_ADD_MEMBER));
  431. // 给老队员发消息
  432. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  433. {
  434. if (g_Team[m_nID].m_nMember[i] < 0 || g_Team[m_nID].m_nMember[i] == nTargetIdx)
  435. continue;
  436. g_pServer->PackDataToClient(Player[g_Team[m_nID].m_nMember[i]].m_nNetConnectIdx, (BYTE*)&sAddMem, sizeof(PLAYER_TEAM_ADD_MEMBER));
  437. }
  438. // ------------------------------------- 给新队员发消息 --------------------------
  439. int nNpcIndex;
  440. PLAYER_SEND_SELF_TEAM_INFO sSelfInfo;
  441. sSelfInfo.ProtocolType = s2c_teamselfinfo;
  442. sSelfInfo.nTeamServerID = m_nID;
  443. sSelfInfo.m_dwLeadExp = Player[nTargetIdx].m_dwLeadExp;
  444. sSelfInfo.m_btState = g_Team[m_nID].m_nState;
  445. // 队长数据
  446. nNpcIndex = Player[nSelfIdx].m_nIndex;
  447. sSelfInfo.m_dwNpcID[0] = Npc[nNpcIndex].m_dwID;
  448. sSelfInfo.m_btLevel[0] = (DWORD)Npc[nNpcIndex].m_Level;
  449. strcpy(sSelfInfo.m_szNpcName[0], Npc[nNpcIndex].Name);
  450. // 队员数据
  451. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  452. {
  453. if (g_Team[m_nID].m_nMember[i] >= 0)
  454. {
  455. nNpcIndex = Player[g_Team[m_nID].m_nMember[i]].m_nIndex;
  456. sSelfInfo.m_dwNpcID[i + 1] = Npc[nNpcIndex].m_dwID;
  457. sSelfInfo.m_btLevel[i + 1] = (DWORD)Npc[nNpcIndex].m_Level;
  458. strcpy(sSelfInfo.m_szNpcName[i + 1], Npc[nNpcIndex].Name);
  459. }
  460. else
  461. {
  462. sSelfInfo.m_dwNpcID[i + 1] = 0;
  463. sSelfInfo.m_btLevel[i + 1] = 0;
  464. memset(sSelfInfo.m_szNpcName[i + 1], 0, sizeof(sSelfInfo.m_szNpcName[i + 1]));
  465. }
  466. }
  467. // 发送消息
  468. g_pServer->PackDataToClient(Player[nTargetIdx].m_nNetConnectIdx, (BYTE*)&sSelfInfo, sizeof(PLAYER_SEND_SELF_TEAM_INFO));
  469. SHOW_MSG_SYNC sMsg;
  470. sMsg.ProtocolType = s2c_msgshow;
  471. sMsg.m_wMsgID = enumMSG_ID_TEAM_SELF_ADD;
  472. sMsg.m_wLength = sizeof(SHOW_MSG_SYNC) - 1 - sizeof(LPVOID);
  473. g_pServer->PackDataToClient(Player[nTargetIdx].m_nNetConnectIdx, &sMsg, sMsg.m_wLength + 1);
  474. // --------------------------------- 给新队员发消息 end --------------------------
  475. }
  476. #endif
  477. #ifdef _SERVER
  478. //---------------------------------------------------------------------------
  479. // 功能:收到邀请加入队伍的回复
  480. //---------------------------------------------------------------------------
  481. void KPlayerTeam::SetCanTeamFlag(int nSelfIdx, BOOL bFlag)
  482. {
  483. if (bFlag)
  484. {
  485. m_bCanTeamFlag = TRUE;
  486. return;
  487. }
  488. m_bCanTeamFlag = FALSE;
  489. if (nSelfIdx >= 0 && nSelfIdx < MAX_PLAYER)
  490. {
  491. PLAYER_APPLY_LEAVE_TEAM sLeaveTeam;
  492. sLeaveTeam.ProtocolType = c2s_teamapplyleave;
  493. Player[nSelfIdx].LeaveTeam((BYTE*)&sLeaveTeam);
  494. }
  495. }
  496. #endif
  497. //---------------------------------------------------------------------------
  498. // 功能:构造函数
  499. //---------------------------------------------------------------------------
  500. KTeam::KTeam()
  501. {
  502. Release();
  503. }
  504. //---------------------------------------------------------------------------
  505. // 功能:清空
  506. //---------------------------------------------------------------------------
  507. void KTeam::Release()
  508. {
  509. m_nCaptain = -1;
  510. m_nMemNum = 0;
  511. int i;
  512. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  513. {
  514. m_nMember[i] = -1;
  515. }
  516. m_nState = Team_S_Close;
  517. #ifndef _SERVER
  518. for (i = 0; i < MAX_TEAM_MEMBER + 1; i++)
  519. {
  520. m_nMemLevel[i] = 0;
  521. memset(m_szMemName, 0, 32 * (MAX_TEAM_MEMBER + 1));
  522. }
  523. m_nTeamServerID = -1;
  524. #endif
  525. }
  526. //---------------------------------------------------------------------------
  527. // 功能:设定 Team 在 g_Team 中的位置
  528. //---------------------------------------------------------------------------
  529. void KTeam::SetIndex(int nIndex)
  530. {
  531. m_nIndex = nIndex;
  532. }
  533. //---------------------------------------------------------------------------
  534. // 功能:寻找队员空位
  535. //---------------------------------------------------------------------------
  536. int KTeam::FindFree()
  537. {
  538. for (int i = 0; i < MAX_TEAM_MEMBER; i++)
  539. {
  540. if (m_nMember[i] < 0)
  541. return i;
  542. }
  543. return -1;
  544. }
  545. //---------------------------------------------------------------------------
  546. // 功能:寻找具有指定npc id的队员(不包括队长)
  547. // 返回值:队员在 m_nMember 数组中的位置
  548. //---------------------------------------------------------------------------
  549. int KTeam::FindMemberID(DWORD dwNpcID)
  550. {
  551. for (int i = 0; i < MAX_TEAM_MEMBER; i++)
  552. {
  553. #ifdef _SERVER
  554. if (m_nMember[i] >= 0 && Npc[Player[m_nMember[i]].m_nIndex].m_dwID == dwNpcID)
  555. return i;
  556. #else
  557. if (m_nMember[i] > 0 && (DWORD)m_nMember[i] == dwNpcID)
  558. return i;
  559. #endif
  560. }
  561. return -1;
  562. }
  563. #ifdef _SERVER
  564. //---------------------------------------------------------------------------
  565. // 功能:判断队伍是否已经满员
  566. // 返回值:FALSE 未满员  TRUE 已满员
  567. //---------------------------------------------------------------------------
  568. BOOL KTeam::CheckFull()
  569. {
  570. if (m_nMemNum < CalcCaptainPower())
  571. return FALSE;
  572. return TRUE;
  573. }
  574. #endif
  575. //---------------------------------------------------------------------------
  576. // 功能:设定队伍状态:打开(允许接受新成员)
  577. //---------------------------------------------------------------------------
  578. BOOL KTeam::SetTeamOpen()
  579. {
  580. #ifdef _SERVER
  581. if (CheckFull())
  582. return FALSE;
  583. m_nState = Team_S_Open;
  584. PLAYER_TEAM_OPEN_CLOSE sTeamState;
  585. sTeamState.ProtocolType = (BYTE)s2c_teamopenclose;
  586. sTeamState.m_btOpenClose = Team_S_Open;
  587. g_pServer->PackDataToClient(Player[m_nCaptain].m_nNetConnectIdx, (BYTE*)&sTeamState, sizeof(PLAYER_TEAM_OPEN_CLOSE));
  588. Player[m_nCaptain].m_cMenuState.SetState(m_nCaptain, PLAYER_MENU_STATE_TEAMOPEN);
  589. NPC_SET_MENU_STATE_SYNC sSync;
  590. sSync.ProtocolType = s2c_npcsetmenustate;
  591. sSync.m_dwID = Npc[Player[m_nCaptain].m_nIndex].m_dwID;
  592. sSync.m_btState = PLAYER_MENU_STATE_TEAMOPEN;
  593. sSync.m_wLength = sizeof(NPC_SET_MENU_STATE_SYNC) - 1 - sizeof(sSync.m_szSentence);
  594. Npc[Player[m_nCaptain].m_nIndex].SendDataToNearRegion((LPVOID)&sSync, sSync.m_wLength + 1);
  595. return TRUE;
  596. #else
  597. m_nState = Team_S_Open;
  598. Player[CLIENT_PLAYER_INDEX].m_cTeam.UpdateInterface();
  599. return TRUE;
  600. #endif
  601. }
  602. //---------------------------------------------------------------------------
  603. // 功能:设定队伍状态:关闭(不允许接受新成员)
  604. //---------------------------------------------------------------------------
  605. BOOL KTeam::SetTeamClose()
  606. {
  607. #ifdef _SERVER
  608. m_nState = Team_S_Close;
  609. PLAYER_TEAM_OPEN_CLOSE sTeamState;
  610. sTeamState.ProtocolType = (BYTE)s2c_teamopenclose;
  611. sTeamState.m_btOpenClose = Team_S_Close;
  612. g_pServer->PackDataToClient(Player[m_nCaptain].m_nNetConnectIdx, (BYTE*)&sTeamState, sizeof(PLAYER_TEAM_OPEN_CLOSE));
  613. Player[m_nCaptain].m_cMenuState.SetState(m_nCaptain, PLAYER_MENU_STATE_NORMAL);
  614. NPC_SET_MENU_STATE_SYNC sSync;
  615. sSync.ProtocolType = s2c_npcsetmenustate;
  616. sSync.m_dwID = Npc[Player[m_nCaptain].m_nIndex].m_dwID;
  617. sSync.m_btState = PLAYER_MENU_STATE_NORMAL;
  618. sSync.m_wLength = sizeof(NPC_SET_MENU_STATE_SYNC) - 1 - sizeof(sSync.m_szSentence);
  619. Npc[Player[m_nCaptain].m_nIndex].SendDataToNearRegion((LPVOID)&sSync, sSync.m_wLength + 1);
  620. return TRUE;
  621. #else
  622. m_nState = Team_S_Close;
  623. Player[CLIENT_PLAYER_INDEX].m_cTeam.UpdateInterface();
  624. return TRUE;
  625. #endif
  626. }
  627. #ifdef _SERVER
  628. //---------------------------------------------------------------------------
  629. // 功能:判断队伍是否是打开状态
  630. //---------------------------------------------------------------------------
  631. BOOL KTeam::IsOpen()
  632. {
  633. return m_nState;
  634. }
  635. #endif
  636. //---------------------------------------------------------------------------
  637. // 功能:计算队长能统帅队员的人数
  638. //---------------------------------------------------------------------------
  639. int KTeam::CalcCaptainPower()
  640. {
  641. #ifdef _SERVER
  642. return PlayerSet.m_cLeadExp.GetMemNumFromLevel(Player[m_nCaptain].m_dwLeadLevel);
  643. #else
  644. return PlayerSet.m_cLeadExp.GetMemNumFromLevel(Player[CLIENT_PLAYER_INDEX].m_dwLeadLevel);
  645. #endif
  646. }
  647. #ifdef _SERVER
  648. //---------------------------------------------------------------------------
  649. // 功能:判断加入队伍的属性条件是否合适
  650. //---------------------------------------------------------------------------
  651. BOOL KTeam::CheckAddCondition(int nPlayerIndex)
  652. {
  653. if (Npc[Player[nPlayerIndex].m_nIndex].m_Camp != camp_begin && Npc[Player[m_nCaptain].m_nIndex].m_Camp == camp_begin)
  654. return FALSE;
  655. return TRUE;
  656. }
  657. #endif
  658. #ifdef _SERVER
  659. //---------------------------------------------------------------------------
  660. // 功能:创建一支队伍
  661. //---------------------------------------------------------------------------
  662. BOOL KTeam::CreateTeam(int nPlayerIndex)//, char *lpszName)
  663. {
  664. Release();
  665. m_nCaptain = nPlayerIndex;
  666. // if (lpszName && lpszName[0])
  667. // strcpy(m_szName, lpszName);
  668. return TRUE;
  669. }
  670. #endif
  671. #ifdef _SERVER
  672. //---------------------------------------------------------------------------
  673. // 功能:添加一个队伍成员
  674. //---------------------------------------------------------------------------
  675. BOOL KTeam::AddMember(int nPlayerIndex)
  676. {
  677. // 队伍是否为空
  678. if (m_nCaptain < 0)
  679. return FALSE;
  680. // 阵营是否相同
  681. if ( !CheckAddCondition(nPlayerIndex) )
  682. return FALSE;
  683. // 是否已经满员
  684. if (CheckFull())
  685. return FALSE;
  686. int n;
  687. n = FindFree();
  688. if (n < 0)
  689. return FALSE;
  690. m_nMember[n] = nPlayerIndex;
  691. m_nMemNum++;
  692. return TRUE;
  693. }
  694. #endif
  695. #ifdef _SERVER
  696. //---------------------------------------------------------------------------
  697. // 功能:删除一个队伍成员
  698. //---------------------------------------------------------------------------
  699. BOOL KTeam::DeleteMember(int nPlayerIndex)
  700. {
  701. int i;
  702. PLAYER_LEAVE_TEAM sLeaveTeam;
  703. if (nPlayerIndex == m_nCaptain)
  704. {
  705. SHOW_MSG_SYNC sMsg;
  706. // 队伍解散消息
  707. sMsg.ProtocolType = s2c_msgshow;
  708. sMsg.m_wMsgID = enumMSG_ID_TEAM_DISMISS;
  709. sMsg.m_wLength = sizeof(SHOW_MSG_SYNC) - 1 - sizeof(LPVOID);
  710. g_pServer->PackDataToClient(Player[nPlayerIndex].m_nNetConnectIdx, &sMsg, sMsg.m_wLength + 1);
  711. Player[nPlayerIndex].m_cTeam.Release();
  712. Npc[Player[nPlayerIndex].m_nIndex].RestoreCurrentCamp();
  713. sLeaveTeam.ProtocolType = s2c_teamleave;
  714. sLeaveTeam.m_dwNpcID = Npc[Player[nPlayerIndex].m_nIndex].m_dwID;
  715. g_pServer->PackDataToClient(Player[nPlayerIndex].m_nNetConnectIdx, (BYTE*)&sLeaveTeam, sizeof(PLAYER_LEAVE_TEAM));
  716. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  717. {
  718. if (m_nMember[i] < 0)
  719. continue;
  720. g_pServer->PackDataToClient(Player[m_nMember[i]].m_nNetConnectIdx, &sMsg, sMsg.m_wLength + 1);
  721. Npc[Player[m_nMember[i]].m_nIndex].RestoreCurrentCamp();
  722. Player[m_nMember[i]].m_cTeam.Release();
  723. sLeaveTeam.m_dwNpcID = Npc[Player[m_nMember[i]].m_nIndex].m_dwID;
  724. g_pServer->PackDataToClient(Player[m_nMember[i]].m_nNetConnectIdx, (BYTE*)&sLeaveTeam, sizeof(PLAYER_LEAVE_TEAM));
  725. }
  726. Release();
  727. return TRUE;
  728. }
  729. else
  730. {
  731. Player[nPlayerIndex].m_cTeam.Release();
  732. sLeaveTeam.ProtocolType = s2c_teamleave;
  733. sLeaveTeam.m_dwNpcID = Npc[Player[nPlayerIndex].m_nIndex].m_dwID;
  734. g_pServer->PackDataToClient(Player[m_nCaptain].m_nNetConnectIdx, (BYTE*)&sLeaveTeam, sizeof(PLAYER_LEAVE_TEAM));
  735. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  736. {
  737. if (m_nMember[i] == nPlayerIndex)
  738. {
  739. Npc[Player[m_nMember[i]].m_nIndex].RestoreCurrentCamp();
  740. m_nMember[i] = -1;
  741. m_nMemNum--;
  742. g_pServer->PackDataToClient(Player[nPlayerIndex].m_nNetConnectIdx, (BYTE*)&sLeaveTeam, sizeof(PLAYER_LEAVE_TEAM));
  743. }
  744. else
  745. {
  746. if (m_nMember[i] >= 0)
  747. g_pServer->PackDataToClient(Player[m_nMember[i]].m_nNetConnectIdx, (BYTE*)&sLeaveTeam, sizeof(PLAYER_LEAVE_TEAM));
  748. }
  749. }
  750. }
  751. return TRUE;
  752. }
  753. #endif
  754. #ifndef _SERVER
  755. //---------------------------------------------------------------------------
  756. // 功能:客户端创建一支队伍(客户端只可能存在一支的队伍,属于本地玩家)
  757. //---------------------------------------------------------------------------
  758. void KTeam::CreateTeam(int nCaptainNpcID, char *lpszCaptainName, int nCaptainLevel, DWORD nTeamServerID)
  759. {
  760. Release();
  761. m_nCaptain = nCaptainNpcID;
  762. m_nMemLevel[0] = nCaptainLevel;
  763. strcpy(m_szMemName[0], lpszCaptainName);
  764. m_nTeamServerID = nTeamServerID; // 队伍在服务器上的唯一标识
  765. }
  766. #endif
  767. #ifndef _SERVER
  768. //---------------------------------------------------------------------------
  769. // 功能:添加一个队伍成员
  770. //---------------------------------------------------------------------------
  771. BOOL KTeam::AddMember(DWORD dwNpcID, int nLevel, char *lpszNpcName)
  772. {
  773. for (int i = 0; i < MAX_TEAM_MEMBER; i++)
  774. {
  775. if (m_nMember[i] == (int)dwNpcID)
  776. return TRUE;
  777. }
  778. int nFreeNo;
  779. nFreeNo = FindFree();
  780. if (nFreeNo < 0)
  781. return FALSE;
  782. m_nMember[nFreeNo] = dwNpcID;
  783. m_nMemLevel[nFreeNo + 1] = nLevel;
  784. strcpy(m_szMemName[nFreeNo + 1], lpszNpcName);
  785. m_nMemNum++;
  786. return TRUE;
  787. }
  788. #endif
  789. #ifndef _SERVER
  790. //---------------------------------------------------------------------------
  791. // 功能:客户端删除一个队伍成员
  792. //---------------------------------------------------------------------------
  793. void KTeam::DeleteMember(DWORD dwNpcID)
  794. {
  795. if (dwNpcID == (DWORD)m_nCaptain)
  796. {
  797. Release();
  798. return;
  799. }
  800. for (int i = 0; i < MAX_TEAM_MEMBER; i++)
  801. {
  802. if ((DWORD)m_nMember[i] == dwNpcID)
  803. {
  804. m_nMemNum--;
  805. m_nMember[i] = -1;
  806. m_nMemLevel[i + 1] = 0;
  807. m_szMemName[i + 1][0] = 0;
  808. return;
  809. }
  810. }
  811. }
  812. #endif
  813. #ifndef _SERVER
  814. //---------------------------------------------------------------------------
  815. // 功能:输出队员信息(按界面的要求,给界面)
  816. //---------------------------------------------------------------------------
  817. int KTeam::GetMemberInfo(KUiPlayerItem *pList, int nCount)
  818. {
  819. if (this->m_nCaptain < 0)
  820. return 0;
  821. int i, nNum = 0;
  822. if (!pList || nCount <= m_nMemNum)
  823. {
  824. return m_nMemNum + 1;
  825. }
  826. // 队长名字
  827. strcpy(pList[nNum].Name, m_szMemName[0]);
  828. pList[nNum].uId = this->m_nCaptain;
  829. pList[nNum].nIndex = 0;
  830. nNum++;
  831. for (i = 0; i < MAX_TEAM_MEMBER; i++)
  832. {
  833. if (nNum >= nCount)
  834. break;
  835. if (this->m_nMember[i] <= 0)
  836. continue;
  837. strcpy(pList[nNum].Name, m_szMemName[i + 1]);
  838. pList[nNum].uId = m_nMember[i];
  839. pList[nNum].nIndex = 0;
  840. nNum++;
  841. }
  842. return nNum;
  843. }
  844. #endif
  845. #ifdef _SERVER
  846. //---------------------------------------------------------------------------
  847. // 功能:判断某人是否是队伍成员
  848. //---------------------------------------------------------------------------
  849. BOOL KTeam::CheckIn(int nPlayerIndex)
  850. {
  851. if (nPlayerIndex <= 0)
  852. return FALSE;
  853. if (nPlayerIndex == m_nCaptain)
  854. return TRUE;
  855. for (int i = 0; i < MAX_TEAM_MEMBER; i++)
  856. {
  857. if (m_nMember[i] == nPlayerIndex)
  858. return TRUE;
  859. }
  860. return FALSE;
  861. }
  862. #endif
  863. #ifdef _SERVER
  864. //---------------------------------------------------------------------------
  865. // 功能:初始化
  866. //---------------------------------------------------------------------------
  867. void KTeamSet::Init()
  868. {
  869. for (int i = 0; i < MAX_TEAM; i++)
  870. g_Team[i].SetIndex(i);
  871. }
  872. #endif
  873. #ifdef _SERVER
  874. //---------------------------------------------------------------------------
  875. // 功能:差找 g_Team 中的空位
  876. //---------------------------------------------------------------------------
  877. int KTeamSet::FindFree()
  878. {
  879. for (int i = 0; i < MAX_TEAM; i++)
  880. {
  881. if (g_Team[i].m_nCaptain < 0)
  882. return i;
  883. }
  884. return -1;
  885. }
  886. #endif
  887. /*
  888. #ifdef _SERVER
  889. //---------------------------------------------------------------------------
  890. // 功能:判断队名是否可用
  891. //---------------------------------------------------------------------------
  892. BOOL KTeamSet::CheckName(char *lpszName)
  893. {
  894. if (!lpszName || !lpszName[0])
  895. return FALSE;
  896. for (int i = 0; i < MAX_TEAM; i++)
  897. {
  898. if (g_Team[i].m_nCaptain < 0)
  899. continue;
  900. if (strcmp(lpszName, g_Team[i].m_szName) == 0)
  901. return FALSE;
  902. }
  903. return TRUE;
  904. }
  905. #endif
  906. */
  907. #ifdef _SERVER
  908. //---------------------------------------------------------------------------
  909. // 功能:创建一支队伍
  910. //---------------------------------------------------------------------------
  911. int KTeamSet::CreateTeam(int nPlayerID)//, char *lpszName)
  912. {
  913. int nTeamID;
  914. nTeamID = FindFree();
  915. if (nTeamID < 0)
  916. return -2;
  917. // if (!CheckName(lpszName))
  918. // return -1;
  919. if (g_Team[nTeamID].CreateTeam(nPlayerID));//, lpszName))
  920. return nTeamID;
  921. return -1;
  922. }
  923. #endif