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

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KCore.cpp
  5. // Date: 2000.08.08
  6. // Code: Daphnis Wang
  7. // Desc: Core class
  8. //---------------------------------------------------------------------------
  9. #include "KCore.h"
  10. #include "KEngine.h"
  11. #include "KFilePath.h"
  12. #ifndef _SERVER
  13. #include "KNpcResList.h"
  14. #include "KBmpFile.h"
  15. #include "ImgRef.h"
  16. #include "../../Represent/iRepresent/iRepresentshell.h"
  17. #endif
  18. #include "KItemChangeRes.h"
  19. #include "KNpcSet.h"
  20. #include "KTabFile.h"
  21. #include "KSkills.h"
  22. #include "KPlayerSet.h"
  23. #include "KPlayerTeam.h"
  24. #include "KMissleSet.h"
  25. #include "KFaction.h"
  26. #include "KMath.h"
  27. #include "time.h"
  28. #include "KPlayerTask.h"
  29. #include "KSubWorldSet.h"
  30. #include "KItemGenerator.h"
  31. #include "KObjSet.h"
  32. #include "KItemSet.h"
  33. #include "KNpc.h"
  34. #include "KNpcTemplate.h"
  35. #include "CoreUseNameDef.h"
  36. #include "KBuySell.h"
  37. #include "KSortScript.h"
  38. #include "KTaskFuns.h"
  39. #include "TaskDef.h"
  40. #include "LuaFuns.h"
  41. #pragma warning (disable: 4512)
  42. #pragma warning (disable: 4786)
  43. #ifdef _SERVER
  44. #ifndef _STANDALONE
  45. #include "../../../lib/S3DBInterface.h"
  46. #else
  47. #include "S3DBInterface.h"
  48. #endif
  49. #endif
  50. #ifdef _SERVER
  51. extern int g_WayPointPriceUnit; //WayPoint表格中价格的单位量,WayPoint价格 = 单位量 * 表格数值
  52. extern int g_StationPriceUnit; //Station表格中价格的单位量,Station价格 = 单位量 * 表格数值
  53. extern int g_DockPriceUnit;
  54. #endif
  55. #ifndef _SERVER
  56. #define CLIENTWEAPONSKILL_TABFILE "\settings\ClientWeaponSkill.txt"
  57. KTabFile g_ClientWeaponSkillTabFile;
  58. #define ADJUSTCOLOR_TABFILE "\settings\AdjustColor.txt"
  59. unsigned int InitAdjustColorTab();
  60. unsigned int * g_pAdjustColorTab = NULL;
  61. unsigned int g_ulAdjustColorCount = 0;
  62. #endif
  63. BOOL InitTaskSetting();
  64. #ifndef _SERVER
  65. #include "KMagicDesc.h"
  66. #include "Scene/KScenePlaceC.h"
  67. BOOL g_bUISelIntelActiveWithServer = FALSE;//当前选择框是否与服务器端交互
  68. BOOL g_bUISpeakActiveWithServer = FALSE;
  69. int g_bUISelLastSelCount = 0;
  70. extern KTabFile g_StringResourseTabFile;
  71. KTabFile g_RankTabSetting;
  72. #endif
  73. //是否将脚本出错信息输出到文件供调试
  74. //#define DEBUGOPT_SCRIPT_MSG_FILEOUT
  75. #ifdef _SERVER
  76. IServer* g_pServer;
  77. extern KTabFile g_EventItemTab;
  78. #else
  79. IClient* g_pClient;
  80. BOOL  g_bPingReply;
  81. #endif
  82. //---------------------------------------------------------------------------
  83. #ifdef TOOLVERSION
  84. KSpriteCache g_SpriteCache;
  85. #endif
  86. KTabFile g_OrdinSkillsSetting, g_MisslesSetting;
  87. KTabFile g_SkillLevelSetting;
  88. KTabFile g_NpcSetting;
  89. KTabFile g_NpcKindFile; //记录Npc人物类型文件
  90. int g_nMeleeWeaponSkill[MAX_MELEEWEAPON_PARTICULARTYPE_NUM];
  91. int g_nRangeWeaponSkill[MAX_RANGEWEAPON_PARTICULARTYPE_NUM];
  92. int g_nHandSkill;
  93. #ifndef _SERVER
  94. KSoundCache g_SoundCache;
  95. KMusic *g_pMusic = NULL;
  96. #endif
  97. //#ifdef _DEBUG
  98. CORE_API BOOL g_bDebugScript;//When True , Testing Debug, Script Will be ReLoaded EveryTime When to be Executed, Not Using ScriptCach.
  99. //#endif
  100. KLuaScript * g_pNpcLevelScript = NULL;
  101. KLuaScript g_WorldScript;
  102. void g_InitProtocol();
  103. //---------------------------------------------------------------------------
  104. CORE_API void g_InitCore()
  105. {
  106. g_InitProtocol();
  107. #ifdef _DEBUG
  108. g_bDebugScript = 0;
  109. g_FindDebugWindow("#32770", "DebugWin");
  110. {
  111. srand((unsigned int) time(NULL));
  112. #ifdef DEBUGOPT_SCRIPT_MSG_FILEOUT
  113. struct tm *newtime;
  114. time_t long_time;
  115. time( &long_time );     /* Get time as long integer. */
  116. newtime = localtime( &long_time ); /* Convert to local time. */
  117. char szFileName[MAX_PATH];
  118. char szFileName1[MAX_PATH];
  119. char szPathName[MAX_PATH];
  120. g_CreatePath("\DebugData");
  121. g_CreatePath("\DebugData\Script");
  122. GetCurrentDirectory(MAX_PATH, szPathName);
  123. sprintf(szFileName, "%s\DebugData\Script\ScriptOut_%d%d%d%d%d.txt", szPathName, newtime->tm_mon, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
  124. sprintf(szFileName1, "%s\DebugData\Script\ScriptErr_%d%d%d%d%d.txt", szPathName, newtime->tm_mon, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
  125. lua_setdebugout(szFileName, szFileName1);
  126. #endif
  127. }
  128. #endif
  129. #ifndef __linux
  130. g_RandomSeed(GetTickCount());
  131. #else
  132. g_RandomSeed(clock());
  133. #endif
  134. srand( (unsigned)time( NULL ) );
  135. time_t ltime;
  136. time( &ltime );
  137. g_DebugLog("Starting Core......%s", ctime( &ltime ));
  138. #ifdef TOOLVERSION
  139. g_SpriteCache.Init(256);
  140. #endif
  141. #ifndef _SERVER
  142. g_bPingReply = TRUE;
  143. g_SoundCache.Init(256);
  144. g_SubWorldSet.m_cMusic.Init();
  145. #endif
  146. #ifndef TOOLVERSION
  147. g_InitSeries();
  148.     if (!g_InitMath())
  149.     {
  150.      g_DebugLog("[Math]初始化数学函数失败!");
  151.     }
  152. ItemSet.Init();
  153. ItemGen.Init();
  154. #ifndef _SERVER
  155. InitAdjustColorTab();//加载偏色表......
  156. g_MagicDesc.Init();
  157. #endif
  158. g_ItemChangeRes.Init();
  159. #endif
  160. NpcSet.Init();
  161. ObjSet.Init();
  162. MissleSet.Init();
  163. g_IniScriptEngine();
  164. // g_SetFilePath(SETTING_PATH);
  165. g_OrdinSkillsSetting.Load(SKILL_SETTING_FILE);
  166. g_MisslesSetting.Load(MISSLES_SETTING_FILE);
  167. g_NpcSetting.Load(NPC_SETTING_FILE);
  168. g_DebugLog("[Script]脚本占空间为%d", sizeof(g_ScriptSet));
  169. InitSkillSetting();
  170. InitMissleSetting();
  171. InitNpcSetting();
  172. InitTaskSetting();
  173. // 这个涉及到与技能相关的东西,所以必须放在技能初始化之后
  174. NpcSet.m_cGoldTemplate.Init();
  175. if (!PlayerSet.Init())
  176. {
  177. printf("Init PlayerSet Error!!!!n");
  178. }
  179. #ifdef _SERVER
  180. memset(g_TaskGlobalValue, 0, sizeof(g_TaskGlobalValue));
  181. g_TeamSet.Init();
  182. // g_SetFilePath("\");
  183. g_SubWorldSet.Load("\maps\WorldSet.ini");
  184. g_WorldScript.Init();
  185. g_WorldScript.RegisterFunctions(WorldScriptFuns, g_GetWorldScriptFunNum());
  186. #endif
  187. #ifndef _SERVER
  188. g_ScenePlace.Initialize();
  189. #endif
  190. time(&ltime);
  191. g_DebugLog("Initting g_Faction %s",  ctime(&ltime));
  192. // 门派初始化:必须在技能初始化之后才能进行
  193. g_Faction.Init();
  194. time(&ltime);
  195. #ifdef _SERVER
  196. #ifdef _DEBUG
  197. KIniFile ServerSettingIni;
  198. // g_SetFilePath("\");
  199. if (ServerSettingIni.Load("\Setting.ini"))
  200. {
  201. ServerSettingIni.GetInteger("Setting", "DebugScript", 0, &g_bDebugScript);
  202. }
  203. #endif
  204. #endif
  205. memset(g_nMeleeWeaponSkill, 0, sizeof(g_nMeleeWeaponSkill));
  206. memset(g_nRangeWeaponSkill, 0, sizeof(g_nRangeWeaponSkill));
  207. KTabFile Weapon_PhysicsSkillIdFile;
  208. if (Weapon_PhysicsSkillIdFile.Load(WEAPON_PHYSICSSKILLFILE))
  209. {
  210. int nHeight = Weapon_PhysicsSkillIdFile.GetHeight() - 1;
  211. int nDetailCol = Weapon_PhysicsSkillIdFile.FindColumn(WEAPON_DETAILTYPE);
  212. int nParticularCol = Weapon_PhysicsSkillIdFile.FindColumn(WEAPON_PARTICULARTYPE);
  213. int nPhysicsSkillCol = Weapon_PhysicsSkillIdFile.FindColumn(WEAPON_SKILLID);
  214. for (int i = 0; i < nHeight; i ++)
  215. {
  216. int nDetail = 0;
  217. int nParticular  = 0;
  218. int nPhysicsSkill = 0;
  219. Weapon_PhysicsSkillIdFile.GetInteger(i + 2, nDetailCol, -1, &nDetail);
  220. Weapon_PhysicsSkillIdFile.GetInteger(i + 2, nParticularCol, -1, &nParticular);
  221. Weapon_PhysicsSkillIdFile.GetInteger(i + 2, nPhysicsSkillCol, -1, &nPhysicsSkill);
  222. //近程武器
  223. if (nDetail == 0 )
  224. {
  225. if (nParticular >= 0 && nParticular < MAX_MELEEWEAPON_PARTICULARTYPE_NUM && nPhysicsSkill > 0 && nPhysicsSkill < MAX_SKILL)
  226. g_nMeleeWeaponSkill[nParticular] = nPhysicsSkill;
  227. }
  228. else if (nDetail == 1)
  229. {
  230. if (nParticular >= 0 && nParticular < MAX_RANGEWEAPON_PARTICULARTYPE_NUM && nPhysicsSkill > 0 && nPhysicsSkill < MAX_SKILL)
  231. g_nRangeWeaponSkill[nParticular] = nPhysicsSkill;
  232. }
  233. else if (nDetail == -1) //空手
  234. {
  235. if (nPhysicsSkill > 0 && nPhysicsSkill < MAX_SKILL)
  236. g_nHandSkill = nPhysicsSkill;
  237. }
  238. }
  239. }
  240. #ifndef TOOLVERSION
  241. if (!BuySell.Init())
  242. {
  243. g_DebugLog("Buysell init failed!");
  244. }
  245. #endif
  246. g_DebugLog("End of Init ,Game Started! %s" , ctime(&ltime));
  247. }
  248. BOOL InitTaskSetting()
  249. {
  250. #ifdef _SERVER
  251. if (!g_EventItemTab.Load(QUESTITEM_TABFILE))
  252. {
  253. g_DebugLog("[TASK]CAN NOT LOAD EventItem Setting File %s", QUESTITEM_TABFILE);
  254. }
  255. if (!g_WayPointTabFile.Load(WORLD_WAYPOINT_TABFILE))
  256. {
  257. g_DebugLog("[TASK]CAN NOT LOAD WayPoint Setting File %s", WORLD_WAYPOINT_TABFILE);
  258. }
  259. if (!g_StationTabFile.Load(WORLD_STATION_TABFILE))
  260. {
  261. g_DebugLog("[TASK]CAN NOT LOAD WayPoint Setting File %s", WORLD_STATION_TABFILE);
  262. }
  263. if (!g_StationPriceTabFile.Load(WORLD_STATIONPRICE_TABFILE))
  264. {
  265. g_DebugLog("[TASK]CAN NOT LOAD STATION PRICE TabFile %s", WORLD_STATIONPRICE_TABFILE);
  266. }
  267. if (!g_WayPointPriceTabFile.Load(WORLD_WAYPOINTPRICE_TABFILE))
  268. {
  269. g_DebugLog("[TASK]CAN NOT LOAD WAYPOINT PRICE TabFile %s", WORLD_WAYPOINTPRICE_TABFILE);
  270. }
  271. if (!g_DockTabFile.Load(WORLD_DOCK_TABFILE))
  272. {
  273. g_DebugLog("[TASK]CAN NOT LOAD DOCK TabFile %s", WORLD_DOCK_TABFILE);
  274. }
  275. if (!g_DockPriceTabFile.Load(WORLD_DOCKPRICE_TABFILE))
  276. {
  277. g_DebugLog("[TASK]CAN NOT LOAD DOCK PRICE TabFile %s", WORLD_DOCKPRICE_TABFILE);
  278. }
  279. int nWidth1 = g_StationPriceTabFile.GetWidth();
  280. int nHeight1 = g_StationPriceTabFile.GetHeight();
  281. int nWidth2 = g_WayPointPriceTabFile.GetWidth();
  282. int nHeight2 = g_WayPointPriceTabFile.GetHeight();
  283. int nWidth3 = g_DockPriceTabFile.GetWidth();
  284. int nHeight3 = g_DockPriceTabFile.GetHeight();
  285. KASSERT(nHeight1 * nWidth1);
  286. KASSERT(nHeight2 * nWidth2);
  287. KASSERT(nHeight3 * nWidth3);
  288. if (nWidth1 * nHeight1)
  289. {
  290. g_StationPriceTabFile.GetInteger(1,1, 1, &g_StationPriceUnit);
  291. g_pStationPriceTab = new int[nWidth1 * nHeight1];
  292. memset(g_pStationPriceTab, -1, nWidth1 * nHeight1 * sizeof(int));
  293. }
  294. if (nWidth2 * nHeight2)
  295. {
  296. g_WayPointPriceTabFile.GetInteger(1,1,1, &g_WayPointPriceUnit);
  297. g_pWayPointPriceTab = new int[nWidth2 * nHeight2];
  298. memset(g_pWayPointPriceTab, -1, nWidth2 * nHeight2 * sizeof(int));
  299. }
  300. if (nWidth3 * nHeight3)
  301. {
  302. g_DockPriceTabFile.GetInteger(1,1,1, &g_DockPriceUnit);
  303. g_pDockPriceTab = new int[nWidth3 * nHeight3];
  304. memset(g_pDockPriceTab, -1, nWidth3 * nHeight3 * sizeof(int));
  305. }
  306. if (!g_TimerTask.Init())
  307. {
  308. g_DebugLog("Timer Task Init Erroorororo!");
  309. }
  310. if (!g_MissionTabFile.Load(TASK_MISSION_SETTING_TABFILE))
  311. {
  312. g_DebugLog("[error]Can Not Open %s", TASK_MISSION_SETTING_TABFILE);
  313. }
  314. #endif
  315. #ifndef _SERVER
  316. if (!g_StringResourseTabFile.Load(STRINGRESOURSE_TABFILE))
  317. {
  318. g_DebugLog("[TASK]CAN NOT LOAD %s", STRINGRESOURSE_TABFILE);
  319. }
  320. if (!g_RankTabSetting.Load(PLAYER_RANK_SETTING_TABFILE))
  321. {
  322. g_DebugLog("[TASK]CAN NOT LOAD %s", PLAYER_RANK_SETTING_TABFILE);
  323. }
  324. #endif
  325. return TRUE;
  326. }
  327. BOOL InitNpcSetting()
  328. {
  329. int nNpcTemplateNum = g_NpcSetting.GetHeight() - 1;
  330. g_DebugLog("人物模板数量%d", nNpcTemplateNum);
  331. //g_pNpcTemplate = new KNpcTemplate[nNpcTemplateNum * MAX_NPC_LEVEL]; //0,0为起点
  332. memset(g_pNpcTemplate, 0, sizeof(void*) * MAX_NPCSTYLE * MAX_NPC_LEVEL);
  333. // 载入文件 人物类型.txt
  334. // g_SetFilePath(RES_INI_FILE_PATH);
  335. if ( !g_NpcKindFile.Load(NPC_RES_KIND_FILE_NAME) )
  336. {
  337. g_DebugLog("无法打开%s文件!!!", NPC_RES_KIND_FILE_NAME);
  338. }
  339. g_DebugLog("NpcTempleSize is %d * %d * %d = %d", sizeof(KNpcTemplate) , nNpcTemplateNum , MAX_NPC_LEVEL, sizeof(KNpcTemplate) * nNpcTemplateNum * MAX_NPC_LEVEL);
  340. //加载Npc等级设定的脚本文件,用于今后加载Npc时使用
  341. #ifdef _SERVER
  342. g_pNpcLevelScript = (KLuaScript*)g_GetScript(NPC_LEVELSCRIPT_FILENAME);
  343. #else
  344. g_pNpcLevelScript = new KLuaScript;
  345. g_pNpcLevelScript->Init();
  346. if (!g_pNpcLevelScript->Load(NPC_LEVELSCRIPT_FILENAME))
  347. {
  348. g_DebugLog ("[error]致命错误,无法正确读取%s", NPC_LEVELSCRIPT_FILENAME);
  349. delete g_pNpcLevelScript;
  350. g_pNpcLevelScript = NULL;
  351. }
  352. #endif
  353. if (!g_pNpcLevelScript) 
  354. {
  355. g_DebugLog("%sNpc等级设定脚本无法加载,可能文件不存在或语法错误", NPC_LEVELSCRIPT_FILENAME);
  356. }
  357. #ifndef _SERVER
  358. g_NpcResList.Init();
  359. #endif
  360. return TRUE;
  361. }
  362. BOOL InitMissleSetting()
  363. {
  364. int nMissleNum = g_MisslesSetting.GetHeight() - 1;
  365. for (int i = 0; i < nMissleNum; i++)
  366. {
  367. int nMissleId = 0;
  368. g_MisslesSetting.GetInteger(i + 2, "MissleId", -1, &nMissleId);
  369. if (nMissleId > 0)
  370. {
  371. g_MisslesLib[nMissleId].GetInfoFromTabFile(i + 2);
  372. g_MisslesLib[nMissleId].m_nMissleId = nMissleId;
  373. }
  374. }
  375. g_DebugLog("子碟类占用空间为%d", sizeof(g_MisslesLib));
  376. return TRUE;
  377. }
  378. extern int g_LoadSkillInfo();
  379. BOOL InitSkillSetting()
  380. {
  381. if (!g_SkillManager.Init())
  382. {
  383. _ASSERT(0);
  384. }
  385. #ifndef _SERVER
  386. if (!g_ClientWeaponSkillTabFile.Load(CLIENTWEAPONSKILL_TABFILE))
  387. {
  388. g_DebugLog("Can Not Load %s", CLIENTWEAPONSKILL_TABFILE);
  389. }
  390. #endif
  391. return TRUE;
  392. }
  393. #ifdef _SERVER
  394. BOOL LoadNpcSettingFromBinFile(LPSTR BinFile = NPC_TEMPLATE_BINFILE)
  395. {
  396. return FALSE;
  397. }
  398. BOOL SaveAsBinFileFromNpcSetting(LPSTR BinFile = NPC_TEMPLATE_BINFILE)
  399. {
  400. return FALSE;
  401. }
  402. #endif
  403. //---------------------------------------------------------------------------
  404. void g_ReleaseCore()
  405. {
  406. int nNpcTemplateNum = g_NpcSetting.GetHeight() - 1;
  407. unsigned long i = 0;
  408. unsigned long j = 0;
  409. for (i = 0; i < nNpcTemplateNum; i++)
  410. {
  411. for (j = 0; j < MAX_NPC_LEVEL; j++)
  412. {
  413. if (g_pNpcTemplate[i][j])
  414. {
  415. delete ((KNpcTemplate *)g_pNpcTemplate[i][j]);
  416. g_pNpcTemplate[i][j] = NULL;
  417. }
  418. }
  419. }
  420. #ifdef _SERVER
  421. if (g_pServer)
  422. {
  423. g_pServer->Release();
  424. g_pServer = NULL;
  425. }
  426. if (g_pStationPriceTab)
  427. {
  428. delete [] g_pStationPriceTab;
  429. g_pStationPriceTab = NULL;
  430. }
  431. if (g_pWayPointPriceTab)
  432. {
  433. delete [] g_pWayPointPriceTab;
  434. g_pWayPointPriceTab = NULL;
  435. }
  436. if (g_pDockPriceTab)
  437. {
  438. delete [] g_pDockPriceTab;
  439. g_pDockPriceTab = NULL;
  440. }
  441. #else
  442. g_SubWorldSet.Close();
  443. g_ScenePlace.ClosePlace();
  444. if (g_pNpcLevelScript)
  445. {
  446. delete g_pNpcLevelScript;
  447. g_pNpcLevelScript = NULL;
  448. }
  449. if (g_pAdjustColorTab)
  450. {
  451. delete []g_pAdjustColorTab;
  452. g_pAdjustColorTab = NULL;
  453. g_ulAdjustColorCount = 0;
  454. }
  455. #endif
  456.     g_UnInitMath();
  457. }
  458. #ifdef _SERVER
  459. void g_SetServer(LPVOID pServer)
  460. {
  461. g_pServer = reinterpret_cast< IServer * >(pServer);
  462. }
  463. #endif
  464. #ifndef _SERVER
  465. void g_SetClient(LPVOID pClient)
  466. {
  467. g_pClient = reinterpret_cast< IClient * >(pClient);
  468. }
  469. unsigned int InitAdjustColorTab()
  470. {
  471. g_pAdjustColorTab = NULL;
  472. g_ulAdjustColorCount = 0;
  473. KTabFile TabFile;
  474. if (!TabFile.Load(ADJUSTCOLOR_TABFILE))
  475. {
  476. _ASSERT(0);
  477. g_DebugLog("无法打开%s", ADJUSTCOLOR_TABFILE);
  478. return 0;
  479. }
  480. int nHeight = TabFile.GetHeight() - 1;
  481. if (nHeight <= 0)
  482. return 0;
  483. g_pAdjustColorTab = (unsigned int *)new unsigned long [nHeight];
  484. g_ulAdjustColorCount = nHeight;
  485. for (int i = 0; i < nHeight; i ++)
  486. {
  487. BYTE bAlpha = 0;
  488. BYTE bRed = 0;
  489. BYTE bGreen = 0;
  490. BYTE bBlue = 0;
  491. int nAlpha;
  492. int nRed;
  493. int nGreen;
  494. int nBlue;
  495. TabFile.GetInteger(i + 2, "ALPHA", 0x000000ff, &nAlpha);
  496. nAlpha &= 0xff;
  497. TabFile.GetInteger(i + 2,"RED", 0, &nRed);
  498. nRed &= 0xff;
  499. TabFile.GetInteger(i + 2,"GREEN",  0, &nGreen);
  500. nGreen &= 0xff;
  501. TabFile.GetInteger(i + 2,"BLUE",  0, &nBlue);
  502. nBlue &= 0xff;
  503. unsigned long ulAdjustColor = nAlpha << 24 | nRed << 16 | nGreen << 8 | nBlue;
  504. g_pAdjustColorTab[i] = ulAdjustColor;
  505. }
  506. return g_ulAdjustColorCount;
  507. }
  508. #endif