XmdsZoneNode.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using CommonLang.Log;
  2. using CommonAI.Zone;
  3. using CommonAI.Zone.Instance;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Web.Helpers;
  7. using XmdsServerNode.Node;
  8. using XmdsCommon.Message;
  9. using XmdsCommon.Plugin;
  10. using XmdsCommonServer.Plugin;
  11. using XmdsCommonServer.Plugin.Units;
  12. using CommonAI.Zone.ZoneEditor;
  13. using XmdsCommon.EditorData;
  14. using CommonLang.Vector;
  15. using CommonLang;
  16. using static CommonAI.XmdsConstConfig;
  17. using CommonAI.Data;
  18. namespace XmdsServerEdgeJS.Zone
  19. {
  20. public class XmdsZoneNode
  21. {
  22. private readonly Logger log;
  23. private readonly ZoneService service;
  24. private readonly string instanceID;
  25. private readonly ServerZoneNode node;
  26. //连斩生效时间
  27. private long continueKillsValidTime = 0;
  28. public string InstanceID { get { return instanceID; } }
  29. public ServerZoneNode Node { get { return node; } }
  30. public void SendToGameServer(string name, Object param)
  31. {
  32. node.SendToGameServer(name, param);
  33. }
  34. public string GetBindGameSrvId()
  35. {
  36. return node.GetBindGameSrvId();
  37. }
  38. internal XmdsZoneNode(ZoneService svc, string instanceId, string gameServerId)
  39. {
  40. this.log = LoggerFactory.GetLogger(string.Format("XmdsZoneNode-{0}", instanceId));
  41. this.service = svc;
  42. this.instanceID = instanceId;
  43. this.node = new ServerZoneNode(gameServerId);
  44. }
  45. internal void Start(int mapTemplateId, string data, Action<XmdsZoneNode> started)
  46. {
  47. dynamic config = Json.Decode(data);
  48. this.node.OnZoneStart += (z) => { started(this); };
  49. string monsterHard = (string)config.monsterHard;
  50. byte sceneType = (byte)config.sceneType;
  51. byte areaType = (byte)config.areaType;
  52. int averageLevel = Convert.ToInt32(config.averageLevel);
  53. double propRatio = 0;
  54. double floorRatio = 0;
  55. if (averageLevel > 0)
  56. {
  57. propRatio = Convert.ToDouble(config.propRatio);
  58. floorRatio = Convert.ToDouble(config.floorRatio);
  59. }
  60. bool canRiding = true;
  61. bool isteam = config.isTeam == null ? false : (bool)config.isTeam;
  62. if (config.canRiding != null)
  63. {
  64. canRiding = config.canRiding;
  65. }
  66. int killInterval = (config.killInterval != null) ? (int)(config.killInterval) : 0;
  67. int killFullNum = (config.killFullNum != null) ? (int)(config.killFullNum) : 0;
  68. int killFullCollSec = (config.killFullCollSec != null) ? (int)(config.killFullCollSec) : 0;
  69. GSCreateAreaData gsData = new GSCreateAreaData();
  70. gsData.value1 = (config.value1 != null) ? ((int)(config.value1)) : 0;
  71. gsData.value2 = (config.value2 != null) ? ((int)(config.value2)) : 0;
  72. this.node.Start(instanceID, mapTemplateId, config.monsterHard, (byte)config.allowAutoGuard, config.calPKValue,
  73. averageLevel, floorRatio, propRatio, killInterval, killFullNum, killFullNum, config.usespaceDiv,
  74. (CommonAI.Data.SceneType)sceneType, gsData, (AreaType)areaType, isteam, canRiding);
  75. //this.node.Zone.OnUnitDead += zone_onUnitDead;
  76. //this.node.Zone.OnGameOver += zone_onGameOver;
  77. this.node.Zone.OnUnitGotInstanceItem += zone_onUnitGotInstanceItem;
  78. this.node.Zone.OnUnitPickUnit += zone_onUnitPickUnit;
  79. this.node.Zone.OnSendMessageToGS += zone_onSendMessageToGS;
  80. this.node.Zone.OnTryPickItem += zone_onTryPickItem;
  81. }
  82. internal void Stop(Action<XmdsZoneNode> stopped)
  83. {
  84. if (node.IsDisposed)
  85. {
  86. stopped(this);
  87. }
  88. else
  89. {
  90. node.OnZoneStop += (z) => { stopped(this); };
  91. node.Stop();
  92. }
  93. if(this.node != null && this.node.Zone != null)
  94. {
  95. //this.node.Zone.OnUnitDead -= zone_onUnitDead;
  96. //this.node.Zone.OnGameOver -= zone_onGameOver;
  97. this.node.Zone.OnUnitGotInstanceItem -= zone_onUnitGotInstanceItem;
  98. this.node.Zone.OnUnitPickUnit -= zone_onUnitPickUnit;
  99. this.node.Zone.OnSendMessageToGS -= zone_onSendMessageToGS;
  100. this.node.Zone.OnTryPickItem -= zone_onTryPickItem;
  101. }
  102. }
  103. //------------------------------------------------------------------------------------------------------
  104. private HashMap<int, TVector2> _BornPlace = null;
  105. private HashMap<String, TVector2> _BornPlaceExt = null;
  106. public TVector2 r2b_get_getBornPlace(int areaId, string pointId)
  107. {
  108. lock (this)
  109. {
  110. if (_BornPlace == null)
  111. {
  112. _BornPlace = new HashMap<int, TVector2>();
  113. _BornPlaceExt = new HashMap<string, TVector2>();
  114. List<DecorationData> Decorations = this.Node.SceneData.Decorations;
  115. foreach (DecorationData decoration in Decorations)
  116. {
  117. foreach (AbilityData ability in decoration.Abilities)
  118. {
  119. XmdsSceneTransportAbilityData sceneAbility = ability as XmdsSceneTransportAbilityData;
  120. if (sceneAbility != null)
  121. {
  122. if (!string.IsNullOrEmpty(sceneAbility.NextScenePosition))
  123. {
  124. string[] pos = sceneAbility.NextScenePosition.Split(new Char[] { ',' });
  125. TVector2 posInfo = new TVector2((float)Convert.ToSingle(pos[0]), (float)Convert.ToSingle(pos[1]));
  126. _BornPlace.Put(sceneAbility.NextSceneID, posInfo);
  127. _BornPlaceExt.Put(decoration.Name, posInfo);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. TVector2 ret;
  134. if (pointId != null && _BornPlaceExt.TryGetValue(pointId, out ret))
  135. {
  136. return ret;
  137. }
  138. else if (_BornPlace.TryGetValue(areaId, out ret))
  139. {
  140. return ret;
  141. }
  142. return new TVector2(0, 0);
  143. }
  144. }
  145. //------------------------------------------------------------------------------------------------------
  146. #region _场景内事件_
  147. private bool zone_onTryPickItem(InstanceZone zone, InstanceUnit unit, InstanceItem item)
  148. {
  149. XmdsInstancePlayer player = unit as XmdsInstancePlayer;
  150. XmdsItemProperties prop = item.Info.Properties as XmdsItemProperties;
  151. if (player != null && prop != null && prop.IsTaskItem)
  152. {
  153. List<CommonAI.Zone.Helper.QuestData> quests = player.GetQuests();
  154. for (int i = 0; i < quests.Count; i++)
  155. {
  156. var q = quests[i];
  157. var t = XmdsQuestData.GetTaskType(q);
  158. var targetId = XmdsQuestData.GetTargetID(q);
  159. var is_complete = XmdsQuestData.IsTargetComplete(q);
  160. if (!is_complete && t == (int)XmdsQuestData.TaskType.InterActiveItem && targetId == item.Info.TemplateID)
  161. {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. else
  168. {
  169. return true;
  170. }
  171. }
  172. private void zone_onSendMessageToGS(InstanceZone zone, CommonAI.ZoneServer.SendMessageB2R e)
  173. {
  174. var eventParam = new
  175. {
  176. instanceId = zone.UUID,
  177. eventName = "message",
  178. msg = e.Message
  179. };
  180. node.SendToGameServer(EventType.areaEvent.ToString(), eventParam);
  181. }
  182. private void zone_onUnitPickUnit(InstanceZone zone, InstanceUnit obj, InstanceUnit pickable)
  183. {
  184. if (obj is InstancePlayer)
  185. {
  186. string uuid = (obj as InstancePlayer).PlayerUUID;
  187. var eventParam = new
  188. {
  189. eventName = "interActiveItem",
  190. playerId = uuid,
  191. itemId = pickable.Info.TemplateID,
  192. x = obj.X,
  193. y = obj.Y
  194. };
  195. node.SendToGameServer(EventType.playerEvent.ToString(), eventParam);
  196. }
  197. }
  198. private void zone_onUnitGotInstanceItem(InstanceZone zone, InstanceUnit unit, InstanceItem item)
  199. {
  200. InstancePlayer player = (unit as InstancePlayer);
  201. string uuid = player.PlayerUUID;
  202. XmdsDropableInstanceItem it = item as XmdsDropableInstanceItem;
  203. XmdsItemProperties prop = it.Info.Properties as XmdsItemProperties;
  204. XmdsCommon.Message.DropItem dropitem = it.GenSyncInfo(false).ExtData as XmdsCommon.Message.DropItem;
  205. if (dropitem != null)
  206. {
  207. var eventParam = new
  208. {
  209. eventName = "pickItem",
  210. instanceId = zone.UUID,
  211. playerId = uuid,
  212. itemId = dropitem.ObjID,
  213. isGuard = player.IsGuard,
  214. objectId = item.Info.ID,
  215. //type = prop.ItemType,
  216. //templateID = it.Info.TemplateID
  217. };
  218. node.SendToGameServer(EventType.areaEvent.ToString(), eventParam);
  219. //u.queueEvent(new UnitGotInstanceItemEvent(u.ID, this.ID));
  220. }
  221. else
  222. {
  223. dynamic eventParam = new
  224. {
  225. eventName = "interActiveItem",
  226. playerId = uuid,
  227. itemId = item.Info.TemplateID,
  228. objId = item.ID,
  229. type = prop.ItemType,
  230. isRemove = item.IsPickAll(),
  231. x = item.X,
  232. y = item.Y
  233. };
  234. node.SendToGameServer(EventType.playerEvent.ToString(), eventParam);
  235. }
  236. }
  237. /// <summary>
  238. /// 单位死亡事件
  239. /// </summary>
  240. /// <param name="zone"></param>
  241. /// <param name="obj"></param>
  242. /// <param name="attacker"></param>
  243. private void zone_onUnitDead(InstanceZone zone, InstanceUnit obj, InstanceUnit attacker)
  244. {
  245. //玩家死亡为1,怪物死亡为0
  246. int type = 0;
  247. //玩家id
  248. string playerId = null;
  249. long pkTime = 0;
  250. int pkValue = 0;
  251. if (obj is XmdsInstancePlayer)
  252. {
  253. type = 1;//玩家.
  254. playerId = (obj as InstancePlayer).PlayerUUID;
  255. XmdsVirtual_Player virPlayer = obj.Virtual as XmdsVirtual_Player;
  256. pkTime = virPlayer.getPkTime();
  257. pkValue = virPlayer.getPkValue();
  258. }
  259. else if (obj is XmdsInstancePet && !(obj is XmdsInstanceSummonUnit))
  260. {
  261. type = 2;//宠物.
  262. var pet = obj as XmdsInstancePet;
  263. if (pet.Master != null && pet.Master is InstancePlayer)
  264. {
  265. playerId = (pet.Master as InstancePlayer).PlayerUUID;
  266. }
  267. }
  268. int attackType = 0;
  269. //如果最后一击是宠物,则找主人
  270. if (attacker is InstancePet)
  271. {
  272. attacker = (attacker as InstancePet).Master;
  273. attackType = 1;
  274. }
  275. else if (attacker is InstanceSummon)
  276. {
  277. attacker = (attacker as InstanceSummon).SummonerUnit;
  278. }
  279. else if (attacker is XmdsInstanceSummonUnit)
  280. {
  281. attacker = (attacker as XmdsInstanceSummonUnit).SummonerUnit;
  282. }
  283. InstancePlayer hitFinal = attacker as InstancePlayer;
  284. //击中列表
  285. List<string> playerIds = new List<string>(2);
  286. //玩家被杀,改玩家添加到列表
  287. if (type == 1)
  288. {
  289. playerIds.Add(playerId);
  290. }
  291. if (hitFinal != null)
  292. {
  293. playerIds.Add(hitFinal.PlayerUUID);
  294. }
  295. #region 怪物ID特殊逻辑.
  296. //怪物生成时会随机出更高级的怪物,能力ID都会变更,但模板ID却一样。游戏服取MonsterID查找掉落和经验.Edit by Alex.Yu.
  297. int tpID = obj.Info.TemplateID;
  298. //击杀助攻列表.
  299. List<string> atkAssistantList = null;
  300. if (type == 0 && obj is XmdsInstanceMonster)
  301. {
  302. tpID = (obj as XmdsInstanceMonster).MonsterID;
  303. if (tpID <= 0)
  304. {
  305. tpID = obj.Info.TemplateID;
  306. log.Info("zone_onUnitDead单位id异常:" + obj.Info.TemplateID + ", " + obj.Info.Name + ", obj: " + obj + ", " + zone.GetSceneID());
  307. }
  308. atkAssistantList = (obj.Virtual as XmdsVirtual).mHateSystem.GetAtkAssistantList();
  309. }
  310. else if (type == 1)
  311. {
  312. atkAssistantList = (obj.Virtual as XmdsVirtual).mHateSystem.GetAtkAssistantList();
  313. }
  314. // log.Warn("单位死亡事件====unitTemplateId=" + tpID + ", type=" + type + ", 死亡了...");
  315. // if (type != 2 && playerIds.Count == 0 && (atkAssistantList == null || atkAssistantList.Count == 0))
  316. // {
  317. // return;
  318. // }
  319. #endregion
  320. //log.Info("monster dead objId:" + obj.ID + " templateId:" + tpID + " type " + type);
  321. int aID = 0;
  322. int aQColor = 0;
  323. int aLevel = 0;
  324. string aName = "";
  325. string sceneType = "";
  326. if (attacker != null)
  327. {
  328. if (attacker is XmdsInstanceMonster)
  329. {
  330. MonsterVisibleDataB2C mvd = attacker.GenSyncUnitInfo().VisibleInfo as MonsterVisibleDataB2C;
  331. sceneType = (attacker.Virtual as XmdsVirtual).GetCurSceneType();
  332. aID = attacker.Info.TemplateID;
  333. aQColor = (int)mvd.Qcolor;
  334. aName = mvd.DisplayName;
  335. aLevel = attacker.GenSyncUnitInfo().Level;
  336. }
  337. else if (attacker.IsPlayerUnit && obj.IsMonster)
  338. {
  339. InstanceUnit attackerUnit = attacker.IsPet ? attacker.Virtual.GetMasterUnit() : attacker;
  340. if (attackerUnit != null)
  341. {
  342. attackerUnit.Virtual.DispatchKillOtherEvent(attackerUnit.Virtual, obj.Virtual);
  343. }
  344. else
  345. {
  346. log.Warn("zone_onUnitDead: 找不到人形攻击者:" + attacker.Info.ID + ", " + zone.GetSceneID());
  347. }
  348. }
  349. }
  350. //连斩数计算
  351. if (zone is EditorScene)
  352. {
  353. EditorScene scene = zone as EditorScene;
  354. if (scene != null && scene.Data.killInterval > 0 && attacker != null)
  355. {
  356. long curTime = CommonLang.CUtils.localTimeMS;
  357. if (continueKillsValidTime < curTime && attacker.Statistic != null &&
  358. curTime - attacker.Statistic.preKillTime < scene.Data.killInterval)
  359. {
  360. attacker.updateContinueKills();
  361. //如果连斩有上限,并且达到上限后有一定冷却时间
  362. if (scene.Data.killMax > 0 && scene.Data.killMaxCoolTime > 0 && attacker.Statistic.continueKills >= scene.Data.killMax)
  363. {
  364. attacker.Statistic.continueKills = 0;
  365. this.continueKillsValidTime = curTime + scene.Data.killMaxCoolTime * 1000;
  366. }
  367. }
  368. else
  369. {
  370. attacker.resetContinueKills();
  371. }
  372. attacker.Statistic.preKillTime = curTime;
  373. }
  374. }
  375. XmdsVirtual vir = (obj.Virtual as XmdsVirtual);
  376. XmdsHateSystem zhs = vir.GetHateSystem() as XmdsHateSystem;
  377. InstancePlayer belongPlayer = zhs.GetHeirs() as InstancePlayer;
  378. //Console.Write("attacker objId:" + aID + " aQColor:" + aQColor + " aName " + aName + " aLevel " + aLevel);
  379. var eventParam = new
  380. {
  381. eventName = "unitDead",
  382. playerIds = playerIds,
  383. areaId = zone.TerrainSrc.TemplateID,
  384. instanceId = zone.UUID,
  385. hitFinal = (hitFinal == null) ? null : hitFinal.PlayerUUID,
  386. belongPlayerId = (belongPlayer == null) ? null : belongPlayer.PlayerUUID,
  387. unitType = type,
  388. unitPlayerId = playerId,
  389. unitTemplateId = tpID,
  390. unitLevel = obj.Level,
  391. objId = obj.ID,
  392. posX = obj.get_x(),
  393. posY = obj.get_y(),
  394. attackerTemplateId = aID,
  395. attackerQColor = aQColor,
  396. attackerName = aName,
  397. attackerLevel = aLevel,
  398. attackerSceneType = sceneType,
  399. attackType = attackType,
  400. atkAssistantList = atkAssistantList,
  401. awardPlayer = zhs.GetSharedList(),
  402. refreshPoint = obj.GetAttribute("RefreshMonsterTag"),
  403. pkTime = pkTime,
  404. pkValue = pkValue,
  405. continuekills = attacker == null ? 0 : attacker.Statistic.continueKills,
  406. gsFlag = obj.gameServerFlag
  407. };
  408. if (obj.IsPlayer)
  409. {
  410. log.Info("玩家死亡:" + (obj == null ? "null" : obj.PlayerUUID) + ", 攻击者:" +
  411. (attacker == null ? "null" : (attacker.PlayerUUID + ", " + attacker.Info.ID)) + ", Scene:" + zone.GetSceneID());
  412. }
  413. node.SendToGameServer(EventType.areaEvent.ToString(), eventParam);
  414. }
  415. #endregion
  416. }
  417. }