No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protocolgameparse.cpp 71KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  1. /*
  2. * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "protocolgame.h"
  23. #include "localplayer.h"
  24. #include "thingtypemanager.h"
  25. #include "game.h"
  26. #include "map.h"
  27. #include "item.h"
  28. #include "effect.h"
  29. #include "missile.h"
  30. #include "tile.h"
  31. #include "luavaluecasts.h"
  32. #include <framework/core/eventdispatcher.h>
  33. void ProtocolGame::parseMessage(const InputMessagePtr& msg)
  34. {
  35. int opcode = -1;
  36. int prevOpcode = -1;
  37. try {
  38. while(!msg->eof()) {
  39. opcode = msg->getU8();
  40. // must be > so extended will be enabled before GameStart.
  41. if(!g_game.getFeature(Otc::GameLoginPending)) {
  42. if(!m_gameInitialized && opcode > Proto::GameServerFirstGameOpcode) {
  43. g_game.processGameStart();
  44. m_gameInitialized = true;
  45. }
  46. }
  47. // try to parse in lua first
  48. int readPos = msg->getReadPos();
  49. if(callLuaField<bool>("onOpcode", opcode, msg))
  50. continue;
  51. else
  52. msg->setReadPos(readPos); // restore read pos
  53. switch(opcode) {
  54. case Proto::GameServerLoginOrPendingState:
  55. if(g_game.getFeature(Otc::GameLoginPending))
  56. parsePendingGame(msg);
  57. else
  58. parseLogin(msg);
  59. break;
  60. case Proto::GameServerGMActions:
  61. parseGMActions(msg);
  62. break;
  63. case Proto::GameServerUpdateNeeded:
  64. parseUpdateNeeded(msg);
  65. break;
  66. case Proto::GameServerLoginError:
  67. parseLoginError(msg);
  68. break;
  69. case Proto::GameServerLoginAdvice:
  70. parseLoginAdvice(msg);
  71. break;
  72. case Proto::GameServerLoginWait:
  73. parseLoginWait(msg);
  74. break;
  75. case Proto::GameServerLoginToken:
  76. parseLoginToken(msg);
  77. break;
  78. case Proto::GameServerPing:
  79. case Proto::GameServerPingBack:
  80. if((opcode == Proto::GameServerPing && g_game.getFeature(Otc::GameClientPing)) ||
  81. (opcode == Proto::GameServerPingBack && !g_game.getFeature(Otc::GameClientPing)))
  82. parsePingBack(msg);
  83. else
  84. parsePing(msg);
  85. break;
  86. case Proto::GameServerChallenge:
  87. parseChallenge(msg);
  88. break;
  89. case Proto::GameServerDeath:
  90. parseDeath(msg);
  91. break;
  92. case Proto::GameServerFullMap:
  93. parseMapDescription(msg);
  94. break;
  95. case Proto::GameServerMapTopRow:
  96. parseMapMoveNorth(msg);
  97. break;
  98. case Proto::GameServerMapRightRow:
  99. parseMapMoveEast(msg);
  100. break;
  101. case Proto::GameServerMapBottomRow:
  102. parseMapMoveSouth(msg);
  103. break;
  104. case Proto::GameServerMapLeftRow:
  105. parseMapMoveWest(msg);
  106. break;
  107. case Proto::GameServerUpdateTile:
  108. parseUpdateTile(msg);
  109. break;
  110. case Proto::GameServerCreateOnMap:
  111. parseTileAddThing(msg);
  112. break;
  113. case Proto::GameServerChangeOnMap:
  114. parseTileTransformThing(msg);
  115. break;
  116. case Proto::GameServerDeleteOnMap:
  117. parseTileRemoveThing(msg);
  118. break;
  119. case Proto::GameServerMoveCreature:
  120. parseCreatureMove(msg);
  121. break;
  122. case Proto::GameServerOpenContainer:
  123. parseOpenContainer(msg);
  124. break;
  125. case Proto::GameServerCloseContainer:
  126. parseCloseContainer(msg);
  127. break;
  128. case Proto::GameServerCreateContainer:
  129. parseContainerAddItem(msg);
  130. break;
  131. case Proto::GameServerChangeInContainer:
  132. parseContainerUpdateItem(msg);
  133. break;
  134. case Proto::GameServerDeleteInContainer:
  135. parseContainerRemoveItem(msg);
  136. break;
  137. case Proto::GameServerSetInventory:
  138. parseAddInventoryItem(msg);
  139. break;
  140. case Proto::GameServerDeleteInventory:
  141. parseRemoveInventoryItem(msg);
  142. break;
  143. case Proto::GameServerOpenNpcTrade:
  144. parseOpenNpcTrade(msg);
  145. break;
  146. case Proto::GameServerPlayerGoods:
  147. parsePlayerGoods(msg);
  148. break;
  149. case Proto::GameServerCloseNpcTrade:
  150. parseCloseNpcTrade(msg);
  151. break;
  152. case Proto::GameServerOwnTrade:
  153. parseOwnTrade(msg);
  154. break;
  155. case Proto::GameServerCounterTrade:
  156. parseCounterTrade(msg);
  157. break;
  158. case Proto::GameServerCloseTrade:
  159. parseCloseTrade(msg);
  160. break;
  161. case Proto::GameServerAmbient:
  162. parseWorldLight(msg);
  163. break;
  164. case Proto::GameServerGraphicalEffect:
  165. parseMagicEffect(msg);
  166. break;
  167. case Proto::GameServerTextEffect:
  168. parseAnimatedText(msg);
  169. break;
  170. case Proto::GameServerMissleEffect:
  171. parseDistanceMissile(msg);
  172. break;
  173. case Proto::GameServerMarkCreature:
  174. parseCreatureMark(msg);
  175. break;
  176. case Proto::GameServerTrappers:
  177. parseTrappers(msg);
  178. break;
  179. case Proto::GameServerCreatureHealth:
  180. parseCreatureHealth(msg);
  181. break;
  182. case Proto::GameServerCreatureLight:
  183. parseCreatureLight(msg);
  184. break;
  185. case Proto::GameServerCreatureOutfit:
  186. parseCreatureOutfit(msg);
  187. break;
  188. case Proto::GameServerCreatureSpeed:
  189. parseCreatureSpeed(msg);
  190. break;
  191. case Proto::GameServerCreatureSkull:
  192. parseCreatureSkulls(msg);
  193. break;
  194. case Proto::GameServerCreatureParty:
  195. parseCreatureShields(msg);
  196. break;
  197. case Proto::GameServerCreatureUnpass:
  198. parseCreatureUnpass(msg);
  199. break;
  200. case Proto::GameServerEditText:
  201. parseEditText(msg);
  202. break;
  203. case Proto::GameServerEditList:
  204. parseEditList(msg);
  205. break;
  206. // PROTOCOL>=1038
  207. case Proto::GameServerPremiumTrigger:
  208. parsePremiumTrigger(msg);
  209. break;
  210. case Proto::GameServerPlayerData:
  211. parsePlayerStats(msg);
  212. break;
  213. case Proto::GameServerPlayerSkills:
  214. parsePlayerSkills(msg);
  215. break;
  216. case Proto::GameServerPlayerState:
  217. parsePlayerState(msg);
  218. break;
  219. case Proto::GameServerClearTarget:
  220. parsePlayerCancelAttack(msg);
  221. break;
  222. case Proto::GameServerPlayerModes:
  223. parsePlayerModes(msg);
  224. break;
  225. case Proto::GameServerTalk:
  226. parseTalk(msg);
  227. break;
  228. case Proto::GameServerChannels:
  229. parseChannelList(msg);
  230. break;
  231. case Proto::GameServerOpenChannel:
  232. parseOpenChannel(msg);
  233. break;
  234. case Proto::GameServerOpenPrivateChannel:
  235. parseOpenPrivateChannel(msg);
  236. break;
  237. case Proto::GameServerRuleViolationChannel:
  238. parseRuleViolationChannel(msg);
  239. break;
  240. case Proto::GameServerRuleViolationRemove:
  241. parseRuleViolationRemove(msg);
  242. break;
  243. case Proto::GameServerRuleViolationCancel:
  244. parseRuleViolationCancel(msg);
  245. break;
  246. case Proto::GameServerRuleViolationLock:
  247. parseRuleViolationLock(msg);
  248. break;
  249. case Proto::GameServerOpenOwnChannel:
  250. parseOpenOwnPrivateChannel(msg);
  251. break;
  252. case Proto::GameServerCloseChannel:
  253. parseCloseChannel(msg);
  254. break;
  255. case Proto::GameServerTextMessage:
  256. parseTextMessage(msg);
  257. break;
  258. case Proto::GameServerCancelWalk:
  259. parseCancelWalk(msg);
  260. break;
  261. case Proto::GameServerWalkWait:
  262. parseWalkWait(msg);
  263. break;
  264. case Proto::GameServerFloorChangeUp:
  265. parseFloorChangeUp(msg);
  266. break;
  267. case Proto::GameServerFloorChangeDown:
  268. parseFloorChangeDown(msg);
  269. break;
  270. case Proto::GameServerChooseOutfit:
  271. parseOpenOutfitWindow(msg);
  272. break;
  273. case Proto::GameServerVipAdd:
  274. parseVipAdd(msg);
  275. break;
  276. case Proto::GameServerVipState:
  277. parseVipState(msg);
  278. break;
  279. case Proto::GameServerVipLogout:
  280. parseVipLogout(msg);
  281. break;
  282. case Proto::GameServerTutorialHint:
  283. parseTutorialHint(msg);
  284. break;
  285. case Proto::GameServerAutomapFlag:
  286. parseAutomapFlag(msg);
  287. break;
  288. case Proto::GameServerQuestLog:
  289. parseQuestLog(msg);
  290. break;
  291. case Proto::GameServerQuestLine:
  292. parseQuestLine(msg);
  293. break;
  294. // PROTOCOL>=870
  295. case Proto::GameServerSpellDelay:
  296. parseSpellCooldown(msg);
  297. break;
  298. case Proto::GameServerSpellGroupDelay:
  299. parseSpellGroupCooldown(msg);
  300. break;
  301. case Proto::GameServerMultiUseDelay:
  302. parseMultiUseCooldown(msg);
  303. break;
  304. // PROTOCOL>=910
  305. case Proto::GameServerChannelEvent:
  306. parseChannelEvent(msg);
  307. break;
  308. case Proto::GameServerItemInfo:
  309. parseItemInfo(msg);
  310. break;
  311. case Proto::GameServerPlayerInventory:
  312. parsePlayerInventory(msg);
  313. break;
  314. // PROTOCOL>=950
  315. case Proto::GameServerPlayerDataBasic:
  316. parsePlayerInfo(msg);
  317. break;
  318. // PROTOCOL>=970
  319. case Proto::GameServerModalDialog:
  320. parseModalDialog(msg);
  321. break;
  322. // PROTOCOL>=980
  323. case Proto::GameServerLoginSuccess:
  324. parseLogin(msg);
  325. break;
  326. case Proto::GameServerEnterGame:
  327. parseEnterGame(msg);
  328. break;
  329. case Proto::GameServerPlayerHelpers:
  330. parsePlayerHelpers(msg);
  331. break;
  332. // PROTOCOL>=1000
  333. case Proto::GameServerCreatureMarks:
  334. parseCreaturesMark(msg);
  335. break;
  336. case Proto::GameServerCreatureType:
  337. parseCreatureType(msg);
  338. break;
  339. // PROTOCOL>=1055
  340. case Proto::GameServerBlessings:
  341. parseBlessings(msg);
  342. break;
  343. case Proto::GameServerUnjustifiedStats:
  344. parseUnjustifiedStats(msg);
  345. break;
  346. case Proto::GameServerPvpSituations:
  347. parsePvpSituations(msg);
  348. break;
  349. case Proto::GameServerPreset:
  350. parsePreset(msg);
  351. break;
  352. // PROTOCOL>=1080
  353. case Proto::GameServerCoinBalanceUpdating:
  354. parseCoinBalanceUpdating(msg);
  355. break;
  356. case Proto::GameServerCoinBalance:
  357. parseCoinBalance(msg);
  358. break;
  359. case Proto::GameServerRequestPurchaseData:
  360. parseRequestPurchaseData(msg);
  361. break;
  362. case Proto::GameServerStoreCompletePurchase:
  363. parseCompleteStorePurchase(msg);
  364. break;
  365. case Proto::GameServerStoreOffers:
  366. parseStoreOffers(msg);
  367. break;
  368. case Proto::GameServerStoreTransactionHistory:
  369. parseStoreTransactionHistory(msg);
  370. break;
  371. case Proto::GameServerStoreError:
  372. parseStoreError(msg);
  373. break;
  374. case Proto::GameServerStore:
  375. parseStore(msg);
  376. break;
  377. // PROTOCOL>=1097
  378. case Proto::GameServerStoreButtonIndicators:
  379. parseStoreButtonIndicators(msg);
  380. break;
  381. case Proto::GameServerSetStoreDeepLink:
  382. parseSetStoreDeepLink(msg);
  383. break;
  384. // otclient ONLY
  385. case Proto::GameServerExtendedOpcode:
  386. parseExtendedOpcode(msg);
  387. break;
  388. case Proto::GameServerChangeMapAwareRange:
  389. parseChangeMapAwareRange(msg);
  390. break;
  391. default:
  392. stdext::throw_exception(stdext::format("unhandled opcode %d", (int)opcode));
  393. break;
  394. }
  395. prevOpcode = opcode;
  396. }
  397. } catch(stdext::exception& e) {
  398. g_logger.error(stdext::format("ProtocolGame parse message exception (%d bytes unread, last opcode is %d, prev opcode is %d): %s",
  399. msg->getUnreadSize(), opcode, prevOpcode, e.what()));
  400. }
  401. }
  402. void ProtocolGame::parseLogin(const InputMessagePtr& msg)
  403. {
  404. uint playerId = msg->getU32();
  405. int serverBeat = msg->getU16();
  406. if(g_game.getFeature(Otc::GameNewSpeedLaw)) {
  407. double speedA = msg->getDouble();
  408. double speedB = msg->getDouble();
  409. double speedC = msg->getDouble();
  410. m_localPlayer->setSpeedFormula(speedA, speedB, speedC);
  411. }
  412. bool canReportBugs = msg->getU8();
  413. if(g_game.getClientVersion() >= 1054)
  414. msg->getU8(); // can change pvp frame option
  415. if(g_game.getClientVersion() >= 1058) {
  416. int expertModeEnabled = msg->getU8();
  417. g_game.setExpertPvpMode(expertModeEnabled);
  418. }
  419. if(g_game.getFeature(Otc::GameIngameStore)) {
  420. // URL to ingame store images
  421. msg->getString();
  422. // premium coin package size
  423. // e.g you can only buy packs of 25, 50, 75, .. coins in the market
  424. msg->getU16();
  425. }
  426. m_localPlayer->setId(playerId);
  427. g_game.setServerBeat(serverBeat);
  428. g_game.setCanReportBugs(canReportBugs);
  429. g_game.processLogin();
  430. }
  431. void ProtocolGame::parsePendingGame(const InputMessagePtr& msg)
  432. {
  433. //set player to pending game state
  434. g_game.processPendingGame();
  435. }
  436. void ProtocolGame::parseEnterGame(const InputMessagePtr& msg)
  437. {
  438. //set player to entered game state
  439. g_game.processEnterGame();
  440. if(!m_gameInitialized) {
  441. g_game.processGameStart();
  442. m_gameInitialized = true;
  443. }
  444. }
  445. void ProtocolGame::parseStoreButtonIndicators(const InputMessagePtr& msg)
  446. {
  447. msg->getU8(); // unknown
  448. msg->getU8(); // unknown
  449. }
  450. void ProtocolGame::parseSetStoreDeepLink(const InputMessagePtr& msg)
  451. {
  452. int currentlyFeaturedServiceType = msg->getU8();
  453. }
  454. void ProtocolGame::parseBlessings(const InputMessagePtr& msg)
  455. {
  456. uint16 blessings = msg->getU16();
  457. m_localPlayer->setBlessings(blessings);
  458. }
  459. void ProtocolGame::parsePreset(const InputMessagePtr& msg)
  460. {
  461. uint32 preset = msg->getU32();
  462. }
  463. void ProtocolGame::parseRequestPurchaseData(const InputMessagePtr& msg)
  464. {
  465. int transactionId = msg->getU32();
  466. int productType = msg->getU8();
  467. }
  468. void ProtocolGame::parseStore(const InputMessagePtr& msg)
  469. {
  470. parseCoinBalance(msg);
  471. // Parse all categories
  472. int count = msg->getU16();
  473. for(int i = 0; i < count; i++) {
  474. std::string category = msg->getString();
  475. std::string description = msg->getString();
  476. int highlightState = 0;
  477. if(g_game.getFeature(Otc::GameIngameStoreHighlights))
  478. highlightState = msg->getU8();
  479. std::vector<std::string> icons;
  480. int iconCount = msg->getU8();
  481. for(int i = 0; i < iconCount; i++) {
  482. std::string icon = msg->getString();
  483. icons.push_back(icon);
  484. }
  485. // If this is a valid category name then
  486. // the category we just parsed is a child of that
  487. std::string parentCategory = msg->getString();
  488. }
  489. }
  490. void ProtocolGame::parseCoinBalance(const InputMessagePtr& msg)
  491. {
  492. bool update = msg->getU8() == 1;
  493. int coins = -1;
  494. int transferableCoins = -1;
  495. if(update) {
  496. // amount of coins that can be used to buy prodcuts
  497. // in the ingame store
  498. coins = msg->getU32();
  499. // amount of coins that can be sold in market
  500. // or be transfered to another player
  501. transferableCoins = msg->getU32();
  502. }
  503. }
  504. void ProtocolGame::parseCoinBalanceUpdating(const InputMessagePtr& msg)
  505. {
  506. // coin balance can be updating and might not be accurate
  507. bool isUpdating = msg->getU8() == 1;
  508. }
  509. void ProtocolGame::parseCompleteStorePurchase(const InputMessagePtr& msg)
  510. {
  511. // not used
  512. msg->getU8();
  513. std::string message = msg->getString();
  514. int coins = msg->getU32();
  515. int transferableCoins = msg->getU32();
  516. g_logger.info(stdext::format("Purchase Complete: %s", message));
  517. }
  518. void ProtocolGame::parseStoreTransactionHistory(const InputMessagePtr &msg)
  519. {
  520. int currentPage;
  521. if(g_game.getClientVersion() <= 1096) {
  522. currentPage = msg->getU16();
  523. bool hasNextPage = msg->getU8() == 1;
  524. } else {
  525. currentPage = msg->getU32();
  526. int pageCount = msg->getU32();
  527. }
  528. int entries = msg->getU8();
  529. for(int i = 0; i < entries; i++) {
  530. int time = msg->getU16();
  531. int productType = msg->getU8();
  532. int coinChange = msg->getU32();
  533. std::string productName = msg->getString();
  534. g_logger.error(stdext::format("Time %i, type %i, change %i, product name %s", time, productType, coinChange, productName));
  535. }
  536. }
  537. void ProtocolGame::parseStoreOffers(const InputMessagePtr& msg)
  538. {
  539. std::string categoryName = msg->getString();
  540. int offers = msg->getU16();
  541. for(int i = 0; i < offers; i++) {
  542. int offerId = msg->getU32();
  543. std::string offerName = msg->getString();
  544. std::string offerDescription = msg->getString();
  545. int price = msg->getU32();
  546. int highlightState = msg->getU8();
  547. if(highlightState == 2 && g_game.getFeature(Otc::GameIngameStoreHighlights) && g_game.getClientVersion() >= 1097) {
  548. int saleValidUntilTimestamp = msg->getU32();
  549. int basePrice = msg->getU32();
  550. }
  551. int disabledState = msg->getU8();
  552. std::string disabledReason = "";
  553. if(g_game.getFeature(Otc::GameIngameStoreHighlights) && disabledState == 1) {
  554. disabledReason = msg->getString();
  555. }
  556. int icons = msg->getU8();
  557. for(int j = 0; j < icons; j++) {
  558. std::string icon = msg->getString();
  559. }
  560. int subOffers = msg->getU16();
  561. for(int j = 0; j < subOffers; j++) {
  562. std::string name = msg->getString();
  563. std::string description = msg->getString();
  564. int subIcons = msg->getU8();
  565. for(int k = 0; k < subIcons; k++) {
  566. std::string icon = msg->getString();
  567. }
  568. std::string serviceType = msg->getString();
  569. }
  570. }
  571. }
  572. void ProtocolGame::parseStoreError(const InputMessagePtr& msg)
  573. {
  574. int errorType = msg->getU8();
  575. std::string message = msg->getString();
  576. g_logger.error(stdext::format("Store Error: %s [%i]", message, errorType));
  577. }
  578. void ProtocolGame::parseUnjustifiedStats(const InputMessagePtr& msg)
  579. {
  580. UnjustifiedPoints unjustifiedPoints;
  581. unjustifiedPoints.killsDay = msg->getU8();
  582. unjustifiedPoints.killsDayRemaining = msg->getU8();
  583. unjustifiedPoints.killsWeek = msg->getU8();
  584. unjustifiedPoints.killsWeekRemaining = msg->getU8();
  585. unjustifiedPoints.killsMonth = msg->getU8();
  586. unjustifiedPoints.killsMonthRemaining = msg->getU8();
  587. unjustifiedPoints.skullTime = msg->getU8();
  588. g_game.setUnjustifiedPoints(unjustifiedPoints);
  589. }
  590. void ProtocolGame::parsePvpSituations(const InputMessagePtr& msg)
  591. {
  592. uint8 openPvpSituations = msg->getU8();
  593. g_game.setOpenPvpSituations(openPvpSituations);
  594. }
  595. void ProtocolGame::parsePlayerHelpers(const InputMessagePtr& msg)
  596. {
  597. uint id = msg->getU32();
  598. int helpers = msg->getU16();
  599. CreaturePtr creature = g_map.getCreatureById(id);
  600. if(creature)
  601. g_game.processPlayerHelpers(helpers);
  602. else
  603. g_logger.traceError(stdext::format("could not get creature with id %d", id));
  604. }
  605. void ProtocolGame::parseGMActions(const InputMessagePtr& msg)
  606. {
  607. std::vector<uint8> actions;
  608. int numViolationReasons;
  609. if(g_game.getClientVersion() >= 850)
  610. numViolationReasons = 20;
  611. else if(g_game.getClientVersion() >= 840)
  612. numViolationReasons = 23;
  613. else
  614. numViolationReasons = 32;
  615. for(int i = 0; i < numViolationReasons; ++i)
  616. actions.push_back(msg->getU8());
  617. g_game.processGMActions(actions);
  618. }
  619. void ProtocolGame::parseUpdateNeeded(const InputMessagePtr& msg)
  620. {
  621. std::string signature = msg->getString();
  622. g_game.processUpdateNeeded(signature);
  623. }
  624. void ProtocolGame::parseLoginError(const InputMessagePtr& msg)
  625. {
  626. std::string error = msg->getString();
  627. g_game.processLoginError(error);
  628. }
  629. void ProtocolGame::parseLoginAdvice(const InputMessagePtr& msg)
  630. {
  631. std::string message = msg->getString();
  632. g_game.processLoginAdvice(message);
  633. }
  634. void ProtocolGame::parseLoginWait(const InputMessagePtr& msg)
  635. {
  636. std::string message = msg->getString();
  637. int time = msg->getU8();
  638. g_game.processLoginWait(message, time);
  639. }
  640. void ProtocolGame::parseLoginToken(const InputMessagePtr& msg)
  641. {
  642. bool unknown = (msg->getU8() == 0);
  643. g_game.processLoginToken(unknown);
  644. }
  645. void ProtocolGame::parsePing(const InputMessagePtr& msg)
  646. {
  647. g_game.processPing();
  648. }
  649. void ProtocolGame::parsePingBack(const InputMessagePtr& msg)
  650. {
  651. g_game.processPingBack();
  652. }
  653. void ProtocolGame::parseChallenge(const InputMessagePtr& msg)
  654. {
  655. uint timestamp = msg->getU32();
  656. uint8 random = msg->getU8();
  657. sendLoginPacket(timestamp, random);
  658. }
  659. void ProtocolGame::parseDeath(const InputMessagePtr& msg)
  660. {
  661. int penality = 100;
  662. int deathType = Otc::DeathRegular;
  663. if(g_game.getFeature(Otc::GameDeathType))
  664. deathType = msg->getU8();
  665. if(g_game.getFeature(Otc::GamePenalityOnDeath) && deathType == Otc::DeathRegular)
  666. penality = msg->getU8();
  667. g_game.processDeath(deathType, penality);
  668. }
  669. void ProtocolGame::parseMapDescription(const InputMessagePtr& msg)
  670. {
  671. Position pos = getPosition(msg);
  672. if(!m_mapKnown)
  673. m_localPlayer->setPosition(pos);
  674. g_map.setCentralPosition(pos);
  675. AwareRange range = g_map.getAwareRange();
  676. setMapDescription(msg, pos.x - range.left, pos.y - range.top, pos.z, range.horizontal(), range.vertical());
  677. if(!m_mapKnown) {
  678. g_dispatcher.addEvent([] { g_lua.callGlobalField("g_game", "onMapKnown"); });
  679. m_mapKnown = true;
  680. }
  681. g_dispatcher.addEvent([] { g_lua.callGlobalField("g_game", "onMapDescription"); });
  682. }
  683. void ProtocolGame::parseMapMoveNorth(const InputMessagePtr& msg)
  684. {
  685. Position pos;
  686. if(g_game.getFeature(Otc::GameMapMovePosition))
  687. pos = getPosition(msg);
  688. else
  689. pos = g_map.getCentralPosition();
  690. pos.y--;
  691. AwareRange range = g_map.getAwareRange();
  692. setMapDescription(msg, pos.x - range.left, pos.y - range.top, pos.z, range.horizontal(), 1);
  693. g_map.setCentralPosition(pos);
  694. }
  695. void ProtocolGame::parseMapMoveEast(const InputMessagePtr& msg)
  696. {
  697. Position pos;
  698. if(g_game.getFeature(Otc::GameMapMovePosition))
  699. pos = getPosition(msg);
  700. else
  701. pos = g_map.getCentralPosition();
  702. pos.x++;
  703. AwareRange range = g_map.getAwareRange();
  704. setMapDescription(msg, pos.x + range.right, pos.y - range.top, pos.z, 1, range.vertical());
  705. g_map.setCentralPosition(pos);
  706. }
  707. void ProtocolGame::parseMapMoveSouth(const InputMessagePtr& msg)
  708. {
  709. Position pos;
  710. if(g_game.getFeature(Otc::GameMapMovePosition))
  711. pos = getPosition(msg);
  712. else
  713. pos = g_map.getCentralPosition();
  714. pos.y++;
  715. AwareRange range = g_map.getAwareRange();
  716. setMapDescription(msg, pos.x - range.left, pos.y + range.bottom, pos.z, range.horizontal(), 1);
  717. g_map.setCentralPosition(pos);
  718. }
  719. void ProtocolGame::parseMapMoveWest(const InputMessagePtr& msg)
  720. {
  721. Position pos;
  722. if(g_game.getFeature(Otc::GameMapMovePosition))
  723. pos = getPosition(msg);
  724. else
  725. pos = g_map.getCentralPosition();
  726. pos.x--;
  727. AwareRange range = g_map.getAwareRange();
  728. setMapDescription(msg, pos.x - range.left, pos.y - range.top, pos.z, 1, range.vertical());
  729. g_map.setCentralPosition(pos);
  730. }
  731. void ProtocolGame::parseUpdateTile(const InputMessagePtr& msg)
  732. {
  733. Position tilePos = getPosition(msg);
  734. setTileDescription(msg, tilePos);
  735. }
  736. void ProtocolGame::parseTileAddThing(const InputMessagePtr& msg)
  737. {
  738. Position pos = getPosition(msg);
  739. int stackPos = -1;
  740. if(g_game.getClientVersion() >= 841)
  741. stackPos = msg->getU8();
  742. ThingPtr thing = getThing(msg);
  743. g_map.addThing(thing, pos, stackPos);
  744. }
  745. void ProtocolGame::parseTileTransformThing(const InputMessagePtr& msg)
  746. {
  747. ThingPtr thing = getMappedThing(msg);
  748. ThingPtr newThing = getThing(msg);
  749. if(!thing) {
  750. g_logger.traceError("no thing");
  751. return;
  752. }
  753. Position pos = thing->getPosition();
  754. int stackpos = thing->getStackPos();
  755. if(!g_map.removeThing(thing)) {
  756. g_logger.traceError("unable to remove thing");
  757. return;
  758. }
  759. g_map.addThing(newThing, pos, stackpos);
  760. }
  761. void ProtocolGame::parseTileRemoveThing(const InputMessagePtr& msg)
  762. {
  763. ThingPtr thing = getMappedThing(msg);
  764. if(!thing) {
  765. g_logger.traceError("no thing");
  766. return;
  767. }
  768. if(!g_map.removeThing(thing))
  769. g_logger.traceError("unable to remove thing");
  770. }
  771. void ProtocolGame::parseCreatureMove(const InputMessagePtr& msg)
  772. {
  773. ThingPtr thing = getMappedThing(msg);
  774. Position newPos = getPosition(msg);
  775. if(!thing || !thing->isCreature()) {
  776. g_logger.traceError("no creature found to move");
  777. return;
  778. }
  779. if(!g_map.removeThing(thing)) {
  780. g_logger.traceError("unable to remove creature");
  781. return;
  782. }
  783. CreaturePtr creature = thing->static_self_cast<Creature>();
  784. creature->allowAppearWalk();
  785. g_map.addThing(thing, newPos, -1);
  786. }
  787. void ProtocolGame::parseOpenContainer(const InputMessagePtr& msg)
  788. {
  789. int containerId = msg->getU8();
  790. ItemPtr containerItem = getItem(msg);
  791. std::string name = msg->getString();
  792. int capacity = msg->getU8();
  793. bool hasParent = (msg->getU8() != 0);
  794. bool isUnlocked = true;
  795. bool hasPages = false;
  796. int containerSize = 0;
  797. int firstIndex = 0;
  798. if(g_game.getFeature(Otc::GameContainerPagination)) {
  799. isUnlocked = (msg->getU8() != 0); // drag and drop
  800. hasPages = (msg->getU8() != 0); // pagination
  801. containerSize = msg->getU16(); // container size
  802. firstIndex = msg->getU16(); // first index
  803. }
  804. int itemCount = msg->getU8();
  805. std::vector<ItemPtr> items(itemCount);
  806. for(int i = 0; i < itemCount; i++)
  807. items[i] = getItem(msg);
  808. g_game.processOpenContainer(containerId, containerItem, name, capacity, hasParent, items, isUnlocked, hasPages, containerSize, firstIndex);
  809. }
  810. void ProtocolGame::parseCloseContainer(const InputMessagePtr& msg)
  811. {
  812. int containerId = msg->getU8();
  813. g_game.processCloseContainer(containerId);
  814. }
  815. void ProtocolGame::parseContainerAddItem(const InputMessagePtr& msg)
  816. {
  817. int containerId = msg->getU8();
  818. int slot = 0;
  819. if(g_game.getFeature(Otc::GameContainerPagination)) {
  820. slot = msg->getU16(); // slot
  821. }
  822. ItemPtr item = getItem(msg);
  823. g_game.processContainerAddItem(containerId, item, slot);
  824. }
  825. void ProtocolGame::parseContainerUpdateItem(const InputMessagePtr& msg)
  826. {
  827. int containerId = msg->getU8();
  828. int slot;
  829. if(g_game.getFeature(Otc::GameContainerPagination)) {
  830. slot = msg->getU16();
  831. } else {
  832. slot = msg->getU8();
  833. }
  834. ItemPtr item = getItem(msg);
  835. g_game.processContainerUpdateItem(containerId, slot, item);
  836. }
  837. void ProtocolGame::parseContainerRemoveItem(const InputMessagePtr& msg)
  838. {
  839. int containerId = msg->getU8();
  840. int slot;
  841. ItemPtr lastItem;
  842. if(g_game.getFeature(Otc::GameContainerPagination)) {
  843. slot = msg->getU16();
  844. int itemId = msg->getU16();
  845. if(itemId != 0)
  846. lastItem = getItem(msg, itemId);
  847. } else {
  848. slot = msg->getU8();
  849. }
  850. g_game.processContainerRemoveItem(containerId, slot, lastItem);
  851. }
  852. void ProtocolGame::parseAddInventoryItem(const InputMessagePtr& msg)
  853. {
  854. int slot = msg->getU8();
  855. ItemPtr item = getItem(msg);
  856. g_game.processInventoryChange(slot, item);
  857. }
  858. void ProtocolGame::parseRemoveInventoryItem(const InputMessagePtr& msg)
  859. {
  860. int slot = msg->getU8();
  861. g_game.processInventoryChange(slot, ItemPtr());
  862. }
  863. void ProtocolGame::parseOpenNpcTrade(const InputMessagePtr& msg)
  864. {
  865. std::vector<std::tuple<ItemPtr, std::string, int, int, int>> items;
  866. std::string npcName;
  867. if(g_game.getFeature(Otc::GameNameOnNpcTrade))
  868. npcName = msg->getString();
  869. int listCount;
  870. if(g_game.getClientVersion() >= 900)
  871. listCount = msg->getU16();
  872. else
  873. listCount = msg->getU8();
  874. for(int i = 0; i < listCount; ++i) {
  875. uint16 itemId = msg->getU16();
  876. uint8 count = msg->getU8();
  877. ItemPtr item = Item::create(itemId);
  878. item->setCountOrSubType(count);
  879. std::string name = msg->getString();
  880. int weight = msg->getU32();
  881. int buyPrice = msg->getU32();
  882. int sellPrice = msg->getU32();
  883. items.push_back(std::make_tuple(item, name, weight, buyPrice, sellPrice));
  884. }
  885. g_game.processOpenNpcTrade(items);
  886. }
  887. void ProtocolGame::parsePlayerGoods(const InputMessagePtr& msg)
  888. {
  889. std::vector<std::tuple<ItemPtr, int>> goods;
  890. int money;
  891. if(g_game.getClientVersion() >= 973)
  892. money = msg->getU64();
  893. else
  894. money = msg->getU32();
  895. int size = msg->getU8();
  896. for(int i = 0; i < size; i++) {
  897. int itemId = msg->getU16();
  898. int amount;
  899. if(g_game.getFeature(Otc::GameDoubleShopSellAmount))
  900. amount = msg->getU16();
  901. else
  902. amount = msg->getU8();
  903. goods.push_back(std::make_tuple(Item::create(itemId), amount));
  904. }
  905. g_game.processPlayerGoods(money, goods);
  906. }
  907. void ProtocolGame::parseCloseNpcTrade(const InputMessagePtr&)
  908. {
  909. g_game.processCloseNpcTrade();
  910. }
  911. void ProtocolGame::parseOwnTrade(const InputMessagePtr& msg)
  912. {
  913. std::string name = g_game.formatCreatureName(msg->getString());
  914. int count = msg->getU8();
  915. std::vector<ItemPtr> items(count);
  916. for(int i = 0; i < count; i++)
  917. items[i] = getItem(msg);
  918. g_game.processOwnTrade(name, items);
  919. }
  920. void ProtocolGame::parseCounterTrade(const InputMessagePtr& msg)
  921. {
  922. std::string name = g_game.formatCreatureName(msg->getString());
  923. int count = msg->getU8();
  924. std::vector<ItemPtr> items(count);
  925. for(int i = 0; i < count; i++)
  926. items[i] = getItem(msg);
  927. g_game.processCounterTrade(name, items);
  928. }
  929. void ProtocolGame::parseCloseTrade(const InputMessagePtr&)
  930. {
  931. g_game.processCloseTrade();
  932. }
  933. void ProtocolGame::parseWorldLight(const InputMessagePtr& msg)
  934. {
  935. Light light;
  936. light.intensity = msg->getU8();
  937. light.color = msg->getU8();
  938. g_map.setLight(light);
  939. }
  940. void ProtocolGame::parseMagicEffect(const InputMessagePtr& msg)
  941. {
  942. Position pos = getPosition(msg);
  943. int effectId;
  944. if(g_game.getFeature(Otc::GameMagicEffectU16))
  945. effectId = msg->getU16();
  946. else
  947. effectId = msg->getU8();
  948. if(!g_things.isValidDatId(effectId, ThingCategoryEffect)) {
  949. g_logger.traceError(stdext::format("invalid effect id %d", effectId));
  950. return;
  951. }
  952. EffectPtr effect = EffectPtr(new Effect());
  953. effect->setId(effectId);
  954. g_map.addThing(effect, pos);
  955. }
  956. void ProtocolGame::parseAnimatedText(const InputMessagePtr& msg)
  957. {
  958. Position position = getPosition(msg);
  959. int color = msg->getU8();
  960. std::string text = msg->getString();
  961. AnimatedTextPtr animatedText = AnimatedTextPtr(new AnimatedText);
  962. animatedText->setColor(color);
  963. animatedText->setText(text);
  964. g_map.addThing(animatedText, position);
  965. }
  966. void ProtocolGame::parseDistanceMissile(const InputMessagePtr& msg)
  967. {
  968. Position fromPos = getPosition(msg);
  969. Position toPos = getPosition(msg);
  970. int shotId = msg->getU8();
  971. if(!g_things.isValidDatId(shotId, ThingCategoryMissile)) {
  972. g_logger.traceError(stdext::format("invalid missile id %d", shotId));
  973. return;
  974. }
  975. MissilePtr missile = MissilePtr(new Missile());
  976. missile->setId(shotId);
  977. missile->setPath(fromPos, toPos);
  978. g_map.addThing(missile, fromPos);
  979. }
  980. void ProtocolGame::parseCreatureMark(const InputMessagePtr& msg)
  981. {
  982. uint id = msg->getU32();
  983. int color = msg->getU8();
  984. CreaturePtr creature = g_map.getCreatureById(id);
  985. if(creature)
  986. creature->addTimedSquare(color);
  987. else
  988. g_logger.traceError("could not get creature");
  989. }
  990. void ProtocolGame::parseTrappers(const InputMessagePtr& msg)
  991. {
  992. int numTrappers = msg->getU8();
  993. if(numTrappers > 8)
  994. g_logger.traceError("too many trappers");
  995. for(int i=0;i<numTrappers;++i) {
  996. uint id = msg->getU32();
  997. CreaturePtr creature = g_map.getCreatureById(id);
  998. if(creature) {
  999. //TODO: set creature as trapper
  1000. } else
  1001. g_logger.traceError("could not get creature");
  1002. }
  1003. }
  1004. void ProtocolGame::parseCreatureHealth(const InputMessagePtr& msg)
  1005. {
  1006. uint id = msg->getU32();
  1007. int healthPercent = msg->getU8();
  1008. CreaturePtr creature = g_map.getCreatureById(id);
  1009. if(creature)
  1010. creature->setHealthPercent(healthPercent);
  1011. // some servers has a bug in get spectators and sends unknown creatures updates
  1012. // so this code is disabled
  1013. /*
  1014. else
  1015. g_logger.traceError("could not get creature");
  1016. */
  1017. }
  1018. void ProtocolGame::parseCreatureLight(const InputMessagePtr& msg)
  1019. {
  1020. uint id = msg->getU32();
  1021. Light light;
  1022. light.intensity = msg->getU8();
  1023. light.color = msg->getU8();
  1024. CreaturePtr creature = g_map.getCreatureById(id);
  1025. if(creature)
  1026. creature->setLight(light);
  1027. else
  1028. g_logger.traceError("could not get creature");
  1029. }
  1030. void ProtocolGame::parseCreatureOutfit(const InputMessagePtr& msg)
  1031. {
  1032. uint id = msg->getU32();
  1033. Outfit outfit = getOutfit(msg);
  1034. CreaturePtr creature = g_map.getCreatureById(id);
  1035. if(creature)
  1036. creature->setOutfit(outfit);
  1037. else
  1038. g_logger.traceError("could not get creature");
  1039. }
  1040. void ProtocolGame::parseCreatureSpeed(const InputMessagePtr& msg)
  1041. {
  1042. uint id = msg->getU32();
  1043. int baseSpeed = -1;
  1044. if(g_game.getClientVersion() >= 1059)
  1045. baseSpeed = msg->getU16();
  1046. int speed = msg->getU16();
  1047. CreaturePtr creature = g_map.getCreatureById(id);
  1048. if(creature) {
  1049. creature->setSpeed(speed);
  1050. if(baseSpeed != -1)
  1051. creature->setBaseSpeed(baseSpeed);
  1052. }
  1053. // some servers has a bug in get spectators and sends unknown creatures updates
  1054. // so this code is disabled
  1055. /*
  1056. else
  1057. g_logger.traceError("could not get creature");
  1058. */
  1059. }
  1060. void ProtocolGame::parseCreatureSkulls(const InputMessagePtr& msg)
  1061. {
  1062. uint id = msg->getU32();
  1063. int skull = msg->getU8();
  1064. CreaturePtr creature = g_map.getCreatureById(id);
  1065. if(creature)
  1066. creature->setSkull(skull);
  1067. else
  1068. g_logger.traceError("could not get creature");
  1069. }
  1070. void ProtocolGame::parseCreatureShields(const InputMessagePtr& msg)
  1071. {
  1072. uint id = msg->getU32();
  1073. int shield = msg->getU8();
  1074. CreaturePtr creature = g_map.getCreatureById(id);
  1075. if(creature)
  1076. creature->setShield(shield);
  1077. else
  1078. g_logger.traceError("could not get creature");
  1079. }
  1080. void ProtocolGame::parseCreatureUnpass(const InputMessagePtr& msg)
  1081. {
  1082. uint id = msg->getU32();
  1083. bool unpass = msg->getU8();
  1084. CreaturePtr creature = g_map.getCreatureById(id);
  1085. if(creature)
  1086. creature->setPassable(!unpass);
  1087. else
  1088. g_logger.traceError("could not get creature");
  1089. }
  1090. void ProtocolGame::parseEditText(const InputMessagePtr& msg)
  1091. {
  1092. uint id = msg->getU32();
  1093. int itemId;
  1094. if(g_game.getClientVersion() >= 1010) {
  1095. // TODO: processEditText with ItemPtr as parameter
  1096. ItemPtr item = getItem(msg);
  1097. itemId = item->getId();
  1098. } else
  1099. itemId = msg->getU16();
  1100. int maxLength = msg->getU16();
  1101. std::string text = msg->getString();
  1102. std::string writer = msg->getString();
  1103. std::string date = "";
  1104. if(g_game.getFeature(Otc::GameWritableDate))
  1105. date = msg->getString();
  1106. g_game.processEditText(id, itemId, maxLength, text, writer, date);
  1107. }
  1108. void ProtocolGame::parseEditList(const InputMessagePtr& msg)
  1109. {
  1110. int doorId = msg->getU8();
  1111. uint id = msg->getU32();
  1112. const std::string& text = msg->getString();
  1113. g_game.processEditList(id, doorId, text);
  1114. }
  1115. void ProtocolGame::parsePremiumTrigger(const InputMessagePtr& msg)
  1116. {
  1117. int triggerCount = msg->getU8();
  1118. std::vector<int> triggers;
  1119. for(int i=0;i<triggerCount;++i) {
  1120. triggers.push_back(msg->getU8());
  1121. }
  1122. if(g_game.getClientVersion() <= 1096) {
  1123. bool something = msg->getU8() == 1;
  1124. }
  1125. }
  1126. void ProtocolGame::parsePlayerInfo(const InputMessagePtr& msg)
  1127. {
  1128. bool premium = msg->getU8(); // premium
  1129. if(g_game.getFeature(Otc::GamePremiumExpiration))
  1130. int premiumEx = msg->getU32(); // premium expiration used for premium advertisement
  1131. int vocation = msg->getU8(); // vocation
  1132. int spellCount = msg->getU16();
  1133. std::vector<int> spells;
  1134. for(int i=0;i<spellCount;++i)
  1135. spells.push_back(msg->getU8()); // spell id
  1136. m_localPlayer->setPremium(premium);
  1137. m_localPlayer->setVocation(vocation);
  1138. m_localPlayer->setSpells(spells);
  1139. }
  1140. void ProtocolGame::parsePlayerStats(const InputMessagePtr& msg)
  1141. {
  1142. double health;
  1143. double maxHealth;
  1144. if(g_game.getFeature(Otc::GameDoubleHealth)) {
  1145. health = msg->getU32();
  1146. maxHealth = msg->getU32();
  1147. } else {
  1148. health = msg->getU16();
  1149. maxHealth = msg->getU16();
  1150. }
  1151. double freeCapacity;
  1152. if(g_game.getFeature(Otc::GameDoubleFreeCapacity))
  1153. freeCapacity = msg->getU32() / 100.0;
  1154. else
  1155. freeCapacity = msg->getU16() / 100.0;
  1156. double totalCapacity = 0;
  1157. if(g_game.getFeature(Otc::GameTotalCapacity))
  1158. totalCapacity = msg->getU32() / 100.0;
  1159. double experience;
  1160. if(g_game.getFeature(Otc::GameDoubleExperience))
  1161. experience = msg->getU64();
  1162. else
  1163. experience = msg->getU32();
  1164. double level = msg->getU16();
  1165. double levelPercent = msg->getU8();
  1166. if(g_game.getFeature(Otc::GameExperienceBonus)) {
  1167. if(g_game.getClientVersion() <= 1096) {
  1168. double experienceBonus = msg->getDouble();
  1169. } else {
  1170. int baseXpGain = msg->getU16();
  1171. int voucherAddend = msg->getU16();
  1172. int grindingAddend = msg->getU16();
  1173. int storeBoostAddend = msg->getU16();
  1174. int huntingBoostFactor = msg->getU16();
  1175. }
  1176. }
  1177. double mana;
  1178. double maxMana;
  1179. if(g_game.getFeature(Otc::GameDoubleHealth)) {
  1180. mana = msg->getU32();
  1181. maxMana = msg->getU32();
  1182. } else {
  1183. mana = msg->getU16();
  1184. maxMana = msg->getU16();
  1185. }
  1186. double magicLevel = msg->getU8();
  1187. double baseMagicLevel;
  1188. if(g_game.getFeature(Otc::GameSkillsBase))
  1189. baseMagicLevel = msg->getU8();
  1190. else
  1191. baseMagicLevel = magicLevel;
  1192. double magicLevelPercent = msg->getU8();
  1193. double soul = msg->getU8();
  1194. double stamina = 0;
  1195. if(g_game.getFeature(Otc::GamePlayerStamina))
  1196. stamina = msg->getU16();
  1197. double baseSpeed = 0;
  1198. if(g_game.getFeature(Otc::GameSkillsBase))
  1199. baseSpeed = msg->getU16();
  1200. double regeneration = 0;
  1201. if(g_game.getFeature(Otc::GamePlayerRegenerationTime))
  1202. regeneration = msg->getU16();
  1203. double training = 0;
  1204. if(g_game.getFeature(Otc::GameOfflineTrainingTime)) {
  1205. training = msg->getU16();
  1206. if(g_game.getClientVersion() >= 1097) {
  1207. int remainingStoreXpBoostSeconds = msg->getU16();
  1208. bool canBuyMoreStoreXpBoosts = msg->getU8();
  1209. }
  1210. }
  1211. m_localPlayer->setHealth(health, maxHealth);
  1212. m_localPlayer->setFreeCapacity(freeCapacity);
  1213. m_localPlayer->setTotalCapacity(totalCapacity);
  1214. m_localPlayer->setExperience(experience);
  1215. m_localPlayer->setLevel(level, levelPercent);
  1216. m_localPlayer->setMana(mana, maxMana);
  1217. m_localPlayer->setMagicLevel(magicLevel, magicLevelPercent);
  1218. m_localPlayer->setBaseMagicLevel(baseMagicLevel);
  1219. m_localPlayer->setStamina(stamina);
  1220. m_localPlayer->setSoul(soul);
  1221. m_localPlayer->setBaseSpeed(baseSpeed);
  1222. m_localPlayer->setRegenerationTime(regeneration);
  1223. m_localPlayer->setOfflineTrainingTime(training);
  1224. }
  1225. void ProtocolGame::parsePlayerSkills(const InputMessagePtr& msg)
  1226. {
  1227. int lastSkill = Otc::Fishing + 1;
  1228. if(g_game.getFeature(Otc::GameAdditionalSkills))
  1229. lastSkill = Otc::LastSkill;
  1230. for(int skill = 0; skill < lastSkill; skill++) {
  1231. int level;
  1232. if(g_game.getFeature(Otc::GameDoubleSkills))
  1233. level = msg->getU16();
  1234. else
  1235. level = msg->getU8();
  1236. int baseLevel;
  1237. if(g_game.getFeature(Otc::GameSkillsBase))
  1238. if(g_game.getFeature(Otc::GameBaseSkillU16))
  1239. baseLevel = msg->getU16();
  1240. else
  1241. baseLevel = msg->getU8();
  1242. else
  1243. baseLevel = level;
  1244. int levelPercent = 0;
  1245. // Critical, Life Leech and Mana Leech have no level percent
  1246. if(skill <= Otc::Fishing)
  1247. levelPercent = msg->getU8();
  1248. m_localPlayer->setSkill((Otc::Skill)skill, level, levelPercent);
  1249. m_localPlayer->setBaseSkill((Otc::Skill)skill, baseLevel);
  1250. }
  1251. }
  1252. void ProtocolGame::parsePlayerState(const InputMessagePtr& msg)
  1253. {
  1254. int states;
  1255. if(g_game.getFeature(Otc::GamePlayerStateU16))
  1256. states = msg->getU16();
  1257. else
  1258. states = msg->getU8();
  1259. m_localPlayer->setStates(states);
  1260. }
  1261. void ProtocolGame::parsePlayerCancelAttack(const InputMessagePtr& msg)
  1262. {
  1263. uint seq = 0;
  1264. if(g_game.getFeature(Otc::GameAttackSeq))
  1265. seq = msg->getU32();
  1266. g_game.processAttackCancel(seq);
  1267. }
  1268. void ProtocolGame::parsePlayerModes(const InputMessagePtr& msg)
  1269. {
  1270. int fightMode = msg->getU8();
  1271. int chaseMode = msg->getU8();
  1272. bool safeMode = msg->getU8();
  1273. int pvpMode = 0;
  1274. if(g_game.getFeature(Otc::GamePVPMode))
  1275. pvpMode = msg->getU8();
  1276. g_game.processPlayerModes((Otc::FightModes)fightMode, (Otc::ChaseModes)chaseMode, safeMode, (Otc::PVPModes)pvpMode);
  1277. }
  1278. void ProtocolGame::parseSpellCooldown(const InputMessagePtr& msg)
  1279. {
  1280. int spellId = msg->getU8();
  1281. int delay = msg->getU32();
  1282. g_lua.callGlobalField("g_game", "onSpellCooldown", spellId, delay);
  1283. }
  1284. void ProtocolGame::parseSpellGroupCooldown(const InputMessagePtr& msg)
  1285. {
  1286. int groupId = msg->getU8();
  1287. int delay = msg->getU32();
  1288. g_lua.callGlobalField("g_game", "onSpellGroupCooldown", groupId, delay);
  1289. }
  1290. void ProtocolGame::parseMultiUseCooldown(const InputMessagePtr& msg)
  1291. {
  1292. int delay = msg->getU32();
  1293. g_lua.callGlobalField("g_game", "onMultiUseCooldown", delay);
  1294. }
  1295. void ProtocolGame::parseTalk(const InputMessagePtr& msg)
  1296. {
  1297. if(g_game.getFeature(Otc::GameMessageStatements))
  1298. msg->getU32(); // channel statement guid
  1299. std::string name = g_game.formatCreatureName(msg->getString());
  1300. int level = 0;
  1301. if(g_game.getFeature(Otc::GameMessageLevel))
  1302. level = msg->getU16();
  1303. Otc::MessageMode mode = Proto::translateMessageModeFromServer(msg->getU8());
  1304. int channelId = 0;
  1305. Position pos;
  1306. switch(mode) {
  1307. case Otc::MessageSay:
  1308. case Otc::MessageWhisper:
  1309. case Otc::MessageYell:
  1310. case Otc::MessageMonsterSay:
  1311. case Otc::MessageMonsterYell:
  1312. case Otc::MessageNpcTo:
  1313. case Otc::MessageBarkLow:
  1314. case Otc::MessageBarkLoud:
  1315. case Otc::MessageSpell:
  1316. case Otc::MessageNpcFromStartBlock:
  1317. pos = getPosition(msg);
  1318. break;
  1319. case Otc::MessageChannel:
  1320. case Otc::MessageChannelManagement:
  1321. case Otc::MessageChannelHighlight:
  1322. case Otc::MessageGamemasterChannel:
  1323. channelId = msg->getU16();
  1324. break;
  1325. case Otc::MessageNpcFrom:
  1326. case Otc::MessagePrivateFrom:
  1327. case Otc::MessageGamemasterBroadcast:
  1328. case Otc::MessageGamemasterPrivateFrom:
  1329. case Otc::MessageRVRAnswer:
  1330. case Otc::MessageRVRContinue:
  1331. break;
  1332. case Otc::MessageRVRChannel:
  1333. msg->getU32();
  1334. break;
  1335. default:
  1336. stdext::throw_exception(stdext::format("unknown message mode %d", mode));
  1337. break;
  1338. }
  1339. std::string text = msg->getString();
  1340. g_game.processTalk(name, level, mode, text, channelId, pos);
  1341. }
  1342. void ProtocolGame::parseChannelList(const InputMessagePtr& msg)
  1343. {
  1344. int count = msg->getU8();
  1345. std::vector<std::tuple<int, std::string> > channelList;
  1346. for(int i = 0; i < count; i++) {
  1347. int id = msg->getU16();
  1348. std::string name = msg->getString();
  1349. channelList.push_back(std::make_tuple(id, name));
  1350. }
  1351. g_game.processChannelList(channelList);
  1352. }
  1353. void ProtocolGame::parseOpenChannel(const InputMessagePtr& msg)
  1354. {
  1355. int channelId = msg->getU16();
  1356. std::string name = msg->getString();
  1357. if(g_game.getFeature(Otc::GameChannelPlayerList)) {
  1358. int joinedPlayers = msg->getU16();
  1359. for(int i=0;i<joinedPlayers;++i)
  1360. g_game.formatCreatureName(msg->getString()); // player name
  1361. int invitedPlayers = msg->getU16();
  1362. for(int i=0;i<invitedPlayers;++i)
  1363. g_game.formatCreatureName(msg->getString()); // player name
  1364. }
  1365. g_game.processOpenChannel(channelId, name);
  1366. }
  1367. void ProtocolGame::parseOpenPrivateChannel(const InputMessagePtr& msg)
  1368. {
  1369. std::string name = g_game.formatCreatureName(msg->getString());
  1370. g_game.processOpenPrivateChannel(name);
  1371. }
  1372. void ProtocolGame::parseOpenOwnPrivateChannel(const InputMessagePtr& msg)
  1373. {
  1374. int channelId = msg->getU16();
  1375. std::string name = msg->getString();
  1376. g_game.processOpenOwnPrivateChannel(channelId, name);
  1377. }
  1378. void ProtocolGame::parseCloseChannel(const InputMessagePtr& msg)
  1379. {
  1380. int channelId = msg->getU16();
  1381. g_game.processCloseChannel(channelId);
  1382. }
  1383. void ProtocolGame::parseRuleViolationChannel(const InputMessagePtr& msg)
  1384. {
  1385. int channelId = msg->getU16();
  1386. g_game.processRuleViolationChannel(channelId);
  1387. }
  1388. void ProtocolGame::parseRuleViolationRemove(const InputMessagePtr& msg)
  1389. {
  1390. std::string name = msg->getString();
  1391. g_game.processRuleViolationRemove(name);
  1392. }
  1393. void ProtocolGame::parseRuleViolationCancel(const InputMessagePtr& msg)
  1394. {
  1395. std::string name = msg->getString();
  1396. g_game.processRuleViolationCancel(name);
  1397. }
  1398. void ProtocolGame::parseRuleViolationLock(const InputMessagePtr& msg)
  1399. {
  1400. g_game.processRuleViolationLock();
  1401. }
  1402. void ProtocolGame::parseTextMessage(const InputMessagePtr& msg)
  1403. {
  1404. int code = msg->getU8();
  1405. Otc::MessageMode mode = Proto::translateMessageModeFromServer(code);
  1406. std::string text;
  1407. switch(mode) {
  1408. case Otc::MessageChannelManagement: {
  1409. int channel = msg->getU16();
  1410. text = msg->getString();
  1411. break;
  1412. }
  1413. case Otc::MessageGuild:
  1414. case Otc::MessagePartyManagement:
  1415. case Otc::MessageParty: {
  1416. int channel = msg->getU16();
  1417. text = msg->getString();
  1418. break;
  1419. }
  1420. case Otc::MessageDamageDealed:
  1421. case Otc::MessageDamageReceived:
  1422. case Otc::MessageDamageOthers: {
  1423. Position pos = getPosition(msg);
  1424. uint value[2];
  1425. int color[2];
  1426. // physical damage
  1427. value[0] = msg->getU32();
  1428. color[0] = msg->getU8();
  1429. // magic damage
  1430. value[1] = msg->getU32();
  1431. color[1] = msg->getU8();
  1432. text = msg->getString();
  1433. for(int i=0;i<2;++i) {
  1434. if(value[i] == 0)
  1435. continue;
  1436. AnimatedTextPtr animatedText = AnimatedTextPtr(new AnimatedText);
  1437. animatedText->setColor(color[i]);
  1438. animatedText->setText(stdext::to_string(value[i]));
  1439. g_map.addThing(animatedText, pos);
  1440. }
  1441. break;
  1442. }
  1443. case Otc::MessageHeal:
  1444. case Otc::MessageMana:
  1445. case Otc::MessageExp:
  1446. case Otc::MessageHealOthers:
  1447. case Otc::MessageExpOthers: {
  1448. Position pos = getPosition(msg);
  1449. uint value = msg->getU32();
  1450. int color = msg->getU8();
  1451. text = msg->getString();
  1452. AnimatedTextPtr animatedText = AnimatedTextPtr(new AnimatedText);
  1453. animatedText->setColor(color);
  1454. animatedText->setText(stdext::to_string(value));
  1455. g_map.addThing(animatedText, pos);
  1456. break;
  1457. }
  1458. case Otc::MessageInvalid:
  1459. stdext::throw_exception(stdext::format("unknown message mode %d", mode));
  1460. break;
  1461. default:
  1462. text = msg->getString();
  1463. break;
  1464. }
  1465. g_game.processTextMessage(mode, text);
  1466. }
  1467. void ProtocolGame::parseCancelWalk(const InputMessagePtr& msg)
  1468. {
  1469. Otc::Direction direction = (Otc::Direction)msg->getU8();
  1470. g_game.processWalkCancel(direction);
  1471. }
  1472. void ProtocolGame::parseWalkWait(const InputMessagePtr& msg)
  1473. {
  1474. int millis = msg->getU16();
  1475. m_localPlayer->lockWalk(millis);
  1476. }
  1477. void ProtocolGame::parseFloorChangeUp(const InputMessagePtr& msg)
  1478. {
  1479. Position pos;
  1480. if(g_game.getFeature(Otc::GameMapMovePosition))
  1481. pos = getPosition(msg);
  1482. else
  1483. pos = g_map.getCentralPosition();
  1484. AwareRange range = g_map.getAwareRange();
  1485. pos.z--;
  1486. int skip = 0;
  1487. if(pos.z == Otc::SEA_FLOOR)
  1488. for(int i = Otc::SEA_FLOOR - Otc::AWARE_UNDEGROUND_FLOOR_RANGE; i >= 0; i--)
  1489. skip = setFloorDescription(msg, pos.x - range.left, pos.y - range.top, i, range.horizontal(), range.vertical(), 8 - i, skip);
  1490. else if(pos.z > Otc::SEA_FLOOR)
  1491. skip = setFloorDescription(msg, pos.x - range.left, pos.y - range.top, pos.z - Otc::AWARE_UNDEGROUND_FLOOR_RANGE, range.horizontal(), range.vertical(), 3, skip);
  1492. pos.x++;
  1493. pos.y++;
  1494. g_map.setCentralPosition(pos);
  1495. }
  1496. void ProtocolGame::parseFloorChangeDown(const InputMessagePtr& msg)
  1497. {
  1498. Position pos;
  1499. if(g_game.getFeature(Otc::GameMapMovePosition))
  1500. pos = getPosition(msg);
  1501. else
  1502. pos = g_map.getCentralPosition();
  1503. AwareRange range = g_map.getAwareRange();
  1504. pos.z++;
  1505. int skip = 0;
  1506. if(pos.z == Otc::UNDERGROUND_FLOOR) {
  1507. int j, i;
  1508. for(i = pos.z, j = -1; i <= pos.z + Otc::AWARE_UNDEGROUND_FLOOR_RANGE; ++i, --j)
  1509. skip = setFloorDescription(msg, pos.x - range.left, pos.y - range.top, i, range.horizontal(), range.vertical(), j, skip);
  1510. }
  1511. else if(pos.z > Otc::UNDERGROUND_FLOOR && pos.z < Otc::MAX_Z-1)
  1512. skip = setFloorDescription(msg, pos.x - range.left, pos.y - range.top, pos.z + Otc::AWARE_UNDEGROUND_FLOOR_RANGE, range.horizontal(), range.vertical(), -3, skip);
  1513. pos.x--;
  1514. pos.y--;
  1515. g_map.setCentralPosition(pos);
  1516. }
  1517. void ProtocolGame::parseOpenOutfitWindow(const InputMessagePtr& msg)
  1518. {
  1519. Outfit currentOutfit = getOutfit(msg);
  1520. std::vector<std::tuple<int, std::string, int> > outfitList;
  1521. if(g_game.getFeature(Otc::GameNewOutfitProtocol)) {
  1522. int outfitCount = msg->getU8();
  1523. for(int i = 0; i < outfitCount; i++) {
  1524. int outfitId = msg->getU16();
  1525. std::string outfitName = msg->getString();
  1526. int outfitAddons = msg->getU8();
  1527. outfitList.push_back(std::make_tuple(outfitId, outfitName, outfitAddons));
  1528. }
  1529. } else {
  1530. int outfitStart, outfitEnd;
  1531. if(g_game.getFeature(Otc::GameLooktypeU16)) {
  1532. outfitStart = msg->getU16();
  1533. outfitEnd = msg->getU16();
  1534. } else {
  1535. outfitStart = msg->getU8();
  1536. outfitEnd = msg->getU8();
  1537. }
  1538. for(int i = outfitStart; i <= outfitEnd; i++)
  1539. outfitList.push_back(std::make_tuple(i, "", 0));
  1540. }
  1541. std::vector<std::tuple<int, std::string> > mountList;
  1542. if(g_game.getFeature(Otc::GamePlayerMounts)) {
  1543. int mountCount = msg->getU8();
  1544. for(int i = 0; i < mountCount; ++i) {
  1545. int mountId = msg->getU16(); // mount type
  1546. std::string mountName = msg->getString(); // mount name
  1547. mountList.push_back(std::make_tuple(mountId, mountName));
  1548. }
  1549. }
  1550. g_game.processOpenOutfitWindow(currentOutfit, outfitList, mountList);
  1551. }
  1552. void ProtocolGame::parseVipAdd(const InputMessagePtr& msg)
  1553. {
  1554. uint id, iconId = 0, status;
  1555. std::string name, desc = "";
  1556. bool notifyLogin = false;
  1557. id = msg->getU32();
  1558. name = g_game.formatCreatureName(msg->getString());
  1559. if(g_game.getFeature(Otc::GameAdditionalVipInfo)) {
  1560. desc = msg->getString();
  1561. iconId = msg->getU32();
  1562. notifyLogin = msg->getU8();
  1563. }
  1564. status = msg->getU8();
  1565. g_game.processVipAdd(id, name, status, desc, iconId, notifyLogin);
  1566. }
  1567. void ProtocolGame::parseVipState(const InputMessagePtr& msg)
  1568. {
  1569. uint id = msg->getU32();
  1570. if(g_game.getFeature(Otc::GameLoginPending)) {
  1571. uint status = msg->getU8();
  1572. g_game.processVipStateChange(id, status);
  1573. }
  1574. else {
  1575. g_game.processVipStateChange(id, 1);
  1576. }
  1577. }
  1578. void ProtocolGame::parseVipLogout(const InputMessagePtr& msg)
  1579. {
  1580. uint id = msg->getU32();
  1581. g_game.processVipStateChange(id, 0);
  1582. }
  1583. void ProtocolGame::parseTutorialHint(const InputMessagePtr& msg)
  1584. {
  1585. int id = msg->getU8();
  1586. g_game.processTutorialHint(id);
  1587. }
  1588. void ProtocolGame::parseAutomapFlag(const InputMessagePtr& msg)
  1589. {
  1590. Position pos = getPosition(msg);
  1591. int icon = msg->getU8();
  1592. std::string description = msg->getString();
  1593. bool remove = false;
  1594. if(g_game.getFeature(Otc::GameMinimapRemove))
  1595. remove = msg->getU8() != 0;
  1596. if(!remove)
  1597. g_game.processAddAutomapFlag(pos, icon, description);
  1598. else
  1599. g_game.processRemoveAutomapFlag(pos, icon, description);
  1600. }
  1601. void ProtocolGame::parseQuestLog(const InputMessagePtr& msg)
  1602. {
  1603. std::vector<std::tuple<int, std::string, bool> > questList;
  1604. int questsCount = msg->getU16();
  1605. for(int i = 0; i < questsCount; i++) {
  1606. int id = msg->getU16();
  1607. std::string name = msg->getString();
  1608. bool completed = msg->getU8();
  1609. questList.push_back(std::make_tuple(id, name, completed));
  1610. }
  1611. g_game.processQuestLog(questList);
  1612. }
  1613. void ProtocolGame::parseQuestLine(const InputMessagePtr& msg)
  1614. {
  1615. std::vector<std::tuple<std::string, std::string>> questMissions;
  1616. int questId = msg->getU16();
  1617. int missionCount = msg->getU8();
  1618. for(int i = 0; i < missionCount; i++) {
  1619. std::string missionName = msg->getString();
  1620. std::string missionDescrition = msg->getString();
  1621. questMissions.push_back(std::make_tuple(missionName, missionDescrition));
  1622. }
  1623. g_game.processQuestLine(questId, questMissions);
  1624. }
  1625. void ProtocolGame::parseChannelEvent(const InputMessagePtr& msg)
  1626. {
  1627. msg->getU16(); // channel id
  1628. g_game.formatCreatureName(msg->getString()); // player name
  1629. msg->getU8(); // event type
  1630. }
  1631. void ProtocolGame::parseItemInfo(const InputMessagePtr& msg)
  1632. {
  1633. std::vector<std::tuple<ItemPtr, std::string>> list;
  1634. int size = msg->getU8();
  1635. for(int i=0;i<size;++i) {
  1636. ItemPtr item(new Item);
  1637. item->setId(msg->getU16());
  1638. item->setCountOrSubType(msg->getU8());
  1639. std::string desc = msg->getString();
  1640. list.push_back(std::make_tuple(item, desc));
  1641. }
  1642. g_lua.callGlobalField("g_game", "onItemInfo", list);
  1643. }
  1644. void ProtocolGame::parsePlayerInventory(const InputMessagePtr& msg)
  1645. {
  1646. int size = msg->getU16();
  1647. for(int i=0;i<size;++i) {
  1648. msg->getU16(); // id
  1649. msg->getU8(); // subtype
  1650. msg->getU16(); // count
  1651. }
  1652. }
  1653. void ProtocolGame::parseModalDialog(const InputMessagePtr& msg)
  1654. {
  1655. uint32 id = msg->getU32();
  1656. std::string title = msg->getString();
  1657. std::string message = msg->getString();
  1658. int sizeButtons = msg->getU8();
  1659. std::vector<std::tuple<int, std::string> > buttonList;
  1660. for(int i = 0; i < sizeButtons; ++i) {
  1661. std::string value = msg->getString();
  1662. int id = msg->getU8();
  1663. buttonList.push_back(std::make_tuple(id, value));
  1664. }
  1665. int sizeChoices = msg->getU8();
  1666. std::vector<std::tuple<int, std::string> > choiceList;
  1667. for(int i = 0; i < sizeChoices; ++i) {
  1668. std::string value = msg->getString();
  1669. int id = msg->getU8();
  1670. choiceList.push_back(std::make_tuple(id, value));
  1671. }
  1672. int enterButton, escapeButton;
  1673. if(g_game.getClientVersion() > 970) {
  1674. escapeButton = msg->getU8();
  1675. enterButton = msg->getU8();
  1676. }
  1677. else {
  1678. enterButton = msg->getU8();
  1679. escapeButton = msg->getU8();
  1680. }
  1681. bool priority = msg->getU8() == 0x01;
  1682. g_game.processModalDialog(id, title, message, buttonList, enterButton, escapeButton, choiceList, priority);
  1683. }
  1684. void ProtocolGame::parseExtendedOpcode(const InputMessagePtr& msg)
  1685. {
  1686. int opcode = msg->getU8();
  1687. std::string buffer = msg->getString();
  1688. if(opcode == 0)
  1689. m_enableSendExtendedOpcode = true;
  1690. else if(opcode == 2)
  1691. parsePingBack(msg);
  1692. else
  1693. callLuaField("onExtendedOpcode", opcode, buffer);
  1694. }
  1695. void ProtocolGame::parseChangeMapAwareRange(const InputMessagePtr& msg)
  1696. {
  1697. int xrange = msg->getU8();
  1698. int yrange = msg->getU8();
  1699. AwareRange range;
  1700. range.left = xrange/2 - ((xrange+1) % 2);
  1701. range.right = xrange/2;
  1702. range.top = yrange/2 - ((yrange+1) % 2);
  1703. range.bottom = yrange/2;
  1704. g_map.setAwareRange(range);
  1705. g_lua.callGlobalField("g_game", "onMapChangeAwareRange", xrange, yrange);
  1706. }
  1707. void ProtocolGame::parseCreaturesMark(const InputMessagePtr& msg)
  1708. {
  1709. int len;
  1710. if(g_game.getClientVersion() >= 1035) {
  1711. len = 1;
  1712. } else {
  1713. len = msg->getU8();
  1714. }
  1715. for(int i=0; i<len; ++i) {
  1716. uint32 id = msg->getU32();
  1717. bool isPermanent = msg->getU8() != 1;
  1718. uint8 markType = msg->getU8();
  1719. CreaturePtr creature = g_map.getCreatureById(id);
  1720. if(creature) {
  1721. if(isPermanent) {
  1722. if(markType == 0xff)
  1723. creature->hideStaticSquare();
  1724. else
  1725. creature->showStaticSquare(Color::from8bit(markType));
  1726. } else
  1727. creature->addTimedSquare(markType);
  1728. } else
  1729. g_logger.traceError("could not get creature");
  1730. }
  1731. }
  1732. void ProtocolGame::parseCreatureType(const InputMessagePtr& msg)
  1733. {
  1734. uint32 id = msg->getU32();
  1735. uint8 type = msg->getU8();
  1736. CreaturePtr creature = g_map.getCreatureById(id);
  1737. if(creature)
  1738. creature->setType(type);
  1739. else
  1740. g_logger.traceError("could not get creature");
  1741. }
  1742. void ProtocolGame::setMapDescription(const InputMessagePtr& msg, int x, int y, int z, int width, int height)
  1743. {
  1744. int startz, endz, zstep;
  1745. if(z > Otc::SEA_FLOOR) {
  1746. startz = z - Otc::AWARE_UNDEGROUND_FLOOR_RANGE;
  1747. endz = std::min<int>(z + Otc::AWARE_UNDEGROUND_FLOOR_RANGE, (int)Otc::MAX_Z);
  1748. zstep = 1;
  1749. }
  1750. else {
  1751. startz = Otc::SEA_FLOOR;
  1752. endz = 0;
  1753. zstep = -1;
  1754. }
  1755. int skip = 0;
  1756. for(int nz = startz; nz != endz + zstep; nz += zstep)
  1757. skip = setFloorDescription(msg, x, y, nz, width, height, z - nz, skip);
  1758. }
  1759. int ProtocolGame::setFloorDescription(const InputMessagePtr& msg, int x, int y, int z, int width, int height, int offset, int skip)
  1760. {
  1761. for(int nx = 0; nx < width; nx++) {
  1762. for(int ny = 0; ny < height; ny++) {
  1763. Position tilePos(x + nx + offset, y + ny + offset, z);
  1764. if(skip == 0)
  1765. skip = setTileDescription(msg, tilePos);
  1766. else {
  1767. g_map.cleanTile(tilePos);
  1768. skip--;
  1769. }
  1770. }
  1771. }
  1772. return skip;
  1773. }
  1774. int ProtocolGame::setTileDescription(const InputMessagePtr& msg, Position position)
  1775. {
  1776. g_map.cleanTile(position);
  1777. bool gotEffect = false;
  1778. for(int stackPos=0;stackPos<256;stackPos++) {
  1779. if(msg->peekU16() >= 0xff00)
  1780. return msg->getU16() & 0xff;
  1781. if(g_game.getFeature(Otc::GameEnvironmentEffect) && !gotEffect) {
  1782. msg->getU16(); // environment effect
  1783. gotEffect = true;
  1784. continue;
  1785. }
  1786. if(stackPos > 10)
  1787. g_logger.traceError(stdext::format("too many things, pos=%s, stackpos=%d", stdext::to_string(position), stackPos));
  1788. ThingPtr thing = getThing(msg);
  1789. g_map.addThing(thing, position, stackPos);
  1790. }
  1791. return 0;
  1792. }
  1793. Outfit ProtocolGame::getOutfit(const InputMessagePtr& msg)
  1794. {
  1795. Outfit outfit;
  1796. int lookType;
  1797. if(g_game.getFeature(Otc::GameLooktypeU16))
  1798. lookType = msg->getU16();
  1799. else
  1800. lookType = msg->getU8();
  1801. if(lookType != 0) {
  1802. outfit.setCategory(ThingCategoryCreature);
  1803. int head = msg->getU8();
  1804. int body = msg->getU8();
  1805. int legs = msg->getU8();
  1806. int feet = msg->getU8();
  1807. int addons = 0;
  1808. if(g_game.getFeature(Otc::GamePlayerAddons))
  1809. addons = msg->getU8();
  1810. if(!g_things.isValidDatId(lookType, ThingCategoryCreature)) {
  1811. g_logger.traceError(stdext::format("invalid outfit looktype %d", lookType));
  1812. lookType = 0;
  1813. }
  1814. outfit.setId(lookType);
  1815. outfit.setHead(head);
  1816. outfit.setBody(body);
  1817. outfit.setLegs(legs);
  1818. outfit.setFeet(feet);
  1819. outfit.setAddons(addons);
  1820. }
  1821. else {
  1822. int lookTypeEx = msg->getU16();
  1823. if(lookTypeEx == 0) {
  1824. outfit.setCategory(ThingCategoryEffect);
  1825. outfit.setAuxId(13); // invisible effect id
  1826. }
  1827. else {
  1828. if(!g_things.isValidDatId(lookTypeEx, ThingCategoryItem)) {
  1829. g_logger.traceError(stdext::format("invalid outfit looktypeex %d", lookTypeEx));
  1830. lookTypeEx = 0;
  1831. }
  1832. outfit.setCategory(ThingCategoryItem);
  1833. outfit.setAuxId(lookTypeEx);
  1834. }
  1835. }
  1836. if(g_game.getFeature(Otc::GamePlayerMounts)) {
  1837. int mount = msg->getU16();
  1838. outfit.setMount(mount);
  1839. }
  1840. return outfit;
  1841. }
  1842. ThingPtr ProtocolGame::getThing(const InputMessagePtr& msg)
  1843. {
  1844. ThingPtr thing;
  1845. int id = msg->getU16();
  1846. if(id == 0)
  1847. stdext::throw_exception("invalid thing id");
  1848. else if(id == Proto::UnknownCreature || id == Proto::OutdatedCreature || id == Proto::Creature)
  1849. thing = getCreature(msg, id);
  1850. else if(id == Proto::StaticText) // otclient only
  1851. thing = getStaticText(msg, id);
  1852. else // item
  1853. thing = getItem(msg, id);
  1854. return thing;
  1855. }
  1856. ThingPtr ProtocolGame::getMappedThing(const InputMessagePtr& msg)
  1857. {
  1858. ThingPtr thing;
  1859. uint16 x = msg->getU16();
  1860. if(x != 0xffff) {
  1861. Position pos;
  1862. pos.x = x;
  1863. pos.y = msg->getU16();
  1864. pos.z = msg->getU8();
  1865. uint8 stackpos = msg->getU8();
  1866. assert(stackpos != 255);
  1867. thing = g_map.getThing(pos, stackpos);
  1868. if(!thing)
  1869. g_logger.traceError(stdext::format("no thing at pos:%s, stackpos:%d", stdext::to_string(pos), stackpos));
  1870. } else {
  1871. uint32 id = msg->getU32();
  1872. thing = g_map.getCreatureById(id);
  1873. if(!thing)
  1874. g_logger.traceError(stdext::format("no creature with id %u", id));
  1875. }
  1876. return thing;
  1877. }
  1878. CreaturePtr ProtocolGame::getCreature(const InputMessagePtr& msg, int type)
  1879. {
  1880. if(type == 0)
  1881. type = msg->getU16();
  1882. CreaturePtr creature;
  1883. bool known = (type != Proto::UnknownCreature);
  1884. if(type == Proto::OutdatedCreature || type == Proto::UnknownCreature) {
  1885. if(known) {
  1886. uint id = msg->getU32();
  1887. creature = g_map.getCreatureById(id);
  1888. if(!creature)
  1889. g_logger.traceError("server said that a creature is known, but it's not");
  1890. } else {
  1891. uint removeId = msg->getU32();
  1892. g_map.removeCreatureById(removeId);
  1893. uint id = msg->getU32();
  1894. int creatureType;
  1895. if(g_game.getClientVersion() >= 910)
  1896. creatureType = msg->getU8();
  1897. else {
  1898. if(id >= Proto::PlayerStartId && id < Proto::PlayerEndId)
  1899. creatureType = Proto::CreatureTypePlayer;
  1900. else if(id >= Proto::MonsterStartId && id < Proto::MonsterEndId)
  1901. creatureType = Proto::CreatureTypeMonster;
  1902. else
  1903. creatureType = Proto::CreatureTypeNpc;
  1904. }
  1905. std::string name = g_game.formatCreatureName(msg->getString());
  1906. if(id == m_localPlayer->getId())
  1907. creature = m_localPlayer;
  1908. else if(creatureType == Proto::CreatureTypePlayer) {
  1909. // fixes a bug server side bug where GameInit is not sent and local player id is unknown
  1910. if(m_localPlayer->getId() == 0 && name == m_localPlayer->getName())
  1911. creature = m_localPlayer;
  1912. else
  1913. creature = PlayerPtr(new Player);
  1914. }
  1915. else if(creatureType == Proto::CreatureTypeMonster)
  1916. creature = MonsterPtr(new Monster);
  1917. else if(creatureType == Proto::CreatureTypeNpc)
  1918. creature = NpcPtr(new Npc);
  1919. else
  1920. g_logger.traceError("creature type is invalid");
  1921. if(creature) {
  1922. creature->setId(id);
  1923. creature->setName(name);
  1924. g_map.addCreature(creature);
  1925. }
  1926. }
  1927. int healthPercent = msg->getU8();
  1928. Otc::Direction direction = (Otc::Direction)msg->getU8();
  1929. Outfit outfit = getOutfit(msg);
  1930. Light light;
  1931. light.intensity = msg->getU8();
  1932. light.color = msg->getU8();
  1933. int speed = msg->getU16();
  1934. int skull = msg->getU8();
  1935. int shield = msg->getU8();
  1936. // emblem is sent only when the creature is not known
  1937. int8 emblem = -1;
  1938. int8 creatureType = -1;
  1939. int8 icon = -1;
  1940. bool unpass = true;
  1941. uint8 mark;
  1942. if(g_game.getFeature(Otc::GameCreatureEmblems) && !known)
  1943. emblem = msg->getU8();
  1944. if(g_game.getFeature(Otc::GameThingMarks)) {
  1945. creatureType = msg->getU8();
  1946. }
  1947. if(g_game.getFeature(Otc::GameCreatureIcons)) {
  1948. icon = msg->getU8();
  1949. }
  1950. if(g_game.getFeature(Otc::GameThingMarks)) {
  1951. mark = msg->getU8(); // mark
  1952. msg->getU16(); // helpers
  1953. if(creature) {
  1954. if(mark == 0xff)
  1955. creature->hideStaticSquare();
  1956. else
  1957. creature->showStaticSquare(Color::from8bit(mark));
  1958. }
  1959. }
  1960. if(g_game.getClientVersion() >= 854)
  1961. unpass = msg->getU8();
  1962. if(creature) {
  1963. creature->setHealthPercent(healthPercent);
  1964. creature->setDirection(direction);
  1965. creature->setOutfit(outfit);
  1966. creature->setSpeed(speed);
  1967. creature->setSkull(skull);
  1968. creature->setShield(shield);
  1969. creature->setPassable(!unpass);
  1970. creature->setLight(light);
  1971. if(emblem != -1)
  1972. creature->setEmblem(emblem);
  1973. if(creatureType != -1)
  1974. creature->setType(creatureType);
  1975. if(icon != -1)
  1976. creature->setIcon(icon);
  1977. if(creature == m_localPlayer && !m_localPlayer->isKnown())
  1978. m_localPlayer->setKnown(true);
  1979. }
  1980. } else if(type == Proto::Creature) {
  1981. uint id = msg->getU32();
  1982. creature = g_map.getCreatureById(id);
  1983. if(!creature)
  1984. g_logger.traceError("invalid creature");
  1985. Otc::Direction direction = (Otc::Direction)msg->getU8();
  1986. if(creature)
  1987. creature->turn(direction);
  1988. if(g_game.getClientVersion() >= 953) {
  1989. bool unpass = msg->getU8();
  1990. if(creature)
  1991. creature->setPassable(!unpass);
  1992. }
  1993. } else {
  1994. stdext::throw_exception("invalid creature opcode");
  1995. }
  1996. return creature;
  1997. }
  1998. ItemPtr ProtocolGame::getItem(const InputMessagePtr& msg, int id)
  1999. {
  2000. if(id == 0)
  2001. id = msg->getU16();
  2002. ItemPtr item = Item::create(id);
  2003. if(item->getId() == 0)
  2004. stdext::throw_exception(stdext::format("unable to create item with invalid id %d", id));
  2005. if(g_game.getFeature(Otc::GameThingMarks)) {
  2006. msg->getU8(); // mark
  2007. }
  2008. if(item->isStackable() || item->isFluidContainer() || item->isSplash() || item->isChargeable())
  2009. item->setCountOrSubType(msg->getU8());
  2010. if(g_game.getFeature(Otc::GameItemAnimationPhase)) {
  2011. if(item->getAnimationPhases() > 1) {
  2012. // 0x00 => automatic phase
  2013. // 0xFE => random phase
  2014. // 0xFF => async phase
  2015. msg->getU8();
  2016. //item->setPhase(msg->getU8());
  2017. }
  2018. }
  2019. return item;
  2020. }
  2021. StaticTextPtr ProtocolGame::getStaticText(const InputMessagePtr& msg, int id)
  2022. {
  2023. int colorByte = msg->getU8();
  2024. Color color = Color::from8bit(colorByte);
  2025. std::string fontName = msg->getString();
  2026. std::string text = msg->getString();
  2027. StaticTextPtr staticText = StaticTextPtr(new StaticText);
  2028. staticText->setText(text);
  2029. staticText->setFont(fontName);
  2030. staticText->setColor(color);
  2031. return staticText;
  2032. }
  2033. Position ProtocolGame::getPosition(const InputMessagePtr& msg)
  2034. {
  2035. uint16 x = msg->getU16();
  2036. uint16 y = msg->getU16();
  2037. uint8 z = msg->getU8();
  2038. return Position(x, y, z);
  2039. }