InstanceZone.cs 109 KB

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