InstanceZone.cs 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864
  1. using CommonAI.Data;
  2. using CommonAI.RTS;
  3. using CommonAI.Zone.EventTrigger;
  4. using CommonAI.Zone.Formula;
  5. using CommonAI.Zone.Helper;
  6. using CommonAI.ZoneClient;
  7. using CommonAI.ZoneEditor;
  8. using CommonAI.ZoneServer.JSGModule;
  9. using CommonLang;
  10. using CommonLang.Concurrent;
  11. using CommonLang.IO;
  12. using CommonLang.Log;
  13. using CommonLang.Property;
  14. using CommonLang.Vector;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using static CommonAI.Zone.SpellTemplate;
  19. using static CommonAI.Zone.UnitInfo;
  20. namespace CommonAI.Zone.Instance
  21. {
  22. /// <summary>
  23. /// 服务端场景
  24. /// </summary>
  25. public partial class InstanceZone : GameEntity
  26. {
  27. private static AtomicInteger s_alloc_zone_count = new AtomicInteger(0);
  28. private static AtomicInteger s_active_zone_count = new AtomicInteger(0);
  29. /// <summary>
  30. /// 分配实例数量
  31. /// </summary>
  32. public static int AllocZoneCount { get { return s_alloc_zone_count.Value; } }
  33. /// <summary>
  34. /// 未释放实例数量
  35. /// </summary>
  36. public static int ActiveZoneCount { get { return s_active_zone_count.Value; } }
  37. //------------------------------------------------------------------------
  38. // 基础数据
  39. private InstanceZoneListener mListener;
  40. private TemplateManager mTemplates;
  41. private IFormula mFormula = TemplateManager.Factory.Formula;
  42. private IQuestAdapter mQuestAdapter;
  43. private SyncMessageQueue<Action> mSyncActionQueue = new SyncMessageQueue<Action>();
  44. private Queue<Event> mSendingEvents = new Queue<Event>();
  45. private InstanceZoneObjectMap mObjects;
  46. public TemplateManager Templates { get { return mTemplates; } }
  47. public IQuestAdapter QuestAdapter { get { return mQuestAdapter; } }
  48. public int UpdateIntervalMS { get { return mLastInterval; } }
  49. private readonly int mMaxUnitCount;
  50. private int mLastInterval = 0;
  51. private ulong mTimer = 0;
  52. private long mCurPassTimeMS = 0;
  53. private long mQueryPassTimeMS = 0;
  54. private int mQueryPassTimeSEC = 0;
  55. private bool mHalfSync = false;
  56. readonly private Random random;
  57. readonly private Logger log;
  58. public string UUID { get; set; }
  59. //------------------------------------------------------------------------
  60. readonly private ZoneInfo m_TerrainSrc;
  61. readonly private SpaceDivision mSpaceDiv;
  62. public ZoneInfo TerrainSrc { get { return m_TerrainSrc; } }
  63. public ZoneInfo Terrain { get { return path_terrain_data.Data; } }
  64. public int SpaceDivSize { get; private set; }
  65. [Desc("单位当前帧位移的最小距离")]
  66. public float MinStep { get; private set; }
  67. [Desc("最大单位数量")]
  68. public int MaxUnitCount { get { return mMaxUnitCount; } }
  69. [Desc("场景中是否存在Area")]
  70. public bool HasArea { get { return mHasArea; } }
  71. //山大王id
  72. protected int mKingID;
  73. //绑定服务器
  74. protected string mBindGameSrvId;
  75. //------------------------------------------------------------------------
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. /// <param name="templates"></param>
  80. /// <param name="listener">消息接收者</param>
  81. /// <param name="data">场景数据</param>
  82. /// <param name="spaceDivSize">空间分割参数</param>
  83. /// <param name="maxUnitCount">最大单位数</param>
  84. /// <param name="randomSeed">随机种子</param>
  85. internal InstanceZone(TemplateManager templates, InstanceZoneListener listener, ZoneEditor.SceneData data, GSCreateAreaData gsData,
  86. int spaceDivSize, int maxUnitCount, int randomSeed)
  87. {
  88. InstanceZone.s_alloc_zone_count++;
  89. InstanceZone.s_active_zone_count++;
  90. var info = data.ZoneData;
  91. this.log = LoggerFactory.GetLogger(string.Format("InstanceZone({0})", info.TemplateID));
  92. this.random = TemplateManager.Factory.CreateRandom(randomSeed);// new Random(randomSeed);
  93. this.mTemplates = templates;
  94. this.IsSyncZ = false;
  95. this.MinStep = MoveHelper.GetDistance(1000 / templates.CFG.SYSTEM_FPS, templates.CFG.OBJECT_MOVE_TO_MIN_STEP_SEC);
  96. if (spaceDivSize < 1)
  97. {
  98. throw new Exception("SpaceDivSize must large than map 1 !");
  99. }
  100. this.mMaxUnitCount = maxUnitCount;
  101. this.mListener = listener;
  102. this.m_TerrainSrc = info;
  103. this.SpaceDivSize = spaceDivSize;
  104. this.mSpaceDiv = new SpaceDivision(
  105. m_TerrainSrc.TotalWidth,
  106. m_TerrainSrc.TotalHeight,
  107. spaceDivSize * m_TerrainSrc.GridCellW,
  108. spaceDivSize * m_TerrainSrc.GridCellH);
  109. this.mObjects = new InstanceZoneObjectMap();
  110. // this.path_terrain_data = new InstanceZoneManhattanMap(templates, info.Clone() as ZoneInfo);
  111. // this.path_finder = new AstarManhattan(path_terrain_data, true, spaceDivSize);
  112. // this.path_terrain_area_gen = new ManhattanMapAreaGenerator(path_terrain_data.Data);
  113. this.InitTerrain(data, spaceDivSize, out this.path_terrain_data, out this.path_finder, out this.path_terrain_area_gen);
  114. this.mQuestAdapter = TemplateManager.Factory.CreateQuestAdapter(this);
  115. this.baseInit(data, gsData);
  116. }
  117. protected virtual void baseInit(ZoneEditor.SceneData data, GSCreateAreaData gsData)
  118. {
  119. }
  120. // -----------------------------------------------------------------------------------
  121. ~InstanceZone()
  122. {
  123. InstanceZone.s_alloc_zone_count--;
  124. }
  125. protected override void Disposing()
  126. {
  127. base.Disposing();
  128. this.ClearEvents();
  129. foreach (var obj in mObjects.Objects)
  130. {
  131. obj.Dispose();
  132. }
  133. this.mObjects.Dispose();
  134. this.mObjects = null;
  135. foreach (var flg in mFlags.Values)
  136. {
  137. flg.Dispose();
  138. }
  139. this.mFlags.Clear();
  140. this.DisposeTerrain();
  141. this.mSpaceDiv.Dispose();
  142. this.mQuestAdapter.Dispose();
  143. this.mQuestAdapter = null;
  144. this.mListener = null;
  145. this.mTemplates = null;
  146. this.mFormula = null;
  147. this.mSyncActionQueue = null;
  148. this.mSendingEvents = null;
  149. this.mTasks.Clear();
  150. this.mTasks = null;
  151. this.mTimeTasks.Dispose();
  152. this.mTimeTasks = null;
  153. this.EnvironmentVarMap.Clear();
  154. this.mFlags.Clear();
  155. this.m_UnitsPos.Clear();
  156. this.m_UnitsPos.Clear();
  157. InstanceZone.s_active_zone_count--;
  158. }
  159. // -----------------------------------------------------------------------------------
  160. protected Logger Log { get { return log; } }
  161. public int TotalWidth { get { return m_TerrainSrc.TotalWidth; } }
  162. public int TotalHeight { get { return m_TerrainSrc.TotalHeight; } }
  163. public int GridCellW { get { return m_TerrainSrc.GridCellW; } }
  164. public int GridCellH { get { return m_TerrainSrc.GridCellH; } }
  165. public int XCount { get { return m_TerrainSrc.XCount; } }
  166. public int YCount { get { return m_TerrainSrc.YCount; } }
  167. public ulong Tick { get { return mTimer; } }
  168. /// <summary>
  169. /// 是否发 SyncPosEvent 包
  170. /// </summary>
  171. public bool SyncPos
  172. {
  173. get { return sync_pos_list.Enable; }
  174. set { sync_pos_list.Enable = value; }
  175. }
  176. /// <summary>
  177. /// 是否为低速率网络同步(将优化为半字通信)
  178. /// </summary>
  179. public bool IsHalfSync
  180. {
  181. get { return mHalfSync; }
  182. set
  183. {
  184. if (value && TotalWidth <= 255 && TotalHeight <= 255)
  185. {
  186. mHalfSync = value;
  187. }
  188. else
  189. {
  190. mHalfSync = false;
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 同步包是否包含Z
  196. /// </summary>
  197. public bool IsSyncZ
  198. {
  199. get;
  200. set;
  201. }
  202. public long PassTimeMS { get { return mQueryPassTimeMS; } }
  203. public int PassTimeSEC { get { return mQueryPassTimeSEC; } }
  204. public Random RandomN { get { return random; } }
  205. // public void Trace(string text)
  206. // {
  207. // log.Info(text);
  208. // }
  209. // -----------------------------------------------------------------------------------
  210. //------------------------------------------------------------------------------------
  211. #region TIMING_AND_TASK
  212. private SyncMessageQueue<Action<InstanceZone>> mTasks = new SyncMessageQueue<Action<InstanceZone>>();
  213. private TimeTaskQueue mTimeTasks = new TimeTaskQueue();
  214. private void doTask(Action<InstanceZone> task)
  215. {
  216. task(this);
  217. }
  218. /// <summary>
  219. /// 【线程安全】向主线程排一个任务
  220. /// </summary>
  221. /// <param name="task"></param>
  222. public void queueTask(Action<InstanceZone> task)
  223. {
  224. mTasks.Enqueue(task);
  225. }
  226. /// <summary>
  227. /// 【线程安全】增加时间任务
  228. /// </summary>
  229. /// <param name="intervalMS"></param>
  230. /// <param name="delayMS"></param>
  231. /// <param name="repeat"></param>
  232. /// <param name="handler"></param>
  233. public TimeTaskMS AddTimeTask(int intervalMS, int delayMS, int repeat, TickHandler handler)
  234. {
  235. return mTimeTasks.AddTimeTask(intervalMS, delayMS, repeat, handler);
  236. }
  237. /// <summary>
  238. /// 【线程安全】增加延时回调方法
  239. /// </summary>
  240. /// <param name="delayMS"></param>
  241. /// <param name="handler"></param>
  242. public TimeTaskMS AddTimeDelayMS(int delayMS, TickHandler handler)
  243. {
  244. return mTimeTasks.AddTimeDelayMS(delayMS, handler);
  245. }
  246. /// <summary>
  247. /// 【线程安全】增加定时回调方法
  248. /// </summary>
  249. /// <param name="intervalMS"></param>
  250. /// <param name="handler"></param>
  251. public TimeTaskMS AddTimePeriodicMS(int intervalMS, TickHandler handler)
  252. {
  253. if(intervalMS <= 0)
  254. {
  255. log.Error("增加定时任务异常:" + intervalMS);
  256. return null;
  257. }
  258. return mTimeTasks.AddTimePeriodicMS(intervalMS, handler);
  259. }
  260. #endregion
  261. //-----------------------------------------------------------------------------------
  262. /// <summary>
  263. /// 【线程安全】向当前场景输入指令
  264. /// </summary>
  265. /// <param name="act"></param>
  266. public void pushAction(Action act)
  267. {
  268. mSyncActionQueue.Enqueue(act);
  269. }
  270. protected internal void queueEventInternal(Event evt)
  271. {
  272. mSendingEvents.Enqueue(evt);
  273. }
  274. /// <summary>
  275. /// 【内部调用】输出消息
  276. /// </summary>
  277. public void queueEvent(ZoneEvent evt, GameEntity sender = null)
  278. {
  279. if (sender != null) { evt.sender = sender; }
  280. this.queueEventInternal(evt);
  281. }
  282. /// <summary>
  283. /// 【内部调用】输出消息
  284. /// </summary>
  285. /// <param name="obj"></param>
  286. /// <param name="evt"></param>
  287. public void queueObjectEvent(InstanceZoneObject obj, ObjectEvent evt, bool force = false)
  288. {
  289. if(mSendingEvents == null)
  290. {
  291. if(mObjects == null)
  292. {
  293. log.Warn("queueObjectEvent exception1: " + this.GetSceneID() + ", mObjects null, " + this.UUID + ", " + evt.MessageID);
  294. }
  295. else
  296. {
  297. log.Warn("queueObjectEvent exception2: " + this.GetSceneID() + ", Players: " + this.AllPlayersCount + ", Items: "
  298. + this.AllItemsCount + ", Units: " + this.AllUnitsCount + ", " + this.UUID + ", " + evt.MessageID);
  299. }
  300. return;
  301. }
  302. if (obj.ID == 0)
  303. return;
  304. if (!force && !obj.IsInZone)
  305. return;
  306. obj.onSendingEvent(ref evt);
  307. if (evt != null)
  308. {
  309. evt.object_id = obj.ID;
  310. evt.sender = obj;
  311. mSendingEvents.Enqueue(evt);
  312. }
  313. }
  314. /// <summary>
  315. /// 立即发送指令
  316. /// </summary>
  317. /// <param name="obj"></param>
  318. /// <param name="req"></param>
  319. /// <param name="rsp"></param>
  320. public void sendActorResponse(InstanceZoneObject obj, ActorRequest req, ActorResponse rsp)
  321. {
  322. if (obj.ID == 0)
  323. return;
  324. if (!obj.IsInZone)
  325. return;
  326. rsp.MessageID = req.MessageID;
  327. rsp.object_id = obj.ID;
  328. rsp.sender = obj;
  329. mSendingEvents.Enqueue(rsp);
  330. }
  331. public void sendMessageBox(string msg)
  332. {
  333. queueEventInternal(new TestMessageBox(msg));
  334. }
  335. public virtual int GetSceneID() { return 0; }
  336. //-----------------------------------------------------------------------------------
  337. public void RecvMessageFromGameServer(ZoneServer.SendMessageR2B msg)
  338. {
  339. LastRecvMessageR2B = msg;
  340. if (mOnRecvFromGS != null)
  341. {
  342. mOnRecvFromGS.Invoke(this, msg);
  343. }
  344. }
  345. public void SendMessageToGameServer(string msg)
  346. {
  347. ZoneServer.SendMessageB2R b2r = new ZoneServer.SendMessageB2R();
  348. b2r.Message = msg;
  349. LastSentMessageB2R = b2r;
  350. if (mOnSendToGS != null)
  351. {
  352. mOnSendToGS.Invoke(this, b2r);
  353. }
  354. }
  355. public void SendEventToGameServer(IExternalizable evt)
  356. {
  357. ZoneServer.SendEventB2R b2r = new ZoneServer.SendEventB2R();
  358. b2r.evt = evt;
  359. if (mOnSendMessageB2REvent != null)
  360. {
  361. mOnSendMessageB2REvent.Invoke(this, b2r);
  362. }
  363. }
  364. public bool CheckAutoDropItem()
  365. {
  366. return this.mListener.CheckDropItem();
  367. }
  368. //-----------------------------------------------------------------------------------
  369. public virtual void Update(int intervalMS, bool slowRefresh)
  370. {
  371. beginEventsRecord();
  372. this.mLastInterval = intervalMS;
  373. this.MinStep = MoveHelper.GetDistance(intervalMS, Templates.CFG.OBJECT_MOVE_TO_MIN_STEP_SEC);
  374. if (mTimer == 0)
  375. {
  376. mCurPassTimeMS = 0;
  377. mQueryPassTimeMS = 0;
  378. mQueryPassTimeSEC = 0;
  379. foreach (InstanceFlag f in mFlags.Values)
  380. {
  381. f.OnStart();
  382. }
  383. if (mOnInit != null)
  384. mOnInit.Invoke(this);
  385. }
  386. mTasks.ProcessMessages(doTask);
  387. mSyncActionQueue.ProcessMessages(updateAction);
  388. mObjects.Refresh();
  389. var objetes = mObjects.Objects;
  390. {
  391. // clean space div state
  392. foreach (InstanceZoneObject u in objetes)
  393. {
  394. u.onUpdate(this, slowRefresh);
  395. }
  396. mSpaceDiv.ClearSpaceNearChanges();
  397. bool dirty = false;
  398. foreach (InstanceZoneObject u in objetes)
  399. {
  400. if (u.Enable)
  401. {
  402. dirty = u.updatePos(this);
  403. u.mCurCellNode.MarkPosDirty(dirty);
  404. if (dirty && u.ClientVisible && u.SyncPos)
  405. {
  406. if (mOnObjectPosChanged != null)
  407. {
  408. mOnObjectPosChanged.Invoke(this, u);
  409. }
  410. sync_pos_list.Add(u);
  411. }
  412. }
  413. }
  414. }
  415. foreach (InstanceFlag f in mFlags.Values)
  416. {
  417. f.update();
  418. }
  419. if (mOnUpdate != null)
  420. {
  421. mOnUpdate.Invoke(this);
  422. }
  423. updateEvents();
  424. mTimeTasks.Update(intervalMS);
  425. mCurPassTimeMS += intervalMS;
  426. mQueryPassTimeMS = mCurPassTimeMS;
  427. mQueryPassTimeSEC = (int)(mCurPassTimeMS / 1000);
  428. mTimer++;
  429. }
  430. private void updateAction(Action act)
  431. {
  432. if (act is SystemMessage)
  433. {
  434. processSystemMessage(act as SystemMessage);
  435. }
  436. else if (act is ObjectAction)
  437. {
  438. ObjectAction oa = (ObjectAction)act;
  439. InstanceUnit unit = act.sender as InstanceUnit;
  440. if (unit == null)
  441. {
  442. unit = mObjects.GetObject<InstanceUnit>(oa.object_id);
  443. }
  444. if (unit != null)
  445. {
  446. unit.doAction(oa);
  447. if (mOnHandleObjectAction != null)
  448. {
  449. mOnHandleObjectAction.Invoke(unit, oa);
  450. }
  451. mFormula.OnUnitHandleNetMessage(unit, oa);
  452. }
  453. else
  454. {
  455. log.Info("Drop message : " + oa);
  456. }
  457. }
  458. else
  459. {
  460. if (mOnHandleAction != null)
  461. {
  462. mOnHandleAction.Invoke(this, act);
  463. }
  464. mFormula.OnZoneHandleNetMessage(this, act);
  465. }
  466. }
  467. private void updateEvents()
  468. {
  469. if (sync_pos_list.Enable)
  470. {
  471. queueEvent(sync_pos_list.AsEvent(this));
  472. }
  473. sync_pos_list.Clear();
  474. while (mSendingEvents.Count > 0)
  475. {
  476. Event evt = mSendingEvents.Dequeue();
  477. //log.Info("---->onEvent:" + evt.ToString());
  478. mListener.onEventHandler(evt);
  479. if (mOnPostEvent != null)
  480. {
  481. mOnPostEvent.Invoke(this, evt);
  482. }
  483. if (evt is GameOverEvent && mOnGameOver != null)
  484. {
  485. mOnGameOver.Invoke(this, evt as GameOverEvent);
  486. }
  487. }
  488. }
  489. virtual protected void processSystemMessage(SystemMessage msg)
  490. {
  491. if (msg is Ping)
  492. {
  493. queueEventInternal(new Pong(msg as Ping));
  494. }
  495. }
  496. //-------------------------------------------------------------------------------------------
  497. #region OBJECTS
  498. private uint mObjectIDIndexer = 0;
  499. internal uint genObjectID()
  500. {
  501. mObjectIDIndexer++;
  502. return mObjectIDIndexer;
  503. }
  504. // 获取一个单位
  505. public T getObject<T>(uint obj_id) where T : InstanceZoneObject
  506. {
  507. if (obj_id == 0) return null;
  508. T go = mObjects.GetObject<T>(obj_id);
  509. if (go != null)
  510. {
  511. return go;
  512. }
  513. return null;
  514. }
  515. public InstanceUnit getUnit(uint obj_id)
  516. {
  517. if (obj_id == 0) return null;
  518. return mObjects.GetObject<InstanceUnit>(obj_id);
  519. }
  520. public InstanceUnit getUnitByTemplateID(int templateId)
  521. {
  522. if (templateId == 0) return null;
  523. foreach (InstanceUnit u in mObjects.Units)
  524. {
  525. if (u.Info.TemplateID == templateId)
  526. {
  527. return u;
  528. }
  529. }
  530. return null;
  531. }
  532. public InstanceUnit getUnitByName(string name)
  533. {
  534. if (string.IsNullOrEmpty(name))
  535. {
  536. return null;
  537. }
  538. foreach (InstanceUnit u in mObjects.Units)
  539. {
  540. if (name.Equals(u.Name))
  541. {
  542. return u;
  543. }
  544. }
  545. return null;
  546. }
  547. public InstancePlayer getPlayerByUUID(string playerUUID)
  548. {
  549. if (string.IsNullOrEmpty(playerUUID))
  550. {
  551. return null;
  552. }
  553. return mObjects.GetPlayer(playerUUID);
  554. }
  555. public InstanceItem getItemByName(string name)
  556. {
  557. if (string.IsNullOrEmpty(name))
  558. {
  559. return null;
  560. }
  561. foreach (InstanceItem u in mObjects.Items)
  562. {
  563. if (name.Equals(u.Name))
  564. {
  565. return u;
  566. }
  567. }
  568. return null;
  569. }
  570. public InstanceUnit getUnitByID(int UnitID)
  571. {
  572. foreach (InstanceUnit obj in mObjects.Units)
  573. {
  574. if (obj.ID == UnitID)
  575. {
  576. return obj;
  577. }
  578. }
  579. return null;
  580. }
  581. public IEnumerable<InstancePlayer> AllPlayers { get { return mObjects.Players; } }
  582. public int AllPlayersCount { get { return mObjects.PlayersCount; } }
  583. public IEnumerable<InstanceUnit> AllUnits { get { return mObjects.Units; } }
  584. public int AllUnitsCount { get { return mObjects.UnitsCount; } }
  585. public IEnumerable<InstanceSpell> AllSpells { get { return mObjects.Spells; } }
  586. public int AllSpellsCount { get { return mObjects.SpellsCount; } }
  587. public IEnumerable<InstanceItem> AllItems { get { return mObjects.Items; } }
  588. public int AllItemsCount { get { return mObjects.ItemsCount; } }
  589. public IEnumerable<InstanceZoneObject> AllObjects { get { return mObjects.Objects; } }
  590. public int AllObjectsCount { get { return mObjects.ObjectsCount; } }
  591. [Obsolete("use {@link #AllUnits} {@link #AllSpells} {@link #AllItems} for best performance!")]
  592. public IEnumerable<InstanceZoneObject> allObjs()
  593. {
  594. return mObjects.Objects;
  595. }
  596. [Obsolete("use {@link #AllUnitsCount} {@link #AllSpellsCount} {@link #AllItemsCount} for best performance!")]
  597. public int allObjsCount()
  598. {
  599. return mObjects.ObjectsCount;
  600. }
  601. [Obsolete]
  602. public List<T> selectUnits<T>(Predicate<T> select) where T : InstanceUnit
  603. {
  604. List<T> ret = new List<T>(mObjects.UnitsCount);
  605. foreach (InstanceUnit obj in mObjects.Units)
  606. {
  607. if (select(obj as T))
  608. {
  609. ret.Add(obj as T);
  610. }
  611. }
  612. return ret;
  613. }
  614. public void selectUnits<T>(Predicate<T> select, List<T> ret) where T : InstanceUnit
  615. {
  616. foreach (InstanceUnit obj in mObjects.Units)
  617. {
  618. if (select(obj as T))
  619. {
  620. ret.Add(obj as T);
  621. }
  622. }
  623. }
  624. public T selectRandomUnit<T>(Predicate<T> select) where T : InstanceUnit
  625. {
  626. T ret = null;
  627. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  628. {
  629. foreach (InstanceUnit obj in mObjects.Units)
  630. {
  631. if (obj is T && select(obj as T))
  632. {
  633. list.Add(obj as T);
  634. }
  635. }
  636. if (list.Count > 0)
  637. {
  638. ret = CUtils.GetRandomInArray<InstanceUnit>(list, random) as T;
  639. }
  640. }
  641. return ret;
  642. }
  643. public T selectUnit<T>(Predicate<T> select) where T : InstanceUnit
  644. {
  645. foreach (InstanceUnit obj in mObjects.Units)
  646. {
  647. if (select(obj as T))
  648. {
  649. return obj as T;
  650. }
  651. }
  652. return null;
  653. }
  654. //-------------------------------------------------------------------------------------------
  655. /// <summary>
  656. /// 创建一个单位实体
  657. /// </summary>
  658. virtual public InstanceZoneObject CreateUnit(UnitInfo info, string name, int force, int alliesForce, int level)
  659. {
  660. InstanceUnit unit = TemplateManager.Factory.CreateUnit(this, info, name, force, alliesForce, level);
  661. if (unit != null)
  662. {
  663. return unit;
  664. }
  665. switch (info.UType)
  666. {
  667. case UnitInfo.UnitType.TYPE_PLAYER:
  668. return new InstancePlayer(this, info, name, force, level, alliesForce);
  669. case UnitInfo.UnitType.TYPE_MANUAL:
  670. return new InstanceManual(this, info, name, force, level);
  671. case UnitInfo.UnitType.TYPE_PET:
  672. return new InstancePet(this, info, name, force, level);
  673. case UnitInfo.UnitType.TYPE_SUMMON:
  674. return new InstanceSummon(this, info, name, force, level);
  675. case UnitInfo.UnitType.TYPE_BUILDING:
  676. return new InstanceBuilding(this, info, name, force, level);
  677. case UnitInfo.UnitType.TYPE_MONSTER:
  678. return new InstanceGuard(this, info, name, force, level);
  679. case UnitInfo.UnitType.TYPE_NPC:
  680. return new InstanceGuard(this, info, name, force, level);
  681. default:
  682. return new InstanceGuard(this, info, name, force, level);
  683. }
  684. }
  685. //-------------------------------------------------------------------------------------------
  686. public InstanceUnit AddUnit(int unitTemplateID, string name, int force, int level, float x, float y, float direction, bool pointLv = false)
  687. {
  688. UnitInfo info = Templates.getUnit(unitTemplateID);
  689. if (info != null)
  690. {
  691. return AddUnit(info, name, force, level, x, y, direction, null, "", 0, pointLv);
  692. }
  693. return null;
  694. }
  695. /// <summary>
  696. /// 添加一个单位
  697. /// </summary>
  698. /// <param name="info">单位模板</param>
  699. /// <param name="name">场景中名字(Key)</param>
  700. /// <param name="force">单位阵营</param>
  701. /// <param name="level">单位等级</param>
  702. /// <param name="x">坐标</param>
  703. /// <param name="y">坐标</param>
  704. /// <param name="direction">方向</param>
  705. /// <param name="summoner">召唤者</param>
  706. /// <returns></returns>
  707. public InstanceUnit AddUnit(UnitInfo info, string name, int force, int level, float x, float y, float direction,
  708. InstanceUnit summoner = null, String clientShowName = "", int gsFlag = 0, bool pointLv = false)
  709. {
  710. AddUnitEvent add;
  711. return AddUnit(info, name, force, level, x, y, direction, out add, summoner, clientShowName, gsFlag, 0, pointLv);
  712. }
  713. /// <summary>
  714. /// 添加一个单位
  715. /// </summary>
  716. /// <param name="info">单位模板</param>
  717. /// <param name="name">场景中名字(Key)</param>
  718. /// <param name="force">单位阵营</param>
  719. /// <param name="level">单位等级</param>
  720. /// <param name="x">坐标</param>
  721. /// <param name="y">坐标</param>
  722. /// <param name="direction">方向</param>
  723. /// <param name="add">输出事件</param>
  724. /// <param name="summoner">召唤者</param>
  725. /// <returns></returns>
  726. public InstanceUnit AddUnit(UnitInfo info, string name, int force, int level, float x, float y, float direction,
  727. out AddUnitEvent add, InstanceUnit summoner = null, String clientShowName = "", int gsFlag = 0, int alliesForce = 0, bool pointLv = false)
  728. {
  729. add = null;
  730. if (mObjects.UnitsCount >= mMaxUnitCount)
  731. {
  732. log.Warn(string.Format("Zone Unit TryAdd max: {0}, {1}, 单位数量:{2}", this.TerrainSrc.ID, info.TemplateID, mObjects.UnitsCount));
  733. return null;
  734. }
  735. if (mTryAddUnit != null && !mTryAddUnit.Invoke(info))
  736. {
  737. //log.Info(string.Format("Zone Unit TryAdd max: {0}, {1}, 单位数量:{2}", this.TerrainSrc.ID, info.TemplateID, mObjects.UnitsCount));
  738. return null;
  739. }
  740. // 创建实体单位
  741. InstanceZoneObject ret = CreateUnit(info, name, force, alliesForce, level);
  742. // 存入单位列表
  743. if (ret is InstanceUnit)
  744. {
  745. InstanceUnit unit = ret as InstanceUnit;
  746. //unit.Name = name;
  747. unit.gameServerFlag = gsFlag;
  748. // 存入单位列表
  749. if (unit.tryAdd(x, y, direction))
  750. {
  751. mObjects.AddObject(unit);
  752. this.LastAddedUnit = unit;
  753. if (unit is ISummonedUnit)
  754. {
  755. ((ISummonedUnit)unit).SummonerUnit = summoner;
  756. }
  757. try
  758. {
  759. unit.onAdded(this, pointLv);
  760. if (mOnUnitAdded != null)
  761. mOnUnitAdded.Invoke(this, unit);
  762. add = new AddUnitEvent();
  763. add.sender = unit;
  764. if (unit.ClientVisible)
  765. {
  766. if (clientShowName != null && !unit.IsPlayer && clientShowName.Length > 0)
  767. {
  768. unit.SetDiaplayerName(clientShowName);
  769. }
  770. //// 阵营判断
  771. //if(info.UType == UnitType.TYPE_MONSTER && force > 1)
  772. //{
  773. // add.flag = 1;
  774. //}
  775. queueEvent(add);
  776. }
  777. }
  778. catch (Exception err)
  779. {
  780. log.Warn("AddUnit catch :" + name + ", e:" + err);
  781. add.ErrorMessage = err.Message;
  782. return null;
  783. }
  784. finally
  785. {
  786. add.Sync = unit.GenSyncUnitInfo(true);
  787. }
  788. return unit;
  789. }
  790. }
  791. return null;
  792. }
  793. public InstanceItem AddItem(ItemTemplate template, string name, float x, float y, float direction, int force, string disPlayName,
  794. out AddItemEvent add, InstanceUnit creater,int from)
  795. {
  796. add = null;
  797. if (mTryAddItem != null && !mTryAddItem.Invoke(template))
  798. {
  799. //log.Info(string.Format("Zone item TryAdd max: {0}, {1}", this.mSceneType, template.TemplateID));
  800. return null;
  801. }
  802. InstanceItem ret = TemplateManager.Factory.CreateItem(this, template, name, force, creater, disPlayName,from);
  803. ret.setPos(x, y);
  804. if (ret.tryAdd(x, y, direction))
  805. {
  806. mObjects.AddObject(ret);
  807. this.LastCreatedInstanceItem = ret;
  808. add = new AddItemEvent();
  809. add.sender = ret;
  810. if (ret.ClientVisible)
  811. {
  812. queueEvent(add);
  813. }
  814. try
  815. {
  816. ret.onAdded(this);
  817. if (mItemAdded != null)
  818. mItemAdded.Invoke(this, ret, creater);
  819. }
  820. catch (Exception err)
  821. {
  822. log.Warn("AddItem: " + template.ID + ", catch: " + err);
  823. add.ErrorMessage = err.Message;
  824. return null;
  825. }
  826. finally
  827. {
  828. add.Sync = ret.GenSyncItemInfo(true, null);
  829. }
  830. return ret;
  831. }
  832. return null;
  833. }
  834. public InstanceItem AddItem(ItemTemplate template, string name, float x, float y, float direction, int force, string disPlayName, InstanceUnit creater,int from=0)
  835. {
  836. AddItemEvent add;
  837. return AddItem(template, name, x, y, direction, force, disPlayName, out add, creater,from);
  838. }
  839. /// <summary>
  840. /// 添加一个法术或飞行道具
  841. /// </summary>
  842. /// <param name="template"></param>
  843. /// <param name="launch"></param>
  844. /// <param name="sender"></param>
  845. /// <param name="launcher">此法术的最初发起者</param>
  846. /// <param name="target_obj_id"></param>
  847. /// <param name="targetPos"></param>
  848. /// <param name="startX"></param>
  849. /// <param name="startY"></param>
  850. /// <param name="direction"></param>
  851. /// <param name="chain"></param>
  852. /// <returns></returns>
  853. public InstanceSpell AddSpell(
  854. XmdsSkillType fromSkillType,
  855. SpellTemplate template,
  856. LaunchSpell launch,
  857. InstanceZoneObject sender,
  858. InstanceUnit launcher,
  859. uint target_obj_id,
  860. Vector2 targetPos,
  861. float startX,
  862. float startY,
  863. float direction,
  864. SpellChainLevelInfo chain = null,
  865. int actionIndex = -1,
  866. int maxAffectUnit = 0,
  867. Dictionary<uint, InstanceUnit> damageList = null,
  868. int spellIndex = 0,
  869. JSGCreateSpellData createData = null)
  870. {
  871. if (template != null && sender != null)
  872. {
  873. InstanceUnit target = getUnit(target_obj_id);
  874. switch (template.MType)
  875. {
  876. // case SpellTemplate.MotionType.Missile:
  877. // if (target == null)
  878. // {
  879. // return null;
  880. // }
  881. // break;
  882. case SpellTemplate.MotionType.BindingTarget:
  883. if (target == null)
  884. {
  885. return null;
  886. }
  887. startX = target.X;
  888. startY = target.Y;
  889. break;
  890. case SpellTemplate.MotionType.SelectTarget:
  891. if (targetPos != null)
  892. {
  893. startX = targetPos.X;
  894. startY = targetPos.Y;
  895. }
  896. break;
  897. case SpellTemplate.MotionType.SeekerMissile:
  898. case SpellTemplate.MotionType.SeekerSelectTarget:
  899. {
  900. target = null;
  901. target_obj_id = 0;
  902. }
  903. break;
  904. case SpellTemplate.MotionType.Chain:
  905. if (target == null)
  906. {
  907. return null;
  908. }
  909. break;
  910. case SpellTemplate.MotionType.Cannon:
  911. if (target == null && targetPos == null)
  912. {
  913. targetPos = new Vector2(startX, startY);
  914. }
  915. break;
  916. }
  917. // 创建实体单位
  918. //InstanceSpell ret = new InstanceSpell(this, template, launch, launcher, sender, chainLevel);
  919. InstanceSpell ret = TemplateManager.Factory.CreateSpell(this, template, launch, launcher, sender, fromSkillType, damageList, spellIndex, createData);
  920. //ret.Direction = direction;
  921. ret.BindActionIndex = actionIndex;
  922. ret.MaxAffectUnit = (maxAffectUnit == 0) ? ret.Info.MaxAffectUnit : maxAffectUnit;
  923. ret.faceTo(direction);
  924. ret.setChainInfo(chain);
  925. ret.setTargetPos(targetPos);
  926. ret.setTarget(target);
  927. // 存入单位列表
  928. float xChanage, yChange;
  929. launch.GetXModify(launcher, out xChanage, out yChange);
  930. if (ret.tryAdd(startX + xChanage, startY + yChange, direction))
  931. {
  932. //System.Console.WriteLine("--AddSpell: " + ret.Info.ID + ", " + ret.ID);
  933. launcher.Virtual.DispatchSendSpellOverEvent(launch, template, ret.X, ret.Y);
  934. mObjects.AddObject(ret);
  935. this.LastLaunchSpell = ret.Info;
  936. ret.onAdded(this);
  937. if (ret.ClientVisible)
  938. {
  939. // 产生事件
  940. AddSpellEvent evt = new AddSpellEvent(ret);
  941. evt.sender = ret;
  942. queueEvent(evt);
  943. }
  944. return ret;
  945. }
  946. }
  947. return null;
  948. }
  949. // 移除一个单位
  950. public bool RemoveObject(InstanceZoneObject obj)
  951. {
  952. //Console.WriteLine("RemoveObject : " + obj.ID);
  953. if (mObjects.RemoveObject(obj))
  954. {
  955. obj.onRemoved(this);
  956. if (obj is InstanceUnit)
  957. {
  958. InstanceUnit unit = obj as InstanceUnit;
  959. mFormula.OnUnitRemoved(unit);
  960. if (mOnUnitRemoved != null)
  961. mOnUnitRemoved.Invoke(this, unit);
  962. }
  963. else if(obj is InstanceItem)
  964. {
  965. if (this.mOnItemRemoved != null)
  966. this.mOnItemRemoved.Invoke(this, (InstanceItem)obj);
  967. }
  968. if (obj.ClientVisible)
  969. {
  970. RemoveObjectEvent remove = new RemoveObjectEvent(obj.ID);
  971. remove.sender = obj;
  972. queueEvent(remove);
  973. }
  974. obj.Dispose();
  975. return true;
  976. }
  977. return false;
  978. }
  979. public InstanceZoneObject RemoveObjectByID(uint oid)
  980. {
  981. var obj = mObjects.GetObject<InstanceZoneObject>(oid);
  982. if (obj != null && RemoveObject(obj))
  983. {
  984. return obj;
  985. }
  986. return null;
  987. }
  988. public InstanceZoneObject RemoveUnitByID(uint unitTemplateId)
  989. {
  990. InstanceUnit obj = mObjects.GetUnit(unitTemplateId);
  991. if (obj != null && RemoveObject(obj))
  992. {
  993. return obj;
  994. }
  995. return null;
  996. }
  997. public InstanceZoneObject RemoveSpellByLaunchIDAndSpellID(int launchId, int spellId)
  998. {
  999. InstanceSpell spell = mObjects.GetSpllByLanuchAndSpellId(launchId, spellId);
  1000. if(spell != null && RemoveObject(spell))
  1001. {
  1002. return spell;
  1003. }
  1004. return null;
  1005. }
  1006. public InstanceZoneObject RemoveItemByID(uint unitTemplateId)
  1007. {
  1008. InstanceItem obj = mObjects.GetItem(unitTemplateId);
  1009. if (obj != null && RemoveObject(obj))
  1010. {
  1011. Console.WriteLine("成功移除单位:" + unitTemplateId);
  1012. return obj;
  1013. }
  1014. return null;
  1015. }
  1016. public int GetZoneKingID()
  1017. {
  1018. return this.mKingID;
  1019. }
  1020. public string GetBindGameSrvID()
  1021. {
  1022. return this.mBindGameSrvId;
  1023. }
  1024. //----------------------------------------------------------------------------------------------------------------------------------------
  1025. private class InstanceZoneObjectMap
  1026. {
  1027. private class DirtyList<K, T> where T : InstanceZoneObject
  1028. {
  1029. private bool dirty = true;
  1030. private List<T> mObjectsCollection = new List<T>();
  1031. private HashMap<K, T> mObjects = new HashMap<K, T>();
  1032. public int Count { get { return mObjects.Count; } }
  1033. public void Add(K key, T obj)
  1034. {
  1035. dirty = true;
  1036. mObjects.Add(key, obj);
  1037. }
  1038. public void Put(K key, T obj)
  1039. {
  1040. dirty = true;
  1041. mObjects.Put(key, obj);
  1042. }
  1043. public bool Remove(K key)
  1044. {
  1045. if (mObjects.Remove(key))
  1046. {
  1047. dirty = true;
  1048. return true;
  1049. }
  1050. return false;
  1051. }
  1052. public void Dispose()
  1053. {
  1054. mObjectsCollection.Clear();
  1055. mObjects.Clear();
  1056. }
  1057. public T Get(K key)
  1058. {
  1059. return mObjects.Get(key);
  1060. }
  1061. public bool ContainsKey(K key)
  1062. {
  1063. return mObjects.ContainsKey(key);
  1064. }
  1065. public IEnumerable<T> GetCollection()
  1066. {
  1067. if (dirty)
  1068. {
  1069. dirty = false;
  1070. mObjectsCollection.Clear();
  1071. mObjectsCollection.AddRange(mObjects.Values);
  1072. }
  1073. return mObjectsCollection;
  1074. }
  1075. }
  1076. private DirtyList<uint, InstanceZoneObject> mObjects = new DirtyList<uint, InstanceZoneObject>();
  1077. private DirtyList<uint, InstanceUnit> mObjects_MirrorUnits = new DirtyList<uint, InstanceUnit>();
  1078. private DirtyList<uint, InstanceSpell> mObjects_MirrorSpells = new DirtyList<uint, InstanceSpell>();
  1079. private DirtyList<uint, InstanceItem> mObjects_MirrorItems = new DirtyList<uint, InstanceItem>();
  1080. private DirtyList<string, InstancePlayer> mObjects_MirrorPlayers = new DirtyList<string, InstancePlayer>();
  1081. internal void Refresh()
  1082. {
  1083. //mObjects.Refresh();
  1084. //mObjects_MirrorUnits.Refresh();
  1085. //mObjects_MirrorSpells.Refresh();
  1086. //mObjects_MirrorItems.Refresh();
  1087. //mObjects_MirrorPlayers.Refresh();
  1088. }
  1089. public void AddObject(InstanceZoneObject obj)
  1090. {
  1091. mObjects.Add(obj.ID, obj);
  1092. if (obj is InstanceUnit)
  1093. {
  1094. mObjects_MirrorUnits.Add(obj.ID, obj as InstanceUnit);
  1095. }
  1096. else if (obj is InstanceSpell)
  1097. {
  1098. mObjects_MirrorSpells.Add(obj.ID, obj as InstanceSpell);
  1099. }
  1100. else if (obj is InstanceItem)
  1101. {
  1102. mObjects_MirrorItems.Add(obj.ID, obj as InstanceItem);
  1103. }
  1104. if (obj is InstancePlayer)
  1105. {
  1106. var p = obj as InstancePlayer;
  1107. mObjects_MirrorPlayers.Put(p.PlayerUUID, p);
  1108. }
  1109. }
  1110. public bool RemoveObject(InstanceZoneObject obj)
  1111. {
  1112. if (mObjects.Remove(obj.ID))
  1113. {
  1114. if (obj is InstanceUnit)
  1115. {
  1116. mObjects_MirrorUnits.Remove(obj.ID);
  1117. }
  1118. else if (obj is InstanceSpell)
  1119. {
  1120. mObjects_MirrorSpells.Remove(obj.ID);
  1121. }
  1122. else if (obj is InstanceItem)
  1123. {
  1124. mObjects_MirrorItems.Remove(obj.ID);
  1125. }
  1126. if (obj is InstancePlayer)
  1127. {
  1128. var p = obj as InstancePlayer;
  1129. mObjects_MirrorPlayers.Remove(p.PlayerUUID);
  1130. }
  1131. return true;
  1132. }
  1133. return false;
  1134. }
  1135. private InstanceZoneObject GetObject(uint id)
  1136. {
  1137. return mObjects.Get(id);
  1138. }
  1139. public bool ContainsObject(InstanceZoneObject obj)
  1140. {
  1141. return mObjects.ContainsKey(obj.ID);
  1142. }
  1143. public bool ContainsObjectByKey(uint id)
  1144. {
  1145. return mObjects.ContainsKey(id);
  1146. }
  1147. public void Dispose()
  1148. {
  1149. mObjects.Dispose();
  1150. mObjects_MirrorUnits.Dispose();
  1151. mObjects_MirrorSpells.Dispose();
  1152. mObjects_MirrorItems.Dispose();
  1153. mObjects_MirrorPlayers.Dispose();
  1154. }
  1155. public T GetObject<T>(uint id) where T : InstanceZoneObject
  1156. {
  1157. Type type = typeof(T);
  1158. if (type.IsSubclassOf(typeof(InstanceUnit)))
  1159. {
  1160. return mObjects_MirrorUnits.Get(id) as T;
  1161. }
  1162. else if (type.IsSubclassOf(typeof(InstanceSpell)))
  1163. {
  1164. return mObjects_MirrorSpells.Get(id) as T;
  1165. }
  1166. else if (type.IsSubclassOf(typeof(InstanceItem)))
  1167. {
  1168. return mObjects_MirrorItems.Get(id) as T;
  1169. }
  1170. else
  1171. {
  1172. return mObjects.Get(id) as T;
  1173. }
  1174. }
  1175. public InstanceUnit GetUnit(uint unitTemplateId)
  1176. {
  1177. foreach (InstanceUnit obj in mObjects_MirrorUnits.GetCollection())
  1178. {
  1179. if (obj.Info.ID == unitTemplateId)
  1180. {
  1181. return obj;
  1182. }
  1183. }
  1184. return null;
  1185. }
  1186. public InstanceSpell GetSpllByLanuchAndSpellId(int launchId, int spellId)
  1187. {
  1188. foreach (InstanceSpell obj in mObjects_MirrorSpells.GetCollection())
  1189. {
  1190. if (obj.Info.ID == spellId && obj.LauncherID == launchId)
  1191. {
  1192. return obj;
  1193. }
  1194. }
  1195. return null;
  1196. }
  1197. public InstanceItem GetItem(uint itemId)
  1198. {
  1199. return mObjects_MirrorItems.Get(itemId);
  1200. }
  1201. public InstancePlayer GetPlayer(string uuid)
  1202. {
  1203. return mObjects_MirrorPlayers.Get(uuid);
  1204. }
  1205. public int ObjectsCount { get { return mObjects.Count; } }
  1206. public IEnumerable<InstanceZoneObject> Objects { get { return mObjects.GetCollection(); } }
  1207. public int UnitsCount { get { return mObjects_MirrorUnits.Count; } }
  1208. public IEnumerable<InstanceUnit> Units { get { return mObjects_MirrorUnits.GetCollection(); } }
  1209. public int SpellsCount { get { return mObjects_MirrorSpells.Count; } }
  1210. public IEnumerable<InstanceSpell> Spells { get { return mObjects_MirrorSpells.GetCollection(); } }
  1211. public int ItemsCount { get { return mObjects_MirrorItems.Count; } }
  1212. public IEnumerable<InstanceItem> Items { get { return mObjects_MirrorItems.GetCollection(); } }
  1213. public int PlayersCount { get { return mObjects_MirrorPlayers.Count; } }
  1214. public IEnumerable<InstancePlayer> Players { get { return mObjects_MirrorPlayers.GetCollection(); } }
  1215. }
  1216. #endregion
  1217. //------------------------------------------------------------------------------------
  1218. #region CALL_BACK
  1219. internal void cb_unitDamageCallBack(InstanceUnit target, InstanceUnit attacker, int reduceHP, AttackSource source)
  1220. {
  1221. LastHittedUnit = target;
  1222. LastAttackUnit = attacker;
  1223. if (attacker != null)
  1224. attacker.callback_onAttack(this, target, reduceHP, source);
  1225. if (target != null)
  1226. target.callback_onDamage(this, attacker, reduceHP, source);
  1227. if (mOnUnitDamage != null)
  1228. mOnUnitDamage.Invoke(this, target, attacker, reduceHP, source);
  1229. }
  1230. internal void cb_unitDeadCallBack(InstanceUnit obj, InstanceUnit attacker)
  1231. {
  1232. statisticForceDead(obj);
  1233. LastHittedUnit = obj;
  1234. LastKilledUnit = obj;
  1235. //if (attacker == null)
  1236. // attacker = obj;
  1237. LastAttackUnit = attacker;
  1238. obj.doDead(attacker);
  1239. if(attacker != null)
  1240. {
  1241. obj.deadDropItems(attacker.Force);
  1242. attacker.CurrentMoney += obj.Info.DropMoney;
  1243. }
  1244. mFormula.OnUnitDead(obj, attacker);
  1245. obj.callback_onDead(this, attacker);
  1246. if (mOnUnitDead != null)
  1247. mOnUnitDead.Invoke(this, obj, attacker);
  1248. obj.mProcessDeadCallbackTime = CommonLang.CUtils.localTimeMS;
  1249. }
  1250. internal void cb_unitActivatedCallBack(InstanceUnit obj)
  1251. {
  1252. nearChange(obj);
  1253. LastActivatedUnit = obj;
  1254. obj.callback_onActivated(this);
  1255. if (mOnUnitActivated != null)
  1256. mOnUnitActivated.Invoke(this, obj);
  1257. }
  1258. internal void cb_unitRebirthCallBack(InstanceUnit obj)
  1259. {
  1260. LastRebirthUnit = obj;
  1261. obj.callback_onRebirth(this);
  1262. if (mOnUnitRebirth != null)
  1263. mOnUnitRebirth.Invoke(this, obj);
  1264. }
  1265. internal void cb_unitGotInventoryItemCallBack(InstanceUnit obj, ItemTemplate item, int count)
  1266. {
  1267. LastUnitGotInventoryItem = item;
  1268. obj.callback_onGotInventoryItem(this, item);
  1269. if (mOnUnitGotInventoryItem != null)
  1270. mOnUnitGotInventoryItem.Invoke(this, obj, item, count);
  1271. }
  1272. internal void cb_unitLostInventoryItemCallBack(InstanceUnit obj, ItemTemplate item, int count)
  1273. {
  1274. LastUnitLostInventoryItem = item;
  1275. obj.callback_onLostInventoryItem(this, item);
  1276. if (mOnUnitLostInventoryItem != null)
  1277. mOnUnitLostInventoryItem.Invoke(this, obj, item, count);
  1278. }
  1279. internal int cb_unitGotInstanceItemCallBack(InstanceUnit obj, InstanceItem item)
  1280. {
  1281. LastUnitGotInstanceItem = item;
  1282. obj.callback_onGotInstanceItem(this, item);
  1283. short times = 0;
  1284. if (mOnUnitGotInstanceItem != null)
  1285. {
  1286. InstancePlayer player = (obj as InstancePlayer);
  1287. if (player == null)
  1288. {
  1289. return 0;
  1290. }
  1291. if (item.unitPickInfo != null)
  1292. {
  1293. if (item.Info.maxPickTimes > 0)
  1294. {
  1295. times = item.unitPickInfo.Get(player.PlayerUUID);
  1296. if (times >= item.Info.maxPickTimes)
  1297. {
  1298. player.Virtual.SendMsgToClient(XmdsConstConfig.TIPS_PICK_MAX);
  1299. return item.Info.maxPickTimes;
  1300. }
  1301. }
  1302. item.unitPickInfo.Put(player.PlayerUUID, ++times);
  1303. item.TotalPickTimes++;
  1304. }
  1305. mOnUnitGotInstanceItem.Invoke(this, obj, item);
  1306. }
  1307. return times;
  1308. }
  1309. internal void cb_unitUseItemCallBack(InstanceUnit obj, ItemTemplate item, InstanceUnit item_creater)
  1310. {
  1311. LastUnitUseItem = item;
  1312. obj.callback_onUseItem(this, item, item_creater);
  1313. if (mOnUnitUseItem != null)
  1314. mOnUnitUseItem.Invoke(this, obj, item, item_creater);
  1315. }
  1316. internal void cb_unitGotBuffCallBack(InstanceUnit obj, InstanceUnit.BuffState buff)
  1317. {
  1318. LastUnitGotBuff = buff.Data;
  1319. obj.callback_onGotBuff(this, buff);
  1320. if (mOnUnitGotBuff != null)
  1321. mOnUnitGotBuff.Invoke(this, obj, buff);
  1322. }
  1323. internal void cb_unitLostBuffCallBack(InstanceUnit obj, InstanceUnit.BuffState buff)
  1324. {
  1325. obj.callback_onLostBuff(this, buff);
  1326. if (mOnUnitLostBuff != null)
  1327. mOnUnitLostBuff.Invoke(this, obj, buff);
  1328. }
  1329. internal void cb_unitGotMoneyCallBack(InstanceUnit obj, int add_money)
  1330. {
  1331. if (mOnUnitGotMoney != null)
  1332. {
  1333. mOnUnitGotMoney.Invoke(obj, add_money);
  1334. }
  1335. }
  1336. internal void cb_unitPickUnitCallBack(InstanceUnit src, InstanceUnit pickable)
  1337. {
  1338. LastPickableUnit = pickable;
  1339. if (mOnUnitPickUnit != null)
  1340. {
  1341. mOnUnitPickUnit.Invoke(this, src, pickable);
  1342. }
  1343. }
  1344. public void cb_unitOutBattleCallBack(InstanceUnit obj)
  1345. {
  1346. if (mOnUnitOutBattle != null)
  1347. {
  1348. mOnUnitOutBattle.Invoke(this, obj);
  1349. }
  1350. }
  1351. internal bool cb_unitTryPickItem(InstanceUnit unit, InstanceItem item)
  1352. {
  1353. LastPickingItem = item;
  1354. LastPickingItemUnit = unit;
  1355. bool ret = true;
  1356. if (mTryPickItem != null)
  1357. {
  1358. foreach (TryPickItemHandler trypick in mTryPickItem.GetInvocationList())
  1359. {
  1360. if (!trypick.Invoke(this, unit, item))
  1361. {
  1362. ret = false;
  1363. }
  1364. }
  1365. }
  1366. return ret;
  1367. }
  1368. internal void cb_unitFinishPickItem(InstanceUnit unit, InstanceItem item)
  1369. {
  1370. if (mFinishPickItem != null)
  1371. {
  1372. mFinishPickItem.Invoke(this, unit, item);
  1373. }
  1374. }
  1375. internal void cb_unitLaunchSkill(InstanceUnit unit, CommonAI.Zone.Instance.InstanceUnit.SkillState ss)
  1376. {
  1377. LastLaunchSkill = ss.Data;
  1378. LastLaunchSkillUnit = unit;
  1379. if (mOnUnitLaunchSkill != null)
  1380. {
  1381. mOnUnitLaunchSkill.Invoke(this, unit, ss);
  1382. }
  1383. }
  1384. internal void cb_playerReady(InstancePlayer player)
  1385. {
  1386. if (mPlayerReady != null)
  1387. {
  1388. mPlayerReady.Invoke(player);
  1389. }
  1390. }
  1391. #endregion
  1392. //-------------------------------------------------------------------------------------------
  1393. #region UNIT_ATTACK
  1394. /// <summary>
  1395. /// 获得两个对象是否是队友
  1396. /// </summary>
  1397. /// <param name="src"></param>
  1398. /// <param name="dst"></param>
  1399. /// <returns></returns>
  1400. public virtual bool IsTeammates(InstanceUnit src, InstanceUnit dst)
  1401. {
  1402. return false;
  1403. }
  1404. /// <summary>
  1405. /// 测试是否可见,单位间可交互(AOI)
  1406. /// </summary>
  1407. /// <param name="src"></param>
  1408. /// <param name="dst"></param>
  1409. /// <returns></returns>
  1410. public virtual bool IsVisibleAOI(InstanceZoneObject src, InstanceZoneObject dst)
  1411. {
  1412. if (src.AoiStatus == dst.AoiStatus)
  1413. {
  1414. if (dst is InstanceUnit)
  1415. {
  1416. return (dst as InstanceUnit).IsVisible;
  1417. }
  1418. return true;
  1419. }
  1420. else if (src.AoiStatus != null)
  1421. {
  1422. return src.AoiStatus.CanSeeOther;
  1423. }
  1424. else if (dst.AoiStatus != null)
  1425. {
  1426. return dst.AoiStatus.CanSeeMe;
  1427. }
  1428. return false;
  1429. }
  1430. /// <summary>
  1431. /// 测试是否可攻击
  1432. /// </summary>
  1433. /// <param name="src"></param>
  1434. /// <param name="target"></param>
  1435. /// <param name="expectTarget"></param>
  1436. /// <param name="reason"></param>
  1437. /// <param name="weapon"></param>
  1438. /// <returns></returns>
  1439. public virtual bool IsAttackable(InstanceUnit src, InstanceUnit target, SkillTemplate.CastTarget expectTarget, AttackReason reason, ITemplateData weapon)
  1440. {
  1441. if(src == null || target == null)
  1442. {
  1443. return false;
  1444. }
  1445. if (!IsVisibleAOI(src, target))
  1446. {
  1447. return false;
  1448. }
  1449. if (target.CanWhiplashDeadBody)
  1450. {
  1451. if (!target.IsAttackable)
  1452. return false;
  1453. if (!target.IsVisible)
  1454. return false;
  1455. if (target.IsInvincible)
  1456. return false;
  1457. }
  1458. else
  1459. {
  1460. if (!target.IsActive)
  1461. return false;
  1462. if (!target.IsVisible)
  1463. return false;
  1464. if (target.IsInvincible)
  1465. return false;
  1466. }
  1467. switch (expectTarget)
  1468. {
  1469. case SkillTemplate.CastTarget.Enemy:
  1470. return src.Force != target.Force;
  1471. case SkillTemplate.CastTarget.PetForMaster:
  1472. if (src is InstancePet)
  1473. {
  1474. return (src as InstancePet).Master == target;
  1475. }
  1476. return false;
  1477. case SkillTemplate.CastTarget.Alias:
  1478. return (src != target) && (src.Force == target.Force);
  1479. case SkillTemplate.CastTarget.AlliesIncludeSelf:
  1480. return (src.Force == target.Force);
  1481. case SkillTemplate.CastTarget.AlliesExcludeSelf:
  1482. return (src != target) && (src.Force == target.Force);
  1483. case SkillTemplate.CastTarget.EveryOne:
  1484. return true;
  1485. case SkillTemplate.CastTarget.EveryOneExcludeSelf:
  1486. return (src != target);
  1487. case SkillTemplate.CastTarget.Self:
  1488. return src == target;
  1489. case SkillTemplate.CastTarget.EnemyAndSelf:
  1490. return src.Force != target.Force || src == target;
  1491. case SkillTemplate.CastTarget.NA:
  1492. default:
  1493. return false;
  1494. }
  1495. }
  1496. /// <summary>
  1497. /// 扫描所有可攻击对象
  1498. /// </summary>
  1499. /// <param name="src"></param>
  1500. /// <param name="list"></param>
  1501. /// <param name="expectTarget"></param>
  1502. /// <param name="reason"></param>
  1503. /// <param name="weapon"></param>
  1504. public void getAttackableUnits(InstanceUnit src, List<InstanceUnit> list, SkillTemplate.CastTarget expectTarget, AttackReason reason, ITemplateData weapon)
  1505. {
  1506. for (int i = list.Count - 1; i >= 0; --i)
  1507. {
  1508. InstanceUnit o = list[i];
  1509. if (!IsAttackable(src, o, expectTarget, reason, weapon))
  1510. {
  1511. list.RemoveAt(i);
  1512. }
  1513. }
  1514. }
  1515. /// <summary>
  1516. /// 对单个单位攻击。
  1517. /// </summary>
  1518. /// <param name="src"></param>
  1519. /// <param name="attack"></param>
  1520. /// <param name="target"></param>
  1521. /// <param name="expectTarget">判断IsAttackable</param>
  1522. /// <returns></returns>
  1523. public bool unitAttackSingle(
  1524. InstanceUnit src,
  1525. AttackSource attack,
  1526. InstanceUnit target,
  1527. SkillTemplate.CastTarget expectTarget)
  1528. {
  1529. if (IsAttackable(src, target, expectTarget, AttackReason.Attack, attack.Weapon))
  1530. {
  1531. target.doHitAttack(src, attack);
  1532. return true;
  1533. }
  1534. return false;
  1535. }
  1536. /// <summary>
  1537. /// 某个单位对指定列表里的 IsAttackable 单位发起攻击。
  1538. /// 此操作会修改List
  1539. /// </summary>
  1540. /// <param name="src"></param>
  1541. /// <param name="attack"></param>
  1542. /// <param name="list">调用完成后,列表中未命中单位会自动移除。</param>
  1543. /// <param name="expectTarget">判断IsAttackable</param>
  1544. /// <returns></returns>
  1545. public int unitAttack(
  1546. InstanceUnit src,
  1547. AttackSource attack,
  1548. List<InstanceUnit> list,
  1549. SkillTemplate.CastTarget expectTarget)
  1550. {
  1551. int count = 0;
  1552. for (int i = list.Count - 1; i >= 0; --i)
  1553. {
  1554. InstanceUnit o = list[i];
  1555. if (IsAttackable(src, o, expectTarget, AttackReason.Attack, attack.Weapon))
  1556. {
  1557. o.doHitAttack(src, attack);
  1558. count++;
  1559. }
  1560. else
  1561. {
  1562. list.RemoveAt(i);
  1563. }
  1564. }
  1565. return count;
  1566. }
  1567. /// <summary>
  1568. /// 直接对列表中的单位攻击,不做任何判断。
  1569. /// </summary>
  1570. /// <param name="src"></param>
  1571. /// <param name="attack"></param>
  1572. /// <param name="list"></param>
  1573. public int unitAttackDirect(
  1574. InstanceUnit src,
  1575. AttackSource attack,
  1576. List<InstanceUnit> list)
  1577. {
  1578. int count = 0;
  1579. for (int i = list.Count - 1; i >= 0; --i)
  1580. {
  1581. InstanceUnit o = list[i];
  1582. o.doHitAttack(src, attack);
  1583. count++;
  1584. }
  1585. return count;
  1586. }
  1587. /// <summary>
  1588. /// 某个单位发起周身攻击
  1589. /// </summary>
  1590. /// <param name="src"></param>
  1591. /// <param name="attack"></param>
  1592. /// <param name="range"></param>
  1593. /// <param name="expectTarget"></param>
  1594. /// <returns></returns>
  1595. public int unitAttackRound(
  1596. InstanceUnit src,
  1597. AttackSource attack,
  1598. float range,
  1599. SkillTemplate.CastTarget expectTarget)
  1600. {
  1601. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1602. {
  1603. getObjectsRoundRange<InstanceUnit>(
  1604. Collider.Object_HitBody_TouchRound,
  1605. src.X, src.Y, range,
  1606. list, src.AoiStatus);
  1607. return unitAttack(src, attack, list, expectTarget);
  1608. }
  1609. }
  1610. /// <summary>
  1611. /// 某个单位发起扇形范围攻击
  1612. /// </summary>
  1613. /// <param name="src"></param>
  1614. /// <param name="attack"></param>
  1615. /// <param name="direction"></param>
  1616. /// <param name="range"></param>
  1617. /// <param name="angle"></param>
  1618. /// <param name="expectTarget"></param>
  1619. /// <returns></returns>
  1620. public int unitAttackFan(
  1621. InstanceUnit src,
  1622. AttackSource attack,
  1623. float direction,
  1624. float range,
  1625. float angle,
  1626. SkillTemplate.CastTarget expectTarget)
  1627. {
  1628. float dr = angle / 2;
  1629. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1630. {
  1631. getObjectsFanRange<InstanceUnit>(
  1632. Collider.Object_HitBody_TouchFan,
  1633. src.X, src.Y, range,
  1634. direction - dr,
  1635. direction + dr,
  1636. list, src.AoiStatus);
  1637. return unitAttack(src, attack, list, expectTarget);
  1638. }
  1639. }
  1640. //-------------------------------------------------------------------------------------------------------
  1641. // 单位释放法术
  1642. //-------------------------------------------------------------------------------------------------------
  1643. public void unitLaunchSpell(
  1644. XmdsSkillType fromSkillType,
  1645. InstanceUnit launcher,
  1646. LaunchSpell launch,
  1647. float startX,
  1648. float startY,
  1649. uint targetUnitID = 0,
  1650. Vector2 targetPos = null, int actionIndex = -1, int maxAffectUnit = 0, float pointDir = 0)
  1651. {
  1652. if (CUtils.RandomPercent(RandomN, launch.LaunchPercent))
  1653. {
  1654. float direction = pointDir == 0 ? launcher.Direction : pointDir;
  1655. if (targetPos != null)
  1656. {
  1657. //direction = MathVector.getDegree(startX, startY, targetPos.x, targetPos.y);
  1658. targetPos = (Vector2)targetPos.Clone();
  1659. }
  1660. SpellTemplate spell = Templates.getSpell(launch.SpellID);
  1661. if(spell == null)
  1662. {
  1663. log.Error("unitLaunchSpell找不到法术模板:" + launcher.PlayerUUID + ", " + launch.SpellID + ", SN: " + launch.SerialNumber);
  1664. return;
  1665. }
  1666. JSGCreateSpellData createData;
  1667. if (spell != null && launch.Count > 0 && mFormula.TryLaunchSpell(launcher, launch, ref spell, out createData, ref startX, ref startY))
  1668. {
  1669. SpellChainLevelInfo chain = null;
  1670. if (launch.ChainLevel > 0)
  1671. {
  1672. chain = new SpellChainLevelInfo(launch);
  1673. }
  1674. switch (launch.PType)
  1675. {
  1676. case LaunchSpell.PosType.POS_TYPE_FAN:
  1677. {
  1678. float startAngle = direction - launch.Angle / 2f;// + launch.StartAngle;
  1679. float interAngle = launch.Count > 0 ? launch.Angle / (launch.Count - 1) : 0;
  1680. for (int i = 0; i < launch.Count; i++)
  1681. {
  1682. AddSpell(fromSkillType, spell, launch, launcher, launcher,
  1683. targetUnitID, targetPos, startX, startY,
  1684. startAngle + interAngle * i, chain, actionIndex, maxAffectUnit);
  1685. }
  1686. }
  1687. break;
  1688. case LaunchSpell.PosType.POS_TYPE_TOSAMETARGET:
  1689. {
  1690. InstanceUnit target = SeekSpellAttackable(launcher, spell, launcher.AoiStatus, startX, startY, launch.SeekingTargetRange,
  1691. spell.ExpectTarget, SeekingExpect.Nearest, null);
  1692. if (target != null)
  1693. {
  1694. launcher.faceTo(target.X, target.Y);
  1695. launcher.SendForceSync();
  1696. targetPos = new Vector2(target.X, target.Y);
  1697. //Console.WriteLine("-------------targetPos 1 - " + target.X + ", " + target.Y);
  1698. //Console.WriteLine("-------------targetPos 2 - " + targetPos);
  1699. int[] index = { 0, -1, 1 };
  1700. float testAngle = (float)Math.Atan2(targetPos.Y - launcher.Y, targetPos.X - launcher.X);
  1701. float xBase = 3.0f;
  1702. float xAdd = (float)(xBase * Math.Sin(testAngle)); //角Dir的对边
  1703. float yAdd = (float)(xBase * Math.Cos(testAngle)); //Dir的邻边
  1704. for (int i = 0; i < launch.Count; i++)
  1705. {
  1706. float tempX = startX - xAdd * index[i];
  1707. float tempY = startY + yAdd * index[i];
  1708. float angle = (float)Math.Atan2(targetPos.Y - tempY, targetPos.X - tempX);
  1709. AddSpell(fromSkillType, spell, launch, launcher, launcher,
  1710. targetUnitID, targetPos, tempX, tempY,
  1711. angle, chain, actionIndex, maxAffectUnit);
  1712. }
  1713. }
  1714. else
  1715. {
  1716. int[] index = { 0, -1, 1 };
  1717. float xBase = 3.0f;
  1718. float xAdd = (float)(xBase * Math.Sin(direction)); //角Dir的对边
  1719. float yAdd = (float)(xBase * Math.Cos(direction)); //Dir的邻边
  1720. float[] startAngleTemp = { 0, launch.Angle, launch.Angle * 1.2f, launch.Angle * 1.4f, launch.Angle * 1.5f };
  1721. for (int i = 0; i < launch.Count; i++)
  1722. {
  1723. float tempX = startX - xAdd * index[i];
  1724. float tempY = startY + yAdd * index[i];
  1725. AddSpell(fromSkillType, spell, launch, launcher, launcher,
  1726. targetUnitID, targetPos, tempX, tempY,
  1727. direction - startAngleTemp[i] * index[i], chain, actionIndex, maxAffectUnit);
  1728. }
  1729. }
  1730. }
  1731. break;
  1732. case LaunchSpell.PosType.POS_TYPE_CYCLE:
  1733. {
  1734. float startAngle = direction + launch.StartAngle;
  1735. float interAngle = CMath.PI_MUL_2 / launch.Count;
  1736. InstanceSpell[] spellBrothers = null;
  1737. if (launch.Count > 1 && spell.MType == SpellTemplate.MotionType.Foxfire)
  1738. {
  1739. spellBrothers = new InstanceSpell[launch.Count];
  1740. }
  1741. for (int i = 0; i < launch.Count; i++)
  1742. {
  1743. var sb = AddSpell(fromSkillType, spell, launch, launcher, launcher,
  1744. targetUnitID, targetPos, startX, startY,
  1745. startAngle + interAngle * i, chain, actionIndex, maxAffectUnit);
  1746. if (spellBrothers != null)
  1747. {
  1748. spellBrothers[i] = sb;
  1749. sb.brothers = spellBrothers;
  1750. }
  1751. }
  1752. }
  1753. break;
  1754. case LaunchSpell.PosType.POS_TYPE_X:
  1755. {
  1756. float startAngle = direction + launch.StartAngle;
  1757. float[] interAngle = { launch.Angle, CMath.PI_F - launch.Angle, CMath.PI_F + launch.Angle, CMath.PI_F * 2 - launch.Angle };
  1758. Dictionary<uint, InstanceUnit> damageList = (spell.HitIntervalMS <= 0 ? new Dictionary<uint, InstanceUnit>() : null);
  1759. for (int i = 0; i < 4; i++)
  1760. {
  1761. AddSpell(fromSkillType, spell, launch, launcher, launcher, targetUnitID, targetPos,
  1762. startX, startY, startAngle + interAngle[i], chain, actionIndex, maxAffectUnit, damageList);
  1763. }
  1764. }
  1765. break;
  1766. case LaunchSpell.PosType.POS_TYPE_RANDOM_DIRECTION:
  1767. {
  1768. for (int i = 0; i < launch.Count; i++)
  1769. {
  1770. float d = (float)(random.NextDouble() * CMath.PI_MUL_2);
  1771. AddSpell(fromSkillType, spell, launch, launcher,
  1772. launcher, targetUnitID, targetPos, startX,
  1773. startY, d, chain, actionIndex, maxAffectUnit);
  1774. }
  1775. }
  1776. break;
  1777. case LaunchSpell.PosType.POS_TYPE_AREA:
  1778. using (var enemy_list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1779. {
  1780. SeekSpellAttackable(enemy_list, startX, startY, launcher, spell, launch, chain);
  1781. for (int i = 0; i < launch.Count && i < enemy_list.Count; i++)
  1782. {
  1783. AddSpell(fromSkillType, spell, launch, launcher, launcher, enemy_list[i].ID, null,
  1784. startX, startY, direction, chain, actionIndex, maxAffectUnit);
  1785. }
  1786. }
  1787. break;
  1788. case LaunchSpell.PosType.POS_TYPE_RANDOM_FOR_SPELL:
  1789. launch_randomTypeSpell(fromSkillType, spell, launcher, launch, chain, launcher, actionIndex);
  1790. break;
  1791. case LaunchSpell.PosType.POS_TYPE_RANDOM_POS:
  1792. launch_randomPosSpell(fromSkillType, spell, launcher, launch, chain, launcher, actionIndex);
  1793. break;
  1794. case LaunchSpell.PosType.POS_TYPE_MANY_CANNON:
  1795. launch_ManyCannon(fromSkillType, spell, launcher, launch, chain, launcher, targetUnitID, actionIndex, createData);
  1796. break;
  1797. case LaunchSpell.PosType.POS_TYPE_DEFAULT_SINGLE:
  1798. default:
  1799. {
  1800. AddSpell(fromSkillType, spell, launch, launcher, launcher,
  1801. targetUnitID, targetPos, startX, startY,
  1802. direction + launch.StartAngle, chain, actionIndex, maxAffectUnit);
  1803. }
  1804. break;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. // 单位释放法术
  1810. public void spellLaunchSpell(
  1811. XmdsSkillType fromSkillType,
  1812. InstanceSpell sender,
  1813. LaunchSpell launch,
  1814. float startX,
  1815. float startY,
  1816. uint targetUnitID = 0,
  1817. Vector2 targetPos = null, int actionIndex = -1, int maxAffectUnit = 0)
  1818. {
  1819. switch (launch.SenderUnit)
  1820. {
  1821. case LaunchSpell.LaunchSpllSenderUnit.Launcher:
  1822. this.unitLaunchSpell(fromSkillType, sender.Launcher, launch, startX, startY, targetUnitID, targetPos);
  1823. return;
  1824. }
  1825. if (CUtils.RandomPercent(RandomN, launch.LaunchPercent))
  1826. {
  1827. SpellChainLevelInfo chain = sender.ChainInfo;
  1828. if (chain != null)
  1829. {
  1830. if (!chain.TryLaunch(launch.SpellID))
  1831. {
  1832. // chain is end //
  1833. return;
  1834. }
  1835. }
  1836. SpellTemplate spell = Templates.getSpell(launch.SpellID);
  1837. JSGCreateSpellData createData;
  1838. if (spell != null && launch.Count > 0 && mFormula.TryLaunchSpell(sender.Launcher, launch, ref spell, out createData, ref startX, ref startY))
  1839. {
  1840. float direction = sender.Direction;
  1841. if (targetPos != null)
  1842. {
  1843. direction = MathVector.getDegree(startX, startY, targetPos.X, targetPos.Y);
  1844. targetPos = (Vector2)targetPos.Clone();
  1845. }
  1846. switch (launch.PType)
  1847. {
  1848. case LaunchSpell.PosType.POS_TYPE_FAN:
  1849. {
  1850. float startAngle = direction - launch.Angle / 2f + launch.StartAngle;
  1851. float interAngle = launch.Count > 0 ? launch.Angle / (launch.Count - 1) : 0;
  1852. for (int i = 0; i < launch.Count; i++)
  1853. {
  1854. AddSpell(fromSkillType, spell, launch, sender, sender.Launcher,
  1855. targetUnitID, targetPos, startX, startY,
  1856. startAngle + interAngle * i, chain, actionIndex, maxAffectUnit);
  1857. }
  1858. }
  1859. break;
  1860. case LaunchSpell.PosType.POS_TYPE_CYCLE:
  1861. {
  1862. float startAngle = direction + launch.StartAngle;
  1863. float interAngle = CMath.PI_MUL_2 / launch.Count;
  1864. for (int i = 0; i < launch.Count; i++)
  1865. {
  1866. AddSpell(fromSkillType, spell, launch, sender, sender.Launcher,
  1867. targetUnitID, targetPos, startX, startY,
  1868. startAngle + interAngle * i, chain, actionIndex, maxAffectUnit);
  1869. }
  1870. }
  1871. break;
  1872. case LaunchSpell.PosType.POS_TYPE_X:
  1873. {
  1874. float startAngle = direction + launch.StartAngle;
  1875. float[] interAngle = { launch.Angle, CMath.PI_F - launch.Angle, CMath.PI_F + launch.Angle, CMath.PI_F * 2 - launch.Angle };
  1876. Dictionary<uint, InstanceUnit> damageList = (spell.HitIntervalMS <= 0 ? new Dictionary<uint, InstanceUnit>() : null);
  1877. for (int i = 0; i < launch.Count; i++)
  1878. {
  1879. AddSpell(fromSkillType, spell, launch, sender, sender.Launcher, targetUnitID,
  1880. targetPos, startX, startY, startAngle + interAngle[i], chain, actionIndex, maxAffectUnit, damageList);
  1881. }
  1882. }
  1883. break;
  1884. case LaunchSpell.PosType.POS_TYPE_RANDOM_FOR_SPELL:
  1885. launch_randomTypeSpell(fromSkillType, spell, sender, launch, chain, sender.Launcher, actionIndex);
  1886. break;
  1887. case LaunchSpell.PosType.POS_TYPE_MANY_CANNON:
  1888. launch_ManyCannon(fromSkillType, spell, sender, launch, chain, sender.Launcher, targetUnitID, actionIndex, createData);
  1889. break;
  1890. case LaunchSpell.PosType.POS_TYPE_RANDOM_POS:
  1891. launch_randomPosSpell(fromSkillType, spell, sender, launch, chain, sender.Launcher, actionIndex);
  1892. break;
  1893. case LaunchSpell.PosType.POS_TYPE_RANDOM_DIRECTION:
  1894. {
  1895. for (int i = 0; i < launch.Count; i++)
  1896. {
  1897. float d = (float)(random.NextDouble() * CMath.PI_MUL_2);
  1898. AddSpell(fromSkillType, spell, launch, sender, sender.Launcher,
  1899. targetUnitID, targetPos, startX, startY, d, chain, actionIndex, maxAffectUnit);
  1900. }
  1901. }
  1902. break;
  1903. case LaunchSpell.PosType.POS_TYPE_AREA:
  1904. using (var enemy_list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1905. {
  1906. SeekSpellAttackable(enemy_list, startX, startY, sender.Launcher, spell, launch, chain);
  1907. for (int i = 0; i < launch.Count && i < enemy_list.Count; i++)
  1908. {
  1909. AddSpell(fromSkillType, spell, launch, sender, sender.Launcher,
  1910. enemy_list[i].ID, targetPos, startX, startY, direction, chain, actionIndex, maxAffectUnit);
  1911. }
  1912. }
  1913. break;
  1914. case LaunchSpell.PosType.POS_TYPE_DEFAULT_SINGLE:
  1915. default:
  1916. {
  1917. AddSpell(fromSkillType, spell, launch, sender, sender.Launcher,
  1918. targetUnitID, targetPos, startX, startY, direction, chain, actionIndex, maxAffectUnit);
  1919. }
  1920. break;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. public void attackLaunchSpell(
  1926. XmdsSkillType fromSkillType,
  1927. InstanceUnit attacker,
  1928. InstanceUnit damage,
  1929. AttackSource source,
  1930. int serverExt = 0)
  1931. {
  1932. LaunchSpell launch = source.Attack.Spell;
  1933. if (launch == null)
  1934. return;
  1935. InstanceZoneObject sender;
  1936. if (source.FromSpellUnit != null)
  1937. {
  1938. sender = source.FromSpellUnit;
  1939. }
  1940. else
  1941. {
  1942. sender = attacker;
  1943. }
  1944. switch (launch.SenderUnit)
  1945. {
  1946. case LaunchSpell.LaunchSpllSenderUnit.Launcher:
  1947. sender = attacker;
  1948. break;
  1949. case LaunchSpell.LaunchSpllSenderUnit.DamagedUnit:
  1950. sender = damage;
  1951. break;
  1952. }
  1953. if (CUtils.RandomPercent(RandomN, launch.LaunchPercent))
  1954. {
  1955. float startX = damage.X;
  1956. float startY = damage.Y;
  1957. SpellTemplate spell = Templates.getSpell(launch.SpellID);
  1958. JSGCreateSpellData createData;
  1959. if (spell != null && launch.Count > 0 && mFormula.TryLaunchSpell(attacker, launch, ref spell, out createData, ref startX, ref startY))
  1960. {
  1961. SpellChainLevelInfo chain = null;
  1962. if (source.FromSpellUnit != null && source.FromSpellUnit.ChainInfo != null)
  1963. {
  1964. chain = source.FromSpellUnit.ChainInfo;
  1965. if (!chain.TryLaunch(launch.SpellID))
  1966. {
  1967. // chain is end //
  1968. return;
  1969. }
  1970. }
  1971. switch (launch.PType)
  1972. {
  1973. case LaunchSpell.PosType.POS_TYPE_FAN:
  1974. {
  1975. float startAngle = sender.Direction - launch.Angle / 2f + launch.StartAngle;
  1976. float interAngle = launch.Count > 0 ? launch.Angle / (launch.Count - 1) : 0;
  1977. for (int i = 0; i < launch.Count; i++)
  1978. {
  1979. AddSpell(fromSkillType, spell, launch, sender, attacker, damage.ID, null,
  1980. startX, startY, startAngle + interAngle * i, chain, serverExt);
  1981. }
  1982. }
  1983. break;
  1984. case LaunchSpell.PosType.POS_TYPE_CYCLE:
  1985. {
  1986. float startAngle = sender.Direction + launch.StartAngle;
  1987. float interAngle = CMath.PI_MUL_2 / launch.Count;
  1988. for (int i = 0; i < launch.Count; i++)
  1989. {
  1990. AddSpell(fromSkillType, spell, launch, sender, attacker, damage.ID, null,
  1991. startX, startY, startAngle + interAngle * i, chain, serverExt);
  1992. }
  1993. }
  1994. break;
  1995. case LaunchSpell.PosType.POS_TYPE_X:
  1996. {
  1997. float startAngle = sender.Direction + launch.StartAngle;
  1998. float[] interAngle = { launch.Angle, CMath.PI_F - launch.Angle, CMath.PI_F + launch.Angle, CMath.PI_F * 2 - launch.Angle };
  1999. Dictionary<uint, InstanceUnit> damageList = (spell.HitIntervalMS <= 0 ? new Dictionary<uint, InstanceUnit>() : null);
  2000. for (int i = 0; i < launch.Count; i++)
  2001. {
  2002. AddSpell(fromSkillType, spell, launch, sender, attacker, damage.ID, null,
  2003. startX, startY, startAngle + interAngle[i], chain, serverExt, 0, damageList);
  2004. }
  2005. }
  2006. break;
  2007. case LaunchSpell.PosType.POS_TYPE_RANDOM_FOR_SPELL:
  2008. launch_randomTypeSpell(fromSkillType, spell, sender, launch, chain, attacker, serverExt);
  2009. break;
  2010. case LaunchSpell.PosType.POS_TYPE_MANY_CANNON:
  2011. launch_ManyCannon(fromSkillType, spell, sender, launch, chain, attacker, damage.ID, serverExt, createData);
  2012. break;
  2013. case LaunchSpell.PosType.POS_TYPE_RANDOM_POS:
  2014. launch_randomPosSpell(fromSkillType, spell, sender, launch, chain, attacker, serverExt);
  2015. break;
  2016. case LaunchSpell.PosType.POS_TYPE_RANDOM_DIRECTION:
  2017. {
  2018. for (int i = 0; i < launch.Count; i++)
  2019. {
  2020. float d = (float)(random.NextDouble() * CMath.PI_MUL_2);
  2021. AddSpell(fromSkillType, spell, launch, sender, attacker, damage.ID, null,
  2022. startX, startY, d, chain, serverExt);
  2023. }
  2024. }
  2025. break;
  2026. case LaunchSpell.PosType.POS_TYPE_AREA:
  2027. using (var enemy_list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2028. {
  2029. SeekSpellAttackable(enemy_list, startX, startY, attacker, spell, launch, chain);
  2030. for (int i = 0; i < launch.Count && i < enemy_list.Count; i++)
  2031. {
  2032. AddSpell(fromSkillType, spell, launch, sender, attacker, enemy_list[i].ID, null,
  2033. startX, startY, 0, chain, serverExt);
  2034. }
  2035. }
  2036. break;
  2037. case LaunchSpell.PosType.POS_TYPE_DEFAULT_SINGLE:
  2038. default:
  2039. {
  2040. AddSpell(fromSkillType, spell, launch, sender, attacker, damage.ID, null,
  2041. startX, startY, sender.Direction, chain, serverExt);
  2042. }
  2043. break;
  2044. }
  2045. }
  2046. }
  2047. }
  2048. private void launch_randomTypeSpell(XmdsSkillType fromSkillType, SpellTemplate spell, InstanceZoneObject sender,
  2049. LaunchSpell launch, SpellChainLevelInfo chain, InstanceUnit attacker, int serverExt = 0)
  2050. {
  2051. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2052. {
  2053. attacker.Parent.getObjectsRoundRange<InstanceUnit>(
  2054. (obj, dx, dy, dr) =>
  2055. {
  2056. var u = obj as InstanceUnit;
  2057. //己方单位.
  2058. if ((attacker.IsPlayer && !u.IsPlayer) || (!attacker.IsPlayer && u.IsPlayer))
  2059. {
  2060. return CMath.includeRoundPoint(dx, dy, dr, u.X, u.Y);
  2061. }
  2062. return false;
  2063. },
  2064. attacker.X,
  2065. attacker.Y,
  2066. launch.SeekingTargetRange,
  2067. list, attacker.AoiStatus);
  2068. int maxCount = launch.Count == 0 ? list.Count : Math.Min(launch.Count, list.Count);
  2069. for (int i = 0; i < maxCount; i++)
  2070. {
  2071. float r = (float)(random.NextDouble() * spell.BodySize);
  2072. float a = (float)(random.NextDouble() * CMath.PI_MUL_2);
  2073. float x = (float)(list[i].X + Math.Cos(a) * r);
  2074. float y = (float)(list[i].Y + Math.Sin(a) * r);
  2075. float d = (float)(random.NextDouble() * CMath.PI_MUL_2);
  2076. AddSpell(fromSkillType, spell, launch, sender, attacker, 0, null,
  2077. x, y, d, chain, serverExt);
  2078. }
  2079. }
  2080. }
  2081. private void launch_randomPosSpell(XmdsSkillType fromSkillType, SpellTemplate spell, InstanceZoneObject sender,
  2082. LaunchSpell launch, SpellChainLevelInfo chain, InstanceUnit attacker, int serverExt = 0)
  2083. {
  2084. for (int i = 0; i < launch.Count; i++)
  2085. {
  2086. float x = (float)(random.NextDouble() * launch.SpellRange/2);
  2087. float y = (float)(random.NextDouble() * launch.SpellRange/2);
  2088. float d = (float)(random.NextDouble() * CMath.PI_MUL_2);
  2089. int randX = random.Next() % 2 == 0 ? 1 : -1;
  2090. int randY = random.Next() % 2 == 0 ? 1 : -1;
  2091. AddSpell(fromSkillType, spell, launch, sender, attacker, 0, null,
  2092. sender.X + x * randX, sender.Y + y * randY, d, chain, serverExt);
  2093. }
  2094. }
  2095. public InstanceSpell launch_ManyCannon(XmdsSkillType fromSkillType, SpellTemplate spell, InstanceZoneObject sender,
  2096. LaunchSpell launch, SpellChainLevelInfo chain, InstanceUnit attacker, uint targetUnitID, int serverExt, JSGCreateSpellData createData)
  2097. {
  2098. //float angleOffset = targetUnitID == 0 ? launch.StartAngle / 3 : launch.StartAngle;
  2099. float angleOffset = JSGModule.GetCurveStartAngleOffect(attacker, getUnit(targetUnitID), launch.StartAngle, spell.SeekingRange);
  2100. float startAngle = CMath.OpitimizeRadians(attacker.Direction) + angleOffset;
  2101. float launchX = attacker.X - (float)(1.0f * Math.Cos(attacker.Direction));
  2102. float launchY = attacker.Y - (float)(1.0f * Math.Sin(attacker.Direction));
  2103. Vector2 targetPos = JSGModule.GetCurveDefaultTargetPos(attacker, spell.SeekingRange);
  2104. //System.Console.WriteLine("targetPos: " + targetPos.X + ", " + targetPos.Y);
  2105. int totalCount = createData == null ? launch.Count : createData.mMaxSpellCount;
  2106. return AddSpell(fromSkillType, spell, launch, sender, attacker, targetUnitID, targetPos,
  2107. launchX, launchY, startAngle, chain, serverExt, 0, null, totalCount-1, createData);
  2108. }
  2109. public void spellSummonUnit(InstanceSpell summoner, SummonUnit summon)
  2110. {
  2111. float x = summoner.X;
  2112. float y = summoner.Y;
  2113. float radius = summoner.BodyBlockSize;
  2114. UnitInfo info = Templates.getUnit(summon.UnitTemplateID);
  2115. if (info.UType != UnitType.TYPE_SUMMON || info.LifeTimeMS <= 0)
  2116. {
  2117. log.Warn("召唤非召唤类单位1:" + summoner.ID + ", 召唤id: " + summon.UnitTemplateID);
  2118. return;
  2119. }
  2120. //UnitInfo un = (UnitInfo)info.Clone();
  2121. //un.UType = UnitInfo.UnitType.TYPE_SUMMON;
  2122. string name = null;
  2123. if (TemplateManager.Formula.TrySummonUnit(summoner.Launcher, summon, ref info, ref name))
  2124. {
  2125. for (int i = 0; i < summon.Count; i++)
  2126. {
  2127. float dx, dy, dr;
  2128. //float angle = (float)RandomN.NextDouble() * CMath.PI_MUL_2;
  2129. //float lengt = (float)RandomN.NextDouble() * radius;
  2130. //dx = x + (float)Math.Cos(angle) * lengt;
  2131. //dy = y + (float)Math.Sin(angle) * lengt;
  2132. //dr = (float)RandomN.NextDouble() * CMath.PI_MUL_2;
  2133. //召唤单位时判断当前位置能否行走,防止被召唤单位卡在地图里.Editor by Alex.
  2134. var pos = PathFinder.FindNearRandomMoveableNode(RandomN, x, y, radius, true);
  2135. if (pos == null)
  2136. {
  2137. dx = summoner.Launcher.X;
  2138. dy = summoner.Launcher.Y;
  2139. }
  2140. else
  2141. {
  2142. dx = pos.PosX;
  2143. dy = pos.PosY;
  2144. }
  2145. dr = summoner.Direction;
  2146. InstanceUnit unit = null;
  2147. if (!summoner.Launcher.IsActive)
  2148. {
  2149. unit = AddUnit(info, name, summoner.Launcher.Force, summon.UnitLevel, dx, dy, dr, null);
  2150. }
  2151. else
  2152. {
  2153. unit = AddUnit(info, name, summoner.Launcher.Force, summon.UnitLevel, dx, dy, dr, summoner.Launcher);
  2154. }
  2155. if (unit != null)
  2156. {
  2157. if (info.LifeTimeMS > 0)
  2158. {
  2159. AddTimeDelayMS(info.LifeTimeMS, (task) =>
  2160. {
  2161. unit.kill();
  2162. });
  2163. }
  2164. if (summon.Effect != null)
  2165. {
  2166. queueEvent(new AddEffectEvent(unit.ID, dx, dy, dr, summon.Effect));
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. public void unitSummonUnit(InstanceUnit summoner, SummonUnit summon)
  2173. {
  2174. float x = summoner.X;
  2175. float y = summoner.Y;
  2176. MathVector.movePolar(ref x, ref y, summoner.Direction, summoner.BodyBlockSize * 4);
  2177. float radius = summoner.BodyBlockSize * 2;
  2178. UnitInfo info = Templates.getUnit(summon.UnitTemplateID);
  2179. if (info.UType != UnitType.TYPE_SUMMON || info.LifeTimeMS <= 0)
  2180. {
  2181. log.Warn("召唤非召唤类单位2:" + summoner.ID + ", 召唤id: " + summon.UnitTemplateID);
  2182. return;
  2183. }
  2184. //UnitInfo un = (UnitInfo)info.Clone();
  2185. //un.UType = UnitInfo.UnitType.TYPE_SUMMON;
  2186. string name = null;
  2187. if (TemplateManager.Formula.TrySummonUnit(summoner, summon, ref info, ref name))
  2188. {
  2189. for (int i = 0; i < summon.Count; i++)
  2190. {
  2191. float angle = (float)RandomN.NextDouble() * CMath.PI_MUL_2;
  2192. float lengt = (float)RandomN.NextDouble() * radius;
  2193. float dx = x + (float)Math.Cos(angle) * lengt;
  2194. float dy = y + (float)Math.Sin(angle) * lengt;
  2195. float dr = (float)RandomN.NextDouble() * CMath.PI_MUL_2;
  2196. InstanceUnit unit = AddUnit(info, name, summoner.Force, summon.UnitLevel, dx, dy, dr, summoner);
  2197. if (unit != null)
  2198. {
  2199. if (info.LifeTimeMS > 0)
  2200. {
  2201. AddTimeDelayMS(info.LifeTimeMS, (task) =>
  2202. {
  2203. unit.kill();
  2204. });
  2205. }
  2206. if (summon.Effect != null)
  2207. {
  2208. queueEvent(new AddEffectEvent(unit.ID, dx, dy, dr, summon.Effect));
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. public InstanceUnit summonUnit(InstanceUnit summoner, UnitInfo info, string name, int level, float x, float y, float direction)
  2215. {
  2216. if (info.UType != UnitType.TYPE_SUMMON || info.LifeTimeMS <= 0)
  2217. {
  2218. log.Warn("召唤非召唤类单位3:" + summoner.ID + ", 召唤id: " + info.ID);
  2219. return null;
  2220. }
  2221. InstanceUnit unit = AddUnit(info, name, summoner.Force, level, x, y, direction, summoner);
  2222. if (unit != null)
  2223. {
  2224. if (info.LifeTimeMS > 0)
  2225. {
  2226. AddTimeDelayMS(info.LifeTimeMS, (task) =>
  2227. {
  2228. unit.kill();
  2229. });
  2230. }
  2231. }
  2232. return unit;
  2233. }
  2234. /// <summary>
  2235. /// 锁定范围内指定目标
  2236. /// </summary>
  2237. /// <param name="spell"></param>
  2238. /// <param name="X"></param>
  2239. /// <param name="Y"></param>
  2240. /// <param name="range"></param>
  2241. /// <param name="expectTarget"></param>
  2242. /// <param name="expectSeeking"></param>
  2243. /// <param name="chain"></param>
  2244. /// <returns></returns>
  2245. public virtual InstanceUnit SeekSpellAttackable(
  2246. InstanceUnit launcher,
  2247. SpellTemplate spellData,
  2248. ObjectAoiStatus aoiStatus,
  2249. float X, float Y, float range,
  2250. SkillTemplate.CastTarget expectTarget,
  2251. SpellTemplate.SeekingExpect expectSeeking,
  2252. SpellChainLevelInfo chain)
  2253. {
  2254. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2255. {
  2256. getObjectsRoundRange<InstanceUnit>((InstanceZoneObject o, float x, float y, float r) =>
  2257. {
  2258. InstanceUnit u = o as InstanceUnit;
  2259. if (chain != null)
  2260. {
  2261. switch (expectSeeking)
  2262. {
  2263. case SpellTemplate.SeekingExpect.RandomIgnoreInChain:
  2264. case SpellTemplate.SeekingExpect.NearestIgnoreInChain:
  2265. case SpellTemplate.SeekingExpect.FarthestIgnoreInChain:
  2266. if (chain.ContainsTarget(u))
  2267. {
  2268. return false;
  2269. }
  2270. break;
  2271. default:
  2272. if (chain.LastTarget == u)
  2273. {
  2274. return false;
  2275. }
  2276. break;
  2277. }
  2278. }
  2279. if (IsAttackable(launcher, u, expectTarget, AttackReason.Look, spellData))
  2280. {
  2281. return CMath.intersectRound(x, y, r, o.X, o.Y, o.BodyHitSize);
  2282. }
  2283. return false;
  2284. }, X, Y, range, list, aoiStatus);
  2285. switch (expectSeeking)
  2286. {
  2287. case SpellTemplate.SeekingExpect.Random:
  2288. case SpellTemplate.SeekingExpect.RandomIgnoreInChain:
  2289. CUtils.RandomList(RandomN, list);
  2290. break;
  2291. case SpellTemplate.SeekingExpect.Nearest:
  2292. case SpellTemplate.SeekingExpect.NearestIgnoreInChain:
  2293. list.Sort(new ObjectSorterNearest<InstanceUnit>(X, Y));
  2294. break;
  2295. case SpellTemplate.SeekingExpect.Farthest:
  2296. case SpellTemplate.SeekingExpect.FarthestIgnoreInChain:
  2297. list.Sort(new ObjectSorterFarthest<InstanceUnit>(X, Y));
  2298. break;
  2299. }
  2300. if (list.Count > 0)
  2301. {
  2302. return list[0];
  2303. }
  2304. }
  2305. return null;
  2306. }
  2307. public virtual void SeekSpellAttackable(List<InstanceUnit> ret, float X, float Y,
  2308. InstanceUnit Launcher,
  2309. SpellTemplate spell,
  2310. LaunchSpell launch,
  2311. SpellChainLevelInfo chain)
  2312. {
  2313. getObjectsRoundRange<InstanceUnit>((InstanceZoneObject o, float x, float y, float r) =>
  2314. {
  2315. InstanceUnit u = o as InstanceUnit;
  2316. if (chain != null)
  2317. {
  2318. switch (launch.SeekingTargetExpect)
  2319. {
  2320. case SpellTemplate.SeekingExpect.RandomIgnoreInChain:
  2321. case SpellTemplate.SeekingExpect.NearestIgnoreInChain:
  2322. case SpellTemplate.SeekingExpect.FarthestIgnoreInChain:
  2323. if (chain.ContainsTarget(u))
  2324. {
  2325. return false;
  2326. }
  2327. break;
  2328. default:
  2329. if (chain.LastTarget == u)
  2330. {
  2331. return false;
  2332. }
  2333. break;
  2334. }
  2335. }
  2336. if (IsAttackable(Launcher, u, spell.ExpectTarget, AttackReason.Look, spell))
  2337. {
  2338. return CMath.intersectRound(x, y, r, o.X, o.Y, o.BodyHitSize);
  2339. }
  2340. return false;
  2341. }, X, Y, launch.SeekingTargetRange, ret, Launcher.AoiStatus);
  2342. switch (launch.SeekingTargetExpect)
  2343. {
  2344. case SpellTemplate.SeekingExpect.Random:
  2345. case SpellTemplate.SeekingExpect.RandomIgnoreInChain:
  2346. CUtils.RandomList(RandomN, ret);
  2347. break;
  2348. case SpellTemplate.SeekingExpect.Nearest:
  2349. case SpellTemplate.SeekingExpect.NearestIgnoreInChain:
  2350. ret.Sort(new ObjectSorterNearest<InstanceUnit>(X, Y));
  2351. break;
  2352. case SpellTemplate.SeekingExpect.Farthest:
  2353. case SpellTemplate.SeekingExpect.FarthestIgnoreInChain:
  2354. ret.Sort(new ObjectSorterFarthest<InstanceUnit>(X, Y));
  2355. break;
  2356. }
  2357. }
  2358. /**游戏服场景标识通知*/
  2359. public virtual void GSZoneFlagNotifyMsg(int value1)
  2360. {
  2361. }
  2362. #endregion
  2363. //-------------------------------------------------------------------------------------------------------//
  2364. #region EDITOR_AND_SCRIPT
  2365. readonly private HashMap<string, InstanceFlag> mFlags = new HashMap<string, InstanceFlag>();
  2366. private bool mHasArea = false;
  2367. protected bool addFlag(InstanceFlag flag)
  2368. {
  2369. if (!mFlags.ContainsKey(flag.Name))
  2370. {
  2371. mFlags.Add(flag.Name, flag);
  2372. flag.onAdded();
  2373. if (flag is ZoneArea)
  2374. {
  2375. mHasArea = true;
  2376. }
  2377. return true;
  2378. }
  2379. return false;
  2380. }
  2381. public InstanceFlag getFlag(string name)
  2382. {
  2383. if(string.IsNullOrEmpty(name))
  2384. {
  2385. return null;
  2386. }
  2387. return mFlags.Get(name);
  2388. }
  2389. public T getFlagAs<T>(string name) where T : InstanceFlag
  2390. {
  2391. return mFlags.Get(name) as T;
  2392. }
  2393. public bool setFlag(string name, bool enable)
  2394. {
  2395. InstanceFlag flag = getFlag(name);
  2396. if (flag != null)
  2397. {
  2398. flag.Enable = enable;
  2399. return true;
  2400. }
  2401. return false;
  2402. }
  2403. #endregion
  2404. //-------------------------------------------------------------------------------------------------------//
  2405. #region Environment
  2406. private HashMap<string, EnvironmentVar> EnvironmentVarMap = new HashMap<string, EnvironmentVar>();
  2407. public void AddEnvironmentVar(string key, object value, bool syncToClient)
  2408. {
  2409. if (!string.IsNullOrEmpty(key))
  2410. {
  2411. EnvironmentVar var = EnvironmentVarMap.Get(key);
  2412. if (var != null)
  2413. {
  2414. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || (var.SyncToClient && var.Value != value))
  2415. {
  2416. queueEvent(new SyncEnvironmentVarEvent(key, value));
  2417. }
  2418. var.Value = value;
  2419. }
  2420. else
  2421. {
  2422. var = new EnvironmentVar(key, syncToClient, value);
  2423. EnvironmentVarMap.Add(key, var);
  2424. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || var.SyncToClient)
  2425. {
  2426. queueEvent(new SyncEnvironmentVarEvent(key, value));
  2427. }
  2428. }
  2429. }
  2430. }
  2431. public void SetEnvironmentVar(string key, object value)
  2432. {
  2433. if (!string.IsNullOrEmpty(key))
  2434. {
  2435. EnvironmentVar var = EnvironmentVarMap.Get(key);
  2436. if (var != null)
  2437. {
  2438. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || (var.SyncToClient && var.Value != value))
  2439. {
  2440. queueEvent(new SyncEnvironmentVarEvent(key, value));
  2441. }
  2442. var.Value = value;
  2443. }
  2444. }
  2445. }
  2446. public T GetEnvironmentVarAs<T>(string key)
  2447. {
  2448. if (!string.IsNullOrEmpty(key))
  2449. {
  2450. EnvironmentVar var = EnvironmentVarMap.Get(key);
  2451. if (var != null)
  2452. {
  2453. try
  2454. {
  2455. return (T)var.Value;
  2456. }
  2457. catch (Exception err)
  2458. {
  2459. log.Warn("GetEnvironmentVarAs : " + key + ", catch: " + err);
  2460. }
  2461. }
  2462. }
  2463. return default(T);
  2464. }
  2465. public int ListEnvironmentVars(List<EnvironmentVar> list)
  2466. {
  2467. list.AddRange(EnvironmentVarMap.Values);
  2468. return EnvironmentVarMap.Count;
  2469. }
  2470. public ClientStruct.ZoneEnvironmentVar[] GetCurrentZoneVars()
  2471. {
  2472. ClientStruct.ZoneEnvironmentVar[] ret = new ClientStruct.ZoneEnvironmentVar[EnvironmentVarMap.Count];
  2473. int i = 0;
  2474. foreach (EnvironmentVar var in EnvironmentVarMap.Values)
  2475. {
  2476. ret[i] = new ClientStruct.ZoneEnvironmentVar();
  2477. ret[i].Key = var.Key;
  2478. ret[i].Value = var.Value;
  2479. ret[i].SyncToClient = var.SyncToClient;
  2480. i++;
  2481. }
  2482. return ret;
  2483. }
  2484. protected void BindZoneVar(ZoneVar var, BindValuesAdapter api)
  2485. {
  2486. if (string.IsNullOrEmpty(var.Key))
  2487. {
  2488. // Error
  2489. return;
  2490. }
  2491. if (var.Value is IEditorValue)
  2492. {
  2493. IEditorValue evalue = (var.Value as IEditorValue);
  2494. AddEnvironmentVar(var.Key, evalue.GetEnvValue(api), var.SyncToClient);
  2495. }
  2496. else if (var.Value is Array)
  2497. {
  2498. Array array = var.Value as Array;
  2499. for (int i = 0; i < array.Length; i++)
  2500. {
  2501. object e = array.GetValue(i);
  2502. if (e is IEditorValue)
  2503. {
  2504. string key = string.Format(var.Key + "[{0}]", i);
  2505. IEditorValue evalue = (e as IEditorValue);
  2506. AddEnvironmentVar(key, evalue.GetEnvValue(api), var.SyncToClient);
  2507. }
  2508. }
  2509. }
  2510. }
  2511. #endregion
  2512. //-------------------------------------------------------------------------------------------------------//
  2513. #region QUEST
  2514. /// <summary>
  2515. /// 【第三方系统通知】任务已接受
  2516. /// </summary>
  2517. /// <param name="playerUUID"></param>
  2518. /// <param name="quest"></param>
  2519. internal void gs_OnQuestAccepted(string playerUUID, string quest)
  2520. {
  2521. queueTask((InstanceZone zone) =>
  2522. {
  2523. InstancePlayer p = getPlayerByUUID(playerUUID);
  2524. if (p != null)
  2525. {
  2526. p.doQuestAccepted(quest);
  2527. if (mOnQuestAccepted != null)
  2528. mOnQuestAccepted.Invoke(p, quest);
  2529. }
  2530. });
  2531. }
  2532. /// <summary>
  2533. /// 【第三方系统通知】任务已完成
  2534. /// </summary>
  2535. /// <param name="playerUUID"></param>
  2536. /// <param name="quest"></param>
  2537. internal void gs_OnQuestCommitted(string playerUUID, string quest)
  2538. {
  2539. queueTask((InstanceZone zone) =>
  2540. {
  2541. InstancePlayer p = getPlayerByUUID(playerUUID);
  2542. if (p != null)
  2543. {
  2544. if (mOnQuestCommitted != null)
  2545. mOnQuestCommitted.Invoke(p, quest);
  2546. p.doQuestCommitted(quest);
  2547. }
  2548. });
  2549. }
  2550. /// <summary>
  2551. /// 【第三方系统通知】任务已放弃
  2552. /// </summary>
  2553. /// <param name="playerUUID"></param>
  2554. /// <param name="quest"></param>
  2555. internal void gs_OnQuestDropped(string playerUUID, string quest)
  2556. {
  2557. queueTask((InstanceZone zone) =>
  2558. {
  2559. InstancePlayer p = getPlayerByUUID(playerUUID);
  2560. if (p != null)
  2561. {
  2562. if (mOnQuestDropped != null)
  2563. mOnQuestDropped.Invoke(p, quest);
  2564. p.doQuestDropped(quest);
  2565. }
  2566. });
  2567. }
  2568. /// <summary>
  2569. /// 【第三方系统通知】任务状态已改变
  2570. /// </summary>
  2571. /// <param name="playerUUID"></param>
  2572. /// <param name="quest"></param>
  2573. /// <param name="key"></param>
  2574. /// <param name="value"></param>
  2575. internal void gs_OnQuestStatusChanged(string playerUUID, string quest, string key, string value)
  2576. {
  2577. queueTask((InstanceZone zone) =>
  2578. {
  2579. InstancePlayer p = getPlayerByUUID(playerUUID);
  2580. if (p != null)
  2581. {
  2582. p.doQuestStatusChanged(quest, key, value);
  2583. if (mOnQuestStatusChanged != null)
  2584. mOnQuestStatusChanged.Invoke(p, quest, key, value);
  2585. }
  2586. });
  2587. }
  2588. //给单个玩家发送消息
  2589. internal void doSendMsgToPlayer(InstancePlayer player, CommonLang.Protocol.IMessage msg)
  2590. {
  2591. mQuestAdapter.DoSendMsgToPlayer(player.PlayerUUID, msg);
  2592. }
  2593. /// <summary>
  2594. /// 本地通知游戏服
  2595. /// </summary>
  2596. /// <param name="player"></param>
  2597. /// <param name="quest"></param>
  2598. /// <param name="args"></param>
  2599. internal void doAcceptQuest(InstancePlayer player, string quest, string args)
  2600. {
  2601. mQuestAdapter.DoAcceptQuest(player.PlayerUUID, quest, args);
  2602. }
  2603. /// <summary>
  2604. /// 本地通知游戏服
  2605. /// </summary>
  2606. /// <param name="player"></param>
  2607. /// <param name="quest"></param>
  2608. /// <param name="args"></param>
  2609. internal void doCommitQuest(InstancePlayer player, string quest, string args)
  2610. {
  2611. mQuestAdapter.DoCommitQuest(player.PlayerUUID, quest, args);
  2612. }
  2613. /// <summary>
  2614. /// 本地通知游戏服
  2615. /// </summary>
  2616. /// <param name="player"></param>
  2617. /// <param name="quest"></param>
  2618. /// <param name="args"></param>
  2619. internal void doDropQuest(InstancePlayer player, string quest, string args)
  2620. {
  2621. mQuestAdapter.DoDropQuest(player.PlayerUUID, quest, args);
  2622. }
  2623. /// <summary>
  2624. /// 本地通知游戏服
  2625. /// </summary>
  2626. /// <param name="player"></param>
  2627. /// <param name="quest"></param>
  2628. /// <param name="key"></param>
  2629. /// <param name="value"></param>
  2630. internal void doUpdateQuestStatus(InstancePlayer player, string quest, string key, string value)
  2631. {
  2632. mQuestAdapter.DoUpdateQuestStatus(player.PlayerUUID, quest, key, value);
  2633. }
  2634. #endregion
  2635. //-------------------------------------------------------------------------------------------------------//
  2636. }
  2637. }