加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
cgaapi.js 127.68 KB
一键复制 编辑 原始数据 按行查看 历史
卜星星 提交于 2021-12-16 13:00 . init
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489
var cga = require('bindings')('node_cga');
var moment = require('moment');
var PF = require('pathfinding');
var request = require('request');
var fs = require('fs');
global.is_array_contain = function(arr, val)
{
for (var i = 0; i < arr.length; i++)
{
if (arr[i] == val)
{
return true;
}
}
return false;
}
module.exports = function(callback){
var port = null;
if(process.argv.length >= 3 && parseInt(process.argv[2]) > 0)
port = parseInt(process.argv[2]);
else if(process.env.CGA_GAME_PORT && parseInt(process.env.CGA_GAME_PORT) > 0)
port = parseInt(process.env.CGA_GAME_PORT);
if(typeof port != 'number')
throw new Error('获取游戏本地服务端口失败!');
cga.AsyncConnect(port, function(err){
if(err){
throw new Error('无法连接到本地服务端口,可能未附加到游戏或者游戏已经闪退!');
}
callback();
});
cga.TRADE_STUFFS_ITEM = 1;
cga.TRADE_STUFFS_PET = 2;
cga.TRADE_STUFFS_PETSKILL = 3;
cga.TRADE_STUFFS_GOLD = 4;
cga.TRADE_STUFFS_TRANSLATION = {
1 : '物品',
2 : '宠物',
3 : '宠物技能',
4 : '金币',
};
cga.REQUEST_TYPE_PK = 1;
cga.REQUEST_TYPE_JOINTEAM = 3;
cga.REQUEST_TYPE_EXCAHNGECARD = 4;
cga.REQUEST_TYPE_TRADE = 5;
cga.REQUEST_TYPE_KICKTEAM = 11;
cga.REQUEST_TYPE_LEAVETEAM = 12;
cga.REQUEST_TYPE_TRADE_CONFIRM = 13;
cga.REQUEST_TYPE_TRADE_REFUSE = 14;
cga.REQUEST_TYPE_REBIRTH_ON = 16;
cga.REQUEST_TYPE_REBIRTH_OFF = 17;
cga.ENABLE_FLAG_PK = 0;
cga.ENABLE_FLAG_TEAMCHAT = 1;
cga.ENABLE_FLAG_JOINTEAM = 2;
cga.ENABLE_FLAG_CARD = 3;
cga.ENABLE_FLAG_TRADE = 4;
cga.ENABLE_FLAG_FAMILY = 5;
cga.ENABLE_FLAG_AVATAR_PUBLIC = 100;
cga.ENABLE_FLAG_BATTLE_POSITION = 101;
cga.TRADE_STATE_CANCEL = 0;
cga.TRADE_STATE_READY = 1;
cga.TRADE_STATE_CONFIRM = 2;
cga.TRADE_STATE_SUCCEED = 3;
cga.TRADE_STATE_TRANSLATION = {
0 : '取消交易',
1 : '准备交易',
2 : '确认交易',
3 : '交易成功',
};
cga.FL_BATTLE_ACTION_ISPLAYER = 1;
cga.FL_BATTLE_ACTION_ISDOUBLE = 2;
cga.FL_BATTLE_ACTION_ISSKILLPERFORMED = 4;
cga.FL_BATTLE_ACTION_END = 8;
cga.FL_BATTLE_ACTION_BEGIN = 16;
cga.FL_SKILL_SELECT_TARGET = 0x1;
cga.FL_SKILL_SELECT_DEAD = 0x2;
cga.FL_SKILL_TO_PET = 0x4;
cga.FL_SKILL_TO_SELF = 0x8;
cga.FL_SKILL_TO_TEAMMATE = 0x10;
cga.FL_SKILL_TO_ENEMY = 0x20;
cga.FL_SKILL_SINGLE = 0x40;
cga.FL_SKILL_MULTI = 0x80;
cga.FL_SKILL_ALL = 0x100;
cga.FL_SKILL_BOOM = 0x200;
cga.FL_SKILL_FRONT_ONLY = 0x400;
cga.MOVE_GOLD_TOBANK = 1;
cga.MOVE_GOLD_FROMBANK = 2;
cga.MOVE_GOLD_DROP = 3
cga.PET_STATE_READY = 1;
cga.PET_STATE_BATTLE = 2;
cga.PET_STATE_REST = 3;
cga.PET_STATE_WALK = 16;
cga.UI_DIALOG_TRADE = 1;
cga.UI_DIALOG_BATTLE_SKILL = 2;
//延迟x毫秒
cga.delay = (millis) => new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, millis);
});
cga.promisify = (fn, args) => new Promise((resolve, reject) => {
args.push((err, reason) => {
console.log(err);
setTimeout(() => {
if (err) reject(err);
else resolve();
}, 0);
});
fn.apply(null, args);
});
cga.moveThinkFnArray = [];
cga.moveThink = (arg)=>{
for(var i = 0; i < cga.moveThinkFnArray.length; ++i){
if(cga.moveThinkFnArray[i](arg) == false){
return false;
}
}
return true;
}
cga.isMoveThinking = false;
cga.registerMoveThink = (fn)=>{
cga.moveThinkFnArray.push(fn);
}
cga.isTeamLeaderEx = ()=>{
return (cga.isTeamLeader == true || cga.getTeamPlayers().length <= 1);
}
cga.getMapInfo = () => {
const info = cga.GetMapXY();
info.indexes = cga.GetMapIndex();
info.name = cga.GetMapName();
return info;
};
cga.getOrientation = (x, y) => {
const p = cga.GetMapXY();
const xy = Math.max(-1, Math.min(1, x - p.x)).toString() + Math.max(-1, Math.min(1, y - p.y)).toString();
switch (xy) {
case '10':
return 0;
case '11':
return 1;
case '01':
return 2;
case '-11':
return 3;
case '-10':
return 4;
case '-1-1':
return 5;
case '0-1':
return 6;
case '1-1':
return 7;
default:
}
return -1;
}
cga.getOrientationPosition = (orientation, offset)=>{
const current = cga.GetMapXY();
switch (orientation) {
case 0:
return [current.x + offset, current.y];
case 1:
return [current.x + offset, current.y + offset];
case 2:
return [current.x, current.y + offset];
case 3:
return [current.x - offset, current.y + offset];
case 4:
return [current.x - offset, current.y];
case 5:
return [current.x - offset, current.y - offset];
case 6:
return [current.x, current.y - offset];
case 7:
return [current.x + offset, current.y - offset];
default:
return new Error('无效参数');
}
}
cga.turnDir = cga.turnOrientation = (orientation, offset = 2) => {
var pos = cga.getOrientationPosition(orientation, offset);
cga.TurnTo(pos[0], pos[1]);
}
//转向(x,y)坐标,默认往前一格避免捡起面前的物品
cga.turnTo = (x, y)=>{
cga.turnOrientation(cga.getOrientation(x, y));
}
//判断是否在战斗状态
cga.isInBattle = function(){
return (cga.GetWorldStatus() == 10) ? true : false;
}
//判断是否在正常状态(非切图非战斗状态)
cga.isInNormalState = function(){
return (cga.GetWorldStatus() == 9 && cga.GetGameStatus() == 3) ? true : false;
}
//将字符串转义为windows下合法的文件名
cga.FileNameEscape = (str)=>{
return str.replace(/[\\/:\*\?"<>|]/g, (c)=>{return {"\\":'%5C','/':'%2F',':':'%3A','*':'%2A','?':'%3F','"':'%22','<':'%3C','>':'%3E','|':'%7C'}[c];});
}
//FileNameEscape的反向操作,反转义
cga.FileNameUnescape = (str)=>{
return str.replace(/%(5C|2F|3A|2A|3F|22|3C|3E|7C)/g, (c)=>{ return {'%5C':'\\','%2F':'/','%3A':':','%2A':'*','%3F':'?','%22':'"','%3C':'<','%3E':'>','%7C':'|'}[c];});
}
//获取制造某种物品所需要的材料信息,返回材料信息object或null
cga.getItemCraftInfo = function(filter){
var result = null;
cga.GetSkillsInfo().forEach((sk)=>{
if(sk.type == 1)
{
var craftInfo = cga.GetCraftsInfo(sk.index).find((craft)=>{
if(typeof filter == 'string')
{
if(filter.charAt(0) == '#')
return craft.itemid == parseInt(filter.substring(1));
else
return craft.name == filter;
}
else if(typeof filter == 'number')
{
return craft.itemid == filter;
}
else if(typeof filter == 'function')
{
return filter(craft);
}
return false;
});
if(craftInfo != undefined){
result = {craft : craftInfo, skill : sk};
return false;
}
}
});
return result;
}
/*鉴定、装饰物品,参数:
cga.manipulateItemEx({
itempos : 操作的物品位置,
immediate : 是否立即完成(高速鉴定),
}, cb回调)
*/
cga.manipulateItemEx = function(options, cb){
var skill = cga.findPlayerSkill(options.skill);
if(!skill){
cb(new Error('你没有'+skillname+'的技能'));
return;
}
cga.SetImmediateDoneWork(options.immediate ? true : false);
cga.StartWork(skill.index, 0);
if(!cga.AssessItem(skill.index, options.itempos)){
cb(new Error('无法操作该物品'));
return;
}
var beginTime = (new Date()).getTime();
var handler = (err, results)=>{
if(results){
cb(null, results);
return;
}
var craftStatus = cga.GetCraftStatus();
var curTime = (new Date()).getTime();
if(err){
if(craftStatus == 0 || craftStatus == 2){
cga.manipulateItemEx(options, cb);
return;
}
//强制重试
var isImmediate = cga.GetImmediateDoneWorkState();
if(isImmediate != 2 && curTime > beginTime + 1000 * 120)
{
cga.manipulateItemEx(options, cb);
return;
}
else if(isImmediate == 2 && curTime > beginTime + 1000 * 5)
{
cga.manipulateItemEx(options, cb);
return;
}
cga.AsyncWaitWorkingResult(handler, 1000);
}
}
cga.AsyncWaitWorkingResult(handler, 1000);
return;
}
//制造物品,参数:物品名,添加的宝石的名字(或物品位置)
//该API已经弃用,请用cga.craftItemEx
cga.craftNamedItem = function(craftItemName, extraItemName){
throw new Error('该API已经弃用,请用cga.craftItemEx')
}
/*制造物品,参数:
cga.craftItemEx({
craftitem : 制造的物品名,
extraitem(可选) : 添加宝石
immediate : 是否立即完成(高速制造),
}, cb回调)
*/
cga.craftItemEx = function(options, cb){
var err = null;
var info = cga.getItemCraftInfo(options.craftitem);
if(info === null)
err = new Error('你没有制造 '+options.craftitem+' 的技能');
if(err){
cb(err);
return;
}
var inventory = cga.getInventoryItems();
var itemArray = [];
info.craft.materials.forEach((mat)=>{
var findRequired = inventory.find((inv)=>{
return (inv.itemid == mat.itemid && inv.count >= mat.count);
});
if(findRequired != undefined){
itemArray.push(findRequired.pos);
} else {
err = new Error('制造' +options.craftitem+'所需物品' +mat.name+'不足!');
return false;
}
});
if(err){
cb(err);
return;
}
if(typeof options.extraitem == 'string'){
var findRequired = inventory.find((inv)=>{
return (inv.name == options.extraitem);
});
if(findRequired != undefined){
itemArray[5] = findRequired.pos;
} else {
err = new Error('制造' +options.extraitem+'所需宝石' +options.extraitem+'不足!');
}
}
if(err){
cb(err);
return;
}
for(var i = 0; i < 6; ++i)
{
if(typeof itemArray[i] != 'number')
itemArray[i] = -1;
}
cga.SetImmediateDoneWork(options.immediate ? true : false);
cga.StartWork(info.skill.index, info.craft.index);
cga.CraftItem(info.skill.index, info.craft.index, 0, itemArray);
var beginTime = (new Date()).getTime();
var handler = (err, results)=>{
if(results){
cb(null, results);
return;
}
var craftStatus = cga.GetCraftStatus();
var curTime = (new Date()).getTime();
if(err){
if(craftStatus == 0 || craftStatus == 2){
cga.craftItemEx(options, cb);
return;
}
//强制重试
var isImmediate = cga.GetImmediateDoneWorkState();
if(isImmediate != 2 && curTime > beginTime + 1000 * 120)
{
cga.craftItemEx(options, cb);
return;
}
else if(isImmediate == 2 && curTime > beginTime + 1000 * 5)
{
cga.craftItemEx(options, cb);
return;
}
cga.AsyncWaitWorkingResult(handler, 1000);
}
}
cga.AsyncWaitWorkingResult(handler, 1000);
}
//获取物品栏里的物品,返回数组
cga.getInventoryItems = function(){
return cga.GetItemsInfo().filter((item)=>{
return item.pos >= 8 && item.pos < 100;
});
}
//获取装备栏里的物品,返回数组
cga.getEquipItems = function(){
return cga.GetItemsInfo().filter((item)=>{
return item.pos >= 0 && item.pos < 8;
});
}
//获取装备耐久,返回数组[当前耐久,最大耐久]
cga.getEquipEndurance = (item)=>{
if(item.attr){
var regex = item.attr.match(/\$4耐久 (\d+)\/(\d+)/);
if(regex && regex.length >= 3){
return [parseInt(regex[1]), parseInt(regex[2])];
}
}
return null;
}
cga.travel = {};
cga.travel.falan = {};
cga.travel.falan.isSettled = false;
cga.travel.falan.xy2name = (x, y, mapname)=>{
if(x == 242 && y == 100 && mapname == '法兰城')
return 'E1';
if(x == 141 && y == 148 && mapname == '法兰城')
return 'S1';
if(x == 63 && y == 79 && mapname == '法兰城')
return 'W1';
if(x == 233 && y == 78 && mapname == '法兰城')
return 'E2';
if(x == 162 && y == 130 && mapname == '法兰城')
return 'S2';
if(x == 72 && y == 123 && mapname == '法兰城')
return 'W2';
if(x == 46 && y == 16 && mapname == '市场三楼 - 修理专区')
return 'M3';
if(x == 46 && y == 16 && mapname == '市场一楼 - 宠物交易区')
return 'M1';
if(x == 151 && y == 122 && mapname == '法兰城')
return 'B1';
if(x == 155 && y == 122 && mapname == '法兰城')
return 'B2';
return null;
}
cga.travel.falan.isvalid = function(stone){
switch(stone.toUpperCase()){
case 'E': return true;
case 'S': return true;
case 'W': return true;
case 'E1': return true;
case 'S1': return true;
case 'W1': return true;
case 'E2': return true;
case 'S2': return true;
case 'W2': return true;
case 'M1': return true;//市场
case 'M3': return true;
case 'B1': return true;//桥头
case 'B2': return true;
case 'C': return true;//里谢里雅堡
}
return false;
}
cga.travel.falan.toStoneInternal = function(stone, cb){
var curXY = cga.GetMapXY();
var curMap = cga.GetMapName();
const desiredMap = ['法兰城','里谢里雅堡','艾尔莎岛','市场一楼 - 宠物交易区','市场三楼 - 修理专区'];
if(curMap == '法兰城'){
if(stone == 'C'){
cga.travel.falan.toCastle(cb);
return;
}
if(stone == 'B1'){
cga.walkList([
[151, 122]
], cb);
return;
}
if(stone == 'B2'){
cga.walkList([
[155, 122]
], cb);
return;
}
var curStone = cga.travel.falan.xy2name(curXY.x, curXY.y, curMap);
if(curStone) {
var turn = false;
if(stone == 'M1' || stone == 'M3') {
if(curStone == stone){
cb(null);
return;
}
turn = true;
}
else if(stone.length >= 2 && curStone.charAt(1) == stone.charAt(1)) {
if(curStone.charAt(0) == 'S' && stone.charAt(0) == 'B'){
cga.walkList([
stone == 'B1' ? [151, 122] : [155, 122]
], cb);
return;
}
if(curStone == stone){
cb(null);
return;
}
turn = true;
} else if(stone.length < 2){
if(curStone.charAt(0) == stone.charAt(0)){
cb(null);
return;
}
turn = true;
}
if(turn){
switch(curStone){
case 'E2':cga.turnDir(6);break;
case 'S2':cga.turnDir(0);break;
case 'W2':cga.turnDir(0);break;
case 'E1':cga.turnDir(0);break;
case 'S1':cga.turnDir(6);break;
case 'W1':cga.turnDir(6);break;
}
cga.AsyncWaitMovement({map:desiredMap, delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.falan.toStoneInternal(stone, cb);
});
return;
}
}
}
if(curMap.indexOf('市场') >= 0 && curXY.x == 46 && curXY.y == 16){
if(stone == 'M1' && curMap == '市场一楼 - 宠物交易区'){
cb(null);
return;
}
if(stone == 'M3' && curMap == '市场三楼 - 修理专区'){
cb(null);
return;
}
cga.turnDir(6);
cga.AsyncWaitMovement({map:desiredMap, delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.falan.toStoneInternal(stone, cb);
});
return;
}
if(curMap == '艾尔莎岛'){
cga.walkList([
[140, 105],
], ()=>{
cga.turnDir(7);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:desiredMap, delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.falan.toStoneInternal(stone, cb);
});
});
})
return;
}
if(curMap == '里谢里雅堡'){
if(stone == 'C'){
cb(null);
return;
}
var walks = null;
const walkOutOfCastle_1 = [
[41, 98, '法兰城'],
[141, 148]
];
const walkOutOfCastle_2 = [
[40, 98, '法兰城'],
[162, 130]
];
const walkOutOfCastle_3 = [
[41, 98, '法兰城'],
];
if(stone == 'M1')
walks = walkOutOfCastle_2;
else if(stone == 'M3')
walks = walkOutOfCastle_1;
else if(stone.length >= 2 && stone.charAt(0) == 'B')
walks = walkOutOfCastle_3;
else if(stone.length == 1)
walks = walkOutOfCastle_2;
else if(stone.length >= 2 && stone.charAt(1) == '1')
walks = walkOutOfCastle_1;
else
walks = walkOutOfCastle_2;
cga.walkList(walks, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.falan.toStoneInternal(stone, cb);
});
return;
}
cga.LogBack();
cga.AsyncWaitMovement({map:desiredMap, delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.falan.toStoneInternal(stone, cb);
});
}
//参数1:传送石名称,有效参数:E1 S1 W1 E2 S2 W2 M1(道具-市场1楼) M3(道具-市场3楼)
//参数2:回调函数function(result), result 为true或false
cga.travel.falan.toStone = function(stone, cb){
if(!cga.travel.falan.isvalid(stone)){
cb(new Error('无效的目的地名称'));
return;
}
cga.travel.falan.toStoneInternal(stone, cb, true);
}
//前往到法兰城东医院
//参数1:回调函数function(result), result 为true或false
cga.travel.falan.toEastHospital = (cb)=>{
cga.travel.falan.toStone('E', ()=>{
cga.walkList([
[221, 83, '医院']
], cb);
});
}
//前往到法兰城西医院
//参数1:回调函数function(result), result 为true或false
cga.travel.falan.toWestHospital = (cb)=>{
cga.travel.falan.toStone('W', (r)=>{
cga.walkList([
[82, 83, '医院'],
], cb);
});
}
//前往到法兰城银行
cga.travel.falan.toBank = (cb)=>{
if(cga.GetMapIndex().index3 == 1121){
cb(null);
return;
}
cga.travel.falan.toStone('E', (r)=>{
cga.walkList([
[238, 111, '银行'],
], cb);
});
}
//从法兰城到里谢里雅堡,启动地点:登出到法兰城即可
cga.travel.falan.toCastle = (cb)=>{
if(cga.GetMapName() == '里谢里雅堡'){
cb(null);
return;
}
if(cga.GetMapName() == '法兰城'){
var curXY = cga.GetMapXY();
var westPath = cga.calculatePath(curXY.x, curXY.y, 141, 88, '里谢里雅堡', null, null, []);
westPath = PF.Util.expandPath(westPath);
var southPath = cga.calculatePath(curXY.x, curXY.y, 153, 100, '里谢里雅堡', null, null, []);
southPath = PF.Util.expandPath(southPath);
var eastPath = cga.calculatePath(curXY.x, curXY.y, 165, 88, '里谢里雅堡', null, null, []);
eastPath = PF.Util.expandPath(eastPath);
var northPath = cga.calculatePath(curXY.x, curXY.y, 153, 70, '里谢里雅堡', null, null, []);
northPath = PF.Util.expandPath(northPath);
var path = westPath;
var target = [141, 88, '里谢里雅堡'];
if(path.length > southPath.length)
{
path = southPath;
target = [153, 100, '里谢里雅堡'];
}
if(path.length > eastPath.length)
{
path = eastPath;
target = [165, 88, '里谢里雅堡'];
}
if(path.length > northPath.length)
{
path = northPath;
target = [153, 70, '里谢里雅堡'];
}
cga.walkList([target], cb);
} else {
cga.travel.falan.toStone('S', ()=>{
cga.travel.falan.toCastle(cb);
});
}
}
cga.travel.falan.toCastleHospital = function(cb){
if(cga.GetMapName() == '里谢里雅堡'){
var pos = cga.GetMapXY();
if(pos.x == 34 && pos.y == 89)
{
cga.TurnTo(36, 87);
cb(true);
return;
}
else
{
cga.walkList([
[34, 89]
], (r)=>{
cga.TurnTo(36, 87);
cb(true);
});
}
return;
}
cga.travel.falan.toStone('C', (r)=>{
cga.walkList([
[34, 89]
], (r)=>{
cga.TurnTo(36, 87);
cb(true);
});
});
}
//前往里堡打卡处并打卡
cga.travel.falan.toCastleClock = (cb)=>{
cga.travel.falan.toStone('C', (r)=>{
cga.walkList([
[58, 83]
], (r)=>{
cga.TurnTo(58, 84);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitNPCDialog(()=>{
cb(null);
});
} else {
cb(new Error('没有卡时,无法打卡'));
}
});
});
});
}
cga.travel.camp = {};
cga.travel.camp.getRegion = (mapname, mapXY)=>{
if(mapname == '肯吉罗岛')
{
if(mapXY.x <= 316 && mapXY.y >= 325)
{
return '矮人城镇域';
}
if(mapXY.x >= 355 && mapXY.x <= 516 && mapXY.y <= 181)
{
return '沼泽洞穴出口域';
}
if(mapXY.x < 355 && mapXY.y <= 212)
{
return '沼泽洞穴出口域';
}
return '圣骑士营地域';
}
return null;
}
//前往圣骑士营地,noWarp为true时只进到曙光骑士团营地
cga.travel.falan.toCamp = (cb, noWarp)=>{
var warp = ()=>{
var teamplayers = cga.getTeamPlayers();
var isTeamLeader = (teamplayers.length > 0 && teamplayers[0].is_me) == true ? true : false;
if(isTeamLeader){
setTimeout(()=>{
cga.DoRequest(cga.REQUEST_TYPE_LEAVETEAM);
setTimeout(warp, 1500);
}, 1500);
return;
}
cga.TurnTo(7, 21);
cga.AsyncWaitMovement({map:'圣骑士营地', delay:1000, timeout:5000}, cb);
}
var castle_2_camp = ()=>{
var shouldWarp = (cga.getItemCount('承认之戒', true) > 0 && noWarp !== true) ? true : false;
var list = shouldWarp ? [
[55,47, '辛希亚探索指挥部'],
[7,4, '辛希亚探索指挥部', 91, 6],
[95, 9, 27101],
[8, 21],
] : [
];
if(cga.GetMapName() == '里谢里雅堡'){
list.unshift([513, 282, '曙光骑士团营地']);
list.unshift([153, 241, '芙蕾雅']);
list.unshift([41, 98, '法兰城']);
} else if(cga.GetMapName() == '法兰城'){
list.unshift([513, 282, '曙光骑士团营地']);
list.unshift([153, 241, '芙蕾雅']);
} else if(cga.GetMapName() == '芙蕾雅'){
list.unshift([513, 282, '曙光骑士团营地']);
}
cga.walkList(list, (shouldWarp) ? warp : cb);
}
var mapname = cga.GetMapName();
if(mapname == '圣骑士营地'){
cb(null);
return;
}
var mapindex = cga.GetMapIndex().index3;
if(mapindex == 44692){
cga.walkList([
[0, 20, '圣骑士营地'],
], cb);
return;
}
if(mapindex == 44693){
cga.walkList([
[30, 37, '圣骑士营地'],
], cb);
return;
}
if(mapindex == 44698){
cga.walkList([
[3, 23, '圣骑士营地'],
], cb);
return;
}
if(mapindex == 44699){
cga.walkList([
[0, 14, '圣骑士营地'],
], cb);
return;
}
if(mapname == '辛希亚探索指挥部' && cga.GetMapIndex().index3 == 27101){
cga.walkList([[8, 21]], warp);
return;
}
if(mapname == '法兰城' || mapname == '里谢里雅堡' || mapname == '芙蕾雅' || mapname == '曙光骑士团营地'){
castle_2_camp(null);
}else{
cga.travel.falan.toStone('C', castle_2_camp);
}
}
//前往流行商店
cga.travel.falan.toFashionStore = cga.travel.falan.toFabricStore = (cb)=>{
if(cga.GetMapName()=='流行商店'){
cb(null);
return;
}
if(cga.GetMapName() == '法兰城'){
cga.travel.falan.toStone('S1', ()=>{
cga.walkList([
[117, 112, '流行商店'],
], cb);
});
} else {
cga.travel.falan.toStone('C', ()=>{
cga.walkList([
[17, 53, '法兰城'],
[117, 112, '流行商店'],
], cb);
});
}
}
//前往凯蒂夫人的店
cga.travel.falan.toKatieStore = cga.travel.falan.toAssessStore = (cb)=>{
if(cga.GetMapName()=='凯蒂夫人的店'){
cb(null);
return;
}
if(cga.GetMapName() == '法兰城'){
cga.travel.falan.toStone('E2', function(r){
cga.walkList([
[196, 78, '凯蒂夫人的店'],
], cb);
});
} else {
cga.travel.falan.toStone('C', ()=>{
cga.walkList([
[65, 53, '法兰城'],
[196, 78, '凯蒂夫人的店'],
], cb);
});
}
}
//前往达美姊妹的店
cga.travel.falan.toDameiStore = cga.travel.falan.toCrystalStore = (cb)=>{
if(cga.GetMapName()=='达美姊妹的店'){
cb(null);
return;
}
if(cga.GetMapName() == '法兰城'){
cga.travel.falan.toStone('W1', function(r){
cga.walkList([
[94, 78, '达美姊妹的店'],
], cb);
});
} else {
cga.travel.falan.toStone('C', ()=>{
cga.walkList([
[17, 53, '法兰城'],
[94, 78, '达美姊妹的店'],
], cb);
});
}
}
//前往法兰工房,mine为要换的矿名
cga.travel.falan.toMineStore = (mine, cb)=>{
var mineExchange = null;
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[26, 5]], ()=>{
cga.turnTo(26, 4);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[28, 6]], ()=>{
cga.turnTo(28, 5);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[29, 6]], ()=>{
cga.turnTo(30, 5);
cb2(null);
});
}
}
if(mine == '纯银'){
mineExchange = (cb2)=>{
cga.walkList([[27, 7]], ()=>{
cga.turnTo(27, 5);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[24, 6]], ()=>{
cga.turnTo(24, 5);
cb2(null);
});
}
}
if(mine == '白金'){
mineExchange = (cb2)=>{
cga.walkList([[29, 6]], ()=>{
cga.turnTo(30, 7);
cb2(null);
});
}
}
if(mine == '幻之钢'){
mineExchange = (cb2)=>{
cga.walkList([[26, 10]], ()=>{
cga.turnTo(28, 10);
cb2(null);
});
}
}
if(mine == '幻之银'){
mineExchange = (cb2)=>{
cga.walkList([[27, 9]], ()=>{
cga.turnTo(28, 8);
cb2(null);
});
}
}
if(mine == '勒格耐席鉧'){
mineExchange = (cb2)=>{
cga.walkList([[23, 7]], ()=>{
cga.turnTo(22, 6);
cb2(null);
});
}
}
if(mine == '奥利哈钢'){
mineExchange = (cb2)=>{
cga.walkList([[26, 12]], ()=>{
cga.turnTo(27, 12);
cb2(null);
});
}
}
if(cga.GetMapName()=='米克尔工房'){
if(mineExchange){
mineExchange(cb);
}else{
cb(null);
}
return;
}
if(cga.GetMapName() == '法兰城'){
cga.travel.falan.toStone('W1', function(r){
cga.walkList([
[100, 61, '米克尔工房'],
], ()=>{
if(mineExchange){
mineExchange(cb);
}else{
cb(null);
}
});
});
} else {
cga.travel.falan.toStone('C', ()=>{
cga.walkList([
[17, 53, '法兰城'],
[100, 61, '米克尔工房'],
], ()=>{
if(mineExchange){
mineExchange(cb);
}else{
cb(null);
}
});
});
}
}
//前往新城工房,mine为要换的矿名
cga.travel.falan.toNewMineStore = (mine, cb)=>{
var mineExchange = null;
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[35, 46]], ()=>{
cga.turnTo(35, 45);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[32, 47]], ()=>{
cga.turnTo(31, 47);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[42, 51]], ()=>{
cga.turnTo(41, 51);
cb2(null);
});
}
}
if(mine == '纯银'){
mineExchange = (cb2)=>{
cga.walkList([[37, 49]], ()=>{
cga.turnTo(37, 48);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[40, 50]], ()=>{
cga.turnTo(39, 49);
cb2(null);
});
}
}
if(mine == '白金'){
mineExchange = (cb2)=>{
cga.walkList([[38, 43]], ()=>{
cga.turnTo(39, 44);
cb2(null);
});
}
}
if(mine == '幻之钢'){
mineExchange = (cb2)=>{
cga.walkList([[26, 10]], ()=>{
cga.turnTo(28, 10);
cb2(null);
});
}
}
if(mine == '幻之银'){
mineExchange = (cb2)=>{
cga.walkList([[37, 44]], ()=>{
cga.turnTo(38, 45);
cb2(null);
});
}
}
if(mine == '勒格耐席鉧'){
mineExchange = (cb2)=>{
cga.walkList([[33, 46]], ()=>{
cga.turnTo(33, 45);
cb2(null);
});
}
}
if(mine == '奥利哈钢'){
mineExchange = (cb2)=>{
cga.walkList([[44, 48]], ()=>{
cga.turnTo(43, 48);
cb2(null);
});
}
}
if(mine == ''){
mineExchange = (cb2)=>{
cga.walkList([[38, 54]], ()=>{
cga.turnTo(38, 53);
cb2(null);
});
}
}
if(cga.GetMapName()=='画廊'){
if(mineExchange){
mineExchange(cb);
}else{
cb(null);
}
return;
}
cga.travel.newisland.toStone('B', ()=>{
cga.walkList([
[144, 120, '武器工房'],
[28, 21, '画廊'],
], ()=>{
if(mineExchange){
mineExchange(cb);
}else{
cb(null);
}
});
});
}
//从法兰城到新城
cga.travel.falan.toNewIsland = (cb)=>{
cga.travel.falan.toStone('C', function(r){
cga.walkList([
[28, 88]
], (r)=>{
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:''}, ()=>{
cga.walkList([
[19, 21, '法兰城遗迹'],
[96, 138, '盖雷布伦森林'],
[124, 168, '温迪尔平原'],
[264, 108, '艾尔莎岛'],
], cb);
});
} else {
cb(new Error('对话失败'));
}
});
});
});
});
}, 1000);
});
});
}
//从法兰城到阿凯鲁法
cga.travel.falan.toAKLF = (cb)=>{
if(cga.GetMapName() == '阿凯鲁法村'){
cb(null);
return;
}
var stage3 = ()=>{
cga.walkList([
[20, 53],
], (r)=>{
cga.TurnTo(18, 53);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'港湾管理处'}, ()=>{
cga.walkList([
[22, 31, '阿凯鲁法'],
[28, 30],
], ()=>{
cga.TurnTo(30, 30);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'阿凯鲁法村'}, cb);
});
});
});
});
});
}
var retry2 = ()=>{
cga.TurnTo(71, 26);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.message.indexOf('现在正停靠在阿凯鲁法港') >= 0 && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往伊尔栈桥'}, ()=>{
stage3();
});
return;
}
setTimeout(retry2, 5000);
});
}
var retry = ()=>{
cga.TurnTo(53, 50);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'艾欧奇亚号'}, retry2);
return;
}
setTimeout(retry, 5000);
});
}
if(cga.GetMapName() != '伊尔村'){
cga.travel.falan.toTeleRoom('伊尔村', ()=>{
cga.walkList([
[12, 17, '村长的家'],
[6, 13, '伊尔村'],
], ()=>{
cga.travel.falan.toAKLF(cb);
});
});
return;
}
cga.walkList([
[58, 71],
], ()=>{
cga.turnTo(60, 71);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'伊尔'}, ()=>{
cga.walkList([
[30, 21, '港湾管理处'],
[23, 25],
], ()=>{
cga.TurnTo(23, 23);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往阿凯鲁法栈桥'}, ()=>{
cga.walkList([
[51, 50],
], retry);
});
});
});
});
});
}, 1000);
});
}
//从法兰城到哥拉尔
cga.travel.falan.toGelaer = (cb)=>{
if(cga.GetMapName() == '哥拉尔'){
cb(null);
return;
}
var stage3 = ()=>{
cga.walkList([
[84, 55],
], ()=>{
cga.turnTo(84, 53);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'哥拉尔镇 港湾管理处'}, ()=>{
cga.walkList([
[14, 15, '哥拉尔镇'],
[118, 214],
], cb);
});
});
});
}
var retry2 = ()=>{
cga.TurnTo(71, 26);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.message.indexOf('正停在哥拉尔港') >= 0 && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往伊尔栈桥'}, ()=>{
stage3();
});
return;
}
setTimeout(retry2, 5000);
});
}
var retry = ()=>{
cga.TurnTo(53, 50);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'铁达尼号'}, retry2);
return;
}
setTimeout(retry, 5000);
});
}
if(cga.GetMapName() != '伊尔村'){
cga.travel.falan.toTeleRoom('伊尔村', ()=>{
cga.walkList([
[12, 17, '村长的家'],
[6, 13, '伊尔村'],
], ()=>{
cga.travel.falan.toGelaer(cb);
});
});
return;
}
cga.walkList([
[58, 71],
], ()=>{
cga.turnTo(60, 71);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'伊尔'}, ()=>{
cga.walkList([
[30, 21, '港湾管理处'],
[25, 25],
], ()=>{
cga.TurnTo(25, 23);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往哥拉尔栈桥'}, ()=>{
cga.walkList([
[51, 50],
], retry);
});
});
});
});
});
}, 1000);
});
}
cga.travel.AKLF = {};
cga.travel.AKLF.isSettled = false;
//前往到阿凯鲁法银行
cga.travel.AKLF.toBank = (cb)=>{
if(cga.GetMapName() != '阿凯鲁法村'){
if(cga.travel.AKLF.isSettled){
cga.LogBack();
setTimeout(cga.travel.AKLF.toBank, 1000, cb);
return;
}
cb(new Error('"前往阿凯鲁法银行"功能必须从阿凯鲁法村启动'));
return;
}
cga.walkList([
[139, 136, '银行'],
[20, 17],
], ()=>{
cga.turnDir(0);
cb(null);
});
}
//从阿凯鲁法到法兰
cga.travel.AKLF.toFalan = (cb)=>{
if(cga.GetMapName() != '阿凯鲁法村'){
cb(new Error('必须从阿凯鲁法村启动'));
return;
}
var stage4 = ()=>{
cga.walkList([
[47, 83, '村长的家'],
[14, 17, '伊尔村的传送点'],
[20, 10],
], (r)=>{
cga.TurnTo(22, 10);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'启程之间'}, ()=>{
cga.walkList([
[25, 24, '里谢里雅堡 1楼'],
[74, 40, '里谢里雅堡'],
], cb);
});
});
});
}
var stage3 = ()=>{
cga.walkList([
[19, 55],
], (r)=>{
cga.TurnTo(19, 53);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'港湾管理处'}, ()=>{
cga.walkList([
[9, 22, '伊尔'],
[24, 19],
], ()=>{
cga.TurnTo(24, 17);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'伊尔村'}, stage4);
});
});
});
});
});
}
var retry2 = ()=>{
cga.TurnTo(71, 26);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.message.indexOf('现在正停靠在伊尔村') >= 0 && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往阿凯鲁法栈桥'}, ()=>{
stage3();
});
return;
}
setTimeout(retry2, 5000);
});
}
var retry = ()=>{
cga.TurnTo(53, 50);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'艾欧奇亚号'}, retry2);
return;
}
setTimeout(retry, 5000);
});
}
cga.walkList([
[57, 176],
], ()=>{
cga.TurnTo(55, 176);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'阿凯鲁法'}, ()=>{
cga.walkList([
[16, 15, '港湾管理处'],
[15, 12],
], ()=>{
cga.TurnTo(17, 12);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往伊尔栈桥'}, ()=>{
cga.walkList([
[51, 50],
], retry);
});
});
});
});
});
});
});
}
cga.travel.falan.toTeleRoomTemplate = (villageName, npcPos, npcPos2, npcPos3, cb)=>{
cga.travel.falan.toStone('C', ()=>{
var teamplayers = cga.getTeamPlayers();
var isTeamLeader = teamplayers.length > 0 && teamplayers[0].is_me == true ? true : false;
var list = [
[41, 50, '里谢里雅堡 1楼'],
[45, 20, '启程之间']
];
if(isTeamLeader){
list.push(npcPos);
list.push(npcPos2);
list.push(npcPos);
list.push(npcPos2);
list.push(npcPos);
} else {
list.push(npcPos);
}
cga.walkList(list, ()=>{
var go = ()=>{
cga.turnTo(npcPos3[0], npcPos3[1]);
cga.AsyncWaitNPCDialog((err, dlg)=>{
//try again if timeout
if(err && err.message.indexOf('timeout') > 0)
{
setTimeout(go, 1500);
return;
}
if(err){
cb(err);
return;
}
if(typeof dlg.message == 'string' && (dlg.message.indexOf('对不起') >= 0 || dlg.message.indexOf('很抱歉') >= 0)){
cb(new Error('无法使用前往'+villageName+'的传送石,可能的原因:没开传送点'));
return;
}
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:villageName+'的传送点', delay:1000, timeout:5000}, (err)=>{
if(err){
cb(new Error('无法使用前往'+villageName+'的传送石,可能的原因:钱不够'));
return;
}
cb(null);
});
});
}
if(isTeamLeader){
setTimeout(()=>{
cga.DoRequest(cga.REQUEST_TYPE_LEAVETEAM);
setTimeout(go, 1500);
}, 1500);
} else {
go();
}
});
});
}
//从启程之间传送到指定村落
cga.travel.falan.toTeleRoom = (villageName, cb)=>{
switch(villageName){
case '亚留特村':
cga.travel.falan.toTeleRoomTemplate('亚留特村', [43, 23], [43, 22], [44, 22], cb);
break;
case '伊尔村':
cga.travel.falan.toTeleRoomTemplate('伊尔村', [43, 33], [43, 32], [44, 32], cb);
break;
case '圣拉鲁卡村':
cga.travel.falan.toTeleRoomTemplate('圣拉鲁卡村', [43, 44], [43, 43], [44, 43], cb);
break;
case '维诺亚村':
cga.travel.falan.toTeleRoomTemplate('维诺亚村', [9, 22], [9, 23], [8, 22], cb);
break;
case '奇利村':
cga.travel.falan.toTeleRoomTemplate('奇利村', [9, 33], [8, 33], [8, 32], cb);
break;
case '加纳村':
cga.travel.falan.toTeleRoomTemplate('加纳村', [9, 44], [8, 44], [8, 43], cb);
break;
case '杰诺瓦镇':
cga.travel.falan.toTeleRoomTemplate('杰诺瓦镇', [15, 4], [15, 5], [16, 4], cb);
break;
case '阿巴尼斯村':
cga.travel.falan.toTeleRoomTemplate('阿巴尼斯村', [37, 4], [37, 5], [38, 4], cb);
break;
case '蒂娜村':
cga.travel.falan.toTeleRoomTemplate('蒂娜村', [25, 4], [25, 5], [26, 4], cb);
break;
case '魔法大学':
var mapname = cga.GetMapName();
if(mapname == '魔法大学'){
cb(null);
return;
}
else if(mapname == '魔法大学内部'){
cga.walkList([
[40, 59, '魔法大学'],
], cb);
return;
}
cga.travel.falan.toTeleRoom('阿巴尼斯村', ()=>{
cga.walkList([
[5, 4, 4313],
[6, 13, 4312],
[6, 13, '阿巴尼斯村'],
[37, 71, '莎莲娜'],
[118, 100, '魔法大学'],
], cb);
});
break;
default:
throw new Error('未知的村子名称:'+villageName);
}
}
cga.travel.falan.toTeleRoomPromisify = (city)=>{
return cga.promisify(cga.travel.falan.toTeleRoom, [city]);
}
//从法兰坐船前往某城镇
cga.travel.falan.toCity = function(city, cb){
switch(city){
case '新城':case '艾尔莎岛':
cga.travel.falan.toNewIsland(cb);
return;
case '阿凯鲁法':case '阿凯鲁法村':
cga.travel.falan.toAKLF(cb);
return;
case '哥拉尔':case '哥拉尔镇':
cga.travel.falan.toGelaer(cb);
return;
}
throw new Error('未知的城市名:'+city);
}
cga.travel.newisland = {};
cga.travel.newisland.isSettled = true;
cga.travel.newisland.xy2name = function(x, y, mapname){
if(x == 140 && y == 105 && mapname == '艾尔莎岛')
return 'X';
if(x == 158 && y == 94 && mapname == '艾尔莎岛')
return 'A';
if(x == 84 && y == 112 && mapname == '艾夏岛')
return 'B';
if(x == 164 && y == 159 && mapname == '艾夏岛')
return 'C';
if(x == 151 && y == 97 && mapname == '艾夏岛')
return 'D';
return null;
}
cga.travel.newisland.isvalid = function(stone){
switch(stone.toUpperCase()){
case 'A': return true;
case 'B': return true;
case 'C': return true;
case 'D': return true;
case 'X': return true;
}
return false;
}
cga.travel.newisland.toStoneInternal = (stone, cb)=>{
var curXY = cga.GetMapXY();
var curMap = cga.GetMapName();
const desiredMap = ['艾尔莎岛', '艾夏岛'];
if(curMap == '艾尔莎岛' || curMap == '艾夏岛'){
var curStone = cga.travel.newisland.xy2name(curXY.x, curXY.y, curMap);
if(curStone !== null) {
var turn = false;
if(stone.length >= 2 && curStone.charAt(1) == stone.charAt(1)) {
if(curStone == stone){
cb(true);
return;
}
turn = true;
} else if(stone.length < 2){
if(curStone.charAt(0) == stone.charAt(0)){
cb(true);
return;
}
turn = true;
}
if(turn){
switch(curStone){
case 'X':{
cga.walkList([
[158, 94],
], ()=>{
cga.travel.newisland.toStoneInternal(stone, cb);
});
return;
}
case 'A':{
if(stone == 'X'){
cga.walkList([
[140, 105],
], ()=>{
cga.travel.newisland.toStoneInternal(stone, cb);
});
return;
}
cga.turnDir(6);
break;
}
case 'B':cga.turnDir(4);break;
case 'C':cga.turnDir(5);break;
case 'D':cga.turnDir(4);break;
}
cga.AsyncWaitMovement({map:desiredMap, delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.newisland.toStoneInternal(stone, cb);
});
return;
}
}
if(curMap == '艾尔莎岛'){
cga.walkList([
stone == 'X' ? [140, 105] : [158, 94],
], ()=>{
cga.travel.newisland.toStoneInternal(stone, cb);
});
return;
}
}
if(cga.travel.newisland.isSettled){
cga.LogBack();
cga.AsyncWaitMovement({map:desiredMap, delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.newisland.toStoneInternal(stone, cb);
});
}
}
//参数1:传送石名称,有效参数:A B C D
//参数2:回调函数function(result), result 为true或false
cga.travel.newisland.toStone = (stone, cb)=>{
if(!cga.travel.newisland.isvalid(stone)){
throw new Error('无效的目的地名称');
return;
}
cga.travel.newisland.toStoneInternal(stone, cb);
}
//前往新城冒险者旅馆
cga.travel.newisland.toPUB = (cb)=>{
cga.travel.newisland.toStone('B', (r)=>{
cga.walkList([
[102,115, '冒险者旅馆'],
], (r)=>{
cb(r);
});
});
}
//前往新城立夏岛
cga.travel.newisland.toLiXiaIsland = (cb)=>{
cga.travel.newisland.toStone('X', (r)=>{
var teamplayers = cga.getTeamPlayers();
cga.walkList(
teamplayers.length > 1 ?
[
[165,153],
[164,153],
[165,153],
[164,153],
[165,153],
] :
[
[165,153],
]
, (r)=>{
cga.TurnTo(165, 155);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, 0);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, 0);
setTimeout(cb, 1500, true);
});
});
});
});
}
cga.travel.gelaer = {};
cga.travel.gelaer.isSettled = false;
cga.travel.gelaer.xy2name = function(x, y, mapname){
if(x == 120 && y == 107 && mapname == '哥拉尔镇')
return 'N';
if(x == 118 && y == 214 && mapname == '哥拉尔镇')
return 'S';
return null;
}
cga.travel.gelaer.isvalid = function(stone){
switch(stone.toUpperCase()){
case 'N': return true;
case 'S': return true;
}
return false;
}
cga.travel.gelaer.toStoneInternal = (stone, cb)=>{
var curXY = cga.GetMapXY();
var curMap = cga.GetMapName();
if(curMap == '哥拉尔镇'){
var curStone = cga.travel.gelaer.xy2name(curXY.x, curXY.y, curMap);
if(curStone !== null) {
if(curStone == stone){
cb(null);
return;
}
var desiredTarget = undefined;
switch(curStone){
case 'N':
cga.turnDir(6);
desiredTarget = [118, 214];
break;
case 'S':
cga.turnDir(0);
desiredTarget = [120, 107];
break;
}
cga.AsyncWaitMovement({x : desiredTarget[0], y : desiredTarget[1], delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.gelaer.toStoneInternal(stone, cb);
});
return;
} else if(!cga.travel.gelaer.isSettle){
var northPath = cga.calculatePath(curXY.x, curXY.y, 118, 214, null, null, null, []);
northPath = PF.Util.expandPath(northPath);
var southPath = cga.calculatePath(curXY.x, curXY.y, 120, 107, null, null, null, []);
southPath = PF.Util.expandPath(southPath);
var path = northPath;
var target = [118, 214];
if(path.length > southPath.length)
{
path = southPath;
target = [120, 107];
}
cga.walkList([target], ()=>{
cga.travel.gelaer.toStoneInternal(stone, cb);
});
return;
}
}
if(cga.travel.gelaer.isSettled){
cga.LogBack();
cga.AsyncWaitMovement({map:'哥拉尔镇', delay:1000, timeout:5000}, (err, reason)=>{
if(err){
cb(err, reason);
return;
}
cga.travel.gelaer.toStoneInternal(stone, cb);
});
}
}
//参数1:传送石名称,有效参数:N S
cga.travel.gelaer.toStone = (stone, cb)=>{
if(!cga.travel.gelaer.isvalid(stone)){
throw new Error('无效的目的地名称');
return;
}
cga.travel.gelaer.toStoneInternal(stone, cb);
}
//前往到哥拉尔医院
cga.travel.gelaer.toHospital = (cb, isPro)=>{
if(cga.GetMapName() != '哥拉尔镇'){
if(cga.travel.gelaer.isSettled){
cga.LogBack();
setTimeout(cga.travel.gelaer.toHospital, 1000, cb, isPro);
return;
}
cb(new Error('"前往哥拉尔医院"功能必须从哥拉尔镇启动'));
return;
}
cga.travel.gelaer.toStone('N', ()=>{
cga.walkList([
[165, 91, '医院'],
isPro ? [28, 25] : [29, 26],
], ()=>{
if(isPro)
cga.turnTo(28, 24);
else
cga.turnTo(30, 26);
cb(null);
});
});
}
//前往到哥拉尔银行
cga.travel.gelaer.toBank = (cb)=>{
if(cga.GetMapName() != '哥拉尔镇'){
if(cga.travel.gelaer.isSettled){
cga.LogBack();
setTimeout(cga.travel.gelaer.toBank, 1000, cb);
return;
}
cb(new Error('"前往哥拉尔银行"功能必须从哥拉尔镇启动'));
return;
}
cga.travel.gelaer.toStone('N', ()=>{
cga.walkList([
[167, 66, '银行'],
[25, 10],
], ()=>{
cga.turnDir(0);
cb(null);
});
});
}
//前往鲁米那斯村
cga.travel.gelaer.toLumi = (cb)=>{
if(cga.GetMapName() != '哥拉尔镇'){
if(cga.travel.gelaer.isSettled){
cga.LogBack();
setTimeout(cga.travel.gelaer.toLumi, 1000, cb);
return;
}
cb(new Error('"前往鲁米那斯村"功能必须从哥拉尔镇启动'));
return;
}
cga.travel.gelaer.toStone('N', ()=>{
cga.walkList([
[176,105,'库鲁克斯岛'],
[477,525],
], ()=>{
cga.turnTo(477, 526);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, 0);
cga.AsyncWaitMovement({x : 476, y : 528, delay:1000, timeout:5000}, ()=>{
cga.walkList([
[322, 883,'鲁米那斯']
], ()=>{
cb(null);
});
});
});
});
});
}
cga.travel.lumi = {};
//前往鲁村商店
cga.travel.lumi.toStore = (cb)=>{
if(cga.GetMapName() != '鲁米那斯'){
cb(new Error('必须从鲁米那斯启动'));
return;
}
cga.walkList([
[88, 51,'杂货店'],
[11, 12],
], ()=>{
cga.turnTo(13, 12);
cb(null);
});
}
//前往鲁村医院
cga.travel.lumi.toHospital = (cb, isPro)=>{
if(cga.GetMapName() != '鲁米那斯'){
cb(new Error('必须从鲁米那斯启动'));
return;
}
cga.walkList(
[
[87, 35, '医院'],
isPro == true ? [17, 5] : [17, 16],
], ()=>{
cga.turnDir(0);
cb(null);
});
}
//从哥拉尔到法兰
cga.travel.gelaer.toFalan = (cb)=>{
if(cga.GetMapName() != '哥拉尔镇'){
cb(new Error('必须从哥拉尔镇启动'));
return;
}
var stage4 = ()=>{
cga.walkList([
[47, 83, '村长的家'],
[14, 17, '伊尔村的传送点'],
[20, 10],
], (r)=>{
cga.TurnTo(22, 10);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'启程之间'}, ()=>{
cga.walkList([
[25, 24, '里谢里雅堡 1楼'],
[74, 40, '里谢里雅堡'],
], cb);
});
});
});
}
var stage3 = ()=>{
cga.walkList([
[19, 55],
], (r)=>{
cga.TurnTo(19, 53);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'港湾管理处'}, ()=>{
cga.walkList([
[9, 22, '伊尔'],
[24, 19],
], ()=>{
cga.TurnTo(24, 17);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'伊尔村'}, stage4);
});
});
});
});
});
}
var retry2 = ()=>{
cga.TurnTo(71, 26);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.message.indexOf('现在正停靠在伊尔村') >= 0 && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往哥拉尔栈桥'}, ()=>{
stage3();
});
return;
}
setTimeout(retry2, 5000);
});
}
var retry = ()=>{
cga.TurnTo(53, 50);
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(dlg && dlg.options == 12){
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'铁达尼号'}, retry2);
return;
}
setTimeout(retry, 5000);
});
}
cga.walkList([
[96, 211, '哥拉尔镇 港湾管理处'],
[8, 5],
], ()=>{
cga.TurnTo(8, 3);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(32, -1);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(4, -1);
cga.AsyncWaitMovement({map:'往伊尔栈桥'}, ()=>{
cga.walkList([
[51, 50],
], retry);
});
});
});
});
}
cga.isPathAvailable = (curX, curY, targetX, targetY)=>{
var walls = cga.buildMapCollisionMatrix();
var grid = new PF.Grid(walls.matrix);
var finder = new PF.AStarFinder({
allowDiagonal: true,
dontCrossCorners: true
});
var frompos = [curX - walls.x_bottom, curY - walls.y_bottom];
var topos = [targetX - walls.x_bottom, targetY - walls.y_bottom];
if(frompos[0] >= 0 && frompos[0] < walls.x_size &&
frompos[1] >= 0 && frompos[1] < walls.y_size &&
topos[0] >= 0 && topos[0] < walls.x_size &&
topos[1] >= 0 && topos[1] < walls.y_size){
//console.log('using AStar path finder...');
var path = finder.findPath(frompos[0], frompos[1], topos[0], topos[1], grid);
if(path.length)
{
return true;
}
}
return false;
}
cga.calculatePath = (curX, curY, targetX, targetY, targetMap, dstX, dstY, newList)=>{
var walls = cga.buildMapCollisionMatrix();
var grid = new PF.Grid(walls.matrix);
var finder = new PF.AStarFinder({
allowDiagonal: true,
dontCrossCorners: true
});
//console.log('x_size ' + walls.x_size);
//console.log('y_size ' + walls.y_size);
//console.log('xbot ' + walls.x_bottom);
//console.log('ybot ' + walls.y_bottom);
var frompos = [curX - walls.x_bottom, curY - walls.y_bottom];
var topos = [targetX - walls.x_bottom, targetY - walls.y_bottom];
//console.log('寻路起始坐标 (' + (frompos[0]) + ', '+ (frompos[1]) + ')');
//console.log('寻路目的坐标 (' + (topos[0]) +', '+(topos[1]) + ')');
if(frompos[0] >= 0 && frompos[0] < walls.x_size &&
frompos[1] >= 0 && frompos[1] < walls.y_size &&
topos[0] >= 0 && topos[0] < walls.x_size &&
topos[1] >= 0 && topos[1] < walls.y_size){
//console.log('using AStar path finder...');
var path = finder.findPath(frompos[0], frompos[1], topos[0], topos[1], grid);
if(path.length)
{
var joint = PF.Util.compressPath(path);
for(var i in joint){
joint[i][0] += walls.x_bottom;
joint[i][1] += walls.y_bottom;
if(joint[i][0] == targetX && joint[i][1] == targetY){
joint[i][2] = targetMap;
joint[i][3] = dstX;
joint[i][4] = dstY;
}
joint[i][5] = true;
}
//console.log('result joints');
//console.log(joint);
newList = joint.concat(newList);
//console.log('新寻路列表:');
//console.log(newList);
return newList;
}
}
throw new Error('发现严重错误:寻路失败!');
//return [];
}
cga.getMapXY = ()=>{
var f = cga.GetMapXYFloat();
return {x: parseInt(f.x/64.0), y:parseInt(f.y/64.0)};
}
cga.NoRollbackMap = [
'艾尔莎岛',
'艾夏岛',
'利夏岛',
'法兰城',
'里谢里雅堡',
'医院',
'工房',
'村长的家',
'曙光骑士团营地',
'辛希亚探索指挥部',
'圣骑士营地',
'哥拉尔镇',
'鲁米那斯',
'阿凯鲁法村',
'坎那贝拉村',
'加纳村',
'奇利村',
'杰诺瓦镇',
'伊尔村',
'伊尔',
'维诺亚村',
'乌克兰村',
'亚留特村',
'圣拉鲁卡村',
'地下工房',
'阿巴尼斯村',
'魔法大学',
'魔法大学内部',
'杂货店',
'启程之间',
'追忆之路',
'港湾管理处',
'往哥拉尔栈桥',
'往伊尔栈桥',
'哥拉尔 港湾管理处',
];
/*自动寻路走路,调用方式:
//走到指定地点:
cga.walkList({
[坐标x, 坐标y]
}, cb回调)
//走到指定地点并切图:
cga.walkList({
[坐标x, 坐标y, 地图名]
}, cb回调)
//走到指定地点并切图:
cga.walkList({
[坐标x, 坐标y, 地图索引]
}, cb回调)
//走到指定地点并传送至同一张地图的另一坐标(比如辛西娅探索指挥部的楼梯):
cga.walkList({
[坐标x, 坐标y, 地图索引, 传送目标x, 传送目标y]
}, cb回调)
*/
cga.walkList = (list, cb)=>{
//console.log('初始化寻路列表');
//console.log(list);
if(cga.isMoveThinking){
throw new Error('发现严重错误:已有walkList在运行中');
}
cga.isMoveThinking = true;
if(!cga.moveThink('walkList')){
console.log('walkList被中断');
cga.isMoveThinking = false;
return;
}
var walkedList = [];
var newList = list.slice(0);
var walkCb = ()=>{
if(newList.length == 0){
cga.isMoveThinking = false;
cb(null);
return;
}
var targetX = newList[0][0];
var targetY = newList[0][1];
var targetMap = newList[0][2];
var dstX = newList[0][3];
var dstY = newList[0][4];
var isAStarPath = newList[0][5];
var walked = newList[0].slice(0);
walkedList.push(walked);
newList.shift();
var curmap = cga.GetMapName();
var curpos = cga.GetMapXY();
var curmapindex = cga.GetMapIndex().index3;
console.log('当前地图: ' + curmap + ', 序号 ' + curmapindex);
console.log('当前 (%d, %d) -> 目标 (%d, %d)', curpos.x, curpos.y, targetX, targetY);
if(targetMap)
{
console.log('目标地图');
console.log(targetMap);
}
var end = (arg)=>{
if(cga.NoRollbackMap.find((n)=>{
return n == curmap;
}) != undefined)
{
cga.isMoveThinking = false;
cb(null);
return;
}
var waitBattle2 = ()=>{
if(!cga.isInNormalState()){
setTimeout(waitBattle2, 1500);
return;
}
if(!cga.moveThink('walkList')){
console.log('walkList被中断');
cga.isMoveThinking = false;
return;
}
var curpos = cga.GetMapXY();
if(typeof walkedList[walkedList.length-1][2] != 'string' &&
typeof walkedList[walkedList.length-1][2] != 'number' &&
(curpos.x != walkedList[walkedList.length-1][0] ||
curpos.y != walkedList[walkedList.length-1][1])
){
//console.log(curpos);
//console.log(walkedList);
console.log('坐标错误,回滚到最后一个路径点');
var endpos = walkedList.pop();
newList = cga.calculatePath(curpos.x, curpos.y, endpos[0], endpos[1], endpos[2], null, null, newList);
walkCb();
return;
}
cga.isMoveThinking = false;
cb(null);
return;
}
setTimeout(waitBattle2, 1500);
}
var walker = (err, reason)=>{
if(!cga.moveThink('walkList')){
console.log('walkList被中断');
cga.isMoveThinking = false;
return;
}
//console.log(result);
//console.log(reason);
if(err){
if(reason == 4){
//console.log('地图发生非预期的切换!');
var curmap = cga.GetMapName();
var curmapindex = cga.GetMapIndex().index3;
console.log('当前地图: ' + curmap);
console.log('当前地图序号: ' + curmapindex);
}
//we are in battle status, wait a second then try again until battle is end
//or we are forcely moved back to an position by server
if(reason == 2 || reason == 5){
var waitBattle = ()=>{
if(!cga.isInNormalState()){
setTimeout(waitBattle, 1000);
return;
}
var curmap = cga.GetMapName();
var curmapindex = cga.GetMapIndex().index3;
var curpos = cga.GetMapXY();
console.log('战斗回滚');
console.log('当前地图 :' + curmap);
console.log('当前地图序号 :' + curmapindex);
console.log('当前坐标:' + curpos.x + ', ' + curpos.y);
if(typeof targetMap == 'string' && curmap == targetMap){
if(newList.length == 0){
console.log('寻路正常结束1');
end({ map : targetMap });
return;
}
walkCb();
return;
}
else if(typeof targetMap == 'number' && curmapindex == targetMap){
if(newList.length == 0){
console.log('寻路正常结束2');
end({ map : targetMap });
return;
}
walkCb();
return;
}
else if(typeof walkedList[walkedList.length-1] != 'undefined' &&
typeof walkedList[walkedList.length-1][2] == 'string' &&
walkedList[walkedList.length-1][2] != '' &&
curmap != walkedList[walkedList.length-1][2])
{
console.log('目标地图错误,回滚到上一路径');
console.log('预期地图 ' + walkedList[walkedList.length-1][2] + ', 当前地图 ' + curmap);
var temp = walkedList.pop();
newList = cga.calculatePath(curpos.x, curpos.y, temp[0], temp[1], temp[2], null, null, newList);
}
else if(typeof walkedList[walkedList.length-2] != 'undefined' &&
typeof walkedList[walkedList.length-2][2] == 'string' &&
walkedList[walkedList.length-2][2] != '' &&
curmap != walkedList[walkedList.length-2][2])
{
console.log('目标地图错误,回滚到上上个路径');
console.log('预期地图 ' + walkedList[walkedList.length-2][2] + ', 当前地图 ' + curmap);
walkedList.pop();
var temp = walkedList.pop();
newList = cga.calculatePath(curpos.x, curpos.y, temp[0], temp[1], temp[2], null, null, newList);
} else {
newList = cga.calculatePath(curpos.x, curpos.y, targetX, targetY, targetMap, dstX, dstY, newList);
}
walkCb();
}
setTimeout(waitBattle, 1000);
return;
} else if(reason == 3){
//console.log('当前寻路卡住,抛出错误!');
throw new Error('发现严重错误:当前寻路卡住!');
}
cga.isMoveThinking = false;
cb(err, reason);
return;
}
if(newList.length == 0){
console.log('寻路正常结束3');
end( {pos : [targetX, targetY], map : targetMap} );
return;
}
walkCb();
}
if(targetX == curpos.x && targetY == curpos.y){
var isEntrance = typeof targetMap == 'string' || typeof targetMap == 'number' || (targetMap instanceof Array) ? true : false;
if(isEntrance){
cga.FixMapWarpStuck(1);
cga.AsyncWalkTo(targetX, targetY, targetMap, null, null, walker);
return;
}
walkCb();
return;
}
if(isAStarPath !== true){
newList = cga.calculatePath(curpos.x, curpos.y, targetX, targetY, targetMap, dstX, dstY, newList);
walkCb();
return;
}
cga.AsyncWalkTo(targetX, targetY, targetMap, dstX, dstY, walker);
};
walkCb();
}
//查找玩家技能,返回技能index,找不到返回-1
//参数1:技能名
//参数2:完全匹配
cga.findPlayerSkill = function(name){
var match = arguments[1] ? arguments[1] : true;
var skill = cga.GetSkillsInfo().find((sk)=>{
if(match && sk.name == name){
return true;
}
if(!match && sk.name.indexOf(name) != -1){
return true;
}
return false;
});
return skill != undefined ? skill : null;
}
//查找宝箱
cga.findCrate = function(filter){
var unit = cga.GetMapUnits().find((u)=>{
if(u.valid == 2 && u.type == 2 && u.model_id != 0 && (u.flags & 1024) != 0)
return filter(u);
return false;
});
return unit != undefined ? unit : null;
}
//搜索NPC,支持过滤器
cga.findNPCEx = function(filter){
var unit = cga.GetMapUnits().find((u)=>{
if(u.valid == 2 && u.type == 1 && u.model_id != 0 && (u.flags & 4096) != 0)
return filter(u);
return false;
});
return unit != undefined ? unit : null;
}
//按名称搜索NPC
cga.findNPC = function(name){
return cga.findNPCEx((u)=>{
return (u.unit_name == name);
});
}
//按坐标搜索NPC
cga.findNPCByPosition = function(name, x, y){
return cga.findNPCEx((u)=>{
return (u.unit_name == name && x == u.xpos && y == u.ypos);
});
}
//取背包中的物品数量
//参数1:物品名, 或#物品id,或过滤函数
//参数2:是否包括装备栏
cga.getItemCount = function(filter){
var includeEquipment = arguments[1] === true ? true : false;
var items = cga.GetItemsInfo();
var count = 0;
if(typeof filter == 'string' && filter.charAt(0) == '#'){
var itemid = parseInt(filter.substring(1));
items.forEach((item)=>{
if(!includeEquipment && item.pos < 8)
return false;
if(item.itemid == itemid)
count += item.count > 0 ? item.count : 1;
});
} else if(typeof filter == 'number'){
var itemid = filter;
items.forEach((item)=>{
if(!includeEquipment && item.pos < 8)
return false;
if(item.itemid == itemid)
count += item.count > 0 ? item.count : 1;
});
} else if(typeof filter == 'function'){
var itemid = filter;
items.forEach((item)=>{
if(!includeEquipment && item.pos < 8)
return false;
if(filter(item) == true)
count += item.count > 0 ? item.count : 1;
});
} else {
items.forEach((item)=>{
if(!includeEquipment && item.pos < 8)
return false;
if(item.name == filter)
count += item.count > 0 ? item.count : 1;
});
}
return count;
}
//任务
cga.task = {};
//任务对象构造函数
cga.task.Task = function(name, stages, requirements){
this.stages = stages;
this.name = name;
this.anyStepDone = true;
this.requirements = requirements
this.isDone = function(index){
for(var i = this.requirements.length - 1; i >= index; --i){
if(typeof this.requirements[i] == 'function' && this.requirements[i]())
return true;
}
return false;
}
this.isDoneSingleStep = function(index){
if(typeof this.requirements[index] == 'function' && this.requirements[index]())
return true;
return false;
}
this.doNext = function(index, cb){
if(index >= this.stages.length){
console.log('任务:'+this.name+' 已完成!');
if(cb)
cb(true);
} else {
this.doStage(index, cb);
}
}
this.doStage = function(index, cb){
if(this.anyStepDone){
if(this.isDone(index)){
console.log(''+(index+1)+'/'+stages.length+'阶段已完成,跳过。');
this.doNext(index+1, cb);
return;
}
} else {
if(this.isDoneSingleStep(index)){
console.log(''+(index+1)+'/'+stages.length+'阶段已完成,跳过。');
this.doNext(index+1, cb);
return;
}
}
console.log('开始执行第'+(index+1)+'阶段:');
console.log(this.stages[index].intro);
var objThis = this;
objThis.stages[index].workFunc(function(r){
if(r === false || r instanceof Error){
if(cb)
cb(r);
return;
}
//console.trace()
if(r === true || r === null){
console.log(''+(index+1)+'阶段执行完成。');
objThis.doNext(index + 1, cb);
} else if( r == 'restart stage' ){
console.log(''+(index+1)+'阶段请求重新执行。');
objThis.doNext(index, cb);
} else if( r == 'restart task' ){
console.log(''+(index+1)+'阶段请求重新执行。');
objThis.doNext(index, cb);
} else {
throw new Error('无效参数');
}
});
}
this.doTask = function(cb){
console.log('任务:'+this.name+' 开始执行,共'+this.stages.length+'阶段。');
this.doStage( (typeof this.jumpToStep != 'undefined') ? this.jumpToStep : 0, cb);
}
return this;
}
//等待NPC出现
cga.task.waitForNPC = (filter, cb)=>{
if(!cga.findNPC(filter)){
setTimeout(cga.task.waitForNPC, 10000, filter, cb);
cga.SayWords('', 0, 3, 1);
return;
}
cb(null);
}
cga.gather = {};
cga.gather.stats = function(itemname, itemgroupnum){
this.begintime = moment();
this.prevcount = cga.getItemCount(itemname);
this.itemname = itemname;
this.itemgroupnum = itemgroupnum;
this.printStats = function(){
var count = cga.getItemCount(this.itemname) - this.prevcount;
console.log('一次采集完成,耗时' + moment.duration(moment() - this.begintime, 'ms').locale('zh-cn').humanize());
console.log('获得 '+ itemname +' x '+count+',共 ' + parseInt(count / this.itemgroupnum) + ' 组。');
this.begintime = moment();
}
return this;
}
cga.craft = {}
cga.craft.buyFabricLv1Multi = (arr, cb)=>{
cga.travel.falan.toFabricStore(()=>{
cga.walkList([
[8, 7],
], ()=>{
cga.TurnTo(8, 6);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(0, 0);
cga.AsyncWaitNPCDialog(()=>{
cga.BuyNPCStore(arr);
cga.AsyncWaitNPCDialog(()=>{
cb(null);
});
});
});
});
});
}
cga.craft.buyFabricLv1 = (id, count, cb)=>{
cga.craft.buyFabricLv1Multi([{index:id, count:count}], cb);
}
cga.craft.buyFabricLv2Multi = (arr, cb)=>{
cga.travel.falan.toTeleRoom('维诺亚村', ()=>{
cga.walkList([
[5, 1, '村长家的小房间'],
[0, 5, '村长的家'],
[9, 16, '维诺亚村'],
[56, 42, '装备品店'],
[13, 8],
], ()=>{
cga.TurnTo(13, 6);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(0, 0);
cga.AsyncWaitNPCDialog(()=>{
cga.BuyNPCStore(arr);
cga.AsyncWaitNPCDialog(()=>{
cb(null);
});
});
});
});
});
}
cga.craft.buyFabricLv2 = (id, count, cb)=>{
cga.craft.buyFabricLv2Multi([{index:id, count:count}], cb);
}
cga.craft.buyFabricLv3Multi = (arr, cb)=>{
cga.travel.falan.toTeleRoom('杰诺瓦镇', ()=>{
cga.walkList([
[14, 6, '村长的家'],
[1, 9, '杰诺瓦镇'],
[43, 23, '杂货店'],
[11, 12],
], ()=>{
cga.TurnTo(13, 12);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(0, 0);
cga.AsyncWaitNPCDialog(()=>{
cga.BuyNPCStore(arr);
cga.AsyncWaitNPCDialog(()=>{
cb(null);
});
});
});
});
});
}
cga.craft.buyFabricLv3 = (id, count, cb)=>{
cga.craft.buyFabricLv3Multi([{index:id, count:count}], cb);
}
cga.craft.buyFabricLv4Multi = (arr, cb)=>{
cga.travel.falan.toTeleRoom('魔法大学', ()=>{
cga.walkList([
[74, 93, '魔法大学内部'],
[29, 43, '更衣室'],
[11, 8],
], ()=>{
cga.TurnTo(11, 6);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(0, 0);
cga.AsyncWaitNPCDialog(()=>{
cga.BuyNPCStore(arr);
cga.AsyncWaitNPCDialog(()=>{
cb(null);
});
});
});
});
});
}
cga.craft.buyFabricLv4 = (id, count, cb)=>{
cga.craft.buyFabricLv4Multi([{index:id, count:count}], cb);
}
cga.craft.buyFabricLv5Multi = (arr, cb)=>{
cga.travel.falan.toTeleRoom('阿巴尼斯村', ()=>{
cga.walkList([
[5, 4, 4313],
[13, 5],
], ()=>{
cga.TurnTo(13, 3);
cga.AsyncWaitNPCDialog(()=>{
cga.ClickNPCDialog(0, 0);
cga.AsyncWaitNPCDialog(()=>{
cga.BuyNPCStore(arr);
cga.AsyncWaitNPCDialog(()=>{
cb(null);
});
});
});
});
});
}
cga.craft.buyFabricLv5 = (id, count, cb)=>{
cga.craft.buyFabricLv5Multi([{index:id, count:count}], cb);
}
//搜索第一个可鉴定的物品
cga.findAssessableItem = ()=>{
var skill = cga.findPlayerSkill('鉴定');
var mp = cga.GetPlayerInfo().mp;
var found = cga.getInventoryItems().find((item)=>{
return !item.assessed && skill.lv >= item.level && mp >= item.level * 10;
});
return found == undefined ? null : found;
}
//鉴定背包中所有的物品
cga.assessAllItems = (cb)=>{
var item = cga.findAssessableItem();
if(item)
{
cga.manipulateItemEx({
skill : '鉴定',
itempos : item.pos,
immediate : true,
}, (err, results)=>{
setTimeout(cga.assessAllItems, 500, cb);
})
} else {
cb(null);
return;
}
}
cga.findItem = (filter) =>{
var items = cga.getInventoryItems();
if(typeof filter == 'string' && filter.charAt(0) == '#'){
var found = items.find((item)=>{
return item.itemid == parseInt(filter.substring(1));
})
return found != undefined ? found.pos : -1;
}
var found = items.find((item)=>{
if(typeof filter == 'string')
return item.name == filter;
else if (typeof filter == 'number')
return item.itemid == filter;
else if (typeof filter == 'function')
return filter(item);
})
return found != undefined ? found.pos : -1;
}
//寻找背包里符合条件的物品,并整合成符合cga.SellStore和cga.AddTradeStuffs的数组格式
cga.findItemArray = (filter) =>{
var arr = [];
var items = cga.getInventoryItems();
if(typeof filter == 'function'){
items.forEach((item)=>{
if(filter(item)){
arr.push({
itempos : item.pos,
itemid : item.itemid,
count : (item.count < 1) ? 1 : item.count,
});
}
})
return arr;
}
if(typeof filter =='string' && filter.charAt(0) == '#'){
items.forEach((item)=>{
if(item.itemid == filter.substring(1)){
arr.push({
itempos : item.pos,
itemid : item.itemid,
count : (item.count < 1) ? 1 : item.count,
});
}
})
return arr;
}
items.forEach((item)=>{
if(filter instanceof RegExp){
//console.log(itemname.exec(items[i].name));
if(filter.exec(item.name)){
arr.push({
itempos : item.pos,
itemid : item.itemid,
count : (item.count < 1) ? 1 : item.count,
});
}
}
else if(typeof filter =='string'){
if(item.name == filter){
arr.push({
itempos : item.pos,
itemid : item.itemid,
count : (item.count < 1) ? 1 : item.count,
});
}
}
});
return arr;
}
//出售物品
cga.sellArray = (sellarray, cb)=>{
cga.AsyncWaitNPCDialog((err, dlg)=>{
var numOpt = dlg.message.charAt(dlg.message.length-1);
cga.ClickNPCDialog(0, numOpt == '3' ? 1 : 0);
cga.AsyncWaitNPCDialog(()=>{
cga.SellNPCStore(sellarray);
cga.AsyncWaitNPCDialog(()=>{
cb(true);
});
});
});
}
//获取背包里能够出售的物品
cga.getSellStoneItem = ()=>{
var pattern = /(.+)的卡片/;
var sellArray = []
cga.getInventoryItems().forEach((item)=>{
if(item.name == '魔石' || item.name == '卡片?' || pattern.exec(item.name) ){
sellArray.push({
itempos : item.pos,
itemid : item.itemid,
count : (item.count < 1) ? 1 : item.count,
});
}
})
return sellArray;
}
//清理背包里无用的物品
cga.cleanInventory = (count, cb)=>{
if(cga.getInventoryItems().length >= 21 - count)
{
var items = cga.getSellStoneItem();
if(items.length > 0){
cga.DropItem(items[0].itempos);
if(cb)
setTimeout(cga.cleanInventory, 500, count, cb);
} else {
cb(new Error('没有可以扔的物品了'));
}
} else {
cb(null);
}
}
//循环清理背包里无用的物品直到无东西可清
cga.cleanInventoryEx = (filter, cb)=>{
var items = cga.getInventoryItems().filter(filter);
if(items.length > 0){
cga.DropItem(items[0].pos);
setTimeout(cga.cleanInventory, 500, count, filter, cb);
} else {
cb(null);
}
}
//出售魔石
cga.sellStone = (cb)=>{
cga.AsyncWaitNPCDialog((err, dlg)=>{
if(err){
cb(err);
return;
}
var numOpt = dlg.message.charAt(dlg.message.length-1);
cga.ClickNPCDialog(0, numOpt == '3' ? 1 : 0);
cga.AsyncWaitNPCDialog(()=>{
cga.SellNPCStore(cga.getSellStoneItem());
setTimeout(cb, 1000, null);
});
});
}
//获取坐标之间的距离
cga.getDistance = (x1, y1, x2, y2)=>{
return Math.sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
}
//判断坐标之间的距离是否小于等于1
cga.isDistanceClose = (x1, y1, x2, y2)=>{
if(x1 - x2 <= 1 && x1 - x2 >= -1 && y1 - y2 <= 1 && y1 - y2 >= -1)
return true;
return false;
}
cga.savePlayerConfig = (config, cb) => {
console.log('正在保存个人配置文件...');
var configPath = __dirname+'\\个人配置';
var configName = configPath+'\\个人配置_'+cga.FileNameEscape(cga.GetPlayerInfo().name)+'.json';
fs.mkdir(configPath, (err)=>{
if(err && err.code != 'EEXIST'){
console.log('个人配置文件保存失败:');
console.log(err);
if(cb) cb(err);
return;
}
fs.writeFile(configName, JSON.stringify(config), (err)=>{
if(err){
console.log('个人配置文件保存失败:');
console.log(err);
if(cb) cb(err);
return;
}
console.log('个人配置文件保存成功!...');
if(cb) cb(null);
});
});
}
cga.loadPlayerConfig = () => {
console.log('正在读取个人配置文件...');
var configPath = __dirname+'\\个人配置';
var configName = configPath+'\\个人配置_'+cga.FileNameEscape(cga.GetPlayerInfo().name)+'.json';
try
{
var json = fs.readFileSync(configName, 'utf8');
if(typeof json != 'string' || !json.length)
throw new Error('个人配置文件格式错误或文件不存在');
var obj = JSON.parse(json);
return obj;
}
catch(e)
{
if(e.code != 'ENOENT'){
console.log('读取个人配置时发生错误:');
console.log(e);
} else {
console.log('读取个人配置文件不存在');
}
}
return null;
}
//异步获取最大银行格,必须跟柜员对话一次
cga.getBankMaxSlots = (filter, cb) => {
var banks = cga.GetBankItemsInfo();
//先从配置文件里获取
var config = cga.loadPlayerConfig();
if(!config)
config = {};
if(config.maxbankslots)
{
console.log('最大银行格为:'+config.maxbankslots);
cb(null, config.maxbankslots);
return;
}
else
{
//看看60~79,40~59,20~39是否有物品
var bank6079 = banks.filter((val)=>{ return val.pos >= 160 });
if(bank6079.length > 0)
{
cb(null, 80);
return;
}
else
{
var testitempos = cga.findItem(filter);
if(testitempos != -1)
{
cga.MoveItem(testitempos, 160, -1);
cga.waitSysMsgTimeout((err, msg)=>{
if(err){
//银行第60格物品保存成功
if(cga.GetBankItemsInfo().find((item)=>{
return item.pos == 160;
}) != undefined)
{
//第60格物品取回包里
cga.MoveItem(160, testitempos, -1);
config.maxbankslots = 80;
cga.savePlayerConfig(config);
console.log('最大银行格为:'+config.maxbankslots);
setTimeout(cb, 1000, config.maxbankslots);
return false;
}
//未知问题
console.log('获取最大银行格时发生未知问题,可能网络不稳定或没有与柜员对话!');
console.log('最大银行格默认为:'+20);
setTimeout(cb, 1000, 20);
return false;
}
if(msg.indexOf('您现在只能使用银行物品栏位中的第') >= 0)
{
var regex = msg.match(/您现在只能使用银行物品栏位中的第 (\d+)(\d+)个!/);
if(regex && regex.length >= 3){
config.maxbankslots = parseInt(regex[2]);
cga.savePlayerConfig(config);
console.log('最大银行格为:'+config.maxbankslots);
setTimeout(cb, 1000, config.maxbankslots);
return false;
}
}
return true;
}, 1000);
}
else
{
console.log('获取最大银行格失败,可能包中没有符合条件的物品!');
console.log('最大银行格默认为:'+20);
setTimeout(cb, 1000, 20);
return false;
}
}
}
};
//寻找银行中的空闲格子, 参数:物品filter、最大堆叠数量、最大银行格
cga.findBankEmptySlot = (filter, maxcount, maxslots = 20) => {
var banks = cga.GetBankItemsInfo();
var arr = [];
for(var i = 0; i < banks.length; ++i){
arr[banks[i].pos-100] = banks[i];
}
for(var i = 0; i < maxslots; ++i){
if(typeof arr[i] != 'undefined'){
if(typeof filter == 'string' && maxcount > 0){
if(arr[i].name == filter && arr[i].count < maxcount)
return 100+i;
}
else if(typeof filter == 'number' && maxcount > 0){
if(arr[i].itemid == filter && arr[i].count < maxcount)
return 100+i;
}
else if(typeof filter == 'function' && maxcount > 0){
if(filter(arr[i]) && arr[i].count < maxcount)
return 100+i;
}
} else {
return 100+i;
}
}
return -1;
}
//寻找背包中的空闲格子
cga.findInventoryEmptySlot = (itemname, maxcount) =>{
var items = cga.GetItemsInfo();
var arr = [];
for(var i = 0; i < items.length; ++i){
arr[items[i].pos-8] = items[i];
}
for(var i = 0; i < 20; ++i){
if(typeof arr[i] != 'undefined'){
if(typeof itemname == 'string' && maxcount > 0){
if(arr[i].name == itemname && arr[i].count < maxcount)
return 8+i;
}
} else {
return 8+i;
}
}
return -1;
}
//获取背包中的空闲格子数量
cga.getInventoryEmptySlotCount = () =>{
var items = cga.GetItemsInfo();
var arr = [];
for(var i = 0; i < items.length; ++i){
arr[items[i].pos-8] = items[i];
}
var count = 0;
for(var i = 0; i < 20; ++i){
if(!arr[i])
count ++;
}
return count;
}
//将符合条件的物品存至银行,maxcount为最大堆叠数量
cga.saveToBankOnce = (filter, maxcount, cb)=>{
var itempos = cga.findItem(filter);
if(itempos == -1){
cb(new Error('包里没有该物品, 无法存放到银行'));
return;
}
cga.getBankMaxSlots(filter, (err, maxslots)=>{
if(err){
cb(err);
return;
}
var emptyslot = cga.findBankEmptySlot(filter, maxcount, maxslots);
if(emptyslot == -1){
cb(new Error('银行没有空位, 无法存放到银行'));
return;
}
cga.MoveItem(itempos, emptyslot, -1);
setTimeout(()=>{
var bankitem = cga.GetBankItemsInfo().find((item)=>{
return item.pos == emptyslot;
});
if(bankitem != undefined)
{
//保存成功
console.log(bankitem.name+' 成功存到银行第 ' + (bankitem.pos - 100 + 1) + ' 格!');
cb(null);
}
else
{
cb(new Error('保存到银行失败,可能银行格子已满、未与柜员对话或网络问题'));
}
}, 1000);
});
}
//循环将符合条件的物品存至银行,maxcount为最大堆叠数量
cga.saveToBankAll = (filter, maxcount, cb)=>{
console.log('开始批量保存物品到银行...');
var repeat = ()=>{
cga.saveToBankOnce(filter, maxcount, (err)=>{
if(err){
console.log(err);
cb(err);
return;
}
if(cga.findItem(filter) == -1){
console.log('包里已经没有指定物品,批量保存到银行执行完毕!');
cb(null);
return;
}
setTimeout(repeat, 1000);
});
}
repeat();
}
//原地高速移动,dir为方向
cga.freqMove = function(dir){
var freqMoveDirTable = [ 4, 5, 6, 7, 0, 1, 2, 3 ];
var freqMoveDir = dir;
var pos = cga.GetMapXY();
var index3 = cga.GetMapIndex().index3;
var counter = 0;
var move = ()=>{
var result = true;
try
{
var curindex3 = cga.GetMapIndex().index3;
if(curindex3 == index3)
{
var curpos = cga.GetMapXY();
if(freqMoveDir == 0){
if(pos.x == curpos.x)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 4){
if(pos.x == curpos.x)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 2){
if(pos.y == curpos.y)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 6){
if(pos.y == curpos.y)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 1){
if(pos.x == curpos.x)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 5){
if(pos.x == curpos.x)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 3){
if(pos.y == curpos.y)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
else if(freqMoveDir == 7){
if(pos.y == curpos.y)
cga.ForceMove(freqMoveDir, false);
else
cga.ForceMove(freqMoveDirTable[freqMoveDir], false);
}
counter++;
if(counter % 4 == 0){
if(!cga.moveThink('freqMove')){
console.log('freqMove被中断');
cga.isMoveThinking = false;
return;
}
}
}
else
{
if(!cga.moveThink('freqMoveMapChanged')){
console.log('freqMoveMapChanged被中断');
cga.isMoveThinking = false;
return;
}
console.log('地图不同,freqMove暂停运行');
}
}
catch(e){
console.log(e);
}
setTimeout(move, 300);
}
move();
}
//从NPC对话框内容解析商店购物列表
cga.parseBuyStoreMsg = (dlg)=>{
if(!dlg){
throw new Error('解析商店购物列表失败,可能对话超时!');
return null;
}
if(!dlg.message){
throw new Error('解析商店购物列表失败,可能对话超时!');
return null;
}
//28?
if(dlg.type != 6){
throw new Error('解析商店购物列表失败,可能对话不是购物商店!');
return null;
}
var reg = new RegExp(/([^|\n]+)/g)
var match = dlg.message.match(reg);
if(match.length < 5){
throw new Error('解析商店购物列表失败,格式错误!');
return null;
}
if((match.length - 5) % 6 != 0){
throw new Error('解析商店购物列表失败,格式错误!');
return null;
}
var storeItemCount = (match.length - 5) / 6;
var obj = {
storeid : match[0],
name : match[1],
welcome : match[2],
insuff_funds : match[3],
insuff_inventory : match[4],
items : []
}
for(var i = 0; i < storeItemCount; ++i){
obj.items.push({
index : i,
name : match[5 + 6 * i + 0],
image_id : parseInt(match[5 + 6 * i + 1]),
cost : parseInt(match[5 + 6 * i + 2]),
attr : match[5 + 6 * i + 3],
batch : parseInt(match[5 + 6 * i + 4]),//最少买多少
max_buy : parseInt(match[5 + 6 * i + 5]),//最多买多少
});
}
return obj;
}
//从NPC对话框内容解析兑换列表
cga.parseExchangeStoreMsg = (dlg)=>{
if(!dlg){
throw new Error('解析兑换列表失败,可能对话超时!');
return null;
}
if(!dlg.message){
throw new Error('解析兑换列表失败,可能对话超时!');
return null;
}
if(dlg.type != 28){
throw new Error('解析兑换列表失败,可能对话不是兑换商店!');
return null;
}
var reg = new RegExp(/([^|\n]+)/g)
var match = dlg.message.match(reg);
if(match.length < 5){
throw new Error('解析兑换列表失败,格式错误!');
return null;
}
if((match.length - 5) % 7 != 0){
throw new Error('解析兑换列表失败,格式错误!');
return null;
}
var storeItemCount = (match.length - 5) / 7;
var obj = {
storeid : match[0],
name : match[1],
welcome : match[2],
insuff_funds : match[3],
insuff_inventory : match[4],
items : []
}
for(var i = 0; i < storeItemCount; ++i){
obj.items.push({
index : i,
item_id : parseInt(match[5 + 6 * i + 0]),
required : match[5 + 6 * i + 1],
name : match[5 + 6 * i + 2],
image_id : parseInt(match[5 + 6 * i + 3]),
count : parseInt(match[5 + 6 * i + 4]),//count个required才能换取一个
batch : parseInt(match[5 + 6 * i + 5]),//最少换多少
attr : match[5 + 6 * i + 6],
});
}
return obj;
}
//从NPC对话框内容解析宠物技能学习列表
cga.parsePetSkillStoreMsg = (dlg)=>{
if(!dlg){
throw new Error('解析宠物学习技能列表失败,可能对话超时!');
return null;
}
if(!dlg.message){
throw new Error('解析宠物学习技能列表失败,可能对话超时!');
return null;
}
if(dlg.type != 24){
throw new Error('解析宠物学习技能列表失败,可能对话不是宠物技能商店!');
return null;
}
var reg = new RegExp(/([^|\n]+)/g)
var match = dlg.message.match(reg);
if(match.length < 5){
throw new Error('解析宠物学习技能列表失败,格式错误!');
return null;
}
if((match.length - 5) % 4 != 0){
throw new Error('解析宠物学习技能列表失败,格式错误!');
return null;
}
var storeItemCount = (match.length - 5) / 4;
var obj = {
storeid : match[0],
name : match[1],
welcome : match[2],
insuff_funds : match[3],
insuff_pets : match[4],
skills : []
}
for(var i = 0; i < storeItemCount; ++i){
obj.skills.push({
index : i,
name : match[5 + 4 * i + 0],
mana : parseInt(match[5 + 4 * i + 1]),
cost : parseInt(match[5 + 4 * i + 2]),
info : match[5 + 4 * i + 3],
});
}
return obj;
}
//从NPC对话框内容解析遗忘技能列表
cga.parseForgetSkillStoreMsg = (dlg)=>{
if(!dlg){
throw new Error('解析遗忘技能列表失败,可能对话超时!');
return null;
}
if(!dlg.message){
throw new Error('解析遗忘技能列表失败,可能对话超时!');
return null;
}
if(dlg.type != 18){
throw new Error('解析遗忘技能列表失败,可能对话不是遗忘技能!');
return null;
}
var reg = new RegExp(/([^|\n]+)/g)
var match = dlg.message.match(reg);
if(match.length < 3){
throw new Error('解析遗忘技能列表失败,格式错误!');
return null;
}
if((match.length - 3) % 3 != 0){
throw new Error('解析遗忘技能列表失败,格式错误!');
return null;
}
var storeItemCount = (match.length - 3) / 3;
var obj = {
storeid : match[0],
name : match[1],
welcome : match[2],
skills : []
}
for(var i = 0; i < storeItemCount; ++i){
obj.skills.push({
index : i,
name : match[3 + 3 * i + 0],
level : parseInt(match[3 + 3 * i + 1]),
slots : parseInt(match[3 + 3 * i + 2]),
});
}
return obj;
}
//获取队伍成员详细信息
cga.getTeamPlayers = ()=>{
var teaminfo = cga.GetTeamPlayerInfo();
var units = cga.GetMapUnits();
var playerinfo = cga.GetPlayerInfo();
for(var i in teaminfo){
for(var j in units){
if(units[j].type == 8 && units[j].unit_id == teaminfo[i].unit_id){
teaminfo[i].name = units[j].unit_name;
teaminfo[i].nick = units[j].nick_name;
teaminfo[i].xpos = units[j].xpos;
teaminfo[i].ypos = units[j].ypos;
teaminfo[i].injury = units[j].injury;
teaminfo[i].level = units[j].level;
break;
}
}
if(playerinfo.unitid == teaminfo[i].unit_id){
teaminfo[i].name = playerinfo.name;
teaminfo[i].level = playerinfo.level;
teaminfo[i].injury = playerinfo.health > 0 ? 1 : 0;
teaminfo[i].is_me = true;
}
}
return teaminfo;
}
//和名字为name的玩家组队(必须在附近1x1范围)
cga.addTeammate = (name, cb)=>{
var unit = cga.findPlayerUnit(name);
var mypos = cga.GetMapXY();
if(unit == null ||
!cga.isDistanceClose(unit.xpos, unit.ypos, mypos.x, mypos.y) ||
(unit.xpos == mypos.x && unit.ypos == mypos.y)){
cb(false);
return;
}
setTimeout(()=>{
unit = cga.findPlayerUnit(name);
if(unit == null){
cb(false);
return;
}
cga.TurnTo(unit.xpos, unit.ypos);
setTimeout(()=>{
cga.DoRequest(cga.REQUEST_TYPE_JOINTEAM);
cga.AsyncWaitNPCDialog((err, dlg)=>{
var stripper = "你要和谁组成队伍?";
if(dlg && dlg.message && dlg.message.indexOf(stripper) >= 0){
var strip = dlg.message.substr(dlg.message.indexOf(stripper) + stripper.length);
strip = strip.replace(/\\z/g,"|");
strip = strip.replace(/\\n/g,"|");
var reg = new RegExp(/([^|\n]+)/g)
var match = strip.match(reg);
//console.log(match);
for(var j = 0; j < match.length; ++j){
if(match[j] == name){
console.log(j);
cga.ClickNPCDialog(0, j);
}
}
}
setTimeout(()=>{
var teamPlayers = cga.getTeamPlayers();
if(teamPlayers.length && teamPlayers[0].name == name){
cb(true);
return;
} else if(teamPlayers.length && teamPlayers[0].name != name){
cga.DoRequest(cga.REQUEST_TYPE_LEAVETEAM);
}
cb(false);
return;
}, 1500);
}, 1500);
}, 1500);
}, 1000);
}
//等待名字在teammates列表中的的玩家组队,并自动踢出不符合teammates列表的陌生人。
cga.waitTeammates = (teammates, cb)=>{
var teamplayers = cga.getTeamPlayers();
if(teammates.length == 0 && teamplayers.length == 0)
{
setTimeout(cb, 2000, true);
return;
}
cga.EnableFlags(cga.ENABLE_FLAG_JOINTEAM, true);
if(teamplayers.length == teammates.length){
for(var i = 0; i < teamplayers.length; ++i){
if(!is_array_contain(teammates, teamplayers[i].name)){
//Unknown teammates, kick
cga.DoRequest(cga.REQUEST_TYPE_KICKTEAM);
cga.AsyncWaitNPCDialog((err, dlg)=>{
var stripper = "你要把谁踢出队伍?";
if(dlg && dlg.message && dlg.message.indexOf(stripper) >= 0){
var strip = dlg.message.substr(dlg.message.indexOf(stripper) + stripper.length);
strip = strip.replace(/\\z/g,"|");
strip = strip.replace(/\\n/g,"|");
console.log(strip);
var reg = new RegExp(/([^|\n]+)/g)
var match = strip.match(reg);
//console.log(match);
for(var j = 0; j < match.length; ++j){
if(match[j] == teamplayers[i].name){
cga.ClickNPCDialog(0, j / 2);
break;
}
}
}
});
cb(false);
return;
}
}
setTimeout(cb, 2000, true);
return;
}
cb(false);
}
//监听队友聊天信息
cga.waitTeammateSay = (cb)=>{
cga.AsyncWaitChatMsg((err, r)=>{
if(!r){
cga.waitTeammateSay(cb);
return;
}
var listen = true;
var fromTeammate = null;
var teamplayers = cga.getTeamPlayers();
if(!teamplayers.length){
var playerInfo = cga.GetPlayerInfo();
if(playerInfo.unitid == r.unitid){
fromTeammate = playerInfo;
fromTeammate.index = 0;
fromTeammate.is_me = true;
}
}
for(var i in teamplayers){
if(teamplayers[i].unit_id == r.unitid){
fromTeammate = teamplayers[i];
fromTeammate.index = i;
break;
}
}
if(fromTeammate){
var msgheader = fromTeammate.name + ': ';
if(r.msg.indexOf(msgheader) >= 0){
var msg = r.msg.substr(r.msg.indexOf(msgheader) + msgheader.length);
if (msg.indexOf('[交易]') == 0)
msg = msg.substr('[交易]' .length);
listen = cb(fromTeammate, msg);
}
}
if(listen == true)
cga.waitTeammateSay(cb);
}, 1000);
}
//监听队友聊天信息 队友必须说“1”
cga.waitTeammateSayNextStage = (teammates, cb)=>{
var teammate_state = {};
var teammate_ready = 0;
cga.waitTeammateSay((player, msg)=>{
if(msg == '1' && teammate_state[player.name] !== true){
teammate_state[player.name] = true;
teammate_ready ++;
}
if((teammates.length && teammate_ready >= teammates.length) || (!teammates.length && teammate_ready == 1)){
//all teammates are ready
cb(true);
return false;
}
return true;
});
}
//监听队友聊天信息 队友必须说“1”或“2”
cga.waitTeammateSayNextStage2 = (teammates, cb)=>{
var teammate_state = {};
var teammate_ready = 0;
var teammate_notready = 0;
cga.waitTeammateSay((player, msg)=>{
if(teammate_state[player.name] !== true && teammate_state[player.name] !== false){
if(msg == '1'){
teammate_state[player.name] = true;
teammate_ready ++;
} else if(msg == '2'){
teammate_state[player.name] = false;
teammate_notready ++;
}
}
if((teammates.length && teammate_ready >= teammates.length) || (!teammates.length && teammate_ready == 1)){
//all teammates are ready
cb(true);
return false;
}
if((teammates.length && teammate_ready + teammate_notready >= teammates.length) || (!teammates.length && teammate_ready + teammate_notready == 1)){
//some teammates are not ready
cb(false);
return false;
}
return true;
});
}
//把队友带至posArray指定的位置
cga.walkTeammateToPosition = (posArray, cb) =>{
console.log('cga.walkTeammateToPosition stage1');
if(cga.getTeamPlayers().length == 0)
{
cb(null);
return;
}
var index = 0;
var walk = ()=>{
console.log('cga.walkTeammateToPosition walk');
cga.AsyncWalkTo(posArray[index][0], posArray[index][1], null, null, null, checkTeammateAtPosition);
}
var checkTeammateAtPosition = (err)=>{
console.log('checkTeammateAtPosition 0');
if(!cga.isInNormalState())
{
console.log('checkTeammateAtPosition 1');
setTimeout(checkTeammateAtPosition, 1000);
return;
}
var teamplayers = cga.getTeamPlayers();
var someoneNotInPosArray = false;
for(var i in teamplayers) {
var isInPosArray = false;
for(var j in posArray) {
if(teamplayers[i].xpos == posArray[j][0] && teamplayers[i].ypos == posArray[j][1]) {
isInPosArray = true;
break;
}
}
if(!isInPosArray){
someoneNotInPosArray = true;
break;
}
}
if(someoneNotInPosArray){
console.log('someoneNotInPosArray');
index ++;
if(index > posArray.length - 1)
index = 0;
walk();
return;
}
cga.waitForChatInput((msg, val)=>{
if(msg.indexOf('遇敌防卡住') >= 0)
{
//restart the walk procedure
if(!cga.isInNormalState())
{
console.log('waitForChatInput 0');
setTimeout(checkTeammateAtPosition, 1000);
}
else
{
console.log('waitForChatInput 1');
//or we are at position
cb(null);
}
return false;
}
return true;
});
cga.SayWords('遇敌防卡住', 0, 3, 1);
}
walk();
}
//监听自己聊天输入(只支持数字)
cga.waitForChatInput = (cb)=>{
cga.waitTeammateSay((player, msg)=>{
if(player.is_me == true){
var pattern_number=/^[1-9]\d*$|^0$/;
if(cb(msg, pattern_number.test(msg) ? parseInt(msg) : null ) == false)
return false;
}
return true;
});
}
//监听系统消息
cga.waitSysMsg = (cb)=>{
cga.AsyncWaitChatMsg((err, r)=>{
if(!r || r.unitid != -1){
cga.waitSysMsg(cb);
return;
}
listen = cb(r.msg);
if(listen == true)
cga.waitSysMsg(cb);
}, 1000);
}
cga.waitSysMsgTimeout = (cb, timeout)=>{
cga.AsyncWaitChatMsg((err, r)=>{
if(err){
listen = cb(err);
if(listen == true)
cga.waitSysMsgTimeout(cb, timeout);
return;
}
if(!r || r.unitid != -1){
cga.waitSysMsgTimeout(cb, timeout);
return;
}
listen = cb(null, r.msg);
if(listen == true)
cga.waitSysMsgTimeout(cb, timeout);
}, timeout);
}
//发送超长聊天信息
cga.sayLongWords = (words, color, range, size)=>{
console.log(words);
var splitCount = words.length / 100;
if(splitCount == 0)
splitCount = 1;
for(var i = 0;i < splitCount; ++i){
cga.SayWords(words.substring(i * 100, i * 100 + 100), color, range, size);
}
}
//监听登录状态
cga.waitConnState = (cb)=>{
cga.AsyncWaitConnectionState((err, r)=>{
if(err){
cga.waitConnState(cb);
return;
}
if(cb(r) == true)
cga.waitSysMsg(cb);
}, 10000);
}
/*等待到达某位置,无超时时间限制
等待到达民家(14,10),如果解散了队伍则自动走到(13,10)处:
cga.waitForLocation({mapname : '民家', pos : [14, 10], leaveteam : true, walkto : [13, 10]}, cb);
等待到达地图索引号为24074的地图的(21,12)处:
cga.waitForLocation({mapindex: 24074, pos:[21, 12] }, cb);
*/
cga.waitForLocation = (obj, cb)=>{
var name = cga.GetMapName();
var fpos = cga.GetMapXYFloat();
var index = cga.GetMapIndex().index3;
var passCheck = true;
if(typeof obj.mapname == 'string')
{
if(name != obj.mapname)
{
passCheck = false;
}
}
if(typeof obj.mapindex == 'number')
{
if(index != obj.mapindex)
{
passCheck = false;
}
}
if(obj.moving !== true && !(parseInt(fpos.x) % 64 == 0 && parseInt(fpos.y) % 64 == 0))
{
passCheck = false;
}
if(obj.pos instanceof Array)
{
if (!(Math.abs(fpos.x - obj.pos[0] * 64.0) < 1.001 * 64.0 && Math.abs(fpos.y - obj.pos[1] * 64.0) < 1.001 * 64.0))
{
passCheck = false;
}
}
if(obj.leaveteam === true)
{
var teamplayersnow = cga.getTeamPlayers();
if(teamplayersnow.length)
passCheck = false;
if(!passCheck && obj.walkto && !teamplayersnow.length && (index == obj.mapindex || name == obj.mapname))
{
cga.WalkTo(obj.walkto[0], obj.walkto[1]);
}
}
if(obj.desired_teamplayers != undefined)
{
var teamplayersnow = cga.getTeamPlayers();
if(teamplayersnow.length < obj.desired_teamplayers.length)
{
cb(new Error('当前队伍人数 '+teamplayersnow.length+' 小于预期值 '+obj.desired_teamplayers.length+', 可能队伍已解散,取消等待。'));
return;
}
}
if(passCheck){
cb(null);
return;
}
setTimeout(cga.waitForLocation, 1000, obj, cb);
}
/*等待到达某位置,无超时时间限制
和cga.waitForLocation一样,只是可以等待多个位置,只要满足其中一个就能触发回调。
*/
cga.waitForMultipleLocation = (arr)=>{
var name = cga.GetMapName();
var fpos = cga.GetMapXYFloat();
var index = cga.GetMapIndex().index3;
for(var i = 0; i < arr.length; ++i){
var obj = arr[i];
var passCheck = true;
if(typeof obj.mapname == 'string')
{
if(name != obj.mapname)
{
passCheck = false;
}
}
if(typeof obj.mapindex == 'number')
{
if(index != obj.mapindex)
{
passCheck = false;
}
}
if(obj.moving !== true && !(parseInt(fpos.x) % 64 == 0 && parseInt(fpos.y) % 64 == 0))
{
passCheck = false;
}
if(obj.pos instanceof Array)
{
if (!(Math.abs(fpos.x - obj.pos[0] * 64.0) < 1.001 * 64.0 && Math.abs(fpos.y - obj.pos[1] * 64.0) < 1.001 * 64.0))
{
passCheck = false;
}
}
if(obj.leaveteam === true)
{
var teamplayersnow = cga.getTeamPlayers();
if(teamplayersnow.length)
passCheck = false;
if(!passCheck && obj.walkto && !teamplayersnow.length && (index == obj.mapindex || name == obj.mapname) )
{
cga.WalkTo(obj.walkto[0], obj.walkto[1]);
}
}
if(passCheck){
if(obj.cb(null) == true)
return;
}
}
if(obj.desired_teamplayers != undefined)
{
var teamplayersnow = cga.getTeamPlayers();
if(teamplayersnow.length < obj.desired_teamplayers.length)
{
if( obj.cb(new Error('当前队伍人数 '+teamplayersnow.length+' 小于预期值 '+obj.desired_teamplayers.length+', 可能队伍已解散,取消等待。')) == true)
return;
}
}
setTimeout(cga.waitForMultipleLocation, 1000, arr);
}
cga.cachedMapTileMatrix = null;
cga.cachedMapTileMatrixTime = 0;
cga.buildMapTileMatrix = ()=>{
var curtime = (new Date()).getTime();
if(1 || cga.cachedMapTileMatrix == null || curtime > cga.cachedMapTileMatrixTime + 200)
{
var wall = cga.GetMapTileTable(true);
var matrix = [];
for(var y = 0; y < wall.y_size; ++y){
if(!matrix[y])
matrix[y] = [];
for(var x = 0; x < wall.x_size; ++x){
matrix[y][x] = wall.cell[x + y * wall.x_size];
}
}
cga.cachedMapTileMatrix = curtime;
cga.cachedMapTileMatrix = {matrix : matrix, x_bottom : wall.x_bottom, y_bottom : wall.y_bottom, x_size : wall.x_size, y_size : wall.y_size};
}
return cga.cachedMapTileMatrix;
}
cga.cachedMapCollisionRawMatrix = null;
cga.cachedMapCollisionRawMatrixTime = 0;
cga.buildMapCollisionRawMatrix = ()=>{
var curtime = (new Date()).getTime();
if(1 || cga.cachedMapCollisionRawMatrix == null || curtime > cga.cachedMapCollisionRawMatrixTime + 200)
{
var wall = cga.GetMapCollisionTableRaw(true);
var matrix = [];
for(var y = 0; y < wall.y_size; ++y){
if(!matrix[y])
matrix[y] = [];
for(var x = 0; x < wall.x_size; ++x){
matrix[y][x] = wall.cell[x + y * wall.x_size];
}
}
cga.cachedMapCollisionRawMatrixTime = curtime;
cga.cachedMapCollisionRawMatrix = {matrix : matrix, x_bottom : wall.x_bottom, y_bottom : wall.y_bottom, x_size : wall.x_size, y_size : wall.y_size};
}
return cga.cachedMapCollisionRawMatrix;
}
cga.cachedMapCollisionMatrix = null;
cga.cachedMapCollisionMatrixTime = 0;
cga.buildMapCollisionMatrix = (exitIsBlocked)=>{
var curtime = (new Date()).getTime();
if(1 || cga.cachedMapCollisionMatrix == null || curtime > cga.cachedMapCollisionMatrixTime + 200)
{
var wall = cga.GetMapCollisionTable(true);
var objs = null;
if(exitIsBlocked == true)
objs = cga.GetMapObjectTable(true);
var matrix = [];
for(var y = 0; y < wall.y_size; ++y){
if(!matrix[y])
matrix[y] = [];
for(var x = 0; x < wall.x_size; ++x){
matrix[y][x] = wall.cell[x + y * wall.x_size] == 1 ? 1 : 0;
if(exitIsBlocked == true){
if(objs.cell[x + y * objs.x_size] & 0xff){
matrix[y][x] = 1;
}
}
}
}
cga.cachedMapCollisionMatrixTime = curtime;
cga.cachedMapCollisionMatrix = {matrix : matrix, x_bottom : wall.x_bottom, y_bottom : wall.y_bottom, x_size : wall.x_size, y_size : wall.y_size};
}
return cga.cachedMapCollisionMatrix;
}
cga.cachedMapObjectMatrix = null;
cga.cachedMapObjectMatrixTime = 0;
cga.buildMapObjectMatrix = ()=>{
var curtime = (new Date()).getTime();
if(1 || cga.cachedMapObjectMatrix == null || curtime > cga.cachedMapObjectMatrixTime + 200)
{
var wall = cga.GetMapObjectTable(true);
var matrix = [];
for(var y = 0; y < wall.y_size; ++y){
if(!matrix[y])
matrix[y] = [];
for(var x = 0; x < wall.x_size; ++x){
matrix[y][x] = wall.cell[x + y * wall.x_size] & 0xff;
}
}
cga.cachedMapObjectMatrixTime = curtime;
cga.cachedMapObjectMatrix = {matrix : matrix, x_bottom : wall.x_bottom, y_bottom : wall.y_bottom, x_size : wall.x_size, y_size : wall.y_size};
}
return cga.cachedMapObjectMatrix;
}
cga.cachedMapObjects = null;
cga.cachedMapObjectsTime = 0;
cga.getMapObjects = ()=>{
var curtime = (new Date()).getTime();
if(1 || cga.cachedMapObjects == null || curtime > cga.cachedMapObjectsTime + 200)
{
var wall = cga.GetMapObjectTable(true);
var objs = [];
for(var y = 0; y < wall.y_size; ++y){
for(var x = 0; x < wall.x_size; ++x){
if((wall.cell[x + y * wall.x_size] & 0xff) != 0)
objs.push({
x:x,
y:y,
mapx:x+wall.x_bottom,
mapy:y+wall.y_bottom,
cell:wall.cell[x + y * wall.x_size] & 0xff,
rawcell:wall.cell[x + y * wall.x_size]
});
}
}
cga.cachedMapObjectsTime = curtime;
cga.cachedMapObjects = objs;
}
return cga.cachedMapObjects;
}
//搜索玩家单位
cga.findPlayerUnit = (filter)=>{
var found = cga.GetMapUnits().find((u)=>{
return u.valid == 2 && u.type == 8 && (u.flags & 256) == 256 && ((typeof filter == 'function' && filter(u)) || (typeof filter == 'string' && filter == u.unit_name)) ;
});
return found != undefined ? found : null;
}
//下载地图的部分区域并等待下载完成
cga.downloadMapEx = (xfrom, yfrom, xsize, ysize, cb)=>{
var last_index3 = cga.GetMapIndex().index3;
var x = xfrom, y = yfrom;
var recursiveDownload = ()=>{
cga.RequestDownloadMap(x, y, x+24, y+24);
x += 24;
if(x > xsize){
y += 24;
x = xfrom;
}
if(y - ysize >= 24){
var waitDownloadEnd = (timeout = 3000) => cga.AsyncWaitDownloadMap((err, msg) => {
if (err) {
if(last_index3 != cga.GetMapIndex().index3){
cb(new Error('地图发生变化,下载失败'));
return;
}
cb(null);
return;
}
if ((msg.xtop >= xsize && msg.ytop >= ysize) || (msg.xbase == 0 && msg.ybase == 0)) {
waitDownloadEnd(500);
} else {
waitDownloadEnd(timeout);
}
}, timeout);
waitDownloadEnd();
return;
}
setTimeout(recursiveDownload, 500);
}
recursiveDownload();
}
//下载整张地图并等待下载完成
cga.downloadMap = (cb)=>{
var walls = cga.buildMapCollisionMatrix(true);
cga.downloadMapEx(0, 0, walls.x_size, walls.y_size, cb);
}
/*走一层迷宫
target_map : 走到目标地图就停止,填null则自动解析地图名中的楼层,填''则允许任何形式的地图作为目标楼层。
filter (可选) : {
layerNameFilter : 自定义解析地图名的方法
entryTileFilter : 自定义解析楼梯的方法
}
*/
cga.walkMaze = (target_map, cb, filter)=>{
var objs = cga.getMapObjects();
var pos = cga.GetMapXY();
var newmap = null;
if(typeof target_map != 'string'){
var mapname = cga.GetMapName();
var regex = mapname.match(/([^\d]*)(\d+)([^\d]*)/);
var layerIndex = 0;
if(regex && regex.length >= 3){
layerIndex = parseInt(regex[2]);
}
if(layerIndex == 0){
cb(new Error('无法从地图名中解析出楼层'));
return;
}
if(filter && (typeof filter.layerNameFilter == 'function'))
{
newmap = filter.layerNameFilter(layerIndex, regex);
}
else
{
newmap = regex[1] + ((layerIndex >= 100) ? (layerIndex + 100) : (layerIndex + 1));
if(typeof regex[3] == 'string')
newmap += regex[3];
}
} else {
newmap = target_map;
}
var target = null;
if(filter && (typeof filter.entryTileFilter == 'function'))
{
var tiles = cga.buildMapTileMatrix();
var colraw = cga.buildMapCollisionRawMatrix();
objs.forEach((obj)=>{
if(target == null && obj.cell == 3 && obj.mapx < colraw.x_size && obj.mapy < colraw.y_size && filter.entryTileFilter({
tile : tiles.matrix[obj.mapy][obj.mapx],
colraw : colraw.matrix[obj.mapy][obj.mapx],
obj : obj,
}) == true ){
target = obj;
return false;
}
});
}
else
{
objs.forEach((obj)=>{
if(obj.mapx == pos.x && obj.mapy == pos.y)
return;
if(target == null && obj.cell == 3){
target = obj;
return false;
}
});
}
if(target == null){
cb(new Error('无法找到迷宫的出口'));
return;
}
var walklist = cga.calculatePath(pos.x, pos.y, target.mapx, target.mapy, newmap, null, null, []);
if(walklist.length == 0){
cb(new Error('无法计算到迷宫出口的路径'));
return;
}
cga.walkList(walklist, (err, reason)=>{
cb(err, reason);
return;
});
}
//判断当前地图是否已经下载完成
cga.isMapDownloaded = ()=>{
var tiles = cga.buildMapTileMatrix(true);
for(var y = 0; y < tiles.y_size; ++y){
for(var x = 0; x < tiles.x_size; ++x){
if(tiles.matrix[y][x] == 0)
return false;
}
}
return true;
}
//走一层随机迷宫,和cga.walkMaze的区别是走之前会先下载地图
cga.walkRandomMaze = (target_map, cb, filter)=>{
if(!cga.isMapDownloaded())
{
cga.downloadMap(()=>{
cga.walkMaze(target_map, cb, filter);
});
}
else
{
cga.walkMaze(target_map, cb, filter);
}
}
/**
* targetFinder返回unit object 或者 true都将停止搜索
* cga.searchMap(units => units.find(u => u.unit_name == '守墓员' && u.type == 1) || cga.GetMapName() == '???', result => {
* console.log(result);
* });
*/
//, recursion = true
cga.searchMap = (targetFinder, cb) => {
const getMovablePoints = (map, start) => {
const foundedPoints = {};
foundedPoints[start.x + '-' + start.y] = start;
const findByNextPoints = (centre) => {
const nextPoints = [];
const push = (p) => {
if (p.x > map.x_bottom && p.x < map.x_size && p.y > map.y_bottom && p.y < map.y_size) {
if (map.matrix[p.y][p.x] === 0) {
const key = p.x + '-' + p.y;
if (!foundedPoints[key]) {
foundedPoints[key] = p;
nextPoints.push(p);
}
}
}
};
push({x: centre.x + 1, y: centre.y});
push({x: centre.x + 1, y: centre.y + 1});
push({x: centre.x, y: centre.y + 1});
push({x: centre.x - 1, y: centre.y + 1});
push({x: centre.x - 1, y: centre.y});
push({x: centre.x - 1, y: centre.y - 1});
push({x: centre.x, y: centre.y - 1});
push({x: centre.x + 1, y: centre.y - 1});
nextPoints.forEach(findByNextPoints);
};
findByNextPoints(start);
return foundedPoints;
};
const getFarthestEntry = (current) => {
return cga.getMapObjects().filter(e => [3,10].indexOf(e.cell) >= 0 && (e.mapx != current.x || e.mapy != current.y)).sort((a, b) => {
const distanceA = Math.abs(a.mapx - current.x) + Math.abs(a.mapy - current.y);
const distanceB = Math.abs(b.mapx - current.x) + Math.abs(b.mapy - current.y);
return distanceB - distanceA;
}).shift();
};
const getTarget = (noTargetCB) => {
const target = targetFinder(cga.GetMapUnits());
if (typeof target == 'object') {
const walkTo = cga.getRandomSpace(target.xpos, target.ypos);
if (walkTo) {
cga.walkList([walkTo], () => cb(null, target));
} else {
noTargetCB();
}
} else if (target === true){
cb(null);
} else{
noTargetCB();
}
};
const toNextPoint = (points, current, toNextCB) => {
const remain = points.filter(p => {
const xd = Math.abs(p.x - current.x);
const yd = Math.abs(p.y - current.y);
p.d = xd + yd;
return !(xd < 12 && yd < 12);
}).sort((a,b) => a.d - b.d);
const next = remain.shift();
if (next)
{
if(cga.isPathAvailable(current.x, current.y, next.x, next.y))
{
cga.walkList([[next.x,next.y]], () => getTarget(() => toNextPoint(remain, next, toNextCB)));
}
else
{
getTarget(() => toNextPoint(remain, next, toNextCB))
}
}
else
{
toNextCB();
}
};
//const start = cga.GetMapXY();
//let entry = null;
const findNext = (walls) => {
const current = cga.GetMapXY();
//if (!entry && recursion) entry = getFarthestEntry(start);
toNextPoint(Object.values(getMovablePoints(walls, current)), current, () => {
cb(null);
});
};
getTarget(() => {
let walls = cga.buildMapCollisionMatrix();
if(walls.matrix[0][0] == 1
|| walls.matrix[walls.y_size-1][0] == 1
|| walls.matrix[walls.y_size-1][walls.x_size-1] == 1
|| walls.matrix[0][walls.x_size-1] == 1
) {
cga.downloadMap(() => findNext(cga.buildMapCollisionMatrix()));
} else findNext(walls);
});
}
//获取一格(x,y)周围1x1区域内的空闲地形格子
cga.getRandomSpace = (x, y)=>{
var walls = cga.buildMapCollisionMatrix(true);
if(walls.matrix[y][x-1] == 0)
return [x-1, y];
if(walls.matrix[y][x+1] == 0)
return [x+1, y];
if(walls.matrix[y-1][x] == 0)
return [x, y-1];
if(walls.matrix[y+1][x] == 0)
return [x, y+1];
if(walls.matrix[y+1][x+1] == 0)
return [x+1,y+1];
if(walls.matrix[y+1][x-1] == 0)
return [x-1,y+1];
if(walls.matrix[y-1][x+1] == 0)
return [x+1,y-1];
if(walls.matrix[y-1][x-1] == 0)
return [x-1,y-1];
return null;
}
//获取一格(x,y)周围1x1区域内的空闲地形格子,并判断其方向
cga.getRandomSpaceDir = (x, y)=>{
var walls = cga.buildMapCollisionMatrix(true);
if(walls.matrix[y][x-1] == 0)
return 4;
if(walls.matrix[y][x+1] == 0)
return 0;
if(walls.matrix[y-1][x] == 0)
return 6;
if(walls.matrix[y+1][x] == 0)
return 2;
if(walls.matrix[y+1][x+1] == 0)
return 1;
if(walls.matrix[y+1][x-1] == 0)
return 3;
if(walls.matrix[y-1][x+1] == 0)
return 7;
if(walls.matrix[y-1][x-1] == 0)
return 5;
return null;
}
cga.tradeInternal = (stuff, checkParty, resolve, playerName, timeout) => {
var savePartyName = null;
var tradeFinished = false;
var receivedStuffs = {};
var beginTime = (new Date()).getTime();
var waitTradeMsg = ()=>{
cga.waitSysMsg((msg)=>{
if(tradeFinished)
return false;
if(msg.indexOf('交易完成') >= 0){
tradeFinished = true;
resolve({
success: true,
received: receivedStuffs,
reason : '交易成功',
});
return false;
} else if(msg.indexOf('交易中止') >= 0 || msg.indexOf('因物品栏已满所以无法交易') >= 0){
cga.DoRequest(cga.REQUEST_TYPE_TRADE_REFUSE);
tradeFinished = true;
resolve({
success: false,
reason : '交易被拒绝',
});
return false;
} else if(msg.indexOf('没有可交易的对象') >= 0){
cga.DoRequest(cga.REQUEST_TYPE_TRADE_REFUSE);
tradeFinished = true;
resolve({
success: false,
reason : '没有可交易的对象',
});
return false;
}
return true;
});
}
var waitDialog = ()=>{
if(tradeFinished)
return;
var getInTradeStuffs = false;
var tradeStuffsChecked = false;
var waitTradeStuffs = ()=>{
cga.AsyncWaitTradeStuffs((err, type, args) => {
if(!args){
if(getInTradeStuffs == false && !tradeFinished)
waitTradeStuffs();
return;
}
if(type >= cga.TRADE_STUFFS_ITEM && type <= cga.TRADE_STUFFS_GOLD )
console.log('正在等待获取交易内容:' + cga.TRADE_STUFFS_TRANSLATION[type]);
getInTradeStuffs = true;
if(type == cga.TRADE_STUFFS_ITEM){
receivedStuffs.items = args;
}else if(type == cga.TRADE_STUFFS_PET){
receivedStuffs.pet = [];
receivedStuffs.pet[args.index] = args;
}else if(type == cga.TRADE_STUFFS_PETSKILL){
if(!(receivedStuffs.pet instanceof Array))
receivedStuffs.pet = [];
if(receivedStuffs.pet[args.index])
receivedStuffs.pet[args.index].skills = args;
}else if(type == cga.TRADE_STUFFS_GOLD){
receivedStuffs.gold = args;
}
}, 1000);
}
var waitTradeState = () => {
cga.AsyncWaitTradeState((err, state) => {
if(tradeFinished)
return;
var timeout_trade = (typeof timeout == 'number') ? timeout : 30000;
if( (new Date()).getTime() > beginTime + timeout_trade){
tradeFinished = true;
cga.DoRequest(cga.REQUEST_TYPE_TRADE_REFUSE);
resolve({
success: false,
reason : '交易被拒绝',
});
return;
}
if(state != undefined)
console.log('交易状态变更为:' + cga.TRADE_STATE_TRANSLATION[state]);
if(!err){
if (state == cga.TRADE_STATE_READY || state == cga.TRADE_STATE_CONFIRM) {
getInTradeStuffs = true;
if (!checkParty || tradeStuffsChecked || checkParty(playerName ? playerName : savePartyName, receivedStuffs)) {
tradeStuffsChecked = true;
console.log('确认交易...');
cga.DoRequest(cga.REQUEST_TYPE_TRADE_CONFIRM);
} else {
console.log('拒绝交易...');
cga.DoRequest(cga.REQUEST_TYPE_TRADE_REFUSE);
}
} else if (state == cga.TRADE_STATE_SUCCEED || state == cga.TRADE_STATE_CANCEL) {
getInTradeStuffs = true;
}
}
waitTradeState();
}, 1000);
}
waitTradeStuffs();
waitTradeState();
const itemFilter = (stuff && typeof stuff.itemFilter == 'function') ? stuff.itemFilter : () => false;
const petFilter = (stuff && typeof stuff.petFilter == 'function') ? stuff.petFilter : () => false;
const tradeItems = cga.getInventoryItems().filter(itemFilter).map(e => {
return {itemid: e.itemid, itempos: e.pos, count: (e.count > 1 ? e.count : 1)};
});
const tracePets = cga.GetPetsInfo().filter(petFilter).map((e)=>{
return e.index;
});
cga.TradeAddStuffs(
tradeItems,
tracePets,
(stuff && stuff.gold) ? stuff.gold : 0
);
}
cga.AsyncWaitTradeDialog((err, partyName, partyLevel) => {
if(tradeFinished)
return;
console.log('正在等待交易对话框...');
savePartyName = partyName;
if (!err && partyLevel > 0) {
waitDialog();
} else {
cga.DoRequest(cga.REQUEST_TYPE_TRADE_REFUSE);
tradeFinished = true;
resolve({
success: false,
reason : '等待交易对话框超时',
});
}
}, 10000);
waitTradeMsg();
};
//主动向名字为name的玩家发起交易,给他stuff里指定的东西,成功或失败时回调resolve,在checkParty里可以根据对方名字和收到的东西判断同意还是拒绝交易
/*
给名字为hzqst的玩家交易3组鹿皮:
var count = 0;
cga.positiveTrade('hzqst', {
itemFilter : (item)=>{
if(item.name == '鹿皮' && item.count == 40 && count < 3){
count ++;
return true;
}
return false;
}
},
null, (arg)=>{
if(arg.success){
console.log('交易成功!');
} else {
console.log('交易失败! 原因:'+arg.reason);
}
});
给名字为hzqst的玩家交易包里所有的鹿皮,并且对方必须给自己1000金币否则拒绝交易:
cga.positiveTrade('hzqst', {
itemFilter : (item)=>{
return item.name == '鹿皮' && item.count == 40;
}
},
(playerName, receivedStuffs)={
if(receivedStuffs.gold != 1000){
console.log('对方没有给自己1000金币!');
return false;
}
return true;
},
(arg)=>{
if(arg.success){
console.log('交易成功!');
} else {
console.log('交易失败! 原因:'+arg.reason);
}
});
给名字为hzqst的玩家交易3只哥布林,并且对方必须给自己一只红帽哥布林否则拒绝交易:
var count = 0;
cga.positiveTrade('hzqst', {
petFilter : (pet)=>{
if(pet.realname == '哥布林' && count < 3){
count ++;
return true;
}
return false;
}
},
(playerName, receivedStuffs)={
if(receivedStuffs && receivedStuffs.pets){
if(receivedStuffs.pets.find((pet)=>{
return pet.realname == '红帽哥布林';
}) == null){
console.log('对方没有给自己红帽哥布林!');
return false;
}
}
return true;
},
(arg)=>{
if(arg.success){
console.log('交易成功!');
} else {
console.log('交易失败! 原因:'+arg.reason);
}
});
//arg中可能的返回值:
{
success: false, //是否交易成功
received: [], //交易成功时接受到的物品、宠物、金币
reason: '交易被拒绝', //交易失败的原因
}
*/
cga.positiveTrade = (name, stuff, checkParty, resolve, timeout) => {
var resulted = false;
cga.AsyncWaitPlayerMenu((err, players) => {
if(resulted)
return false;
if(err){
resulted = true;
resolve({
success: false,
reason : '等待交易玩家选择菜单超时',
});
return;
}
if (!(players instanceof Array)) players = [];
var player = players.find((e, index) => typeof name == 'number' ? index == name : e.name == name);
if (player !== undefined) {
resulted = true;
cga.tradeInternal(stuff, checkParty, resolve, name, timeout);
cga.PlayerMenuSelect(player.index);
} else {
resulted = true;
resolve({
success: false,
reason : '未找到目标交易对象',
});
}
}, 5000);
cga.waitSysMsgTimeout((err, msg)=>{
if(resulted)
return false;
if(err)
return false;
if(msg && msg.indexOf('没有可交易的对象!') >= 0)
{
resulted = true;
resolve({
success: false,
reason : '没有可交易的对象',
});
return false;
}
return true;
}, 2000);
cga.DoRequest(cga.REQUEST_TYPE_TRADE);
}
//主动向name玩家发起交易(到开启交易对话框为止),成功或失败时回调resolve
cga.requestTrade = (name, resolve) => {
var resulted = false;
cga.AsyncWaitPlayerMenu((err, players) => {
if(resulted)
return;
if(err){
resulted = true;
resolve({
success: false,
reason : '等待交易玩家选择菜单超时',
});
return;
}
if (!(players instanceof Array)) players = [];
var player = players.find((e, index) => typeof name == 'number' ? index == name : e.name == name);
if (player !== undefined) {
resulted = true;
resolve({success: true});
cga.PlayerMenuSelect(player.index);
} else {
resulted = true;
resolve({
success: false,
reason : '未找到目标交易对象',
});
}
}, 5000);
cga.waitSysMsgTimeout((err, msg)=>{
if(resulted)
return false;
if(err)
return false;
if(msg && msg.indexOf('没有可交易的对象!') >= 0)
{
resulted = true;
resolve({
success: false,
reason : '没有可交易的对象',
});
return false;
}
return true;
}, 2000);
cga.DoRequest(cga.REQUEST_TYPE_TRADE);
}
//等待其他玩家向自己发起交易,成功或失败时回调resolve,在checkParty里可以根据对方名字和收到的东西判断同意还是拒绝交易
//提示:receivedStuffs可能为空数组,所以访问receivedStuffs.items或其他成员之前必须先检查有效性!
/*
等待任意玩家给自己交易3组鹿皮:
cga.waitTrade({},
(playerName, receivedStuffs)=>{
if(receivedStuffs && receivedStuffs.items){
if( receivedStuffs.items.filter((item)=>{
return item.name == '鹿皮' && item.count == 40;
}).length == 3 )
{
return true;
}
}
return false;
},
(arg)=>{
if(arg.success){
console.log('交易成功!');
} else {
console.log('交易失败! 原因:'+arg.reason);
}
});
等待名为hzqst的玩家给自己交易3组鹿皮,并给他1000金币:
cga.waitTrade({
gold : 1000
},
(playerName, receivedStuffs)=>{
if(receivedStuffs && receivedStuffs.items){
if( playerName == 'hzqst' && receivedStuffs.items.filter((item)=>{
return item.name == '鹿皮' && item.count == 40;
}).length == 3 )
{
return true;
}
}
return false;
},
(arg)=>{
if(arg.success){
console.log('交易成功!');
} else {
console.log('交易失败! 原因:'+arg.reason);
}
});
*/
cga.waitTrade = (stuff, checkParty, resolve, timeout) => {
cga.EnableFlags(cga.ENABLE_FLAG_TRADE, true)
cga.tradeInternal(stuff, checkParty, resolve, timeout);
}
//主动向名为name的玩家发起交易并同时等待名为name的玩家向自己发起交易,成功或失败时回调resolve
cga.trade = (name, stuff, checkParty, resolve, timeout) => {
cga.EnableFlags(cga.ENABLE_FLAG_TRADE, true);
cga.AsyncWaitPlayerMenu((err, players) => {
if (!(players instanceof Array)) players = [];
var player = players.find((e, index) => typeof name == 'number' ? index == name : e.name == name);
if (player) {
cga.tradeInternal(stuff, checkParty, resolve, name, timeout);
cga.PlayerMenuSelect(player.index);
}
}, 5000);
cga.DoRequest(cga.REQUEST_TYPE_TRADE);
}
//判断是否是满血满蓝
cga.needSupplyInitial = (obj)=>{
var playerinfo = cga.GetPlayerInfo();
var petinfo = cga.GetPetInfo(playerinfo.petid);
if(!obj)
obj = {};
if(!obj.playerhp)
obj.playerhp = 1.0;
if(!obj.playermp)
obj.playermp = 1.0;
if(!obj.pethp)
obj.pethp = 1.0;
if(!obj.petmp)
obj.petmp = 1.0;
if( playerinfo.hp < playerinfo.maxhp * obj.playerhp ||
playerinfo.mp < playerinfo.maxmp * obj.playermp ||
petinfo.hp < petinfo.maxhp * obj.playerhp ||
petinfo.mp < petinfo.maxmp * obj.playermp)
return true;
return false;
}
//判断是否需要找医生治疗
cga.needDoctor = ()=>{
var playerinfo = cga.GetPlayerInfo();
var pets = cga.GetPetsInfo();
if( playerinfo.health > 0)
return true;
for(var i = 0;i < pets.length; ++i){
if(pets[i].health > 0)
return true;
}
return false;
}
//等待战斗结束
cga.waitForBattleEnd = (cb, timeout = 30000)=>{
cga.AsyncWaitBattleAction((err, result) => {
if(err){
cb(err);
return;
}
if(result == cga.FL_BATTLE_ACTION_END)
{
setTimeout(cb, 1000, null, true);
}
else
{
cga.waitForBattleEnd(cb, timeout);
}
}, timeout);
}
cga.gui = {};
cga.gui.port = null;
cga.gui.init = ()=>{
if(!cga.gui.port){
var p = process.env.CGA_GUI_PORT;
if(!p || !parseInt(p))
throw new Error('获取CGA主进程本地服务端口失败!');
cga.gui.port = parseInt(p);
}
}
/*
获取当前附加的进程的信息
cga.gui.GetGameProcInfo((err, result)=>{
console.log(result);
})
*/
cga.gui.GetGameProcInfo = (cb)=>{
cga.gui.init();
request.get({
url : "http://127.0.0.1:"+cga.gui.port+'/cga/GetGameProcInfo',
json : true,
},
function (error, response, body) {
if(error)
{
cb(error);
return;
}
if(response.statusCode && response.statusCode == 200){
try{
cb(null, body);
return;
}catch(e){
cb(e);
return;
}
} else {
cb(new Error('HTTP 请求失败'));
return;
}
});
}
/*
获取玩家设置、物品设置、自动战斗设置
cga.gui.GetSettings((err, result)=>{
console.log(result);
})
*/
cga.gui.GetSettings = (cb)=>{
cga.gui.init();
request.get({
url : "http://127.0.0.1:"+cga.gui.port+'/cga/GetSettings',
json : true,
},
function (error, response, body) {
if(error)
{
cb(error);
return;
}
if(response.statusCode && response.statusCode == 200){
try{
cb(null, body);
return;
}catch(e){
cb(e);
return;
}
} else {
cb(new Error('HTTP 请求失败'));
return;
}
});
}
/*
加载玩家设置、物品设置、自动战斗设置
开启自动战斗:
cga.gui.LoadSettings({
battle : {
autobattle : true
}
}, (err, result)=>{
console.log(result);
})
参数settings的格式见CGA保存出来的玩家设置json文件,不填的选项代表保持不变
*/
cga.gui.LoadSettings = (settings, cb)=>{
cga.gui.init();
request.post({
url : "http://127.0.0.1:"+cga.gui.port+'/cga/LoadSettings',
json : true,
body: settings
},
function (error, response, body) {
if(error)
{
cb(error);
return;
}
if(response.statusCode && response.statusCode == 200){
try{
cb(null, body);
return;
}catch(e){
cb(e);
return;
}
} else {
cb(new Error('HTTP 请求失败'));
return;
}
});
}
/*
加载脚本
cga.gui.LoadScript({
path : "路径",
autorestart : true, //自动重启脚本开启
autoterm : true, //自动关闭脚本开启
injuryprot : true, //受伤保护开启
soulprot : true, //掉魂受伤保护开启
}, (err, result)=>{
console.log(result);
})
*/
cga.gui.LoadScript = (arg, cb)=>{
cga.gui.init();
request.post({
url : "http://127.0.0.1:"+cga.gui.port+'/cga/LoadScript',
json : true,
body: arg
},
function (error, response, body) {
if(error)
{
cb(error);
return;
}
if(response.statusCode && response.statusCode == 200){
try{
cb(null, body);
return;
}catch(e){
cb(e);
return;
}
} else {
cb(new Error('HTTP 请求失败'));
return;
}
});
}
/*
加载自动登录设置
cga.gui.LoadAccount({
user : "通行证",
pwd : "密码",
gid : "子账号",
game : 4, //区服
bigserver : 1, //电信or网通
server : 8, //线路
character : 1, //左边or右边
autologin : true, //自动登录开启
skipupdate : false, //禁用登录器更新开启
}, (err, result)=>{
console.log(result);
})
调整自动登录到10线
cga.gui.LoadAccount({
server : 10,
}, (err, result)=>{
console.log(result);
})
*/
cga.gui.LoadAccount = (arg, cb)=>{
cga.gui.init();
request.post({
url : "http://127.0.0.1:"+cga.gui.port+'/cga/LoadAccount',
json : true,
body: arg
},
function (error, response, body) {
if(error)
{
cb(error);
return;
}
if(response.statusCode && response.statusCode == 200){
try{
cb(null, body);
return;
}catch(e){
cb(e);
return;
}
} else {
cb(new Error('HTTP 请求失败'));
return;
}
});
}
return cga;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化