char.c 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h"
  5. #include "../common/db.h"
  6. #include "../common/lock.h"
  7. #include "../common/malloc.h"
  8. #include "../common/mapindex.h"
  9. #include "../common/mmo.h"
  10. #include "../common/showmsg.h"
  11. #include "../common/socket.h"
  12. #include "../common/strlib.h"
  13. #include "../common/timer.h"
  14. #include "../common/utils.h"
  15. #include "../common/version.h"
  16. #include "inter.h"
  17. #include "int_guild.h"
  18. #include "int_homun.h"
  19. #include "int_pet.h"
  20. #include "int_party.h"
  21. #include "int_storage.h"
  22. #include "int_status.h"
  23. #include "char.h"
  24. #include <sys/types.h>
  25. #include <time.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. // private declarations
  32. #define CHAR_CONF_NAME "conf/char_athena.conf"
  33. #define LAN_CONF_NAME "conf/subnet_athena.conf"
  34. char char_txt[1024] = "save/athena.txt";
  35. char friends_txt[1024] = "save/friends.txt";
  36. char hotkeys_txt[1024] = "save/hotkeys.txt";
  37. char char_log_filename[1024] = "log/char.log";
  38. // show loading/saving messages
  39. #ifndef TXT_SQL_CONVERT
  40. int save_log = 1;
  41. #endif
  42. //If your code editor is having problems syntax highlighting this file, uncomment this and RECOMMENT IT BEFORE COMPILING
  43. //#undef TXT_SQL_CONVERT
  44. #ifndef TXT_SQL_CONVERT
  45. char db_path[1024] = "db";
  46. struct mmo_map_server {
  47. int fd;
  48. uint32 ip;
  49. uint16 port;
  50. int users;
  51. unsigned short map[MAX_MAP_PER_SERVER];
  52. } server[MAX_MAP_SERVERS];
  53. int login_fd=-1, char_fd=-1;
  54. char userid[24];
  55. char passwd[24];
  56. char server_name[20];
  57. char wisp_server_name[NAME_LENGTH] = "Server";
  58. char login_ip_str[128];
  59. uint32 login_ip = 0;
  60. uint16 login_port = 6900;
  61. char char_ip_str[128];
  62. uint32 char_ip = 0;
  63. char bind_ip_str[128];
  64. uint32 bind_ip = INADDR_ANY;
  65. uint16 char_port = 6121;
  66. int char_maintenance = 0;
  67. bool char_new = true;
  68. int char_new_display = 0;
  69. int email_creation = 0; // disabled by default
  70. bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  71. int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  72. char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined
  73. #define TRIM_CHARS "\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  74. char char_name_letters[1024] = ""; // list of letters/symbols allowed (or not) in a character name. by [Yor]
  75. int char_per_account = 0; //Maximum charas per account (default unlimited) [Sirius]
  76. int char_del_level = 0; //From which level u can delete character [Lupus]
  77. int char_del_delay = 86400;
  78. int log_char = 1; // loggin char or not [devil]
  79. int log_inter = 1; // loggin inter or not [devil]
  80. // Advanced subnet check [LuzZza]
  81. struct s_subnet {
  82. uint32 mask;
  83. uint32 char_ip;
  84. uint32 map_ip;
  85. } subnet[16];
  86. int subnet_count = 0;
  87. struct char_session_data {
  88. bool auth; // whether the session is authed or not
  89. int account_id, login_id1, login_id2, sex;
  90. int found_char[MAX_CHARS]; // ids of chars on this account
  91. char email[40]; // e-mail (default: a@a.com) by [Yor]
  92. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  93. int gmlevel;
  94. uint32 version;
  95. uint8 clienttype;
  96. char birthdate[10+1]; // YYYY-MM-DD
  97. };
  98. int char_id_count = START_CHAR_NUM;
  99. struct character_data *char_dat;
  100. int char_num, char_max;
  101. int max_connect_user = 0;
  102. int gm_allow_level = 99;
  103. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  104. int start_zeny = 0;
  105. int start_weapon = 1201;
  106. int start_armor = 2301;
  107. int guild_exp_rate = 100;
  108. //Custom limits for the fame lists. [Skotlex]
  109. int fame_list_size_chemist = MAX_FAME_LIST;
  110. int fame_list_size_smith = MAX_FAME_LIST;
  111. int fame_list_size_taekwon = MAX_FAME_LIST;
  112. // Char-server-side stored fame lists [DracoRPG]
  113. struct fame_list smith_fame_list[MAX_FAME_LIST];
  114. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  115. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  116. // Initial position (it's possible to set it in conf file)
  117. struct point start_point = { 0, 53, 111 };
  118. // online players by [Yor]
  119. char online_txt_filename[1024] = "online.txt";
  120. char online_html_filename[1024] = "online.html";
  121. int online_sorting_option = 0; // sorting option to display online players in online files
  122. int online_display_option = 1; // display options: to know which columns must be displayed
  123. int online_refresh_html = 20; // refresh time (in sec) of the html file in the explorer
  124. int online_gm_display_min_level = 20; // minimum GM level to display 'GM' when we want to display it
  125. int console = 0;
  126. //-----------------------------------------------------
  127. // Auth database
  128. //-----------------------------------------------------
  129. #define AUTH_TIMEOUT 30000
  130. struct auth_node {
  131. int account_id;
  132. int char_id;
  133. uint32 login_id1;
  134. uint32 login_id2;
  135. uint32 ip;
  136. int sex;
  137. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  138. int gmlevel;
  139. };
  140. static DBMap* auth_db; // int account_id -> struct auth_node*
  141. //-----------------------------------------------------
  142. // Online User Database
  143. //-----------------------------------------------------
  144. struct online_char_data {
  145. int account_id;
  146. int char_id;
  147. int fd;
  148. int waiting_disconnect;
  149. short server; // -2: unknown server, -1: not connected, 0+: id of server
  150. };
  151. static DBMap* online_char_db; // int account_id -> struct online_char_data*
  152. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data);
  153. static void* create_online_char_data(DBKey key, va_list args)
  154. {
  155. struct online_char_data* character;
  156. CREATE(character, struct online_char_data, 1);
  157. character->account_id = key.i;
  158. character->char_id = -1;
  159. character->server = -1;
  160. character->fd = -1;
  161. character->waiting_disconnect = INVALID_TIMER;
  162. return character;
  163. }
  164. void set_char_charselect(int account_id)
  165. {
  166. struct online_char_data* character;
  167. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  168. if( character->server > -1 )
  169. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  170. server[character->server].users--;
  171. character->char_id = -1;
  172. character->server = -1;
  173. if(character->waiting_disconnect != INVALID_TIMER) {
  174. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  175. character->waiting_disconnect = INVALID_TIMER;
  176. }
  177. if (login_fd > 0 && !session[login_fd]->flag.eof)
  178. {
  179. WFIFOHEAD(login_fd,6);
  180. WFIFOW(login_fd,0) = 0x272b;
  181. WFIFOL(login_fd,2) = account_id;
  182. WFIFOSET(login_fd,6);
  183. }
  184. }
  185. void set_char_online(int map_id, int char_id, int account_id)
  186. {
  187. struct online_char_data* character;
  188. //Check to see for online conflicts
  189. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  190. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  191. {
  192. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  193. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  194. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  195. }
  196. //Update state data
  197. character->char_id = char_id;
  198. character->server = map_id;
  199. if( character->server > -1 )
  200. server[character->server].users++;
  201. //Get rid of disconnect timer
  202. if(character->waiting_disconnect != INVALID_TIMER) {
  203. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  204. character->waiting_disconnect = INVALID_TIMER;
  205. }
  206. //Notify login server
  207. if (login_fd > 0 && !session[login_fd]->flag.eof)
  208. {
  209. WFIFOHEAD(login_fd,6);
  210. WFIFOW(login_fd,0) = 0x272b;
  211. WFIFOL(login_fd,2) = account_id;
  212. WFIFOSET(login_fd,6);
  213. }
  214. }
  215. void set_char_offline(int char_id, int account_id)
  216. {
  217. struct online_char_data* character;
  218. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  219. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  220. if( character->server > -1 )
  221. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  222. server[character->server].users--;
  223. if(character->waiting_disconnect != INVALID_TIMER){
  224. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  225. character->waiting_disconnect = INVALID_TIMER;
  226. }
  227. if(character->char_id == char_id)
  228. {
  229. character->char_id = -1;
  230. character->server = -1;
  231. }
  232. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  233. }
  234. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  235. if (login_fd > 0 && !session[login_fd]->flag.eof && (char_id == -1 || character == NULL || character->fd == -1))
  236. {
  237. WFIFOHEAD(login_fd,6);
  238. WFIFOW(login_fd,0) = 0x272c;
  239. WFIFOL(login_fd,2) = account_id;
  240. WFIFOSET(login_fd,6);
  241. }
  242. }
  243. static int char_db_setoffline(DBKey key, void* data, va_list ap)
  244. {
  245. struct online_char_data* character = (struct online_char_data*)data;
  246. int server = va_arg(ap, int);
  247. if (server == -1) {
  248. character->char_id = -1;
  249. character->server = -1;
  250. if(character->waiting_disconnect != INVALID_TIMER){
  251. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  252. character->waiting_disconnect = INVALID_TIMER;
  253. }
  254. } else if (character->server == server)
  255. character->server = -2; //In some map server that we aren't connected to.
  256. return 0;
  257. }
  258. static int char_db_kickoffline(DBKey key, void* data, va_list ap)
  259. {
  260. struct online_char_data* character = (struct online_char_data*)data;
  261. int server_id = va_arg(ap, int);
  262. if (server_id > -1 && character->server != server_id)
  263. return 0;
  264. //Kick out any connected characters, and set them offline as appropiate.
  265. if (character->server > -1)
  266. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
  267. else if (character->waiting_disconnect == INVALID_TIMER)
  268. set_char_offline(character->char_id, character->account_id);
  269. else
  270. return 0; // fail
  271. return 1;
  272. }
  273. void set_all_offline(int id)
  274. {
  275. if (id < 0)
  276. ShowNotice("Sending all users offline.\n");
  277. else
  278. ShowNotice("Sending users of map-server %d offline.\n",id);
  279. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  280. if (id >= 0 || login_fd <= 0 || session[login_fd]->flag.eof)
  281. return;
  282. //Tell login-server to also mark all our characters as offline.
  283. WFIFOHEAD(login_fd,2);
  284. WFIFOW(login_fd,0) = 0x2737;
  285. WFIFOSET(login_fd,2);
  286. }
  287. //------------------------------
  288. // Writing function of logs file
  289. //------------------------------
  290. int char_log(char *fmt, ...)
  291. {
  292. if(log_char)
  293. {
  294. FILE *logfp;
  295. va_list ap;
  296. time_t raw_time;
  297. char tmpstr[2048];
  298. va_start(ap, fmt);
  299. logfp = fopen(char_log_filename, "a");
  300. if (logfp) {
  301. if (fmt[0] == '\0') // jump a line if no message
  302. fprintf(logfp, "\n");
  303. else {
  304. time(&raw_time);
  305. strftime(tmpstr, 24, "%d-%m-%Y %H:%M:%S", localtime(&raw_time));
  306. sprintf(tmpstr + 19, ": %s", fmt);
  307. vfprintf(logfp, tmpstr, ap);
  308. }
  309. fclose(logfp);
  310. }
  311. va_end(ap);
  312. }
  313. return 0;
  314. }
  315. /// Find all characters for given session and update the session character cache.
  316. int char_find_characters(struct char_session_data* sd)
  317. {
  318. int i, found_num = 0;
  319. for( i = 0; i < char_num; i++ )
  320. {// find character entries and save them
  321. if( char_dat[i].status.account_id == sd->account_id )
  322. {
  323. sd->found_char[found_num++] = i;
  324. if( found_num >= MAX_CHARS )
  325. {
  326. break;
  327. }
  328. }
  329. }
  330. for( i = found_num; i < MAX_CHARS; i++ )
  331. {// fill remaining blanks
  332. sd->found_char[i] = -1;
  333. }
  334. return found_num;
  335. }
  336. /// Search character data from given session.
  337. struct mmo_charstatus* search_session_character(struct char_session_data* sd, int char_id)
  338. {
  339. int i;
  340. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == char_id );
  341. if( i == MAX_CHARS )
  342. {
  343. return NULL;
  344. }
  345. return &char_dat[sd->found_char[i]].status;
  346. }
  347. //Search character data from the aid/cid givem
  348. struct mmo_charstatus* search_character(int aid, int cid)
  349. {
  350. int i;
  351. for (i = 0; i < char_num; i++) {
  352. if (char_dat[i].status.char_id == cid && char_dat[i].status.account_id == aid)
  353. return &char_dat[i].status;
  354. }
  355. return NULL;
  356. }
  357. struct mmo_charstatus* search_character_byname(char* character_name)
  358. {
  359. int i = search_character_index(character_name);
  360. if (i == -1) return NULL;
  361. return &char_dat[i].status;
  362. }
  363. // Searches if the given character is online, and returns the fd of the
  364. // map-server it is connected to.
  365. int search_character_online(int aid, int cid)
  366. {
  367. //Look for online character.
  368. struct online_char_data* character;
  369. character = idb_get(online_char_db, aid);
  370. if(character &&
  371. character->char_id == cid &&
  372. character->server > -1)
  373. return server[character->server].fd;
  374. return -1;
  375. }
  376. //----------------------------------------------
  377. // Search an character id
  378. // (return character index or -1 (if not found))
  379. // If exact character name is not found,
  380. // the function checks without case sensitive
  381. // and returns index if only 1 character is found
  382. // and similar to the searched name.
  383. //----------------------------------------------
  384. int search_character_index(char* character_name)
  385. {
  386. int i, quantity, index;
  387. quantity = 0;
  388. index = -1;
  389. for(i = 0; i < char_num; i++) {
  390. // Without case sensitive check (increase the number of similar character names found)
  391. if (stricmp(char_dat[i].status.name, character_name) == 0) {
  392. // Strict comparison (if found, we finish the function immediatly with correct value)
  393. if (strcmp(char_dat[i].status.name, character_name) == 0)
  394. return i;
  395. quantity++;
  396. index = i;
  397. }
  398. }
  399. // Here, the exact character name is not found
  400. // We return the found index of a similar account ONLY if there is 1 similar character
  401. if (quantity == 1)
  402. return index;
  403. // Exact character name is not found and 0 or more than 1 similar characters have been found ==> we say not found
  404. return -1;
  405. }
  406. /*---------------------------------------------------
  407. Make a data line for friends list
  408. --------------------------------------------------*/
  409. int mmo_friends_list_data_str(char *str, struct mmo_charstatus *p)
  410. {
  411. int i;
  412. char *str_p = str;
  413. str_p += sprintf(str_p, "%d", p->char_id);
  414. for (i=0;i<MAX_FRIENDS;i++){
  415. if (p->friends[i].account_id > 0 && p->friends[i].char_id > 0 && p->friends[i].name[0])
  416. str_p += sprintf(str_p, ",%d,%d,%s", p->friends[i].account_id, p->friends[i].char_id, p->friends[i].name);
  417. }
  418. str_p += '\0';
  419. return 0;
  420. }
  421. /*---------------------------------------------------
  422. Make a data line for hotkeys list
  423. --------------------------------------------------*/
  424. int mmo_hotkeys_tostr(char *str, struct mmo_charstatus *p)
  425. {
  426. #ifdef HOTKEY_SAVING
  427. int i;
  428. char *str_p = str;
  429. str_p += sprintf(str_p, "%d", p->char_id);
  430. for (i=0;i<MAX_HOTKEYS;i++)
  431. str_p += sprintf(str_p, ",%d,%d,%d", p->hotkeys[i].type, p->hotkeys[i].id, p->hotkeys[i].lv);
  432. str_p += '\0';
  433. #endif
  434. return 0;
  435. }
  436. //-------------------------------------------------
  437. // Function to create the character line (for save)
  438. //-------------------------------------------------
  439. int mmo_char_tostr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int reg_num)
  440. {
  441. int i,j;
  442. char *str_p = str;
  443. str_p += sprintf(str_p,
  444. "%d\t%d,%d\t%s\t%d,%d,%d\t%u,%u,%d" //Up to Zeny field
  445. "\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" //Up to Skill Point
  446. "\t%d,%d,%d\t%d,%d,%d,%d" //Up to hom id
  447. "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d" //Up to robe
  448. "\t%d,%d,%d\t%d,%d,%d" //last point + save point
  449. ",%d,%d,%d,%d,%d,%lu\t", //Family info + delete date
  450. p->char_id, p->account_id, p->slot, p->name, //
  451. p->class_, p->base_level, p->job_level,
  452. p->base_exp, p->job_exp, p->zeny,
  453. p->hp, p->max_hp, p->sp, p->max_sp,
  454. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  455. p->status_point, p->skill_point,
  456. p->option, p->karma, p->manner, //
  457. p->party_id, p->guild_id, p->pet_id, p->hom_id,
  458. p->hair, p->hair_color, p->clothes_color,
  459. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, p->robe,
  460. p->last_point.map, p->last_point.x, p->last_point.y, //
  461. p->save_point.map, p->save_point.x, p->save_point.y,
  462. p->partner_id,p->father,p->mother,p->child,p->fame, //
  463. (unsigned long)p->delete_date); // FIXME: platform-dependent size
  464. for(i = 0; i < MAX_MEMOPOINTS; i++)
  465. if (p->memo_point[i].map) {
  466. str_p += sprintf(str_p, "%d,%d,%d ", p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y);
  467. }
  468. *(str_p++) = '\t';
  469. for(i = 0; i < MAX_INVENTORY; i++)
  470. if (p->inventory[i].nameid) {
  471. str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d",
  472. p->inventory[i].id,p->inventory[i].nameid,p->inventory[i].amount,p->inventory[i].equip,
  473. p->inventory[i].identify,p->inventory[i].refine,p->inventory[i].attribute);
  474. for(j=0; j<MAX_SLOTS; j++)
  475. str_p += sprintf(str_p,",%d",p->inventory[i].card[j]);
  476. str_p += sprintf(str_p," ");
  477. }
  478. *(str_p++) = '\t';
  479. for(i = 0; i < MAX_CART; i++)
  480. if (p->cart[i].nameid) {
  481. str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d",
  482. p->cart[i].id,p->cart[i].nameid,p->cart[i].amount,p->cart[i].equip,
  483. p->cart[i].identify,p->cart[i].refine,p->cart[i].attribute);
  484. for(j=0; j<MAX_SLOTS; j++)
  485. str_p += sprintf(str_p,",%d",p->cart[i].card[j]);
  486. str_p += sprintf(str_p," ");
  487. }
  488. *(str_p++) = '\t';
  489. for(i = 0; i < MAX_SKILL; i++)
  490. if (p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY) {
  491. str_p += sprintf(str_p, "%d,%d ", p->skill[i].id, (p->skill[i].flag == SKILL_FLAG_PERMANENT) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  492. }
  493. *(str_p++) = '\t';
  494. for(i = 0; i < reg_num; i++)
  495. if (reg[i].str[0])
  496. str_p += sprintf(str_p, "%s,%s ", reg[i].str, reg[i].value);
  497. *(str_p++) = '\t';
  498. *str_p = '\0';
  499. return 0;
  500. }
  501. #endif //TXT_SQL_CONVERT
  502. //-------------------------------------------------------------------------
  503. // Function to set the character from the line (at read of characters file)
  504. //-------------------------------------------------------------------------
  505. int mmo_char_fromstr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int *reg_num)
  506. {
  507. char tmp_str[3][128]; //To avoid deleting chars with too long names.
  508. int tmp_int[256];
  509. unsigned int tmp_uint[2]; //To read exp....
  510. int next, len, i, j;
  511. unsigned long tmp_ulong[1];
  512. // initilialise character
  513. memset(p, '\0', sizeof(struct mmo_charstatus));
  514. // Char structure of version 14797 (robe)
  515. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  516. "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d,%d"
  517. "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d,%lu%n",
  518. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0],
  519. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  520. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  521. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  522. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  523. &tmp_int[19], &tmp_int[20],
  524. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  525. &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44],
  526. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  527. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], &tmp_int[47],
  528. &tmp_int[45], &tmp_int[35], &tmp_int[36],
  529. &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  530. &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &tmp_ulong[0], &next) != 50)
  531. {
  532. tmp_int[47] = 0; // robe
  533. // Char structure of version 14700 (delete date)
  534. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  535. "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  536. "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d,%lu%n",
  537. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0],
  538. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  539. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  540. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  541. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  542. &tmp_int[19], &tmp_int[20],
  543. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  544. &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44],
  545. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  546. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  547. &tmp_int[45], &tmp_int[35], &tmp_int[36],
  548. &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  549. &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &tmp_ulong[0], &next) != 49)
  550. {
  551. tmp_ulong[0] = 0; // delete date
  552. // Char structure of version 1500 (homun + mapindex maps)
  553. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  554. "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  555. "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d%n",
  556. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0],
  557. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  558. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  559. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  560. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  561. &tmp_int[19], &tmp_int[20],
  562. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  563. &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44],
  564. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  565. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  566. &tmp_int[45], &tmp_int[35], &tmp_int[36],
  567. &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  568. &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next) != 48)
  569. {
  570. tmp_int[44] = 0; //Hom ID.
  571. // Char structure of version 1488 (fame field addition)
  572. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  573. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  574. "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d,%d%n",
  575. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0],
  576. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  577. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  578. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  579. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  580. &tmp_int[19], &tmp_int[20],
  581. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  582. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  583. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  584. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  585. tmp_str[1], &tmp_int[35], &tmp_int[36],
  586. tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  587. &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next) != 47)
  588. {
  589. tmp_int[43] = 0; //Fame
  590. // Char structure of version 1363 (family data addition)
  591. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  592. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  593. "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d%n",
  594. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  595. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  596. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  597. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  598. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  599. &tmp_int[19], &tmp_int[20],
  600. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  601. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  602. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  603. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  604. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  605. tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  606. &tmp_int[40], &tmp_int[41], &tmp_int[42], &next) != 46)
  607. {
  608. tmp_int[40] = 0; // father
  609. tmp_int[41] = 0; // mother
  610. tmp_int[42] = 0; // child
  611. // Char structure version 1008 (marriage partner addition)
  612. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  613. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  614. "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d%n",
  615. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  616. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  617. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  618. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  619. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  620. &tmp_int[19], &tmp_int[20],
  621. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  622. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  623. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  624. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  625. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  626. tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], &next) != 43)
  627. {
  628. tmp_int[39] = 0; // partner id
  629. // Char structure version 384 (pet addition)
  630. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  631. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  632. "\t%127[^,],%d,%d\t%127[^,],%d,%d%n",
  633. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  634. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  635. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  636. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  637. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  638. &tmp_int[19], &tmp_int[20],
  639. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  640. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  641. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  642. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  643. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  644. tmp_str[2], &tmp_int[37], &tmp_int[38], &next) != 42)
  645. {
  646. tmp_int[26] = 0; // pet id
  647. // Char structure of a version 1 (original data structure)
  648. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  649. "\t%d,%d,%d\t%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  650. "\t%127[^,],%d,%d\t%127[^,],%d,%d%n",
  651. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  652. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  653. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  654. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  655. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  656. &tmp_int[19], &tmp_int[20],
  657. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  658. &tmp_int[24], &tmp_int[25], //
  659. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  660. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  661. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  662. tmp_str[2], &tmp_int[37], &tmp_int[38], &next) != 41)
  663. {
  664. ShowError("Char-loading: Unrecognized character data version, info lost!\n");
  665. ShowDebug("Character info: %s\n", str);
  666. return 0;
  667. }
  668. } // Char structure version 384 (pet addition)
  669. } // Char structure version 1008 (marriage partner addition)
  670. } // Char structure of version 1363 (family data addition)
  671. } // Char structure of version 1488 (fame field addition)
  672. //Convert save data from string to integer for older formats
  673. tmp_int[45] = mapindex_name2id(tmp_str[1]);
  674. tmp_int[46] = mapindex_name2id(tmp_str[2]);
  675. } // Char structure of version 1500 (homun + mapindex maps)
  676. } // Char structure of version 14700 (delete date)
  677. } // Char structure of version 14797 (robe)
  678. safestrncpy(p->name, tmp_str[0], NAME_LENGTH); //Overflow protection [Skotlex]
  679. p->char_id = tmp_int[0];
  680. p->account_id = tmp_int[1];
  681. p->slot = tmp_int[2];
  682. p->class_ = tmp_int[3];
  683. p->base_level = tmp_int[4];
  684. p->job_level = tmp_int[5];
  685. p->base_exp = tmp_uint[0];
  686. p->job_exp = tmp_uint[1];
  687. p->zeny = tmp_int[8];
  688. p->hp = tmp_int[9];
  689. p->max_hp = tmp_int[10];
  690. p->sp = tmp_int[11];
  691. p->max_sp = tmp_int[12];
  692. p->str = tmp_int[13];
  693. p->agi = tmp_int[14];
  694. p->vit = tmp_int[15];
  695. p->int_ = tmp_int[16];
  696. p->dex = tmp_int[17];
  697. p->luk = tmp_int[18];
  698. p->status_point = tmp_int[19];
  699. p->skill_point = tmp_int[20];
  700. p->option = tmp_int[21];
  701. p->karma = tmp_int[22];
  702. p->manner = tmp_int[23];
  703. p->party_id = tmp_int[24];
  704. p->guild_id = tmp_int[25];
  705. p->pet_id = tmp_int[26];
  706. p->hair = tmp_int[27];
  707. p->hair_color = tmp_int[28];
  708. p->clothes_color = tmp_int[29];
  709. p->weapon = tmp_int[30];
  710. p->shield = tmp_int[31];
  711. p->head_top = tmp_int[32];
  712. p->head_mid = tmp_int[33];
  713. p->head_bottom = tmp_int[34];
  714. p->last_point.x = tmp_int[35];
  715. p->last_point.y = tmp_int[36];
  716. p->save_point.x = tmp_int[37];
  717. p->save_point.y = tmp_int[38];
  718. p->partner_id = tmp_int[39];
  719. p->father = tmp_int[40];
  720. p->mother = tmp_int[41];
  721. p->child = tmp_int[42];
  722. p->fame = tmp_int[43];
  723. p->hom_id = tmp_int[44];
  724. p->last_point.map = tmp_int[45];
  725. p->save_point.map = tmp_int[46];
  726. p->delete_date = tmp_ulong[0];
  727. p->robe = tmp_int[47];
  728. #ifndef TXT_SQL_CONVERT
  729. // Some checks
  730. for(i = 0; i < char_num; i++) {
  731. if (char_dat[i].status.char_id == p->char_id) {
  732. ShowError(CL_RED"mmmo_auth_init: a character has an identical id to another.\n");
  733. ShowError(" character id #%d -> new character not readed.\n", p->char_id);
  734. ShowError(" Character saved in log file."CL_RESET"\n");
  735. return -1;
  736. } else if (strcmp(char_dat[i].status.name, p->name) == 0) {
  737. ShowError(CL_RED"mmmo_auth_init: a character name already exists.\n");
  738. ShowError(" character name '%s' -> new character not read.\n", p->name);
  739. ShowError(" Character saved in log file."CL_RESET"\n");
  740. return -2;
  741. }
  742. }
  743. if (strcmpi(wisp_server_name, p->name) == 0) {
  744. ShowWarning("mmo_auth_init: ******WARNING: character name has wisp server name.\n");
  745. ShowWarning(" Character name '%s' = wisp server name '%s'.\n", p->name, wisp_server_name);
  746. ShowWarning(" Character readed. Suggestion: change the wisp server name.\n");
  747. char_log("mmo_auth_init: ******WARNING: character name has wisp server name: Character name '%s' = wisp server name '%s'.\n",
  748. p->name, wisp_server_name);
  749. }
  750. #endif //TXT_SQL_CONVERT
  751. if (str[next] == '\n' || str[next] == '\r')
  752. return 1; // 新規データ
  753. next++;
  754. for(i = 0; str[next] && str[next] != '\t'; i++) {
  755. //mapindex memo format
  756. if (sscanf(str+next, "%d,%d,%d%n", &tmp_int[2], &tmp_int[0], &tmp_int[1], &len) != 3)
  757. { //Old string-based memo format.
  758. if (sscanf(str+next, "%[^,],%d,%d%n", tmp_str[0], &tmp_int[0], &tmp_int[1], &len) != 3)
  759. return -3;
  760. tmp_int[2] = mapindex_name2id(tmp_str[0]);
  761. }
  762. if (i < MAX_MEMOPOINTS)
  763. { //Avoid overflowing (but we must also read through all saved memos)
  764. p->memo_point[i].x = tmp_int[0];
  765. p->memo_point[i].y = tmp_int[1];
  766. p->memo_point[i].map = tmp_int[2];
  767. }
  768. next += len;
  769. if (str[next] == ' ')
  770. next++;
  771. }
  772. next++;
  773. for(i = 0; str[next] && str[next] != '\t'; i++) {
  774. if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n",
  775. &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3],
  776. &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8)
  777. {
  778. p->inventory[i].id = tmp_int[0];
  779. p->inventory[i].nameid = tmp_int[1];
  780. p->inventory[i].amount = tmp_int[2];
  781. p->inventory[i].equip = tmp_int[3];
  782. p->inventory[i].identify = tmp_int[4];
  783. p->inventory[i].refine = tmp_int[5];
  784. p->inventory[i].attribute = tmp_int[6];
  785. for(j = 0; j < MAX_SLOTS && tmp_str[0][0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++)
  786. p->inventory[i].card[j] = tmp_int[0];
  787. next += len;
  788. if (str[next] == ' ')
  789. next++;
  790. } else // invalid structure
  791. return -4;
  792. }
  793. next++;
  794. for(i = 0; str[next] && str[next] != '\t'; i++) {
  795. if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n",
  796. &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3],
  797. &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8)
  798. {
  799. p->cart[i].id = tmp_int[0];
  800. p->cart[i].nameid = tmp_int[1];
  801. p->cart[i].amount = tmp_int[2];
  802. p->cart[i].equip = tmp_int[3];
  803. p->cart[i].identify = tmp_int[4];
  804. p->cart[i].refine = tmp_int[5];
  805. p->cart[i].attribute = tmp_int[6];
  806. for(j = 0; j < MAX_SLOTS && tmp_str[0][0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++)
  807. p->cart[i].card[j] = tmp_int[0];
  808. next += len;
  809. if (str[next] == ' ')
  810. next++;
  811. } else // invalid structure
  812. return -5;
  813. }
  814. next++;
  815. for(i = 0; str[next] && str[next] != '\t'; i++) {
  816. if (sscanf(str + next, "%d,%d%n", &tmp_int[0], &tmp_int[1], &len) != 2)
  817. return -6;
  818. p->skill[tmp_int[0]].id = tmp_int[0];
  819. p->skill[tmp_int[0]].lv = tmp_int[1];
  820. next += len;
  821. if (str[next] == ' ')
  822. next++;
  823. }
  824. next++;
  825. for(i = 0; str[next] && str[next] != '\t' && str[next] != '\n' && str[next] != '\r'; i++) { // global_reg実装以前のathena.txt互換のため一応'\n'チェック
  826. if (sscanf(str + next, "%[^,],%[^ ] %n", reg[i].str, reg[i].value, &len) != 2) {
  827. // because some scripts are not correct, the str can be "". So, we must check that.
  828. // If it's, we must not refuse the character, but just this REG value.
  829. // Character line will have something like: nov_2nd_cos,9 ,9 nov_1_2_cos_c,1 (here, ,9 is not good)
  830. if (str[next] == ',' && sscanf(str + next, ",%[^ ] %n", reg[i].value, &len) == 1)
  831. i--;
  832. else
  833. return -7;
  834. }
  835. next += len;
  836. if (str[next] == ' ')
  837. next++;
  838. }
  839. *reg_num = i;
  840. return 1;
  841. }
  842. //---------------------------------
  843. // Function to read friend list
  844. //---------------------------------
  845. int parse_friend_txt(struct mmo_charstatus *p)
  846. {
  847. char line[1024], temp[1024];
  848. int pos = 0, count = 0, next;
  849. int i,len;
  850. FILE *fp;
  851. // Open the file and look for the ID
  852. fp = fopen(friends_txt, "r");
  853. if(fp == NULL)
  854. return -1;
  855. while(fgets(line, sizeof(line), fp))
  856. {
  857. if(line[0] == '/' && line[1] == '/')
  858. continue;
  859. if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id)
  860. continue; //Not this line...
  861. //Read friends
  862. len = strlen(line);
  863. next = pos;
  864. for (count = 0; next < len && count < MAX_FRIENDS; count++)
  865. { //Read friends.
  866. if (sscanf(line+next, ",%d,%d,%23[^,^\n]%n",&p->friends[count].account_id,&p->friends[count].char_id, p->friends[count].name, &pos) < 3)
  867. { //Invalid friend?
  868. memset(&p->friends[count], 0, sizeof(p->friends[count]));
  869. break;
  870. }
  871. next+=pos;
  872. //What IF the name contains a comma? while the next field is not a
  873. //number, we assume it belongs to the current name. [Skotlex]
  874. //NOTE: Of course, this will fail if someone sets their name to something like
  875. //Bob,2005 but... meh, it's the problem of parsing a text file (encasing it in "
  876. //won't do as quotes are also valid name chars!)
  877. while(next < len && sscanf(line+next, ",%23[^,^\n]%n", temp, &pos) > 0)
  878. {
  879. if (atoi(temp)) //We read the next friend, just continue.
  880. break;
  881. //Append the name.
  882. next+=pos;
  883. i = strlen(p->friends[count].name);
  884. if (i + strlen(temp) +1 < NAME_LENGTH)
  885. {
  886. p->friends[count].name[i] = ',';
  887. strcpy(p->friends[count].name+i+1, temp);
  888. }
  889. } //End Guess Block
  890. } //Friend's for.
  891. break; //Found friends.
  892. }
  893. fclose(fp);
  894. return count;
  895. }
  896. //---------------------------------
  897. // Function to read hotkey list
  898. //---------------------------------
  899. int parse_hotkey_txt(struct mmo_charstatus *p)
  900. {
  901. #ifdef HOTKEY_SAVING
  902. char line[1024];
  903. int pos = 0, count = 0, next;
  904. int i,len;
  905. int type, id, lv;
  906. FILE *fp;
  907. // Open the file and look for the ID
  908. fp = fopen(hotkeys_txt, "r");
  909. if(fp == NULL)
  910. return -1;
  911. while(fgets(line, sizeof(line), fp))
  912. {
  913. if(line[0] == '/' && line[1] == '/')
  914. continue;
  915. if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id)
  916. continue; //Not this line...
  917. //Read hotkeys
  918. len = strlen(line);
  919. next = pos;
  920. for (count = 0; next < len && count < MAX_HOTKEYS; count++)
  921. {
  922. if (sscanf(line+next, ",%d,%d,%d%n",&type,&id,&lv, &pos) < 3)
  923. //Invalid entry?
  924. break;
  925. p->hotkeys[count].type = type;
  926. p->hotkeys[count].id = id;
  927. p->hotkeys[count].lv = lv;
  928. next+=pos;
  929. }
  930. break; //Found hotkeys.
  931. }
  932. fclose(fp);
  933. return count;
  934. #else
  935. return 0;
  936. #endif
  937. }
  938. #ifndef TXT_SQL_CONVERT
  939. //---------------------------------
  940. // Function to read characters file
  941. //---------------------------------
  942. int mmo_char_init(void)
  943. {
  944. char line[65536];
  945. int ret, line_count;
  946. FILE* fp;
  947. char_num = 0;
  948. char_max = 0;
  949. char_dat = NULL;
  950. fp = fopen(char_txt, "r");
  951. if (fp == NULL) {
  952. ShowError("Characters file not found: %s.\n", char_txt);
  953. char_log("Characters file not found: %s.\n", char_txt);
  954. char_log("Id for the next created character: %d.\n", char_id_count);
  955. return 0;
  956. }
  957. line_count = 0;
  958. while(fgets(line, sizeof(line), fp))
  959. {
  960. int i, j;
  961. line_count++;
  962. if (line[0] == '/' && line[1] == '/')
  963. continue;
  964. j = 0;
  965. if (sscanf(line, "%d\t%%newid%%%n", &i, &j) == 1 && j > 0) {
  966. if (char_id_count < i)
  967. char_id_count = i;
  968. continue;
  969. }
  970. if (char_num >= char_max) {
  971. char_max += 256;
  972. char_dat = (struct character_data*)aRealloc(char_dat, sizeof(struct character_data) * char_max);
  973. if (!char_dat) {
  974. ShowFatalError("Out of memory: mmo_char_init (realloc of char_dat).\n");
  975. char_log("Out of memory: mmo_char_init (realloc of char_dat).\n");
  976. exit(EXIT_FAILURE);
  977. }
  978. }
  979. ret = mmo_char_fromstr(line, &char_dat[char_num].status, char_dat[char_num].global, &char_dat[char_num].global_num);
  980. // Initialize friends list
  981. parse_friend_txt(&char_dat[char_num].status); // Grab friends for the character
  982. // Initialize hotkey list
  983. parse_hotkey_txt(&char_dat[char_num].status); // Grab hotkeys for the character
  984. if (ret > 0) { // negative value or zero for errors
  985. if (char_dat[char_num].status.char_id >= char_id_count)
  986. char_id_count = char_dat[char_num].status.char_id + 1;
  987. char_num++;
  988. } else {
  989. ShowError("mmo_char_init: in characters file, unable to read the line #%d.\n", line_count);
  990. ShowError(" -> Character saved in log file.\n");
  991. switch (ret) {
  992. case -1:
  993. char_log("Duplicate character id in the next character line (character not readed):\n");
  994. break;
  995. case -2:
  996. char_log("Duplicate character name in the next character line (character not readed):\n");
  997. break;
  998. case -3:
  999. char_log("Invalid memo point structure in the next character line (character not readed):\n");
  1000. break;
  1001. case -4:
  1002. char_log("Invalid inventory item structure in the next character line (character not readed):\n");
  1003. break;
  1004. case -5:
  1005. char_log("Invalid cart item structure in the next character line (character not readed):\n");
  1006. break;
  1007. case -6:
  1008. char_log("Invalid skill structure in the next character line (character not readed):\n");
  1009. break;
  1010. case -7:
  1011. char_log("Invalid register structure in the next character line (character not readed):\n");
  1012. break;
  1013. default: // 0
  1014. char_log("Unabled to get a character in the next line - Basic structure of line (before inventory) is incorrect (character not readed):\n");
  1015. break;
  1016. }
  1017. char_log("%s", line);
  1018. }
  1019. }
  1020. fclose(fp);
  1021. if (char_num == 0) {
  1022. ShowNotice("mmo_char_init: No character found in %s.\n", char_txt);
  1023. char_log("mmo_char_init: No character found in %s.\n", char_txt);
  1024. } else if (char_num == 1) {
  1025. ShowStatus("mmo_char_init: 1 character read in %s.\n", char_txt);
  1026. char_log("mmo_char_init: 1 character read in %s.\n", char_txt);
  1027. } else {
  1028. ShowStatus("mmo_char_init: %d characters read in %s.\n", char_num, char_txt);
  1029. char_log("mmo_char_init: %d characters read in %s.\n", char_num, char_txt);
  1030. }
  1031. char_log("Id for the next created character: %d.\n", char_id_count);
  1032. return 0;
  1033. }
  1034. //---------------------------------------------------------
  1035. // Function to save characters in files (speed up by [Yor])
  1036. //---------------------------------------------------------
  1037. void mmo_char_sync(void)
  1038. {
  1039. char line[65536],f_line[1024];
  1040. int i, j, k;
  1041. int lock;
  1042. FILE *fp,*f_fp;
  1043. CREATE_BUFFER(id, int, char_num);
  1044. // Sorting before save (by [Yor])
  1045. for(i = 0; i < char_num; i++) {
  1046. id[i] = i;
  1047. for(j = 0; j < i; j++) {
  1048. if ((char_dat[i].status.account_id < char_dat[id[j]].status.account_id) ||
  1049. // if same account id, we sort by slot.
  1050. (char_dat[i].status.account_id == char_dat[id[j]].status.account_id &&
  1051. char_dat[i].status.slot < char_dat[id[j]].status.slot)) {
  1052. for(k = i; k > j; k--)
  1053. id[k] = id[k-1];
  1054. id[j] = i; // id[i]
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. // Data save
  1060. fp = lock_fopen(char_txt, &lock);
  1061. if (fp == NULL) {
  1062. ShowWarning("Server cannot save characters.\n");
  1063. char_log("WARNING: Server cannot save characters.\n");
  1064. } else {
  1065. for(i = 0; i < char_num; i++) {
  1066. mmo_char_tostr(line, &char_dat[id[i]].status, char_dat[id[i]].global, char_dat[id[i]].global_num); // use of sorted index
  1067. fprintf(fp, "%s\n", line);
  1068. }
  1069. fprintf(fp, "%d\t%%newid%%\n", char_id_count);
  1070. lock_fclose(fp, char_txt, &lock);
  1071. }
  1072. // Friends List data save (davidsiaw)
  1073. f_fp = lock_fopen(friends_txt, &lock);
  1074. for(i = 0; i < char_num; i++) {
  1075. mmo_friends_list_data_str(f_line, &char_dat[id[i]].status);
  1076. fprintf(f_fp, "%s\n", f_line);
  1077. }
  1078. lock_fclose(f_fp, friends_txt, &lock);
  1079. #ifdef HOTKEY_SAVING
  1080. // Hotkey List data save (Skotlex)
  1081. f_fp = lock_fopen(hotkeys_txt, &lock);
  1082. for(i = 0; i < char_num; i++) {
  1083. mmo_hotkeys_tostr(f_line, &char_dat[id[i]].status);
  1084. fprintf(f_fp, "%s\n", f_line);
  1085. }
  1086. lock_fclose(f_fp, hotkeys_txt, &lock);
  1087. #endif
  1088. DELETE_BUFFER(id);
  1089. return;
  1090. }
  1091. //----------------------------------------------------
  1092. // Function to save (in a periodic way) datas in files
  1093. //----------------------------------------------------
  1094. int mmo_char_sync_timer(int tid, unsigned int tick, int id, intptr_t data)
  1095. {
  1096. if (save_log)
  1097. ShowInfo("Saving all files...\n");
  1098. mmo_char_sync();
  1099. inter_save();
  1100. return 0;
  1101. }
  1102. int check_char_name(char * name)
  1103. {
  1104. int i;
  1105. // check length of character name
  1106. if( name[0] == '\0' )
  1107. return -2; // empty character name
  1108. // check content of character name
  1109. if( remove_control_chars(name) )
  1110. return -2; // control chars in name
  1111. // check for reserved names
  1112. if( strcmpi(name, main_chat_nick) == 0 || strcmpi(name, wisp_server_name) == 0 )
  1113. return -1; // nick reserved for internal server messages
  1114. // Check Authorised letters/symbols in the name of the character
  1115. if( char_name_option == 1 )
  1116. { // only letters/symbols in char_name_letters are authorised
  1117. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1118. if( strchr(char_name_letters, name[i]) == NULL )
  1119. return -2;
  1120. }
  1121. else if( char_name_option == 2 )
  1122. { // letters/symbols in char_name_letters are forbidden
  1123. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1124. if( strchr(char_name_letters, name[i]) != NULL )
  1125. return -2;
  1126. }
  1127. // check name (already in use?)
  1128. if( name_ignoring_case )
  1129. {
  1130. ARR_FIND( 0, char_num, i, strncmp(char_dat[i].status.name, name, NAME_LENGTH) == 0 );
  1131. }
  1132. else
  1133. {
  1134. ARR_FIND( 0, char_num, i, strncmpi(char_dat[i].status.name, name, NAME_LENGTH) == 0 );
  1135. }
  1136. if( i < char_num )
  1137. return -1; // name already exists
  1138. return 0;
  1139. }
  1140. //-----------------------------------
  1141. // Function to create a new character
  1142. //-----------------------------------
  1143. int make_new_char(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style)
  1144. {
  1145. char name[NAME_LENGTH];
  1146. int i, flag;
  1147. safestrncpy(name, name_, NAME_LENGTH);
  1148. normalize_name(name,TRIM_CHARS);
  1149. flag = check_char_name(name);
  1150. if( flag < 0 )
  1151. return flag;
  1152. //check other inputs
  1153. if((slot >= MAX_CHARS) // slots
  1154. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1155. || (str < 1 || str > 9 || agi < 1 || agi > 9 || vit < 1 || vit > 9 || int_ < 1 || int_ > 9 || dex < 1 || dex > 9 || luk < 1 || luk > 9) // individual stat values
  1156. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1157. return -2; // invalid input
  1158. // check the number of already existing chars in this account
  1159. if( char_per_account != 0 ) {
  1160. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == -1 );
  1161. if( i >= char_per_account )
  1162. return -2; // character account limit exceeded
  1163. }
  1164. // check char slot
  1165. ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == sd->account_id && char_dat[i].status.slot == slot );
  1166. if( i < char_num )
  1167. return -2; // slot already in use
  1168. if (char_num >= char_max) {
  1169. char_max += 256;
  1170. RECREATE(char_dat, struct character_data, char_max);
  1171. if (!char_dat) {
  1172. ShowFatalError("Out of memory: make_new_char (realloc of char_dat).\n");
  1173. char_log("Out of memory: make_new_char (realloc of char_dat).\n");
  1174. exit(EXIT_FAILURE);
  1175. }
  1176. }
  1177. // validation success, log result
  1178. char_log("make new char: account: %d, slot %d, name: %s, stats: %d/%d/%d/%d/%d/%d, hair: %d, hair color: %d.\n",
  1179. sd->account_id, slot, name, str, agi, vit, int_, dex, luk, hair_style, hair_color);
  1180. i = char_num;
  1181. memset(&char_dat[i], 0, sizeof(struct character_data));
  1182. char_dat[i].status.char_id = char_id_count++;
  1183. char_dat[i].status.account_id = sd->account_id;
  1184. char_dat[i].status.slot = slot;
  1185. safestrncpy(char_dat[i].status.name,name,NAME_LENGTH);
  1186. char_dat[i].status.class_ = 0;
  1187. char_dat[i].status.base_level = 1;
  1188. char_dat[i].status.job_level = 1;
  1189. char_dat[i].status.base_exp = 0;
  1190. char_dat[i].status.job_exp = 0;
  1191. char_dat[i].status.zeny = start_zeny;
  1192. char_dat[i].status.str = str;
  1193. char_dat[i].status.agi = agi;
  1194. char_dat[i].status.vit = vit;
  1195. char_dat[i].status.int_ = int_;
  1196. char_dat[i].status.dex = dex;
  1197. char_dat[i].status.luk = luk;
  1198. char_dat[i].status.max_hp = 40 * (100 + char_dat[i].status.vit) / 100;
  1199. char_dat[i].status.max_sp = 11 * (100 + char_dat[i].status.int_) / 100;
  1200. char_dat[i].status.hp = char_dat[i].status.max_hp;
  1201. char_dat[i].status.sp = char_dat[i].status.max_sp;
  1202. char_dat[i].status.status_point = 0;
  1203. char_dat[i].status.skill_point = 0;
  1204. char_dat[i].status.option = 0;
  1205. char_dat[i].status.karma = 0;
  1206. char_dat[i].status.manner = 0;
  1207. char_dat[i].status.party_id = 0;
  1208. char_dat[i].status.guild_id = 0;
  1209. char_dat[i].status.hair = hair_style;
  1210. char_dat[i].status.hair_color = hair_color;
  1211. char_dat[i].status.clothes_color = 0;
  1212. char_dat[i].status.inventory[0].nameid = start_weapon; // Knife
  1213. char_dat[i].status.inventory[0].amount = 1;
  1214. char_dat[i].status.inventory[0].identify = 1;
  1215. char_dat[i].status.inventory[1].nameid = start_armor; // Cotton Shirt
  1216. char_dat[i].status.inventory[1].amount = 1;
  1217. char_dat[i].status.inventory[1].identify = 1;
  1218. char_dat[i].status.weapon = 0; // W_FIST
  1219. char_dat[i].status.shield = 0;
  1220. char_dat[i].status.head_top = 0;
  1221. char_dat[i].status.head_mid = 0;
  1222. char_dat[i].status.head_bottom = 0;
  1223. memcpy(&char_dat[i].status.last_point, &start_point, sizeof(start_point));
  1224. memcpy(&char_dat[i].status.save_point, &start_point, sizeof(start_point));
  1225. char_num++;
  1226. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, i, slot, name);
  1227. mmo_char_sync();
  1228. return i;
  1229. }
  1230. //----------------------------------------------------
  1231. // This function return the name of the job (by [Yor])
  1232. //----------------------------------------------------
  1233. char * job_name(int class_)
  1234. {
  1235. switch (class_) {
  1236. case JOB_NOVICE: return "Novice";
  1237. case JOB_SWORDMAN: return "Swordsman";
  1238. case JOB_MAGE: return "Mage";
  1239. case JOB_ARCHER: return "Archer";
  1240. case JOB_ACOLYTE: return "Acolyte";
  1241. case JOB_MERCHANT: return "Merchant";
  1242. case JOB_THIEF: return "Thief";
  1243. case JOB_KNIGHT: return "Knight";
  1244. case JOB_PRIEST: return "Priest";
  1245. case JOB_WIZARD: return "Wizard";
  1246. case JOB_BLACKSMITH: return "Blacksmith";
  1247. case JOB_HUNTER: return "Hunter";
  1248. case JOB_ASSASSIN: return "Assassin";
  1249. case JOB_KNIGHT2: return "Peco-Knight";
  1250. case JOB_CRUSADER: return "Crusader";
  1251. case JOB_MONK: return "Monk";
  1252. case JOB_SAGE: return "Sage";
  1253. case JOB_ROGUE: return "Rogue";
  1254. case JOB_ALCHEMIST: return "Alchemist";
  1255. case JOB_BARD: return "Bard";
  1256. case JOB_DANCER: return "Dancer";
  1257. case JOB_CRUSADER2: return "Peco-Crusader";
  1258. case JOB_WEDDING: return "Wedding";
  1259. case JOB_SUPER_NOVICE: return "Super Novice";
  1260. case JOB_GUNSLINGER: return "Gunslinger";
  1261. case JOB_NINJA: return "Ninja";
  1262. case JOB_XMAS: return "Christmas";
  1263. case JOB_NOVICE_HIGH: return "Novice High";
  1264. case JOB_SWORDMAN_HIGH: return "Swordsman High";
  1265. case JOB_MAGE_HIGH: return "Mage High";
  1266. case JOB_ARCHER_HIGH: return "Archer High";
  1267. case JOB_ACOLYTE_HIGH: return "Acolyte High";
  1268. case JOB_MERCHANT_HIGH: return "Merchant High";
  1269. case JOB_THIEF_HIGH: return "Thief High";
  1270. case JOB_LORD_KNIGHT: return "Lord Knight";
  1271. case JOB_HIGH_PRIEST: return "High Priest";
  1272. case JOB_HIGH_WIZARD: return "High Wizard";
  1273. case JOB_WHITESMITH: return "Whitesmith";
  1274. case JOB_SNIPER: return "Sniper";
  1275. case JOB_ASSASSIN_CROSS: return "Assassin Cross";
  1276. case JOB_LORD_KNIGHT2: return "Peko Knight";
  1277. case JOB_PALADIN: return "Paladin";
  1278. case JOB_CHAMPION: return "Champion";
  1279. case JOB_PROFESSOR: return "Professor";
  1280. case JOB_STALKER: return "Stalker";
  1281. case JOB_CREATOR: return "Creator";
  1282. case JOB_CLOWN: return "Clown";
  1283. case JOB_GYPSY: return "Gypsy";
  1284. case JOB_PALADIN2: return "Peko Paladin";
  1285. case JOB_BABY: return "Baby Novice";
  1286. case JOB_BABY_SWORDMAN: return "Baby Swordsman";
  1287. case JOB_BABY_MAGE: return "Baby Mage";
  1288. case JOB_BABY_ARCHER: return "Baby Archer";
  1289. case JOB_BABY_ACOLYTE: return "Baby Acolyte";
  1290. case JOB_BABY_MERCHANT: return "Baby Merchant";
  1291. case JOB_BABY_THIEF: return "Baby Thief";
  1292. case JOB_BABY_KNIGHT: return "Baby Knight";
  1293. case JOB_BABY_PRIEST: return "Baby Priest";
  1294. case JOB_BABY_WIZARD: return "Baby Wizard";
  1295. case JOB_BABY_BLACKSMITH: return "Baby Blacksmith";
  1296. case JOB_BABY_HUNTER: return "Baby Hunter";
  1297. case JOB_BABY_ASSASSIN: return "Baby Assassin";
  1298. case JOB_BABY_KNIGHT2: return "Baby Peco Knight";
  1299. case JOB_BABY_CRUSADER: return "Baby Crusader";
  1300. case JOB_BABY_MONK: return "Baby Monk";
  1301. case JOB_BABY_SAGE: return "Baby Sage";
  1302. case JOB_BABY_ROGUE: return "Baby Rogue";
  1303. case JOB_BABY_ALCHEMIST: return "Baby Alchemist";
  1304. case JOB_BABY_BARD: return "Baby Bard";
  1305. case JOB_BABY_DANCER: return "Baby Dancer";
  1306. case JOB_BABY_CRUSADER2: return "Baby Peco Crusader";
  1307. case JOB_SUPER_BABY: return "Super Baby";
  1308. case JOB_TAEKWON: return "Taekwon";
  1309. case JOB_STAR_GLADIATOR: return "Star Gladiator";
  1310. case JOB_STAR_GLADIATOR2: return "Flying Star Gladiator";
  1311. case JOB_SOUL_LINKER: return "Soul Linker";
  1312. }
  1313. return "Unknown Job";
  1314. }
  1315. static int create_online_files_sub(DBKey key, void* data, va_list va)
  1316. {
  1317. struct online_char_data *character;
  1318. int* players;
  1319. int *id;
  1320. int j,k,l;
  1321. character = (struct online_char_data*) data;
  1322. players = va_arg(va, int*);
  1323. id = va_arg(va, int*);
  1324. // check if map-server is online
  1325. if (character->server == -1 || character->char_id == -1) { //Character not currently online.
  1326. return -1;
  1327. }
  1328. j = character->server;
  1329. if (server[j].fd < 0) {
  1330. server[j].users = 0;
  1331. return -1;
  1332. }
  1333. // search position of character in char_dat and sort online characters.
  1334. for(j = 0; j < char_num; j++) {
  1335. if (char_dat[j].status.char_id != character->char_id)
  1336. continue;
  1337. id[*players] = j;
  1338. // use sorting option
  1339. switch (online_sorting_option) {
  1340. case 1: // by name (without case sensitive)
  1341. for(k = 0; k < *players; k++)
  1342. if (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0 ||
  1343. // if same name, we sort with case sensitive.
  1344. (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) == 0 &&
  1345. strcmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) {
  1346. for(l = *players; l > k; l--)
  1347. id[l] = id[l-1];
  1348. id[k] = j; // id[*players]
  1349. break;
  1350. }
  1351. break;
  1352. case 2: // by zeny
  1353. for(k = 0; k < *players; k++)
  1354. if (char_dat[j].status.zeny < char_dat[id[k]].status.zeny ||
  1355. // if same number of zenys, we sort by name.
  1356. (char_dat[j].status.zeny == char_dat[id[k]].status.zeny &&
  1357. stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) {
  1358. for(l = *players; l > k; l--)
  1359. id[l] = id[l-1];
  1360. id[k] = j; // id[*players]
  1361. break;
  1362. }
  1363. break;
  1364. case 3: // by base level
  1365. for(k = 0; k < *players; k++)
  1366. if (char_dat[j].status.base_level < char_dat[id[k]].status.base_level ||
  1367. // if same base level, we sort by base exp.
  1368. (char_dat[j].status.base_level == char_dat[id[k]].status.base_level &&
  1369. char_dat[j].status.base_exp < char_dat[id[k]].status.base_exp)) {
  1370. for(l = *players; l > k; l--)
  1371. id[l] = id[l-1];
  1372. id[k] = j; // id[*players]
  1373. break;
  1374. }
  1375. break;
  1376. case 4: // by job (and job level)
  1377. for(k = 0; k < *players; k++)
  1378. if (char_dat[j].status.class_ < char_dat[id[k]].status.class_ ||
  1379. // if same job, we sort by job level.
  1380. (char_dat[j].status.class_ == char_dat[id[k]].status.class_ &&
  1381. char_dat[j].status.job_level < char_dat[id[k]].status.job_level) ||
  1382. // if same job and job level, we sort by job exp.
  1383. (char_dat[j].status.class_ == char_dat[id[k]].status.class_ &&
  1384. char_dat[j].status.job_level == char_dat[id[k]].status.job_level &&
  1385. char_dat[j].status.job_exp < char_dat[id[k]].status.job_exp)) {
  1386. for(l = *players; l > k; l--)
  1387. id[l] = id[l-1];
  1388. id[k] = j; // id[*players]
  1389. break;
  1390. }
  1391. break;
  1392. case 5: // by location map name
  1393. {
  1394. const char *map1, *map2;
  1395. map1 = mapindex_id2name(char_dat[j].status.last_point.map);
  1396. for(k = 0; k < *players; k++) {
  1397. map2 = mapindex_id2name(char_dat[id[k]].status.last_point.map);
  1398. if (!map1 || !map2 || //Avoid sorting if either one failed to resolve.
  1399. stricmp(map1, map2) < 0 ||
  1400. // if same map name, we sort by name.
  1401. (stricmp(map1, map2) == 0 &&
  1402. stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) {
  1403. for(l = *players; l > k; l--)
  1404. id[l] = id[l-1];
  1405. id[k] = j; // id[*players]
  1406. break;
  1407. }
  1408. }
  1409. }
  1410. break;
  1411. default: // 0 or invalid value: no sorting
  1412. break;
  1413. }
  1414. (*players)++;
  1415. break;
  1416. }
  1417. return 0;
  1418. }
  1419. //-------------------------------------------------------------
  1420. // Function to create the online files (txt and html). by [Yor]
  1421. //-------------------------------------------------------------
  1422. void create_online_files(void)
  1423. {
  1424. unsigned int k, j; // for loop with strlen comparing
  1425. int i, l; // for loops
  1426. int players; // count the number of players
  1427. FILE *fp; // for the txt file
  1428. FILE *fp2; // for the html file
  1429. char temp[256]; // to prepare what we must display
  1430. time_t time_server; // for number of seconds
  1431. struct tm *datetime; // variable for time in structure ->tm_mday, ->tm_sec, ...
  1432. int id[4096];
  1433. if (online_display_option == 0) // we display nothing, so return
  1434. return;
  1435. // Get number of online players, id of each online players, and verify if a server is offline
  1436. players = 0;
  1437. online_char_db->foreach(online_char_db, create_online_files_sub, &players, &id);
  1438. // write files
  1439. fp = fopen(online_txt_filename, "w");
  1440. if (fp != NULL) {
  1441. fp2 = fopen(online_html_filename, "w");
  1442. if (fp2 != NULL) {
  1443. // get time
  1444. time(&time_server); // get time in seconds since 1/1/1970
  1445. datetime = localtime(&time_server); // convert seconds in structure
  1446. strftime(temp, sizeof(temp), "%d %b %Y %X", datetime); // like sprintf, but only for date/time (05 dec 2003 15:12:52)
  1447. // write heading
  1448. fprintf(fp2, "<HTML>\n");
  1449. fprintf(fp2, " <META http-equiv=\"Refresh\" content=\"%d\">\n", online_refresh_html); // update on client explorer every x seconds
  1450. fprintf(fp2, " <HEAD>\n");
  1451. fprintf(fp2, " <TITLE>Online Players on %s</TITLE>\n", server_name);
  1452. fprintf(fp2, " </HEAD>\n");
  1453. fprintf(fp2, " <BODY>\n");
  1454. fprintf(fp2, " <H3>Online Players on %s (%s):</H3>\n", server_name, temp);
  1455. fprintf(fp, "Online Players on %s (%s):\n", server_name, temp);
  1456. fprintf(fp, "\n");
  1457. for (i = 0; i < players; i++) {
  1458. // if it's the first player
  1459. if (i == 0) {
  1460. j = 0; // count the number of characters for the txt version and to set the separate line
  1461. fprintf(fp2, " <table border=\"1\" cellspacing=\"1\">\n");
  1462. fprintf(fp2, " <tr>\n");
  1463. if ((online_display_option & 1) || (online_display_option & 64)) {
  1464. fprintf(fp2, " <td><b>Name</b></td>\n");
  1465. if (online_display_option & 64) {
  1466. fprintf(fp, "Name "); // 30
  1467. j += 30;
  1468. } else {
  1469. fprintf(fp, "Name "); // 25
  1470. j += 25;
  1471. }
  1472. }
  1473. if ((online_display_option & 6) == 6) {
  1474. fprintf(fp2, " <td><b>Job (levels)</b></td>\n");
  1475. fprintf(fp, "Job Levels "); // 27
  1476. j += 27;
  1477. } else if (online_display_option & 2) {
  1478. fprintf(fp2, " <td><b>Job</b></td>\n");
  1479. fprintf(fp, "Job "); // 19
  1480. j += 19;
  1481. } else if (online_display_option & 4) {
  1482. fprintf(fp2, " <td><b>Levels</b></td>\n");
  1483. fprintf(fp, " Levels "); // 8
  1484. j += 8;
  1485. }
  1486. if (online_display_option & 24) { // 8 or 16
  1487. fprintf(fp2, " <td><b>Location</b></td>\n");
  1488. if (online_display_option & 16) {
  1489. fprintf(fp, "Location ( x , y ) "); // 23
  1490. j += 23;
  1491. } else {
  1492. fprintf(fp, "Location "); // 13
  1493. j += 13;
  1494. }
  1495. }
  1496. if (online_display_option & 32) {
  1497. fprintf(fp2, " <td ALIGN=CENTER><b>zenys</b></td>\n");
  1498. fprintf(fp, " Zenys "); // 16
  1499. j += 16;
  1500. }
  1501. fprintf(fp2, " </tr>\n");
  1502. fprintf(fp, "\n");
  1503. for (k = 0; k < j; k++)
  1504. fprintf(fp, "-");
  1505. fprintf(fp, "\n");
  1506. }
  1507. fprintf(fp2, " <tr>\n");
  1508. // get id of the character (more speed)
  1509. j = id[i];
  1510. // displaying the character name
  1511. if ((online_display_option & 1) || (online_display_option & 64)) { // without/with 'GM' display
  1512. safestrncpy(temp, char_dat[j].status.name, sizeof(temp));
  1513. //l = isGM(char_dat[j].status.account_id);
  1514. l = 0; //FIXME: how to get the gm level?
  1515. if (online_display_option & 64) {
  1516. if (l >= online_gm_display_min_level)
  1517. fprintf(fp, "%-24s (GM) ", temp);
  1518. else
  1519. fprintf(fp, "%-24s ", temp);
  1520. } else
  1521. fprintf(fp, "%-24s ", temp);
  1522. // name of the character in the html (no < >, because that create problem in html code)
  1523. fprintf(fp2, " <td>");
  1524. if ((online_display_option & 64) && l >= online_gm_display_min_level)
  1525. fprintf(fp2, "<b>");
  1526. for (k = 0; k < strlen(temp); k++) {
  1527. switch(temp[k]) {
  1528. case '<': // <
  1529. fprintf(fp2, "&lt;");
  1530. break;
  1531. case '>': // >
  1532. fprintf(fp2, "&gt;");
  1533. break;
  1534. default:
  1535. fprintf(fp2, "%c", temp[k]);
  1536. break;
  1537. };
  1538. }
  1539. if ((online_display_option & 64) && l >= online_gm_display_min_level)
  1540. fprintf(fp2, "</b> (GM)");
  1541. fprintf(fp2, "</td>\n");
  1542. }
  1543. // displaying of the job
  1544. if (online_display_option & 6) {
  1545. char * jobname = job_name(char_dat[j].status.class_);
  1546. if ((online_display_option & 6) == 6) {
  1547. fprintf(fp2, " <td>%s %d/%d</td>\n", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level);
  1548. fprintf(fp, "%-18s %3d/%3d ", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level);
  1549. } else if (online_display_option & 2) {
  1550. fprintf(fp2, " <td>%s</td>\n", jobname);
  1551. fprintf(fp, "%-18s ", jobname);
  1552. } else if (online_display_option & 4) {
  1553. fprintf(fp2, " <td>%d/%d</td>\n", char_dat[j].status.base_level, char_dat[j].status.job_level);
  1554. fprintf(fp, "%3d/%3d ", char_dat[j].status.base_level, char_dat[j].status.job_level);
  1555. }
  1556. }
  1557. // displaying of the map
  1558. if (online_display_option & 24) { // 8 or 16
  1559. // prepare map name
  1560. safestrncpy(temp, mapindex_id2name(char_dat[j].status.last_point.map), sizeof(temp));
  1561. // write map name
  1562. if (online_display_option & 16) { // map-name AND coordinates
  1563. fprintf(fp2, " <td>%s (%d, %d)</td>\n", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y);
  1564. fprintf(fp, "%-12s (%3d,%3d) ", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y);
  1565. } else {
  1566. fprintf(fp2, " <td>%s</td>\n", temp);
  1567. fprintf(fp, "%-12s ", temp);
  1568. }
  1569. }
  1570. // displaying nimber of zenys
  1571. if (online_display_option & 32) {
  1572. // write number of zenys
  1573. if (char_dat[j].status.zeny == 0) { // if no zeny
  1574. fprintf(fp2, " <td ALIGN=RIGHT>no zeny</td>\n");
  1575. fprintf(fp, " no zeny ");
  1576. } else {
  1577. fprintf(fp2, " <td ALIGN=RIGHT>%d z</td>\n", char_dat[j].status.zeny);
  1578. fprintf(fp, "%13d z ", char_dat[j].status.zeny);
  1579. }
  1580. }
  1581. fprintf(fp, "\n");
  1582. fprintf(fp2, " </tr>\n");
  1583. }
  1584. // If we display at least 1 player
  1585. if (players > 0) {
  1586. fprintf(fp2, " </table>\n");
  1587. fprintf(fp, "\n");
  1588. }
  1589. // Displaying number of online players
  1590. if (players == 0) {
  1591. fprintf(fp2, " <p>No user is online.</p>\n");
  1592. fprintf(fp, "No user is online.\n");
  1593. } else if (players == 1) {
  1594. fprintf(fp2, " <p>%d user is online.</p>\n", players);
  1595. fprintf(fp, "%d user is online.\n", players);
  1596. } else {
  1597. fprintf(fp2, " <p>%d users are online.</p>\n", players);
  1598. fprintf(fp, "%d users are online.\n", players);
  1599. }
  1600. fprintf(fp2, " </BODY>\n");
  1601. fprintf(fp2, "</HTML>\n");
  1602. fclose(fp2);
  1603. }
  1604. fclose(fp);
  1605. }
  1606. return;
  1607. }
  1608. //---------------------------------------------------------------------
  1609. // This function return the number of online players in all map-servers
  1610. //---------------------------------------------------------------------
  1611. int count_users(void)
  1612. {
  1613. int i, users;
  1614. users = 0;
  1615. for(i = 0; i < ARRAYLENGTH(server); i++) {
  1616. if (server[i].fd > 0) {
  1617. users += server[i].users;
  1618. }
  1619. }
  1620. return users;
  1621. }
  1622. // Writes char data to the buffer in the format used by the client.
  1623. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1624. // Returns the size
  1625. #define MAX_CHAR_BUF 136 //Max size (for WFIFOHEAD calls)
  1626. int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1627. {
  1628. unsigned short offset = 0;
  1629. uint8* buf;
  1630. if( buffer == NULL || p == NULL )
  1631. return 0;
  1632. buf = WBUFP(buffer,0);
  1633. WBUFL(buf,0) = p->char_id;
  1634. WBUFL(buf,4) = min(p->base_exp, INT32_MAX);
  1635. WBUFL(buf,8) = p->zeny;
  1636. WBUFL(buf,12) = min(p->job_exp, INT32_MAX);
  1637. WBUFL(buf,16) = p->job_level;
  1638. WBUFL(buf,20) = 0; // probably opt1
  1639. WBUFL(buf,24) = 0; // probably opt2
  1640. WBUFL(buf,28) = p->option;
  1641. WBUFL(buf,32) = p->karma;
  1642. WBUFL(buf,36) = p->manner;
  1643. WBUFW(buf,40) = min(p->status_point, INT16_MAX);
  1644. #if PACKETVER > 20081217
  1645. WBUFL(buf,42) = p->hp;
  1646. WBUFL(buf,46) = p->max_hp;
  1647. offset+=4;
  1648. buf = WBUFP(buffer,offset);
  1649. #else
  1650. WBUFW(buf,42) = min(p->hp, INT16_MAX);
  1651. WBUFW(buf,44) = min(p->max_hp, INT16_MAX);
  1652. #endif
  1653. WBUFW(buf,46) = min(p->sp, INT16_MAX);
  1654. WBUFW(buf,48) = min(p->max_sp, INT16_MAX);
  1655. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1656. WBUFW(buf,52) = p->class_;
  1657. WBUFW(buf,54) = p->hair;
  1658. WBUFW(buf,56) = p->option&0x20 ? 0 : p->weapon; //When the weapon is sent and your option is riding, the client crashes on login!?
  1659. WBUFW(buf,58) = p->base_level;
  1660. WBUFW(buf,60) = min(p->skill_point, INT16_MAX);
  1661. WBUFW(buf,62) = p->head_bottom;
  1662. WBUFW(buf,64) = p->shield;
  1663. WBUFW(buf,66) = p->head_top;
  1664. WBUFW(buf,68) = p->head_mid;
  1665. WBUFW(buf,70) = p->hair_color;
  1666. WBUFW(buf,72) = p->clothes_color;
  1667. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1668. WBUFB(buf,98) = min(p->str, UINT8_MAX);
  1669. WBUFB(buf,99) = min(p->agi, UINT8_MAX);
  1670. WBUFB(buf,100) = min(p->vit, UINT8_MAX);
  1671. WBUFB(buf,101) = min(p->int_, UINT8_MAX);
  1672. WBUFB(buf,102) = min(p->dex, UINT8_MAX);
  1673. WBUFB(buf,103) = min(p->luk, UINT8_MAX);
  1674. WBUFW(buf,104) = p->slot;
  1675. #if PACKETVER >= 20061023
  1676. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1677. offset += 2;
  1678. #endif
  1679. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1680. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
  1681. offset += MAP_NAME_LENGTH_EXT;
  1682. #endif
  1683. #if PACKETVER >= 20100803
  1684. WBUFL(buf,124) = TOL(p->delete_date);
  1685. offset += 4;
  1686. #endif
  1687. #if PACKETVER >= 20110111
  1688. WBUFL(buf,128) = p->robe;
  1689. offset += 4;
  1690. #endif
  1691. return 106+offset;
  1692. }
  1693. //----------------------------------------
  1694. // Function to send characters to a player
  1695. //----------------------------------------
  1696. int mmo_char_send006b(int fd, struct char_session_data* sd)
  1697. {
  1698. int i, j, found_num, offset = 0;
  1699. #if PACKETVER >= 20100413
  1700. offset += 3;
  1701. #endif
  1702. found_num = char_find_characters(sd);
  1703. j = 24 + offset; // offset
  1704. WFIFOHEAD(fd,j + found_num*MAX_CHAR_BUF);
  1705. WFIFOW(fd,0) = 0x6b;
  1706. #if PACKETVER >= 20100413
  1707. WFIFOB(fd,4) = MAX_CHARS; // Max slots.
  1708. WFIFOB(fd,5) = MAX_CHARS; // Available slots.
  1709. WFIFOB(fd,6) = MAX_CHARS; // Premium slots.
  1710. #endif
  1711. memset(WFIFOP(fd,4 + offset), 0, 20); // unknown bytes
  1712. for(i = 0; i < found_num; i++)
  1713. j += mmo_char_tobuf(WFIFOP(fd,j), &char_dat[sd->found_char[i]].status);
  1714. WFIFOW(fd,2) = j; // packet len
  1715. WFIFOSET(fd,j);
  1716. return 0;
  1717. }
  1718. // 離婚(char削除時に使用)
  1719. int char_divorce(struct mmo_charstatus *cs)
  1720. {
  1721. if (cs == NULL)
  1722. return 0;
  1723. if (cs->partner_id > 0){
  1724. int i, j;
  1725. for(i = 0; i < char_num; i++) {
  1726. if (char_dat[i].status.char_id == cs->partner_id && char_dat[i].status.partner_id == cs->char_id) {
  1727. cs->partner_id = 0;
  1728. char_dat[i].status.partner_id = 0;
  1729. for(j = 0; j < MAX_INVENTORY; j++)
  1730. {
  1731. if (char_dat[i].status.inventory[j].nameid == WEDDING_RING_M || char_dat[i].status.inventory[j].nameid == WEDDING_RING_F)
  1732. memset(&char_dat[i].status.inventory[j], 0, sizeof(char_dat[i].status.inventory[0]));
  1733. if (cs->inventory[j].nameid == WEDDING_RING_M || cs->inventory[j].nameid == WEDDING_RING_F)
  1734. memset(&cs->inventory[j], 0, sizeof(cs->inventory[0]));
  1735. }
  1736. return 0;
  1737. }
  1738. }
  1739. }
  1740. return 0;
  1741. }
  1742. int char_married(int pl1, int pl2)
  1743. {
  1744. return (char_dat[pl1].status.char_id == char_dat[pl2].status.partner_id && char_dat[pl2].status.char_id == char_dat[pl1].status.partner_id);
  1745. }
  1746. int char_child(int parent_id, int child_id)
  1747. {
  1748. return (char_dat[parent_id].status.child == char_dat[child_id].status.char_id &&
  1749. ((char_dat[parent_id].status.char_id == char_dat[child_id].status.father) ||
  1750. (char_dat[parent_id].status.char_id == char_dat[child_id].status.mother)));
  1751. }
  1752. int char_family(int cid1, int cid2, int cid3)
  1753. {
  1754. int i, idx1 = -1, idx2 =-1;//, idx3 =-1;
  1755. for(i = 0; i < char_num && (idx1 == -1 || idx2 == -1/* || idx3 == 1*/); i++)
  1756. {
  1757. if (char_dat[i].status.char_id == cid1)
  1758. idx1 = i;
  1759. if (char_dat[i].status.char_id == cid2)
  1760. idx2 = i;
  1761. // if (char_dat[i].status.char_id == cid2)
  1762. // idx3 = i;
  1763. }
  1764. if (idx1 == -1 || idx2 == -1/* || idx3 == -1*/)
  1765. return 0; //Some character not found??
  1766. //Unless the dbs are corrupted, these 3 checks should suffice, even though
  1767. //we could do a lot more checks and force cross-reference integrity.
  1768. if(char_dat[idx1].status.partner_id == cid2 &&
  1769. char_dat[idx1].status.child == cid3)
  1770. return cid3; //cid1/cid2 parents. cid3 child.
  1771. if(char_dat[idx1].status.partner_id == cid3 &&
  1772. char_dat[idx1].status.child == cid2)
  1773. return cid2; //cid1/cid3 parents. cid2 child.
  1774. if(char_dat[idx2].status.partner_id == cid3 &&
  1775. char_dat[idx2].status.child == cid1)
  1776. return cid1; //cid2/cid3 parents. cid1 child.
  1777. return 0;
  1778. }
  1779. //----------------------------------------------------------------------
  1780. // Force disconnection of an online player (with account value) by [Yor]
  1781. //----------------------------------------------------------------------
  1782. void disconnect_player(int account_id)
  1783. {
  1784. int i;
  1785. struct char_session_data* sd;
  1786. // disconnect player if online on char-server
  1787. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1788. if( i < fd_max )
  1789. set_eof(i);
  1790. }
  1791. // キャラ削除に伴うデータ削除
  1792. static int char_delete(struct mmo_charstatus *cs)
  1793. {
  1794. int j;
  1795. // ペット削除
  1796. if (cs->pet_id)
  1797. inter_pet_delete(cs->pet_id);
  1798. if (cs->hom_id)
  1799. inter_homun_delete(cs->hom_id);
  1800. for (j = 0; j < MAX_INVENTORY; j++)
  1801. if (cs->inventory[j].card[0] == (short)0xff00)
  1802. inter_pet_delete(MakeDWord(cs->inventory[j].card[1],cs->inventory[j].card[2]));
  1803. for (j = 0; j < MAX_CART; j++)
  1804. if (cs->cart[j].card[0] == (short)0xff00)
  1805. inter_pet_delete( MakeDWord(cs->cart[j].card[1],cs->cart[j].card[2]) );
  1806. // ギルド脱退
  1807. if (cs->guild_id)
  1808. inter_guild_leave(cs->guild_id, cs->account_id, cs->char_id);
  1809. // パーティー脱退
  1810. if (cs->party_id)
  1811. inter_party_leave(cs->party_id, cs->account_id, cs->char_id);
  1812. // 離婚
  1813. if (cs->partner_id){
  1814. // 離婚情報をmapに通知
  1815. unsigned char buf[10];
  1816. WBUFW(buf,0) = 0x2b12;
  1817. WBUFL(buf,2) = cs->char_id;
  1818. WBUFL(buf,6) = cs->partner_id;
  1819. mapif_sendall(buf,10);
  1820. // 離婚
  1821. char_divorce(cs);
  1822. }
  1823. #ifdef ENABLE_SC_SAVING
  1824. status_delete_scdata(cs->account_id, cs->char_id);
  1825. #endif
  1826. return 0;
  1827. }
  1828. static void char_auth_ok(int fd, struct char_session_data *sd)
  1829. {
  1830. struct online_char_data* character;
  1831. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1832. { // check if character is not online already. [Skotlex]
  1833. if (character->server > -1)
  1834. { //Character already online. KICK KICK KICK
  1835. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1836. if (character->waiting_disconnect == INVALID_TIMER)
  1837. character->waiting_disconnect = add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  1838. WFIFOHEAD(fd,3);
  1839. WFIFOW(fd,0) = 0x81;
  1840. WFIFOB(fd,2) = 8;
  1841. WFIFOSET(fd,3);
  1842. return;
  1843. }
  1844. if (character->fd >= 0 && character->fd != fd)
  1845. { //There's already a connection from this account that hasn't picked a char yet.
  1846. WFIFOHEAD(fd,3);
  1847. WFIFOW(fd,0) = 0x81;
  1848. WFIFOB(fd,2) = 8;
  1849. WFIFOSET(fd,3);
  1850. return;
  1851. }
  1852. character->fd = fd;
  1853. }
  1854. if (login_fd > 0) {
  1855. // request account data
  1856. WFIFOHEAD(login_fd,6);
  1857. WFIFOW(login_fd,0) = 0x2716;
  1858. WFIFOL(login_fd,2) = sd->account_id;
  1859. WFIFOSET(login_fd,6);
  1860. }
  1861. // mark session as 'authed'
  1862. sd->auth = true;
  1863. // set char online on charserver
  1864. set_char_charselect(sd->account_id);
  1865. // continues when account data is received...
  1866. }
  1867. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data);
  1868. void mapif_server_reset(int id);
  1869. /// Resets all the data.
  1870. void loginif_reset(void)
  1871. {
  1872. int id;
  1873. // TODO kick everyone out and reset everything or wait for connect and try to reaquire locks [FlavioJS]
  1874. for( id = 0; id < ARRAYLENGTH(server); ++id )
  1875. mapif_server_reset(id);
  1876. flush_fifos();
  1877. exit(EXIT_FAILURE);
  1878. }
  1879. /// Checks the conditions for the server to stop.
  1880. /// If all the conditions are met, it stops the core loop.
  1881. void loginif_check_shutdown(void)
  1882. {
  1883. if( runflag != CHARSERVER_ST_SHUTDOWN )
  1884. return;
  1885. runflag = CORE_ST_STOP;
  1886. }
  1887. /// Called when the connection to Login Server is disconnected.
  1888. void loginif_on_disconnect(void)
  1889. {
  1890. ShowWarning("Connection to Login Server lost.\n\n");
  1891. }
  1892. /// Called when all the connection steps are completed.
  1893. void loginif_on_ready(void)
  1894. {
  1895. int i;
  1896. loginif_check_shutdown();
  1897. //Send online accounts to login server.
  1898. send_accounts_tologin(INVALID_TIMER, gettick(), 0, 0);
  1899. // if no map-server already connected, display a message...
  1900. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd > 0 && server[i].map[0] );
  1901. if( i == ARRAYLENGTH(server) )
  1902. ShowStatus("Awaiting maps from map-server.\n");
  1903. }
  1904. int parse_fromlogin(int fd)
  1905. {
  1906. struct char_session_data* sd = NULL;
  1907. int i;
  1908. // only process data from the login-server
  1909. if( fd != login_fd )
  1910. {
  1911. ShowDebug("parse_fromlogin: Disconnecting invalid session #%d (is not the login-server)\n", fd);
  1912. do_close(fd);
  1913. return 0;
  1914. }
  1915. if( session[fd]->flag.eof )
  1916. {
  1917. do_close(fd);
  1918. login_fd = -1;
  1919. loginif_on_disconnect();
  1920. return 0;
  1921. }
  1922. sd = (struct char_session_data*)session[fd]->session_data;
  1923. while(RFIFOREST(fd) >= 2)
  1924. {
  1925. uint16 command = RFIFOW(fd,0);
  1926. switch( command )
  1927. {
  1928. // acknowledgement of connect-to-loginserver request
  1929. case 0x2711:
  1930. if (RFIFOREST(fd) < 3)
  1931. return 0;
  1932. if (RFIFOB(fd,2)) {
  1933. //printf("connect login server error : %d\n", RFIFOB(fd,2));
  1934. ShowError("Can not connect to login-server.\n");
  1935. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1936. ShowError("Also, please make sure your accounts file (default: accounts.txt) has the correct communication username/passwords and the gender of the account is S.\n");
  1937. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1938. set_eof(fd);
  1939. return 0;
  1940. } else {
  1941. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1942. loginif_on_ready();
  1943. }
  1944. RFIFOSKIP(fd,3);
  1945. break;
  1946. // acknowledgement of account authentication request
  1947. case 0x2713:
  1948. if (RFIFOREST(fd) < 25)
  1949. return 0;
  1950. {
  1951. int account_id = RFIFOL(fd,2);
  1952. uint32 login_id1 = RFIFOL(fd,6);
  1953. uint32 login_id2 = RFIFOL(fd,10);
  1954. uint8 sex = RFIFOB(fd,14);
  1955. uint8 result = RFIFOB(fd,15);
  1956. int request_id = RFIFOL(fd,16);
  1957. uint32 version = RFIFOL(fd,20);
  1958. uint8 clienttype = RFIFOB(fd,24);
  1959. RFIFOSKIP(fd,25);
  1960. if( session_isActive(request_id) && (sd=(struct char_session_data*)session[request_id]->session_data) &&
  1961. !sd->auth && sd->account_id == account_id && sd->login_id1 == login_id1 && sd->login_id2 == login_id2 && sd->sex == sex )
  1962. {
  1963. int client_fd = request_id;
  1964. sd->version = version;
  1965. sd->clienttype = clienttype;
  1966. switch( result )
  1967. {
  1968. case 0:// ok
  1969. char_auth_ok(client_fd, sd);
  1970. break;
  1971. case 1:// auth failed
  1972. WFIFOHEAD(client_fd,3);
  1973. WFIFOW(client_fd,0) = 0x6c;
  1974. WFIFOB(client_fd,2) = 0;// rejected from server
  1975. WFIFOSET(client_fd,3);
  1976. break;
  1977. }
  1978. }
  1979. }
  1980. break;
  1981. case 0x2717: // account data
  1982. if (RFIFOREST(fd) < 62)
  1983. return 0;
  1984. // find the authenticated session with this account id
  1985. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->auth && sd->account_id == RFIFOL(fd,2) );
  1986. if( i < fd_max )
  1987. {
  1988. memcpy(sd->email, RFIFOP(fd,6), 40);
  1989. sd->expiration_time = (time_t)RFIFOL(fd,46);
  1990. sd->gmlevel = RFIFOB(fd,50);
  1991. safestrncpy(sd->birthdate, RFIFOP(fd,51), sizeof(sd->birthdate));
  1992. // continued from char_auth_ok...
  1993. if( max_connect_user && count_users() >= max_connect_user && sd->gmlevel < gm_allow_level )
  1994. {
  1995. // refuse connection (over populated)
  1996. WFIFOHEAD(i,3);
  1997. WFIFOW(i,0) = 0x6c;
  1998. WFIFOW(i,2) = 0;
  1999. WFIFOSET(i,3);
  2000. }
  2001. else
  2002. {
  2003. // send characters to player
  2004. mmo_char_send006b(i, sd);
  2005. }
  2006. }
  2007. RFIFOSKIP(fd,62);
  2008. break;
  2009. // login-server alive packet
  2010. case 0x2718:
  2011. if (RFIFOREST(fd) < 2)
  2012. return 0;
  2013. RFIFOSKIP(fd,2);
  2014. break;
  2015. // changesex reply
  2016. case 0x2723:
  2017. if (RFIFOREST(fd) < 7)
  2018. return 0;
  2019. {
  2020. int j;
  2021. unsigned char buf[7];
  2022. int acc = RFIFOL(fd,2);
  2023. int sex = RFIFOB(fd,6);
  2024. RFIFOSKIP(fd,7);
  2025. if( acc > 0 )
  2026. {// TODO: Is this even possible?
  2027. struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc);
  2028. if( node != NULL )
  2029. node->sex = sex;
  2030. ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == acc );
  2031. if( i < char_num )
  2032. {
  2033. int jobclass = char_dat[i].status.class_;
  2034. char_dat[i].status.sex = sex;
  2035. if (jobclass == JOB_BARD || jobclass == JOB_DANCER ||
  2036. jobclass == JOB_CLOWN || jobclass == JOB_GYPSY ||
  2037. jobclass == JOB_BABY_BARD || jobclass == JOB_BABY_DANCER) {
  2038. // job modification
  2039. if (jobclass == JOB_BARD || jobclass == JOB_DANCER) {
  2040. char_dat[i].status.class_ = (sex) ? JOB_BARD : JOB_DANCER;
  2041. } else if (jobclass == JOB_CLOWN || jobclass == JOB_GYPSY) {
  2042. char_dat[i].status.class_ = (sex) ? JOB_CLOWN : JOB_GYPSY;
  2043. } else if (jobclass == JOB_BABY_BARD || jobclass == JOB_BABY_DANCER) {
  2044. char_dat[i].status.class_ = (sex) ? JOB_BABY_BARD : JOB_BABY_DANCER;
  2045. }
  2046. // remove specifical skills of classes 19, 4020 and 4042
  2047. for(j = 315; j <= 322; j++) {
  2048. if (char_dat[i].status.skill[j].id > 0 && char_dat[i].status.skill[j].flag == SKILL_FLAG_PERMANENT) {
  2049. char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv;
  2050. char_dat[i].status.skill[j].id = 0;
  2051. char_dat[i].status.skill[j].lv = 0;
  2052. }
  2053. }
  2054. // remove specifical skills of classes 20, 4021 and 4043
  2055. for(j = 323; j <= 330; j++) {
  2056. if (char_dat[i].status.skill[j].id > 0 && char_dat[i].status.skill[j].flag == SKILL_FLAG_PERMANENT) {
  2057. char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv;
  2058. char_dat[i].status.skill[j].id = 0;
  2059. char_dat[i].status.skill[j].lv = 0;
  2060. }
  2061. }
  2062. }
  2063. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  2064. for (j = 0; j < MAX_INVENTORY; j++) {
  2065. if (char_dat[i].status.inventory[j].nameid && char_dat[i].status.inventory[j].equip)
  2066. char_dat[i].status.inventory[j].equip = 0;
  2067. }
  2068. char_dat[i].status.weapon = 0;
  2069. char_dat[i].status.shield = 0;
  2070. char_dat[i].status.head_top = 0;
  2071. char_dat[i].status.head_mid = 0;
  2072. char_dat[i].status.head_bottom = 0;
  2073. if (char_dat[i].status.guild_id) //If there is a guild, update the guild_member data [Skotlex]
  2074. inter_guild_sex_changed(char_dat[i].status.guild_id, acc, char_dat[i].status.char_id, sex);
  2075. }
  2076. // disconnect player if online on char-server
  2077. disconnect_player(acc);
  2078. }
  2079. // notify all mapservers about this change
  2080. WBUFW(buf,0) = 0x2b0d;
  2081. WBUFL(buf,2) = acc;
  2082. WBUFB(buf,6) = sex;
  2083. mapif_sendall(buf, 7);
  2084. }
  2085. break;
  2086. // reply to an account_reg2 registry request
  2087. case 0x2729:
  2088. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2089. return 0;
  2090. { //Receive account_reg2 registry, forward to map servers.
  2091. unsigned char buf[13+ACCOUNT_REG2_NUM*sizeof(struct global_reg)];
  2092. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  2093. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  2094. mapif_sendall(buf, WBUFW(buf,2));
  2095. RFIFOSKIP(fd, RFIFOW(fd,2));
  2096. }
  2097. break;
  2098. // State change of account/ban notification (from login-server)
  2099. case 0x2731:
  2100. if (RFIFOREST(fd) < 11)
  2101. return 0;
  2102. { // send to all map-servers to disconnect the player
  2103. unsigned char buf[11];
  2104. WBUFW(buf,0) = 0x2b14;
  2105. WBUFL(buf,2) = RFIFOL(fd,2);
  2106. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  2107. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  2108. mapif_sendall(buf, 11);
  2109. }
  2110. // disconnect player if online on char-server
  2111. disconnect_player(RFIFOL(fd,2));
  2112. RFIFOSKIP(fd,11);
  2113. break;
  2114. // Login server request to kick a character out. [Skotlex]
  2115. case 0x2734:
  2116. if (RFIFOREST(fd) < 6)
  2117. return 0;
  2118. {
  2119. int aid = RFIFOL(fd,2);
  2120. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, aid);
  2121. RFIFOSKIP(fd,6);
  2122. if( character != NULL )
  2123. {// account is already marked as online!
  2124. if( character->server > -1 )
  2125. { //Kick it from the map server it is on.
  2126. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2127. if (character->waiting_disconnect == INVALID_TIMER)
  2128. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, chardb_waiting_disconnect, character->account_id, 0);
  2129. }
  2130. else
  2131. {// Manual kick from char server.
  2132. struct char_session_data *tsd;
  2133. int i;
  2134. ARR_FIND( 0, fd_max, i, session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid );
  2135. if( i < fd_max )
  2136. {
  2137. WFIFOHEAD(i,3);
  2138. WFIFOW(i,0) = 0x81;
  2139. WFIFOB(i,2) = 2; // "Someone has already logged in with this id"
  2140. WFIFOSET(i,3);
  2141. set_eof(i);
  2142. }
  2143. else // still moving to the map-server
  2144. set_char_offline(-1, aid);
  2145. }
  2146. }
  2147. idb_remove(auth_db, aid);// reject auth attempts from map-server
  2148. }
  2149. break;
  2150. // ip address update signal from login server
  2151. case 0x2735:
  2152. {
  2153. unsigned char buf[2];
  2154. uint32 new_ip = 0;
  2155. WBUFW(buf,0) = 0x2b1e;
  2156. mapif_sendall(buf, 2);
  2157. new_ip = host2ip(login_ip_str);
  2158. if (new_ip && new_ip != login_ip)
  2159. login_ip = new_ip; //Update login ip, too.
  2160. new_ip = host2ip(char_ip_str);
  2161. if (new_ip && new_ip != char_ip)
  2162. { //Update ip.
  2163. char_ip = new_ip;
  2164. ShowInfo("Updating IP for [%s].\n", char_ip_str);
  2165. // notify login server about the change
  2166. WFIFOHEAD(fd,6);
  2167. WFIFOW(fd,0) = 0x2736;
  2168. WFIFOL(fd,2) = htonl(char_ip);
  2169. WFIFOSET(fd,6);
  2170. }
  2171. RFIFOSKIP(fd,2);
  2172. }
  2173. break;
  2174. default:
  2175. ShowError("Unknown packet 0x%04x received from login-server, disconnecting.\n", command);
  2176. set_eof(fd);
  2177. return 0;
  2178. }
  2179. }
  2180. RFIFOFLUSH(fd);
  2181. return 0;
  2182. }
  2183. int check_connect_login_server(int tid, unsigned int tick, int id, intptr_t data);
  2184. int ping_login_server(int tid, unsigned int tick, int id, intptr_t data);
  2185. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data);
  2186. void do_init_loginif(void)
  2187. {
  2188. // establish char-login connection if not present
  2189. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  2190. add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000);
  2191. // keep the char-login connection alive
  2192. add_timer_func_list(ping_login_server, "ping_login_server");
  2193. add_timer_interval(gettick() + 1000, ping_login_server, 0, 0, ((int)stall_time-2) * 1000);
  2194. // send a list of all online account IDs to login server
  2195. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  2196. add_timer_interval(gettick() + 1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour
  2197. }
  2198. void do_final_loginif(void)
  2199. {
  2200. if( login_fd != -1 )
  2201. {
  2202. do_close(login_fd);
  2203. login_fd = -1;
  2204. }
  2205. }
  2206. int request_accreg2(int account_id, int char_id)
  2207. {
  2208. if (login_fd > 0) {
  2209. WFIFOHEAD(login_fd,10);
  2210. WFIFOW(login_fd,0) = 0x272e;
  2211. WFIFOL(login_fd,2) = account_id;
  2212. WFIFOL(login_fd,6) = char_id;
  2213. WFIFOSET(login_fd,10);
  2214. return 1;
  2215. }
  2216. return 0;
  2217. }
  2218. //Send packet forward to login-server for account saving
  2219. int save_accreg2(unsigned char* buf, int len)
  2220. {
  2221. if (login_fd > 0) {
  2222. WFIFOHEAD(login_fd,len+4);
  2223. memcpy(WFIFOP(login_fd,4), buf, len);
  2224. WFIFOW(login_fd,0) = 0x2728;
  2225. WFIFOW(login_fd,2) = len+4;
  2226. WFIFOSET(login_fd,len+4);
  2227. return 1;
  2228. }
  2229. return 0;
  2230. }
  2231. //Receive Registry information for a character.
  2232. int char_parse_Registry(int account_id, int char_id, unsigned char *buf, int buf_len)
  2233. {
  2234. int i,j,p,len;
  2235. for (i = 0; i < char_num; i++) {
  2236. if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id)
  2237. break;
  2238. }
  2239. if(i >= char_num) //Character not found?
  2240. return 1;
  2241. for(j=0,p=0;j<GLOBAL_REG_NUM && p<buf_len;j++){
  2242. sscanf((char*)WBUFP(buf,p), "%31c%n",char_dat[i].global[j].str,&len);
  2243. char_dat[i].global[j].str[len]='\0';
  2244. p +=len+1; //+1 to skip the '\0' between strings.
  2245. sscanf((char*)WBUFP(buf,p), "%255c%n",char_dat[i].global[j].value,&len);
  2246. char_dat[i].global[j].value[len]='\0';
  2247. p +=len+1;
  2248. }
  2249. char_dat[i].global_num = j;
  2250. return 0;
  2251. }
  2252. //Reply to map server with acc reg values.
  2253. int char_account_reg_reply(int fd,int account_id,int char_id)
  2254. {
  2255. int i,j,p;
  2256. WFIFOHEAD(fd, GLOBAL_REG_NUM*288 + 13);
  2257. WFIFOW(fd,0)=0x3804;
  2258. WFIFOL(fd,4)=account_id;
  2259. WFIFOL(fd,8)=char_id;
  2260. WFIFOB(fd,12)=3; //Type 3: char acc reg.
  2261. for (i = 0;i < char_num; i++) {
  2262. if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id)
  2263. break;
  2264. }
  2265. if(i >= char_num){ //Character not found? Sent empty packet.
  2266. WFIFOW(fd,2)=13;
  2267. }else{
  2268. for (p=13,j = 0; j < char_dat[i].global_num; j++) {
  2269. if (char_dat[i].global[j].str[0]) {
  2270. p+= sprintf((char*)WFIFOP(fd,p), "%s", char_dat[i].global[j].str)+1; //We add 1 to consider the '\0' in place.
  2271. p+= sprintf((char*)WFIFOP(fd,p), "%s", char_dat[i].global[j].value)+1;
  2272. }
  2273. }
  2274. WFIFOW(fd,2)=p;
  2275. }
  2276. WFIFOSET(fd,WFIFOW(fd,2));
  2277. return 0;
  2278. }
  2279. void char_read_fame_list(void)
  2280. {
  2281. int i, j, k;
  2282. struct fame_list fame_item;
  2283. CREATE_BUFFER(id, int, char_num);
  2284. for(i = 0; i < char_num; i++) {
  2285. id[i] = i;
  2286. for(j = 0; j < i; j++) {
  2287. if (char_dat[i].status.fame > char_dat[id[j]].status.fame) {
  2288. for(k = i; k > j; k--)
  2289. id[k] = id[k-1];
  2290. id[j] = i; // id[i]
  2291. break;
  2292. }
  2293. }
  2294. }
  2295. // Empty ranking lists
  2296. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  2297. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  2298. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  2299. // Build Blacksmith ranking list
  2300. for (i = 0, j = 0; i < char_num && j < fame_list_size_smith; i++) {
  2301. if (char_dat[id[i]].status.fame && (
  2302. char_dat[id[i]].status.class_ == JOB_BLACKSMITH ||
  2303. char_dat[id[i]].status.class_ == JOB_WHITESMITH ||
  2304. char_dat[id[i]].status.class_ == JOB_BABY_BLACKSMITH))
  2305. {
  2306. fame_item.id = char_dat[id[i]].status.char_id;
  2307. fame_item.fame = char_dat[id[i]].status.fame;
  2308. safestrncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH);
  2309. memcpy(&smith_fame_list[j],&fame_item,sizeof(struct fame_list));
  2310. j++;
  2311. }
  2312. }
  2313. // Build Alchemist ranking list
  2314. for (i = 0, j = 0; i < char_num && j < fame_list_size_chemist; i++) {
  2315. if (char_dat[id[i]].status.fame && (
  2316. char_dat[id[i]].status.class_ == JOB_ALCHEMIST ||
  2317. char_dat[id[i]].status.class_ == JOB_CREATOR ||
  2318. char_dat[id[i]].status.class_ == JOB_BABY_ALCHEMIST))
  2319. {
  2320. fame_item.id = char_dat[id[i]].status.char_id;
  2321. fame_item.fame = char_dat[id[i]].status.fame;
  2322. safestrncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH);
  2323. memcpy(&chemist_fame_list[j],&fame_item,sizeof(struct fame_list));
  2324. j++;
  2325. }
  2326. }
  2327. // Build Taekwon ranking list
  2328. for (i = 0, j = 0; i < char_num && j < fame_list_size_taekwon; i++) {
  2329. if (char_dat[id[i]].status.fame &&
  2330. char_dat[id[i]].status.class_ == JOB_TAEKWON)
  2331. {
  2332. fame_item.id = char_dat[id[i]].status.char_id;
  2333. fame_item.fame = char_dat[id[i]].status.fame;
  2334. safestrncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH);
  2335. memcpy(&taekwon_fame_list[j],&fame_item,sizeof(struct fame_list));
  2336. j++;
  2337. }
  2338. }
  2339. DELETE_BUFFER(id);
  2340. }
  2341. // Send map-servers the fame ranking lists
  2342. int char_send_fame_list(int fd)
  2343. {
  2344. int i, len = 8;
  2345. unsigned char buf[32000];
  2346. WBUFW(buf,0) = 0x2b1b;
  2347. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  2348. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  2349. len += sizeof(struct fame_list);
  2350. }
  2351. // add blacksmith's block length
  2352. WBUFW(buf, 6) = len;
  2353. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  2354. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  2355. len += sizeof(struct fame_list);
  2356. }
  2357. // add alchemist's block length
  2358. WBUFW(buf, 4) = len;
  2359. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  2360. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  2361. len += sizeof(struct fame_list);
  2362. }
  2363. // add total packet length
  2364. WBUFW(buf, 2) = len;
  2365. if (fd != -1)
  2366. mapif_send(fd, buf, len);
  2367. else
  2368. mapif_sendall(buf, len);
  2369. return 0;
  2370. }
  2371. void char_update_fame_list(int type, int index, int fame)
  2372. {
  2373. unsigned char buf[8];
  2374. WBUFW(buf,0) = 0x2b22;
  2375. WBUFB(buf,2) = type;
  2376. WBUFB(buf,3) = index;
  2377. WBUFL(buf,4) = fame;
  2378. mapif_sendall(buf, 8);
  2379. }
  2380. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  2381. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  2382. int char_loadName(int char_id, char* name)
  2383. {
  2384. int j;
  2385. ARR_FIND( 0, char_num, j, char_dat[j].status.char_id == char_id );
  2386. if( j < char_num )
  2387. {
  2388. safestrncpy(name, char_dat[j].status.name, NAME_LENGTH);
  2389. return 1;
  2390. }
  2391. else
  2392. {
  2393. safestrncpy(name, unknown_char_name, NAME_LENGTH);
  2394. }
  2395. return 0;
  2396. }
  2397. int search_mapserver(unsigned short map, uint32 ip, uint16 port);
  2398. /// Initializes a server structure.
  2399. void mapif_server_init(int id)
  2400. {
  2401. memset(&server[id], 0, sizeof(server[id]));
  2402. server[id].fd = -1;
  2403. }
  2404. /// Destroys a server structure.
  2405. void mapif_server_destroy(int id)
  2406. {
  2407. if( server[id].fd == -1 )
  2408. {
  2409. do_close(server[id].fd);
  2410. server[id].fd = -1;
  2411. }
  2412. }
  2413. /// Resets all the data related to a server.
  2414. void mapif_server_reset(int id)
  2415. {
  2416. int i,j;
  2417. unsigned char buf[16384];
  2418. int fd = server[id].fd;
  2419. //Notify other map servers that this one is gone. [Skotlex]
  2420. WBUFW(buf,0) = 0x2b20;
  2421. WBUFL(buf,4) = htonl(server[id].ip);
  2422. WBUFW(buf,8) = htons(server[id].port);
  2423. j = 0;
  2424. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2425. if (server[id].map[i])
  2426. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  2427. if (j > 0) {
  2428. WBUFW(buf,2) = j * 4 + 10;
  2429. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2430. }
  2431. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2432. create_online_files();
  2433. mapif_server_destroy(id);
  2434. mapif_server_init(id);
  2435. }
  2436. /// Called when the connection to a Map Server is disconnected.
  2437. void mapif_on_disconnect(int id)
  2438. {
  2439. ShowStatus("Map-server #%d has disconnected.\n", id);
  2440. mapif_server_reset(id);
  2441. }
  2442. int parse_frommap(int fd)
  2443. {
  2444. int i, j;
  2445. int id;
  2446. ARR_FIND( 0, ARRAYLENGTH(server), id, server[id].fd == fd );
  2447. if( id == ARRAYLENGTH(server) )
  2448. {// not a map server
  2449. ShowDebug("parse_frommap: Disconnecting invalid session #%d (is not a map-server)\n", fd);
  2450. do_close(fd);
  2451. return 0;
  2452. }
  2453. if( session[fd]->flag.eof )
  2454. {
  2455. do_close(fd);
  2456. server[id].fd = -1;
  2457. mapif_on_disconnect(id);
  2458. return 0;
  2459. }
  2460. while(RFIFOREST(fd) >= 2)
  2461. {
  2462. switch(RFIFOW(fd,0))
  2463. {
  2464. case 0x2afa: // Receiving map names list from the map-server
  2465. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2466. return 0;
  2467. memset(server[id].map, 0, sizeof(server[id].map));
  2468. j = 0;
  2469. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2470. server[id].map[j] = RFIFOW(fd,i);
  2471. j++;
  2472. }
  2473. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2474. id, j, CONVIP(server[id].ip), server[id].port);
  2475. ShowStatus("Map-server %d loading complete.\n", id);
  2476. char_log("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d. Map-server %d loading complete.\n",
  2477. id, j, CONVIP(server[id].ip), server[id].port, id);
  2478. // send name for wisp to player
  2479. WFIFOHEAD(fd, 3 + NAME_LENGTH);
  2480. WFIFOW(fd,0) = 0x2afb;
  2481. WFIFOB(fd,2) = 0;
  2482. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH);
  2483. WFIFOSET(fd,3+NAME_LENGTH);
  2484. char_send_fame_list(fd); //Send fame list.
  2485. {
  2486. unsigned char buf[16384];
  2487. int x;
  2488. if (j == 0) {
  2489. ShowWarning("Map-server %d has NO maps.\n", id);
  2490. char_log("WARNING: Map-server %d has NO maps.\n", id);
  2491. } else {
  2492. // Transmitting maps information to the other map-servers
  2493. WBUFW(buf,0) = 0x2b04;
  2494. WBUFW(buf,2) = j * 4 + 10;
  2495. WBUFL(buf,4) = htonl(server[id].ip);
  2496. WBUFW(buf,8) = htons(server[id].port);
  2497. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2498. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2499. }
  2500. // Transmitting the maps of the other map-servers to the new map-server
  2501. for(x = 0; x < ARRAYLENGTH(server); x++) {
  2502. if (server[x].fd > 0 && x != id) {
  2503. WFIFOHEAD(fd,10 +4*ARRAYLENGTH(server));
  2504. WFIFOW(fd,0) = 0x2b04;
  2505. WFIFOL(fd,4) = htonl(server[x].ip);
  2506. WFIFOW(fd,8) = htons(server[x].port);
  2507. j = 0;
  2508. for(i = 0; i < ARRAYLENGTH(server); i++)
  2509. if (server[x].map[i])
  2510. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2511. if (j > 0) {
  2512. WFIFOW(fd,2) = j * 4 + 10;
  2513. WFIFOSET(fd,WFIFOW(fd,2));
  2514. }
  2515. }
  2516. }
  2517. }
  2518. RFIFOSKIP(fd,RFIFOW(fd,2));
  2519. break;
  2520. case 0x2afc: //Packet command is now used for sc_data request. [Skotlex]
  2521. if (RFIFOREST(fd) < 10)
  2522. return 0;
  2523. {
  2524. #ifdef ENABLE_SC_SAVING
  2525. int aid, cid;
  2526. struct scdata *data;
  2527. aid = RFIFOL(fd,2);
  2528. cid = RFIFOL(fd,6);
  2529. data = status_search_scdata(aid, cid);
  2530. if (data->count > 0)
  2531. { //Deliver status change data.
  2532. WFIFOHEAD(fd,14 + data->count*sizeof(struct status_change_data));
  2533. WFIFOW(fd,0) = 0x2b1d;
  2534. WFIFOW(fd,2) = 14 + data->count*sizeof(struct status_change_data);
  2535. WFIFOL(fd,4) = aid;
  2536. WFIFOL(fd,8) = cid;
  2537. WFIFOW(fd,12) = data->count;
  2538. for (i = 0; i < data->count; i++)
  2539. memcpy(WFIFOP(fd,14+i*sizeof(struct status_change_data)), &data->data[i], sizeof(struct status_change_data));
  2540. WFIFOSET(fd, WFIFOW(fd,2));
  2541. status_delete_scdata(aid, cid); //Data sent, so it needs be discarded now.
  2542. }
  2543. #endif
  2544. RFIFOSKIP(fd, 10);
  2545. }
  2546. break;
  2547. case 0x2afe: //set MAP user count
  2548. if (RFIFOREST(fd) < 4)
  2549. return 0;
  2550. if (RFIFOW(fd,2) != server[id].users) {
  2551. server[id].users = RFIFOW(fd,2);
  2552. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2553. }
  2554. RFIFOSKIP(fd, 4);
  2555. break;
  2556. case 0x2aff: //set MAP users
  2557. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2558. return 0;
  2559. {
  2560. //TODO: When data mismatches memory, update guild/party online/offline states.
  2561. int aid, cid;
  2562. struct online_char_data* character;
  2563. server[id].users = RFIFOW(fd,4);
  2564. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2565. for(i = 0; i < server[id].users; i++) {
  2566. aid = RFIFOL(fd,6+i*8);
  2567. cid = RFIFOL(fd,6+i*8+4);
  2568. character = (struct online_char_data*)idb_ensure(online_char_db, aid, create_online_char_data);
  2569. if( character->server > -1 && character->server != id )
  2570. {
  2571. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2572. character->account_id, character->char_id, character->server, id, aid, cid);
  2573. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2574. }
  2575. character->server = id;
  2576. character->char_id = cid;
  2577. }
  2578. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2579. RFIFOSKIP(fd,RFIFOW(fd,2));
  2580. }
  2581. break;
  2582. case 0x2b01: // Receive character data from map-server for saving
  2583. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2584. return 0;
  2585. {
  2586. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8), size = RFIFOW(fd,2);
  2587. struct mmo_charstatus* cs;
  2588. if (size - 13 != sizeof(struct mmo_charstatus))
  2589. {
  2590. ShowError("parse_from_map (save-char): Size mismatch! %d != %d\n", size-13, sizeof(struct mmo_charstatus));
  2591. RFIFOSKIP(fd,size);
  2592. break;
  2593. }
  2594. if( ( cs = search_character(aid, cid) ) != NULL )
  2595. {
  2596. memcpy(cs, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2597. storage_save(cs->account_id, &cs->storage);
  2598. }
  2599. if (RFIFOB(fd,12))
  2600. { //Flag, set character offline after saving. [Skotlex]
  2601. set_char_offline(cid, aid);
  2602. WFIFOHEAD(fd,10);
  2603. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  2604. WFIFOL(fd,2) = aid;
  2605. WFIFOL(fd,6) = cid;
  2606. WFIFOSET(fd,10);
  2607. }
  2608. RFIFOSKIP(fd,size);
  2609. }
  2610. break;
  2611. case 0x2b02: // req char selection
  2612. if( RFIFOREST(fd) < 18 )
  2613. return 0;
  2614. {
  2615. struct auth_node* node;
  2616. int account_id = RFIFOL(fd,2);
  2617. uint32 login_id1 = RFIFOL(fd,6);
  2618. uint32 login_id2 = RFIFOL(fd,10);
  2619. uint32 ip = RFIFOL(fd,14);
  2620. RFIFOSKIP(fd,18);
  2621. if( runflag != CHARSERVER_ST_RUNNING )
  2622. {
  2623. WFIFOHEAD(fd,7);
  2624. WFIFOW(fd,0) = 0x2b03;
  2625. WFIFOL(fd,2) = account_id;
  2626. WFIFOB(fd,6) = 0;// not ok
  2627. WFIFOSET(fd,7);
  2628. }
  2629. else
  2630. {
  2631. // create temporary auth entry
  2632. CREATE(node, struct auth_node, 1);
  2633. node->account_id = account_id;
  2634. node->char_id = 0;
  2635. node->login_id1 = login_id1;
  2636. node->login_id2 = login_id2;
  2637. //node->sex = 0;
  2638. node->ip = ntohl(ip);
  2639. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  2640. //node->gmlevel = 0;
  2641. idb_put(auth_db, account_id, node);
  2642. //Set char to "@ char select" in online db [Kevin]
  2643. set_char_charselect(account_id);
  2644. WFIFOHEAD(fd,7);
  2645. WFIFOW(fd,0) = 0x2b03;
  2646. WFIFOL(fd,2) = account_id;
  2647. WFIFOB(fd,6) = 1;// ok
  2648. WFIFOSET(fd,7);
  2649. }
  2650. }
  2651. break;
  2652. case 0x2b05: // request "change map server"
  2653. if (RFIFOREST(fd) < 35)
  2654. return 0;
  2655. {
  2656. int map_id, map_fd = -1;
  2657. struct online_char_data* data;
  2658. struct mmo_charstatus* char_data;
  2659. map_id = search_mapserver(RFIFOW(fd,18), ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port.
  2660. if (map_id >= 0)
  2661. map_fd = server[map_id].fd;
  2662. char_data = search_character(RFIFOL(fd,2), RFIFOL(fd,14));
  2663. if( runflag == CHARSERVER_ST_RUNNING &&
  2664. session_isActive(map_fd) &&
  2665. char_data )
  2666. { //Send the map server the auth of this player.
  2667. struct auth_node* node;
  2668. //Update the "last map" as this is where the player must be spawned on the new map server.
  2669. char_data->last_point.map = RFIFOW(fd,18);
  2670. char_data->last_point.x = RFIFOW(fd,20);
  2671. char_data->last_point.y = RFIFOW(fd,22);
  2672. char_data->sex = RFIFOB(fd,30);
  2673. // create temporary auth entry
  2674. CREATE(node, struct auth_node, 1);
  2675. node->account_id = RFIFOL(fd,2);
  2676. node->char_id = RFIFOL(fd,14);
  2677. node->login_id1 = RFIFOL(fd,6);
  2678. node->login_id2 = RFIFOL(fd,10);
  2679. node->sex = RFIFOB(fd,30);
  2680. node->expiration_time = 0; // FIXME
  2681. node->ip = ntohl(RFIFOL(fd,31));
  2682. idb_put(auth_db, RFIFOL(fd,2), node);
  2683. data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
  2684. data->char_id = char_data->char_id;
  2685. data->server = map_id; //Update server where char is.
  2686. //Reply with an ack.
  2687. WFIFOHEAD(fd,30);
  2688. WFIFOW(fd,0) = 0x2b06;
  2689. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2690. WFIFOSET(fd,30);
  2691. } else { //Reply with nak
  2692. WFIFOHEAD(fd,30);
  2693. WFIFOW(fd,0) = 0x2b06;
  2694. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2695. WFIFOL(fd,6) = 0; //Set login1 to 0.
  2696. WFIFOSET(fd,30);
  2697. }
  2698. RFIFOSKIP(fd,35);
  2699. }
  2700. break;
  2701. case 0x2b08: // char name request
  2702. if (RFIFOREST(fd) < 6)
  2703. return 0;
  2704. WFIFOHEAD(fd,30);
  2705. WFIFOW(fd,0) = 0x2b09;
  2706. WFIFOL(fd,2) = RFIFOL(fd,2);
  2707. char_loadName((int)RFIFOL(fd,2), (char*)WFIFOP(fd,6));
  2708. WFIFOSET(fd,30);
  2709. RFIFOSKIP(fd,6);
  2710. break;
  2711. case 0x2b0c: // Map server send information to change an email of an account -> login-server
  2712. if (RFIFOREST(fd) < 86)
  2713. return 0;
  2714. if (login_fd > 0) { // don't send request if no login-server
  2715. WFIFOHEAD(login_fd,86);
  2716. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2717. WFIFOW(login_fd,0) = 0x2722;
  2718. WFIFOSET(login_fd,86);
  2719. }
  2720. RFIFOSKIP(fd, 86);
  2721. break;
  2722. case 0x2b0e: // Request from map-server to change an account's status (will just be forwarded to login server)
  2723. if (RFIFOREST(fd) < 44)
  2724. return 0;
  2725. {
  2726. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline
  2727. char character_name[NAME_LENGTH];
  2728. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  2729. const char* name = (char*)RFIFOP(fd,6); // name of the target character
  2730. int type = RFIFOW(fd,30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2731. short year = RFIFOW(fd,32);
  2732. short month = RFIFOW(fd,34);
  2733. short day = RFIFOW(fd,36);
  2734. short hour = RFIFOW(fd,38);
  2735. short minute = RFIFOW(fd,40);
  2736. short second = RFIFOW(fd,42);
  2737. RFIFOSKIP(fd,44);
  2738. safestrncpy(character_name, name, NAME_LENGTH);
  2739. i = search_character_index(character_name);
  2740. if( i < 0 )
  2741. {
  2742. result = 1; // 1-player not found
  2743. }
  2744. else
  2745. {
  2746. char name[NAME_LENGTH];
  2747. int account_id;
  2748. account_id = char_dat[i].status.account_id;
  2749. safestrncpy(name, char_dat[i].status.name, NAME_LENGTH);
  2750. if( login_fd <= 0 )
  2751. result = 3; // 3-login-server offline
  2752. //FIXME: need to move this check to login server [ultramage]
  2753. // else
  2754. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  2755. // result = 2; // 2-gm level too low
  2756. else
  2757. switch( type ) {
  2758. case 1: // block
  2759. WFIFOHEAD(login_fd,10);
  2760. WFIFOW(login_fd,0) = 0x2724;
  2761. WFIFOL(login_fd,2) = account_id;
  2762. WFIFOL(login_fd,6) = 5; // new account status
  2763. WFIFOSET(login_fd,10);
  2764. break;
  2765. case 2: // ban
  2766. WFIFOHEAD(login_fd,18);
  2767. WFIFOW(login_fd, 0) = 0x2725;
  2768. WFIFOL(login_fd, 2) = account_id;
  2769. WFIFOW(login_fd, 6) = year;
  2770. WFIFOW(login_fd, 8) = month;
  2771. WFIFOW(login_fd,10) = day;
  2772. WFIFOW(login_fd,12) = hour;
  2773. WFIFOW(login_fd,14) = minute;
  2774. WFIFOW(login_fd,16) = second;
  2775. WFIFOSET(login_fd,18);
  2776. break;
  2777. case 3: // unblock
  2778. WFIFOHEAD(login_fd,10);
  2779. WFIFOW(login_fd,0) = 0x2724;
  2780. WFIFOL(login_fd,2) = account_id;
  2781. WFIFOL(login_fd,6) = 0; // new account status
  2782. WFIFOSET(login_fd,10);
  2783. break;
  2784. case 4: // unban
  2785. WFIFOHEAD(login_fd,6);
  2786. WFIFOW(login_fd,0) = 0x272a;
  2787. WFIFOL(login_fd,2) = account_id;
  2788. WFIFOSET(login_fd,6);
  2789. break;
  2790. case 5: // changesex
  2791. WFIFOHEAD(login_fd,6);
  2792. WFIFOW(login_fd,0) = 0x2727;
  2793. WFIFOL(login_fd,2) = account_id;
  2794. WFIFOSET(login_fd,6);
  2795. break;
  2796. }
  2797. }
  2798. // send answer if a player ask, not if the server ask
  2799. if( acc != -1 && type != 5) { // Don't send answer for changesex
  2800. WFIFOHEAD(fd,34);
  2801. WFIFOW(fd, 0) = 0x2b0f;
  2802. WFIFOL(fd, 2) = acc;
  2803. safestrncpy((char*)WFIFOP(fd,6), name, NAME_LENGTH);
  2804. WFIFOW(fd,30) = type;
  2805. WFIFOW(fd,32) = result;
  2806. WFIFOSET(fd,34);
  2807. }
  2808. }
  2809. break;
  2810. case 0x2b10: // Update and send fame ranking list
  2811. if (RFIFOREST(fd) < 11)
  2812. return 0;
  2813. {
  2814. int cid = RFIFOL(fd, 2);
  2815. int fame = RFIFOL(fd, 6);
  2816. char type = RFIFOB(fd, 10);
  2817. int size;
  2818. struct fame_list* list;
  2819. int player_pos;
  2820. int fame_pos;
  2821. switch(type)
  2822. {
  2823. case 1: size = fame_list_size_smith; list = smith_fame_list; break;
  2824. case 2: size = fame_list_size_chemist; list = chemist_fame_list; break;
  2825. case 3: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  2826. default: size = 0; list = NULL; break;
  2827. }
  2828. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  2829. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  2830. if( player_pos == size && fame_pos == size )
  2831. ;// not on list and not enough fame to get on it
  2832. else if( fame_pos == player_pos )
  2833. {// same position
  2834. list[player_pos].fame = fame;
  2835. char_update_fame_list(type, player_pos, fame);
  2836. }
  2837. else
  2838. {// move in the list
  2839. if( player_pos == size )
  2840. {// new ranker - not in the list
  2841. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  2842. list[fame_pos].id = cid;
  2843. list[fame_pos].fame = fame;
  2844. char_loadName(cid, list[fame_pos].name);
  2845. }
  2846. else
  2847. {// already in the list
  2848. if( fame_pos == size )
  2849. --fame_pos;// move to the end of the list
  2850. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  2851. list[fame_pos].fame = fame;
  2852. }
  2853. char_send_fame_list(-1);
  2854. }
  2855. RFIFOSKIP(fd,11);
  2856. }
  2857. break;
  2858. case 0x2b16: // Receive rates [Wizputer]
  2859. if( RFIFOREST(fd) < 14 )
  2860. return 0;
  2861. // Txt doesn't need this packet, so just skip it
  2862. RFIFOSKIP(fd,14);
  2863. break;
  2864. case 0x2b17: // Character disconnected set online 0 [Wizputer]
  2865. if (RFIFOREST(fd) < 6)
  2866. return 0;
  2867. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2868. RFIFOSKIP(fd,10);
  2869. break;
  2870. case 0x2b18: // Reset all chars to offline [Wizputer]
  2871. set_all_offline(id);
  2872. RFIFOSKIP(fd,2);
  2873. break;
  2874. case 0x2b19: // Character set online [Wizputer]
  2875. if (RFIFOREST(fd) < 10)
  2876. return 0;
  2877. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2878. RFIFOSKIP(fd,10);
  2879. break;
  2880. case 0x2b1a: // Build and send fame ranking lists [DracoRPG]
  2881. if (RFIFOREST(fd) < 2)
  2882. return 0;
  2883. char_read_fame_list();
  2884. char_send_fame_list(-1);
  2885. RFIFOSKIP(fd,2);
  2886. break;
  2887. case 0x2b1c: //Request to save status change data. [Skotlex]
  2888. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2889. return 0;
  2890. {
  2891. #ifdef ENABLE_SC_SAVING
  2892. int count, aid, cid;
  2893. struct scdata *data;
  2894. aid = RFIFOL(fd, 4);
  2895. cid = RFIFOL(fd, 8);
  2896. count = RFIFOW(fd, 12);
  2897. data = status_search_scdata(aid, cid);
  2898. if (data->count != count)
  2899. {
  2900. data->count = count;
  2901. data->data = (struct status_change_data*)aRealloc(data->data, count*sizeof(struct status_change_data));
  2902. }
  2903. for (i = 0; i < count; i++)
  2904. memcpy (&data->data[i], RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2905. #endif
  2906. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2907. }
  2908. break;
  2909. case 0x2b23: // map-server alive packet
  2910. WFIFOHEAD(fd,2);
  2911. WFIFOW(fd,0) = 0x2b24;
  2912. WFIFOSET(fd,2);
  2913. RFIFOSKIP(fd,2);
  2914. break;
  2915. case 0x2b26: // auth request from map-server
  2916. if (RFIFOREST(fd) < 19)
  2917. return 0;
  2918. {
  2919. int account_id;
  2920. int char_id;
  2921. int login_id1;
  2922. char sex;
  2923. uint32 ip;
  2924. struct auth_node* node;
  2925. struct mmo_charstatus* cd;
  2926. account_id = RFIFOL(fd,2);
  2927. char_id = RFIFOL(fd,6);
  2928. login_id1 = RFIFOL(fd,10);
  2929. sex = RFIFOB(fd,14);
  2930. ip = ntohl(RFIFOL(fd,15));
  2931. RFIFOSKIP(fd,19);
  2932. node = (struct auth_node*)idb_get(auth_db, account_id);
  2933. cd = search_character(account_id, char_id);
  2934. if( runflag == CHARSERVER_ST_RUNNING &&
  2935. cd != NULL &&
  2936. node != NULL &&
  2937. node->account_id == account_id &&
  2938. node->char_id == char_id &&
  2939. node->login_id1 == login_id1 &&
  2940. node->sex == sex /*&&
  2941. node->ip == ip*/ )
  2942. {// auth ok
  2943. cd->sex = sex;
  2944. WFIFOHEAD(fd,24 + sizeof(struct mmo_charstatus));
  2945. WFIFOW(fd,0) = 0x2afd;
  2946. WFIFOW(fd,2) = 24 + sizeof(struct mmo_charstatus);
  2947. WFIFOL(fd,4) = account_id;
  2948. WFIFOL(fd,8) = node->login_id1;
  2949. WFIFOL(fd,12) = node->login_id2;
  2950. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  2951. WFIFOL(fd,20) = node->gmlevel;
  2952. storage_load(cd->account_id, &cd->storage); //FIXME: storage is used as a temp buffer here
  2953. memcpy(WFIFOP(fd,24), cd, sizeof(struct mmo_charstatus));
  2954. WFIFOSET(fd, WFIFOW(fd,2));
  2955. // only use the auth once and mark user online
  2956. idb_remove(auth_db, account_id);
  2957. set_char_online(id, char_id, account_id);
  2958. }
  2959. else
  2960. {// auth failed
  2961. WFIFOHEAD(fd,19);
  2962. WFIFOW(fd,0) = 0x2b27;
  2963. WFIFOL(fd,2) = account_id;
  2964. WFIFOL(fd,6) = char_id;
  2965. WFIFOL(fd,10) = login_id1;
  2966. WFIFOB(fd,14) = sex;
  2967. WFIFOL(fd,15) = htonl(ip);
  2968. WFIFOSET(fd,19);
  2969. }
  2970. }
  2971. break;
  2972. case 0x2736: // ip address update
  2973. if (RFIFOREST(fd) < 6) return 0;
  2974. server[id].ip = ntohl(RFIFOL(fd, 2));
  2975. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(server[id].ip));
  2976. RFIFOSKIP(fd,6);
  2977. break;
  2978. default:
  2979. {
  2980. // inter server - packet
  2981. int r = inter_parse_frommap(fd);
  2982. if (r == 1) break; // processed
  2983. if (r == 2) return 0; // need more packet
  2984. // no inter server packet. no char server packet -> disconnect
  2985. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  2986. set_eof(fd);
  2987. return 0;
  2988. }
  2989. } // switch
  2990. } // while
  2991. return 0;
  2992. }
  2993. void do_init_mapif(void)
  2994. {
  2995. int i;
  2996. for( i = 0; i < ARRAYLENGTH(server); ++i )
  2997. mapif_server_init(i);
  2998. }
  2999. void do_final_mapif(void)
  3000. {
  3001. int i;
  3002. for( i = 0; i < ARRAYLENGTH(server); ++i )
  3003. mapif_server_destroy(i);
  3004. }
  3005. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  3006. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  3007. int search_mapserver(unsigned short map, uint32 ip, uint16 port)
  3008. {
  3009. int i, j;
  3010. for(i = 0; i < ARRAYLENGTH(server); i++)
  3011. {
  3012. if (server[i].fd > 0
  3013. && (ip == (uint32)-1 || server[i].ip == ip)
  3014. && (port == (uint16)-1 || server[i].port == port))
  3015. {
  3016. for (j = 0; server[i].map[j]; j++)
  3017. if (server[i].map[j] == map)
  3018. return i;
  3019. }
  3020. }
  3021. return -1;
  3022. }
  3023. // char_mapifの初期化処理(現在はinter_mapif初期化のみ)
  3024. static int char_mapif_init(int fd)
  3025. {
  3026. return inter_mapif_init(fd);
  3027. }
  3028. //--------------------------------------------
  3029. // Test to know if an IP come from LAN or WAN.
  3030. //--------------------------------------------
  3031. int lan_subnetcheck(uint32 ip)
  3032. {
  3033. int i;
  3034. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  3035. if( i < subnet_count ) {
  3036. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches "CL_CYAN"%u.%u.%u.%u/%u.%u.%u.%u"CL_RESET"\n", CONVIP(ip), CONVIP(subnet[i].char_ip & subnet[i].mask), CONVIP(subnet[i].mask));
  3037. return subnet[i].char_ip;
  3038. } else {
  3039. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  3040. return 0;
  3041. }
  3042. }
  3043. /// @param result
  3044. /// 0 (0x718): An unknown error has occurred.
  3045. /// 1: none/success
  3046. /// 3 (0x719): A database error occurred.
  3047. /// 4 (0x71a): To delete a character you must withdraw from the guild.
  3048. /// 5 (0x71b): To delete a character you must withdraw from the party.
  3049. /// Any (0x718): An unknown error has occurred.
  3050. void char_delete2_ack(int fd, int char_id, uint32 result, time_t delete_date)
  3051. {// HC: <0828>.W <char id>.L <Msg:0-5>.L <deleteDate>.L
  3052. WFIFOHEAD(fd,14);
  3053. WFIFOW(fd,0) = 0x828;
  3054. WFIFOL(fd,2) = char_id;
  3055. WFIFOL(fd,6) = result;
  3056. WFIFOL(fd,10) = TOL(delete_date);
  3057. WFIFOSET(fd,14);
  3058. }
  3059. /// @param result
  3060. /// 0 (0x718): An unknown error has occurred.
  3061. /// 1: none/success
  3062. /// 2 (0x71c): Due to system settings can not be deleted.
  3063. /// 3 (0x719): A database error occurred.
  3064. /// 4 (0x71d): Deleting not yet possible time.
  3065. /// 5 (0x71e): Date of birth do not match.
  3066. /// Any (0x718): An unknown error has occurred.
  3067. void char_delete2_accept_ack(int fd, int char_id, uint32 result)
  3068. {// HC: <082a>.W <char id>.L <Msg:0-5>.L
  3069. WFIFOHEAD(fd,10);
  3070. WFIFOW(fd,0) = 0x82a;
  3071. WFIFOL(fd,2) = char_id;
  3072. WFIFOL(fd,6) = result;
  3073. WFIFOSET(fd,10);
  3074. }
  3075. /// @param result
  3076. /// 1 (0x718): none/success, (if char id not in deletion process): An unknown error has occurred.
  3077. /// 2 (0x719): A database error occurred.
  3078. /// Any (0x718): An unknown error has occurred.
  3079. void char_delete2_cancel_ack(int fd, int char_id, uint32 result)
  3080. {// HC: <082c>.W <char id>.L <Msg:1-2>.L
  3081. WFIFOHEAD(fd,10);
  3082. WFIFOW(fd,0) = 0x82c;
  3083. WFIFOL(fd,2) = char_id;
  3084. WFIFOL(fd,6) = result;
  3085. WFIFOSET(fd,10);
  3086. }
  3087. static void char_delete2_req(int fd, struct char_session_data* sd)
  3088. {// CH: <0827>.W <char id>.L
  3089. int char_id;
  3090. struct mmo_charstatus* cs;
  3091. char_id = RFIFOL(fd,2);
  3092. if( ( cs = search_session_character(sd, char_id) ) == NULL )
  3093. {// character not found
  3094. char_delete2_ack(fd, char_id, 3, 0);
  3095. return;
  3096. }
  3097. if( cs->delete_date )
  3098. {// character already queued for deletion
  3099. char_delete2_ack(fd, char_id, 0, 0);
  3100. return;
  3101. }
  3102. /*
  3103. // Aegis imposes these checks probably to avoid dead member
  3104. // entries in guilds/parties, otherwise they are not required.
  3105. // TODO: Figure out how these are enforced during waiting.
  3106. if( cs->guild_id )
  3107. {// character in guild
  3108. char_delete2_ack(fd, char_id, 4, 0);
  3109. return;
  3110. }
  3111. if( cs->party_id )
  3112. {// character in party
  3113. char_delete2_ack(fd, char_id, 5, 0);
  3114. return;
  3115. }
  3116. */
  3117. // success
  3118. cs->delete_date = time(NULL)+char_del_delay;
  3119. char_delete2_ack(fd, char_id, 1, cs->delete_date);
  3120. }
  3121. static void char_delete2_accept(int fd, struct char_session_data* sd)
  3122. {// CH: <0829>.W <char id>.L <birth date:YYMMDD>.6B
  3123. char birthdate[8+1];
  3124. int char_id, i;
  3125. struct mmo_charstatus* cs;
  3126. char_id = RFIFOL(fd,2);
  3127. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, char_id);
  3128. // construct "YY-MM-DD"
  3129. birthdate[0] = RFIFOB(fd,6);
  3130. birthdate[1] = RFIFOB(fd,7);
  3131. birthdate[2] = '-';
  3132. birthdate[3] = RFIFOB(fd,8);
  3133. birthdate[4] = RFIFOB(fd,9);
  3134. birthdate[5] = '-';
  3135. birthdate[6] = RFIFOB(fd,10);
  3136. birthdate[7] = RFIFOB(fd,11);
  3137. birthdate[8] = 0;
  3138. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == char_id );
  3139. if( i == MAX_CHARS )
  3140. {// character not found
  3141. char_delete2_accept_ack(fd, char_id, 3);
  3142. return;
  3143. }
  3144. cs = &char_dat[sd->found_char[i]].status;
  3145. if( !cs->delete_date || cs->delete_date>time(NULL) )
  3146. {// not queued or delay not yet passed
  3147. char_delete2_accept_ack(fd, char_id, 4);
  3148. return;
  3149. }
  3150. if( strcmp(sd->birthdate+2, birthdate) ) // +2 to cut off the century
  3151. {// birth date is wrong
  3152. char_delete2_accept_ack(fd, char_id, 5);
  3153. return;
  3154. }
  3155. if( ( char_del_level > 0 && cs->base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && cs->base_level <= (unsigned int)(-char_del_level) ) )
  3156. {// character level config restriction
  3157. char_delete2_accept_ack(fd, char_id, 2);
  3158. return;
  3159. }
  3160. // success
  3161. char_delete(cs);
  3162. // drop character entry
  3163. if( --char_num > 0 && sd->found_char[i] != char_num )
  3164. {
  3165. int s, c;
  3166. // move the last entry to the place of the deleted character
  3167. memcpy(&char_dat[sd->found_char[i]], &char_dat[char_num], sizeof(struct mmo_charstatus));
  3168. // scan currently online accounts, if the moved character
  3169. // entry requires an update of the cached character list
  3170. for( s = 0; s < fd_max; s++ )
  3171. {
  3172. struct char_session_data* osd;
  3173. if( session[s] && ( osd = (struct char_session_data*)session[s]->session_data ) != NULL && osd->account_id == char_dat[char_num].status.account_id )
  3174. {
  3175. for( c = 0; c < MAX_CHARS; c++ )
  3176. {
  3177. if( osd->found_char[c] == char_num )
  3178. {
  3179. osd->found_char[c] = sd->found_char[i];
  3180. break;
  3181. }
  3182. }
  3183. break;
  3184. }
  3185. }
  3186. // wipe the last entry
  3187. memset(&char_dat[char_num], 0, sizeof(struct mmo_charstatus));
  3188. }
  3189. // refresh character list cache
  3190. char_find_characters(sd);
  3191. char_delete2_accept_ack(fd, char_id, 1);
  3192. }
  3193. static void char_delete2_cancel(int fd, struct char_session_data* sd)
  3194. {// CH: <082b>.W <char id>.L
  3195. int char_id;
  3196. struct mmo_charstatus* cs;
  3197. char_id = RFIFOL(fd,2);
  3198. if( ( cs = search_session_character(sd, char_id) ) == NULL )
  3199. {// character not found
  3200. char_delete2_cancel_ack(fd, char_id, 2);
  3201. return;
  3202. }
  3203. // there is no need to check, whether or not the character was
  3204. // queued for deletion, as the client prints an error message by
  3205. // itself, if it was not the case (@see char_delete2_cancel_ack)
  3206. cs->delete_date = 0;
  3207. char_delete2_cancel_ack(fd, char_id, 1);
  3208. }
  3209. int parse_char(int fd)
  3210. {
  3211. int i, ch;
  3212. char email[40];
  3213. unsigned short cmd;
  3214. int map_fd;
  3215. struct char_session_data* sd;
  3216. uint32 ipl = session[fd]->client_addr;
  3217. sd = (struct char_session_data*)session[fd]->session_data;
  3218. // disconnect any player if no login-server.
  3219. if(login_fd < 0)
  3220. set_eof(fd);
  3221. if(session[fd]->flag.eof)
  3222. {
  3223. if( sd != NULL && sd->auth )
  3224. { // already authed client
  3225. struct online_char_data* data = (struct online_char_data*)idb_get(online_char_db, sd->account_id);
  3226. if( data != NULL && data->fd == fd)
  3227. data->fd = -1;
  3228. if( data == NULL || data->server == -1) //If it is not in any server, send it offline. [Skotlex]
  3229. set_char_offline(-1,sd->account_id);
  3230. }
  3231. do_close(fd);
  3232. return 0;
  3233. }
  3234. while( RFIFOREST(fd) >= 2 )
  3235. {
  3236. //For use in packets that depend on an sd being present [Skotlex]
  3237. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,rest); return 0; } }
  3238. cmd = RFIFOW(fd,0);
  3239. switch( cmd )
  3240. {
  3241. // request to connect
  3242. // 0065 <account id>.L <login id1>.L <login id2>.L <???>.W <sex>.B
  3243. case 0x65:
  3244. if( RFIFOREST(fd) < 17 )
  3245. return 0;
  3246. {
  3247. struct auth_node* node;
  3248. int account_id = RFIFOL(fd,2);
  3249. uint32 login_id1 = RFIFOL(fd,6);
  3250. uint32 login_id2 = RFIFOL(fd,10);
  3251. int sex = RFIFOB(fd,16);
  3252. RFIFOSKIP(fd,17);
  3253. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", account_id, login_id1, login_id2);
  3254. if (sd) {
  3255. //Received again auth packet for already authentified account?? Discard it.
  3256. //TODO: Perhaps log this as a hack attempt?
  3257. //TODO: and perhaps send back a reply?
  3258. break;
  3259. }
  3260. CREATE(session[fd]->session_data, struct char_session_data, 1);
  3261. sd = (struct char_session_data*)session[fd]->session_data;
  3262. sd->account_id = account_id;
  3263. sd->login_id1 = login_id1;
  3264. sd->login_id2 = login_id2;
  3265. sd->sex = sex;
  3266. sd->auth = false; // not authed yet
  3267. // send back account_id
  3268. WFIFOHEAD(fd,4);
  3269. WFIFOL(fd,0) = account_id;
  3270. WFIFOSET(fd,4);
  3271. if( runflag != CHARSERVER_ST_RUNNING )
  3272. {
  3273. WFIFOHEAD(fd,3);
  3274. WFIFOW(fd,0) = 0x6c;
  3275. WFIFOB(fd,2) = 0;// rejected from server
  3276. WFIFOSET(fd,3);
  3277. break;
  3278. }
  3279. // search authentification
  3280. node = (struct auth_node*)idb_get(auth_db, account_id);
  3281. if( node != NULL &&
  3282. node->account_id == account_id &&
  3283. node->login_id1 == login_id1 &&
  3284. node->login_id2 == login_id2 /*&&
  3285. node->ip == ipl*/ )
  3286. {// authentication found (coming from map server)
  3287. idb_remove(auth_db, account_id);
  3288. char_auth_ok(fd, sd);
  3289. }
  3290. else
  3291. {// authentication not found (coming from login server)
  3292. if (login_fd > 0) { // don't send request if no login-server
  3293. WFIFOHEAD(login_fd,23);
  3294. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  3295. WFIFOL(login_fd,2) = sd->account_id;
  3296. WFIFOL(login_fd,6) = sd->login_id1;
  3297. WFIFOL(login_fd,10) = sd->login_id2;
  3298. WFIFOB(login_fd,14) = sd->sex;
  3299. WFIFOL(login_fd,15) = htonl(ipl);
  3300. WFIFOL(login_fd,19) = fd;
  3301. WFIFOSET(login_fd,23);
  3302. } else { // if no login-server, we must refuse connection
  3303. WFIFOHEAD(fd,3);
  3304. WFIFOW(fd,0) = 0x6c;
  3305. WFIFOB(fd,2) = 0;
  3306. WFIFOSET(fd,3);
  3307. }
  3308. }
  3309. }
  3310. break;
  3311. // char select
  3312. case 0x66:
  3313. FIFOSD_CHECK(3);
  3314. {
  3315. struct mmo_charstatus *cd;
  3316. uint32 subnet_map_ip;
  3317. struct auth_node* node;
  3318. int slot = RFIFOB(fd,2);
  3319. RFIFOSKIP(fd,3);
  3320. // if we activated email creation and email is default email
  3321. if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online
  3322. WFIFOHEAD(fd,3);
  3323. WFIFOW(fd,0) = 0x70;
  3324. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3325. WFIFOSET(fd,3);
  3326. break;
  3327. }
  3328. // otherwise, load the character
  3329. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] >= 0 && char_dat[sd->found_char[ch]].status.slot == slot );
  3330. if (ch == MAX_CHARS)
  3331. { //Not found?? May be forged packet.
  3332. WFIFOHEAD(fd,3);
  3333. WFIFOW(fd,0) = 0x6c;
  3334. WFIFOB(fd,2) = 0; // rejected from server
  3335. WFIFOSET(fd,3);
  3336. break;
  3337. }
  3338. cd = &char_dat[sd->found_char[ch]].status;
  3339. char_log("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s.\n", sd->account_id, slot, cd->name);
  3340. cd->sex = sd->sex;
  3341. ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, cd->name);
  3342. // searching map server
  3343. i = search_mapserver(cd->last_point.map, -1, -1);
  3344. // if map is not found, we check major cities
  3345. if (i < 0) {
  3346. unsigned short j;
  3347. //First check that there's actually a map server online.
  3348. ARR_FIND( 0, ARRAYLENGTH(server), j, server[j].fd >= 0 && server[j].map[0] );
  3349. if (j == ARRAYLENGTH(server)) {
  3350. ShowInfo("Connection Closed. No map servers available.\n");
  3351. WFIFOHEAD(fd,3);
  3352. WFIFOW(fd,0) = 0x81;
  3353. WFIFOB(fd,2) = 1; // 01 = Server closed
  3354. WFIFOSET(fd,3);
  3355. break;
  3356. }
  3357. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  3358. cd->last_point.x = 273;
  3359. cd->last_point.y = 354;
  3360. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  3361. cd->last_point.x = 120;
  3362. cd->last_point.y = 100;
  3363. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  3364. cd->last_point.x = 160;
  3365. cd->last_point.y = 94;
  3366. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  3367. cd->last_point.x = 116;
  3368. cd->last_point.y = 57;
  3369. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  3370. cd->last_point.x = 87;
  3371. cd->last_point.y = 117;
  3372. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  3373. cd->last_point.x = 94;
  3374. cd->last_point.y = 103;
  3375. } else {
  3376. ShowInfo("Connection Closed. No map server available that has a major city, and unable to find map-server for '%s'.\n", mapindex_id2name(cd->last_point.map));
  3377. WFIFOHEAD(fd,3);
  3378. WFIFOW(fd,0) = 0x81;
  3379. WFIFOB(fd,2) = 1; // 01 = Server closed
  3380. WFIFOSET(fd,3);
  3381. break;
  3382. }
  3383. ShowWarning("Unable to find map-server for '%s', sending to major city '%s'.\n", mapindex_id2name(cd->last_point.map), mapindex_id2name(j));
  3384. cd->last_point.map = j;
  3385. }
  3386. //Send NEW auth packet [Kevin]
  3387. //FIXME: is this case even possible? [ultramage]
  3388. if ((map_fd = server[i].fd) < 1 || session[map_fd] == NULL)
  3389. {
  3390. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  3391. server[i].fd = -1;
  3392. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3393. //Send server closed.
  3394. WFIFOHEAD(fd,3);
  3395. WFIFOW(fd,0) = 0x81;
  3396. WFIFOB(fd,2) = 1; // 01 = Server closed
  3397. WFIFOSET(fd,3);
  3398. break;
  3399. }
  3400. //Send player to map
  3401. WFIFOHEAD(fd,28);
  3402. WFIFOW(fd,0) = 0x71;
  3403. WFIFOL(fd,2) = cd->char_id;
  3404. mapindex_getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
  3405. subnet_map_ip = lan_subnetcheck(ipl); // Advanced subnet check [LuzZza]
  3406. WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
  3407. WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  3408. WFIFOSET(fd,28);
  3409. // create temporary auth entry
  3410. CREATE(node, struct auth_node, 1);
  3411. node->account_id = sd->account_id;
  3412. node->char_id = cd->char_id;
  3413. node->login_id1 = sd->login_id1;
  3414. node->login_id2 = sd->login_id2;
  3415. node->sex = sd->sex;
  3416. node->expiration_time = sd->expiration_time;
  3417. node->gmlevel = sd->gmlevel;
  3418. node->ip = ipl;
  3419. idb_put(auth_db, sd->account_id, node);
  3420. }
  3421. break;
  3422. // create new char
  3423. // S 0067 <name>.24B <str>.B <agi>.B <vit>.B <int>.B <dex>.B <luk>.B <slot>.B <hair color>.W <hair style>.W
  3424. case 0x67:
  3425. FIFOSD_CHECK(37);
  3426. if( !char_new ) //turn character creation on/off [Kevin]
  3427. i = -2;
  3428. else
  3429. i = make_new_char(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35));
  3430. //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3)
  3431. if (i < 0)
  3432. {
  3433. WFIFOHEAD(fd,3);
  3434. WFIFOW(fd,0) = 0x6e;
  3435. switch (i) {
  3436. case -1: WFIFOB(fd,2) = 0x00; break;
  3437. case -2: WFIFOB(fd,2) = 0xFF; break;
  3438. case -3: WFIFOB(fd,2) = 0x01; break;
  3439. }
  3440. WFIFOSET(fd,3);
  3441. }
  3442. else
  3443. {
  3444. int len;
  3445. // send to player
  3446. WFIFOHEAD(fd,2+MAX_CHAR_BUF);
  3447. WFIFOW(fd,0) = 0x6d;
  3448. len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat[i].status);
  3449. WFIFOSET(fd,len);
  3450. // add new entry to the chars list
  3451. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 );
  3452. if( ch < MAX_CHARS )
  3453. sd->found_char[ch] = i; // position of the new char in the char_dat[] array
  3454. }
  3455. RFIFOSKIP(fd,37);
  3456. break;
  3457. // delete char
  3458. case 0x68:
  3459. // 2004-04-19aSakexe+ langtype 12 char deletion packet
  3460. case 0x1fb:
  3461. if (cmd == 0x68) FIFOSD_CHECK(46);
  3462. if (cmd == 0x1fb) FIFOSD_CHECK(56);
  3463. {
  3464. int cid = RFIFOL(fd,2);
  3465. struct mmo_charstatus* cs = NULL;
  3466. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  3467. memcpy(email, RFIFOP(fd,6), 40);
  3468. RFIFOSKIP(fd,( cmd == 0x68 ) ? 46 : 56);
  3469. if (e_mail_check(email) == 0)
  3470. safestrncpy(email, "a@a.com", sizeof(email)); // default e-mail
  3471. // BEGIN HACK: "change email using the char deletion 'confirm email' menu"
  3472. // if we activated email creation and email is default email
  3473. if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online
  3474. // if sended email is incorrect e-mail
  3475. if (strcmp(email, "a@a.com") == 0) {
  3476. WFIFOHEAD(fd,3);
  3477. WFIFOW(fd,0) = 0x70;
  3478. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3479. WFIFOSET(fd,3);
  3480. break;
  3481. }
  3482. // we change the packet to set it like selection.
  3483. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == cid );
  3484. if( i < MAX_CHARS )
  3485. {
  3486. // we save new e-mail
  3487. memcpy(sd->email, email, 40);
  3488. // we send new e-mail to login-server ('online' login-server is checked before)
  3489. WFIFOHEAD(login_fd,46);
  3490. WFIFOW(login_fd,0) = 0x2715;
  3491. WFIFOL(login_fd,2) = sd->account_id;
  3492. memcpy(WFIFOP(login_fd, 6), email, 40);
  3493. WFIFOSET(login_fd,46);
  3494. // change value to put new packet (char selection)
  3495. RFIFOSKIP(fd,-3); //FIXME: Will this work? Messing with the received buffer is ugly anyway...
  3496. RFIFOW(fd,0) = 0x66;
  3497. RFIFOB(fd,2) = char_dat[sd->found_char[i]].status.slot;
  3498. // not send packet, it's modify of actual packet
  3499. } else {
  3500. WFIFOHEAD(fd,3);
  3501. WFIFOW(fd,0) = 0x70;
  3502. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3503. WFIFOSET(fd,3);
  3504. }
  3505. break;
  3506. }
  3507. // END HACK
  3508. // otherwise, we delete the character
  3509. if (strcmpi(email, sd->email) != 0) { // if it's an invalid email
  3510. WFIFOHEAD(fd,3);
  3511. WFIFOW(fd,0) = 0x70;
  3512. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3513. WFIFOSET(fd,3);
  3514. break;
  3515. }
  3516. // check if this char exists
  3517. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == cid );
  3518. if( i == MAX_CHARS )
  3519. { // Such a character does not exist in the account
  3520. WFIFOHEAD(fd,3);
  3521. WFIFOW(fd,0) = 0x70;
  3522. WFIFOB(fd,2) = 0;
  3523. WFIFOSET(fd,3);
  3524. break;
  3525. }
  3526. // deletion process
  3527. cs = &char_dat[sd->found_char[i]].status;
  3528. //check for config char del condition [Lupus]
  3529. if( ( char_del_level > 0 && cs->base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && cs->base_level <= (unsigned int)(-char_del_level) ) )
  3530. {
  3531. WFIFOHEAD(fd,3);
  3532. WFIFOW(fd,0) = 0x70;
  3533. WFIFOB(fd,2) = 1; // This character cannot be deleted.
  3534. WFIFOSET(fd,3);
  3535. break;
  3536. }
  3537. char_delete(cs);
  3538. if (sd->found_char[i] != char_num - 1) {
  3539. int j, k;
  3540. struct char_session_data *sd2;
  3541. memcpy(&char_dat[sd->found_char[i]], &char_dat[char_num-1], sizeof(struct mmo_charstatus));
  3542. // Correct moved character reference in the character's owner
  3543. for (j = 0; j < fd_max; j++) {
  3544. if (session[j] && (sd2 = (struct char_session_data*)session[j]->session_data) &&
  3545. sd2->account_id == char_dat[char_num-1].status.account_id) {
  3546. for (k = 0; k < MAX_CHARS; k++) {
  3547. if (sd2->found_char[k] == char_num-1) {
  3548. sd2->found_char[k] = sd->found_char[i];
  3549. break;
  3550. }
  3551. }
  3552. break;
  3553. }
  3554. }
  3555. }
  3556. char_num--;
  3557. // remove char from list and compact it
  3558. for(ch = i; ch < MAX_CHARS-1; ch++)
  3559. sd->found_char[ch] = sd->found_char[ch+1];
  3560. sd->found_char[MAX_CHARS-1] = -1;
  3561. /* Char successfully deleted.*/
  3562. WFIFOHEAD(fd,2);
  3563. WFIFOW(fd,0) = 0x6f;
  3564. WFIFOSET(fd,2);
  3565. }
  3566. break;
  3567. // client keep-alive packet (every 12 seconds)
  3568. // R 0187 <account ID>.l
  3569. case 0x187:
  3570. if (RFIFOREST(fd) < 6)
  3571. return 0;
  3572. RFIFOSKIP(fd,6);
  3573. break;
  3574. // char rename request
  3575. // R 028d <account ID>.l <char ID>.l <new name>.24B
  3576. case 0x28d:
  3577. FIFOSD_CHECK(34);
  3578. {
  3579. //not implemented
  3580. RFIFOSKIP(fd,34);
  3581. }
  3582. break;
  3583. //Confirm change name.
  3584. // 0x28f <char_id>.L
  3585. case 0x28f:
  3586. // 0: Sucessfull
  3587. // 1: This character's name has already been changed. You cannot change a character's name more than once.
  3588. // 2: User information is not correct.
  3589. // 3: You have failed to change this character's name.
  3590. // 4: Another user is using this character name, so please select another one.
  3591. FIFOSD_CHECK(6);
  3592. {
  3593. //not implemented
  3594. RFIFOSKIP(fd,6);
  3595. }
  3596. break;
  3597. // captcha code request (not implemented)
  3598. // R 07e5 <?>.w <aid>.l
  3599. case 0x7e5:
  3600. WFIFOHEAD(fd,5);
  3601. WFIFOW(fd,0) = 0x7e9;
  3602. WFIFOW(fd,2) = 5;
  3603. WFIFOB(fd,4) = 1;
  3604. WFIFOSET(fd,5);
  3605. RFIFOSKIP(fd,8);
  3606. break;
  3607. // captcha code check (not implemented)
  3608. // R 07e7 <len>.w <aid>.l <code>.b10 <?>.b14
  3609. case 0x7e7:
  3610. WFIFOHEAD(fd,5);
  3611. WFIFOW(fd,0) = 0x7e9;
  3612. WFIFOW(fd,2) = 5;
  3613. WFIFOB(fd,4) = 1;
  3614. WFIFOSET(fd,5);
  3615. RFIFOSKIP(fd,32);
  3616. break;
  3617. // deletion timer request
  3618. case 0x827:
  3619. FIFOSD_CHECK(6);
  3620. char_delete2_req(fd, sd);
  3621. RFIFOSKIP(fd,6);
  3622. break;
  3623. // deletion accept request
  3624. case 0x829:
  3625. FIFOSD_CHECK(12);
  3626. char_delete2_accept(fd, sd);
  3627. RFIFOSKIP(fd,12);
  3628. break;
  3629. // deletion cancel request
  3630. case 0x82b:
  3631. FIFOSD_CHECK(6);
  3632. char_delete2_cancel(fd, sd);
  3633. RFIFOSKIP(fd,6);
  3634. break;
  3635. // login as map-server
  3636. case 0x2af8:
  3637. if (RFIFOREST(fd) < 60)
  3638. return 0;
  3639. {
  3640. char* l_user = (char*)RFIFOP(fd,2);
  3641. char* l_pass = (char*)RFIFOP(fd,26);
  3642. l_user[23] = '\0';
  3643. l_pass[23] = '\0';
  3644. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd <= 0 );
  3645. if( runflag != CHARSERVER_ST_RUNNING ||
  3646. i == ARRAYLENGTH(server) ||
  3647. strcmp(l_user, userid) != 0 ||
  3648. strcmp(l_pass, passwd) != 0 )
  3649. {
  3650. WFIFOHEAD(fd,3);
  3651. WFIFOW(fd,0) = 0x2af9;
  3652. WFIFOB(fd,2) = 3;
  3653. WFIFOSET(fd,3);
  3654. } else {
  3655. WFIFOHEAD(fd,3);
  3656. WFIFOW(fd,0) = 0x2af9;
  3657. WFIFOB(fd,2) = 0;
  3658. WFIFOSET(fd,3);
  3659. server[i].fd = fd;
  3660. server[i].ip = ntohl(RFIFOL(fd,54));
  3661. server[i].port = ntohs(RFIFOW(fd,58));
  3662. server[i].users = 0;
  3663. memset(server[i].map, 0, sizeof(server[i].map));
  3664. session[fd]->func_parse = parse_frommap;
  3665. session[fd]->flag.server = 1;
  3666. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3667. char_mapif_init(fd);
  3668. }
  3669. RFIFOSKIP(fd,60);
  3670. }
  3671. return 0; // avoid processing of followup packets here
  3672. // unknown packet received
  3673. default:
  3674. ShowError("parse_char: Received unknown packet "CL_WHITE"0x%x"CL_RESET" from ip '"CL_WHITE"%s"CL_RESET"'! Disconnecting!\n", RFIFOW(fd,0), ip2str(ipl, NULL));
  3675. set_eof(fd);
  3676. return 0;
  3677. }
  3678. }
  3679. RFIFOFLUSH(fd);
  3680. return 0;
  3681. }
  3682. // Console Command Parser [Wizputer]
  3683. int parse_console(const char* command)
  3684. {
  3685. ShowNotice("Console command: %s\n", command);
  3686. if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 || strcmpi("end", command) == 0 )
  3687. runflag = 0;
  3688. else if( strcmpi("alive", command) == 0 || strcmpi("status", command) == 0 )
  3689. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  3690. else if( strcmpi("help", command) == 0 )
  3691. {
  3692. ShowInfo("To shutdown the server:\n");
  3693. ShowInfo(" 'shutdown|exit|quit|end'\n");
  3694. ShowInfo("To know if server is alive:\n");
  3695. ShowInfo(" 'alive|status'\n");
  3696. }
  3697. return 0;
  3698. }
  3699. int mapif_sendall(unsigned char *buf, unsigned int len)
  3700. {
  3701. int i, c;
  3702. c = 0;
  3703. for(i = 0; i < ARRAYLENGTH(server); i++) {
  3704. int fd;
  3705. if ((fd = server[i].fd) > 0) {
  3706. WFIFOHEAD(fd,len);
  3707. memcpy(WFIFOP(fd,0), buf, len);
  3708. WFIFOSET(fd,len);
  3709. c++;
  3710. }
  3711. }
  3712. return c;
  3713. }
  3714. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  3715. {
  3716. int i, c;
  3717. c = 0;
  3718. for(i = 0; i < ARRAYLENGTH(server); i++) {
  3719. int fd;
  3720. if ((fd = server[i].fd) > 0 && fd != sfd) {
  3721. WFIFOHEAD(fd,len);
  3722. memcpy(WFIFOP(fd,0), buf, len);
  3723. WFIFOSET(fd,len);
  3724. c++;
  3725. }
  3726. }
  3727. return c;
  3728. }
  3729. int mapif_send(int fd, unsigned char *buf, unsigned int len)
  3730. {
  3731. int i;
  3732. if (fd >= 0) {
  3733. ARR_FIND( 0, ARRAYLENGTH(server), i, fd == server[i].fd );
  3734. if( i < ARRAYLENGTH(server) )
  3735. {
  3736. WFIFOHEAD(fd,len);
  3737. memcpy(WFIFOP(fd,0), buf, len);
  3738. WFIFOSET(fd,len);
  3739. return 1;
  3740. }
  3741. }
  3742. return 0;
  3743. }
  3744. int broadcast_user_count(int tid, unsigned int tick, int id, intptr_t data)
  3745. {
  3746. uint8 buf[6];
  3747. int users = count_users();
  3748. // only send an update when needed
  3749. static int prev_users = 0;
  3750. if( prev_users == users )
  3751. return 0;
  3752. prev_users = users;
  3753. if( login_fd > 0 && session[login_fd] )
  3754. {
  3755. // send number of user to login server
  3756. WFIFOHEAD(login_fd,6);
  3757. WFIFOW(login_fd,0) = 0x2714;
  3758. WFIFOL(login_fd,2) = users;
  3759. WFIFOSET(login_fd,6);
  3760. }
  3761. // send number of players to all map-servers
  3762. WBUFW(buf,0) = 0x2b00;
  3763. WBUFL(buf,2) = users;
  3764. mapif_sendall(buf,6);
  3765. // refresh online files (txt and html)
  3766. create_online_files();
  3767. return 0;
  3768. }
  3769. /// load this char's account id into the 'online accounts' packet
  3770. static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap)
  3771. {
  3772. struct online_char_data* character = (struct online_char_data*)data;
  3773. int* i = va_arg(ap, int*);
  3774. if(character->server > -1)
  3775. {
  3776. WFIFOL(login_fd,8+(*i)*4) = character->account_id;
  3777. (*i)++;
  3778. return 1;
  3779. }
  3780. return 0;
  3781. }
  3782. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data)
  3783. {
  3784. if (login_fd > 0 && session[login_fd])
  3785. {
  3786. // send account list to login server
  3787. int users = online_char_db->size(online_char_db);
  3788. int i = 0;
  3789. WFIFOHEAD(login_fd,8+users*4);
  3790. WFIFOW(login_fd,0) = 0x272d;
  3791. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  3792. WFIFOW(login_fd,2) = 8+ i*4;
  3793. WFIFOL(login_fd,4) = i;
  3794. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  3795. }
  3796. return 0;
  3797. }
  3798. int check_connect_login_server(int tid, unsigned int tick, int id, intptr_t data)
  3799. {
  3800. if (login_fd > 0 && session[login_fd] != NULL)
  3801. return 0;
  3802. ShowInfo("Attempt to connect to login-server...\n");
  3803. login_fd = make_connection(login_ip, login_port);
  3804. if (login_fd == -1)
  3805. { //Try again later. [Skotlex]
  3806. login_fd = 0;
  3807. return 0;
  3808. }
  3809. session[login_fd]->func_parse = parse_fromlogin;
  3810. session[login_fd]->flag.server = 1;
  3811. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3812. WFIFOHEAD(login_fd,86);
  3813. WFIFOW(login_fd,0) = 0x2710;
  3814. memcpy(WFIFOP(login_fd,2), userid, 24);
  3815. memcpy(WFIFOP(login_fd,26), passwd, 24);
  3816. WFIFOL(login_fd,50) = 0;
  3817. WFIFOL(login_fd,54) = htonl(char_ip);
  3818. WFIFOW(login_fd,58) = htons(char_port);
  3819. memcpy(WFIFOP(login_fd,60), server_name, 20);
  3820. WFIFOW(login_fd,80) = 0;
  3821. WFIFOW(login_fd,82) = char_maintenance;
  3822. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  3823. WFIFOSET(login_fd,86);
  3824. return 1;
  3825. }
  3826. // sends a ping packet to login server (will receive pong 0x2718)
  3827. int ping_login_server(int tid, unsigned int tick, int id, intptr_t data)
  3828. {
  3829. if (login_fd > 0 && session[login_fd] != NULL)
  3830. {
  3831. WFIFOHEAD(login_fd,2);
  3832. WFIFOW(login_fd,0) = 0x2719;
  3833. WFIFOSET(login_fd,2);
  3834. }
  3835. return 0;
  3836. }
  3837. //------------------------------------------------
  3838. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  3839. //replies/disconnect the player we tried to kick. [Skotlex]
  3840. //------------------------------------------------
  3841. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data)
  3842. {
  3843. struct online_char_data* character;
  3844. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  3845. { //Mark it offline due to timeout.
  3846. character->waiting_disconnect = INVALID_TIMER;
  3847. set_char_offline(character->char_id, character->account_id);
  3848. }
  3849. return 0;
  3850. }
  3851. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  3852. {
  3853. struct online_char_data *character= (struct online_char_data*)data;
  3854. if (character->fd != -1)
  3855. return 0; //Character still connected
  3856. if (character->server == -2) //Unknown server.. set them offline
  3857. set_char_offline(character->char_id, character->account_id);
  3858. if (character->server < 0)
  3859. //Free data from players that have not been online for a while.
  3860. db_remove(online_char_db, key);
  3861. return 0;
  3862. }
  3863. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr_t data)
  3864. {
  3865. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  3866. return 0;
  3867. }
  3868. //----------------------------------
  3869. // Reading Lan Support configuration
  3870. // Rewrote: Anvanced subnet check [LuzZza]
  3871. //----------------------------------
  3872. int char_lan_config_read(const char *lancfgName)
  3873. {
  3874. FILE *fp;
  3875. int line_num = 0;
  3876. char line[1024], w1[64], w2[64], w3[64], w4[64];
  3877. if((fp = fopen(lancfgName, "r")) == NULL) {
  3878. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  3879. return 1;
  3880. }
  3881. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  3882. while(fgets(line, sizeof(line), fp))
  3883. {
  3884. line_num++;
  3885. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  3886. continue;
  3887. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4) {
  3888. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  3889. continue;
  3890. }
  3891. remove_control_chars(w1);
  3892. remove_control_chars(w2);
  3893. remove_control_chars(w3);
  3894. remove_control_chars(w4);
  3895. if( strcmpi(w1, "subnet") == 0 )
  3896. {
  3897. subnet[subnet_count].mask = str2ip(w2);
  3898. subnet[subnet_count].char_ip = str2ip(w3);
  3899. subnet[subnet_count].map_ip = str2ip(w4);
  3900. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  3901. {
  3902. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  3903. continue;
  3904. }
  3905. subnet_count++;
  3906. }
  3907. }
  3908. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  3909. fclose(fp);
  3910. return 0;
  3911. }
  3912. #endif //TXT_SQL_CONVERT
  3913. int char_config_read(const char *cfgName)
  3914. {
  3915. char line[1024], w1[1024], w2[1024];
  3916. FILE* fp = fopen(cfgName, "r");
  3917. if (fp == NULL) {
  3918. ShowError("Configuration file not found: %s.\n", cfgName);
  3919. return 1;
  3920. }
  3921. ShowInfo("Reading configuration file %s...\n", cfgName);
  3922. while(fgets(line, sizeof(line), fp))
  3923. {
  3924. if (line[0] == '/' && line[1] == '/')
  3925. continue;
  3926. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3927. continue;
  3928. remove_control_chars(w1);
  3929. remove_control_chars(w2);
  3930. if(strcmpi(w1,"timestamp_format") == 0) {
  3931. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  3932. } else if(strcmpi(w1,"console_silent")==0){
  3933. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3934. msg_silent = atoi(w2);
  3935. #ifndef TXT_SQL_CONVERT
  3936. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  3937. stdout_with_ansisequence = config_switch(w2);
  3938. } else if (strcmpi(w1, "userid") == 0) {
  3939. safestrncpy(userid, w2, sizeof(userid));
  3940. } else if (strcmpi(w1, "passwd") == 0) {
  3941. safestrncpy(passwd, w2, sizeof(passwd));
  3942. } else if (strcmpi(w1, "server_name") == 0) {
  3943. safestrncpy(server_name, w2, sizeof(server_name));
  3944. ShowStatus("%s server has been initialized\n", w2);
  3945. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  3946. if (strlen(w2) >= 4) {
  3947. safestrncpy(wisp_server_name, w2, sizeof(wisp_server_name));
  3948. }
  3949. } else if (strcmpi(w1, "login_ip") == 0) {
  3950. char ip_str[16];
  3951. login_ip = host2ip(w2);
  3952. if (login_ip) {
  3953. safestrncpy(login_ip_str, w2, sizeof(login_ip_str));
  3954. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(login_ip, ip_str));
  3955. }
  3956. } else if (strcmpi(w1, "login_port") == 0) {
  3957. login_port = atoi(w2);
  3958. } else if (strcmpi(w1, "char_ip") == 0) {
  3959. char ip_str[16];
  3960. char_ip = host2ip(w2);
  3961. if (char_ip){
  3962. safestrncpy(char_ip_str, w2, sizeof(char_ip_str));
  3963. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(char_ip, ip_str));
  3964. }
  3965. } else if (strcmpi(w1, "bind_ip") == 0) {
  3966. char ip_str[16];
  3967. bind_ip = host2ip(w2);
  3968. if (bind_ip) {
  3969. safestrncpy(bind_ip_str, w2, sizeof(bind_ip_str));
  3970. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(bind_ip, ip_str));
  3971. }
  3972. } else if (strcmpi(w1, "char_port") == 0) {
  3973. char_port = atoi(w2);
  3974. } else if (strcmpi(w1, "char_maintenance") == 0) {
  3975. char_maintenance = atoi(w2);
  3976. } else if (strcmpi(w1, "char_new") == 0) {
  3977. char_new = (bool)atoi(w2);
  3978. } else if (strcmpi(w1, "char_new_display") == 0) {
  3979. char_new_display = atoi(w2);
  3980. } else if (strcmpi(w1, "email_creation") == 0) {
  3981. email_creation = config_switch(w2);
  3982. } else if (strcmpi(w1, "scdata_txt") == 0) { //By Skotlex
  3983. safestrncpy(scdata_txt, w2, sizeof(scdata_txt));
  3984. #endif
  3985. } else if (strcmpi(w1, "char_txt") == 0) {
  3986. safestrncpy(char_txt, w2, sizeof(char_txt));
  3987. } else if (strcmpi(w1, "friends_txt") == 0) { //By davidsiaw
  3988. safestrncpy(friends_txt, w2, sizeof(friends_txt));
  3989. } else if (strcmpi(w1, "hotkeys_txt") == 0) { //By davidsiaw
  3990. safestrncpy(hotkeys_txt, w2, sizeof(hotkeys_txt));
  3991. #ifndef TXT_SQL_CONVERT
  3992. } else if (strcmpi(w1, "max_connect_user") == 0) {
  3993. max_connect_user = atoi(w2);
  3994. if (max_connect_user < 0)
  3995. max_connect_user = 0; // unlimited online players
  3996. } else if(strcmpi(w1, "gm_allow_level") == 0) {
  3997. gm_allow_level = atoi(w2);
  3998. if(gm_allow_level < 0)
  3999. gm_allow_level = 99;
  4000. } else if (strcmpi(w1, "autosave_time") == 0) {
  4001. autosave_interval = atoi(w2)*1000;
  4002. if (autosave_interval <= 0)
  4003. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  4004. } else if (strcmpi(w1, "save_log") == 0) {
  4005. save_log = config_switch(w2);
  4006. } else if (strcmpi(w1, "start_point") == 0) {
  4007. char map[MAP_NAME_LENGTH_EXT];
  4008. int x, y;
  4009. if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
  4010. continue;
  4011. start_point.map = mapindex_name2id(map);
  4012. if (!start_point.map)
  4013. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  4014. start_point.x = x;
  4015. start_point.y = y;
  4016. } else if (strcmpi(w1, "start_zeny") == 0) {
  4017. start_zeny = atoi(w2);
  4018. if (start_zeny < 0)
  4019. start_zeny = 0;
  4020. } else if (strcmpi(w1, "start_weapon") == 0) {
  4021. start_weapon = atoi(w2);
  4022. if (start_weapon < 0)
  4023. start_weapon = 0;
  4024. } else if (strcmpi(w1, "start_armor") == 0) {
  4025. start_armor = atoi(w2);
  4026. if (start_armor < 0)
  4027. start_armor = 0;
  4028. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  4029. log_char = atoi(w2);
  4030. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  4031. safestrncpy(unknown_char_name, w2, sizeof(unknown_char_name));
  4032. unknown_char_name[NAME_LENGTH-1] = '\0';
  4033. } else if (strcmpi(w1, "char_log_filename") == 0) {
  4034. safestrncpy(char_log_filename, w2, sizeof(char_log_filename));
  4035. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  4036. name_ignoring_case = (bool)config_switch(w2);
  4037. } else if (strcmpi(w1, "char_name_option") == 0) {
  4038. char_name_option = atoi(w2);
  4039. } else if (strcmpi(w1, "char_name_letters") == 0) {
  4040. safestrncpy(char_name_letters, w2, sizeof(char_name_letters));
  4041. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  4042. char_per_account = atoi(w2);
  4043. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  4044. char_del_level = atoi(w2);
  4045. } else if (strcmpi(w1, "char_del_delay") == 0) {
  4046. char_del_delay = atoi(w2);
  4047. // online files options
  4048. } else if (strcmpi(w1, "online_txt_filename") == 0) {
  4049. safestrncpy(online_txt_filename, w2, sizeof(online_txt_filename));
  4050. } else if (strcmpi(w1, "online_html_filename") == 0) {
  4051. safestrncpy(online_html_filename, w2, sizeof(online_html_filename));
  4052. } else if (strcmpi(w1, "online_sorting_option") == 0) {
  4053. online_sorting_option = atoi(w2);
  4054. } else if (strcmpi(w1, "online_display_option") == 0) {
  4055. online_display_option = atoi(w2);
  4056. } else if (strcmpi(w1, "online_gm_display_min_level") == 0) { // minimum GM level to display 'GM' when we want to display it
  4057. online_gm_display_min_level = atoi(w2);
  4058. if (online_gm_display_min_level < 5) // send online file every 5 seconds to player is enough
  4059. online_gm_display_min_level = 5;
  4060. } else if (strcmpi(w1, "online_refresh_html") == 0) {
  4061. online_refresh_html = atoi(w2);
  4062. if (online_refresh_html < 1)
  4063. online_refresh_html = 1;
  4064. } else if(strcmpi(w1,"db_path")==0) {
  4065. safestrncpy(db_path, w2, sizeof(db_path));
  4066. } else if (strcmpi(w1, "console") == 0) {
  4067. console = config_switch(w2);
  4068. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  4069. fame_list_size_chemist = atoi(w2);
  4070. if (fame_list_size_chemist > MAX_FAME_LIST) {
  4071. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  4072. fame_list_size_chemist = MAX_FAME_LIST;
  4073. }
  4074. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  4075. fame_list_size_smith = atoi(w2);
  4076. if (fame_list_size_smith > MAX_FAME_LIST) {
  4077. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  4078. fame_list_size_smith = MAX_FAME_LIST;
  4079. }
  4080. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  4081. fame_list_size_taekwon = atoi(w2);
  4082. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  4083. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  4084. fame_list_size_taekwon = MAX_FAME_LIST;
  4085. }
  4086. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  4087. guild_exp_rate = atoi(w2);
  4088. #endif //TXT_SQL_CONVERT
  4089. } else if (strcmpi(w1, "import") == 0) {
  4090. char_config_read(w2);
  4091. }
  4092. }
  4093. fclose(fp);
  4094. ShowInfo("Done reading %s.\n", cfgName);
  4095. return 0;
  4096. }
  4097. #ifndef TXT_SQL_CONVERT
  4098. void do_final(void)
  4099. {
  4100. ShowStatus("Terminating...\n");
  4101. mmo_char_sync();
  4102. inter_save();
  4103. set_all_offline(-1);
  4104. flush_fifos();
  4105. do_final_mapif();
  4106. do_final_loginif();
  4107. // write online players files with no player
  4108. online_char_db->clear(online_char_db, NULL);
  4109. create_online_files();
  4110. online_char_db->destroy(online_char_db, NULL);
  4111. auth_db->destroy(auth_db, NULL);
  4112. if(char_dat) aFree(char_dat);
  4113. if( char_fd != -1 )
  4114. {
  4115. do_close(char_fd);
  4116. char_fd = -1;
  4117. }
  4118. #ifdef ENABLE_SC_SAVING
  4119. status_final();
  4120. #endif
  4121. inter_final();
  4122. mapindex_final();
  4123. char_log("----End of char-server (normal end with closing of all files).\n");
  4124. ShowStatus("Finished.\n");
  4125. }
  4126. //------------------------------
  4127. // Function called when the server
  4128. // has received a crash signal.
  4129. //------------------------------
  4130. void do_abort(void)
  4131. {
  4132. }
  4133. void set_server_type(void)
  4134. {
  4135. SERVER_TYPE = ATHENA_SERVER_CHAR;
  4136. }
  4137. /// Called when a terminate signal is received.
  4138. void do_shutdown(void)
  4139. {
  4140. if( runflag != CHARSERVER_ST_SHUTDOWN )
  4141. {
  4142. int id;
  4143. runflag = CHARSERVER_ST_SHUTDOWN;
  4144. ShowStatus("Shutting down...\n");
  4145. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  4146. for( id = 0; id < ARRAYLENGTH(server); ++id )
  4147. mapif_server_reset(id);
  4148. loginif_check_shutdown();
  4149. flush_fifos();
  4150. runflag = CORE_ST_STOP;
  4151. }
  4152. }
  4153. int do_init(int argc, char **argv)
  4154. {
  4155. //Read map indexes
  4156. mapindex_init();
  4157. start_point.map = mapindex_name2id("new_zone01");
  4158. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  4159. char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
  4160. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  4161. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  4162. ShowNotice("Please edit your save/account.txt file to create a proper inter-server user/password (gender 'S')\n");
  4163. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  4164. }
  4165. ShowInfo("Finished reading the char-server configuration.\n");
  4166. // a newline in the log...
  4167. char_log("");
  4168. char_log("The char-server starting...\n");
  4169. ShowInfo("Initializing char server.\n");
  4170. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  4171. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  4172. mmo_char_init();
  4173. char_read_fame_list(); //Read fame lists.
  4174. #ifdef ENABLE_SC_SAVING
  4175. status_init();
  4176. #endif
  4177. inter_init_txt((argc > 2) ? argv[2] : inter_cfgName); // inter server 初期化
  4178. ShowInfo("char server initialized.\n");
  4179. if ((naddr_ != 0) && (!login_ip || !char_ip))
  4180. {
  4181. char ip_str[16];
  4182. ip2str(addr_[0], ip_str);
  4183. if (naddr_ > 1)
  4184. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  4185. else
  4186. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  4187. if (!login_ip) {
  4188. safestrncpy(login_ip_str, ip_str, sizeof(login_ip_str));
  4189. login_ip = str2ip(login_ip_str);
  4190. }
  4191. if (!char_ip) {
  4192. safestrncpy(char_ip_str, ip_str, sizeof(char_ip_str));
  4193. char_ip = str2ip(char_ip_str);
  4194. }
  4195. }
  4196. do_init_loginif();
  4197. do_init_mapif();
  4198. // periodically update the overall user count on all mapservers + login server
  4199. add_timer_func_list(broadcast_user_count, "broadcast_user_count");
  4200. add_timer_interval(gettick() + 1000, broadcast_user_count, 0, 0, 5 * 1000);
  4201. // ???
  4202. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  4203. // ???
  4204. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  4205. add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000);
  4206. // periodic flush of all saved data to disk
  4207. add_timer_func_list(mmo_char_sync_timer, "mmo_char_sync_timer");
  4208. add_timer_interval(gettick() + 1000, mmo_char_sync_timer, 0, 0, autosave_interval);
  4209. if( console )
  4210. {
  4211. //##TODO invoke a CONSOLE_START plugin event
  4212. }
  4213. set_defaultparse(parse_char);
  4214. char_fd = make_listen_bind(bind_ip, char_port);
  4215. char_log("The char-server is ready (Server is listening on the port %d).\n", char_port);
  4216. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  4217. if( runflag != CORE_ST_STOP )
  4218. {
  4219. shutdown_callback = do_shutdown;
  4220. runflag = CHARSERVER_ST_RUNNING;
  4221. }
  4222. return 0;
  4223. }
  4224. #endif //TXT_SQL_CONVERT