You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Map.cs 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using Preparation.Interface;
  4. using Preparation.Utility;
  5. using System;
  6. namespace GameClass.GameObj
  7. {
  8. public partial class Map : IMap
  9. {
  10. private readonly Dictionary<uint, XY> birthPointList; // 出生点列表
  11. public Dictionary<uint, XY> BirthPointList => birthPointList;
  12. private uint numOfRepairedGenerators = 0;
  13. public uint NumOfRepairedGenerators
  14. {
  15. get => Interlocked.CompareExchange(ref numOfRepairedGenerators, 0, 0);
  16. }
  17. public void AddNumOfRepairedGenerators()
  18. {
  19. uint value = Interlocked.Increment(ref numOfRepairedGenerators);
  20. if (value == GameData.numOfGeneratorRequiredForEmergencyExit)
  21. {
  22. GameObjLockDict[GameObjType.EmergencyExit].EnterWriteLock();
  23. try
  24. {
  25. Random r = new Random(Environment.TickCount);
  26. EmergencyExit emergencyExit = (EmergencyExit)(GameObjDict[GameObjType.EmergencyExit][r.Next(0, GameObjDict[GameObjType.EmergencyExit].Count)]);
  27. emergencyExit.CanOpen = true;
  28. Preparation.Utility.Debugger.Output(emergencyExit, emergencyExit.Position.ToString());
  29. }
  30. finally
  31. {
  32. GameObjLockDict[GameObjType.EmergencyExit].ExitWriteLock();
  33. }
  34. }
  35. else
  36. if (value == GameData.numOfGeneratorRequiredForRepair)
  37. {
  38. GameObjLockDict[GameObjType.Doorway].EnterWriteLock();
  39. try
  40. {
  41. foreach (Doorway doorway in GameObjDict[GameObjType.Doorway])
  42. doorway.PowerSupply = true;
  43. }
  44. finally
  45. {
  46. GameObjLockDict[GameObjType.Doorway].ExitWriteLock();
  47. }
  48. }
  49. }
  50. private uint numOfDeceasedStudent = 0;
  51. public uint NumOfDeceasedStudent
  52. {
  53. get => Interlocked.CompareExchange(ref numOfDeceasedStudent, 0, 0);
  54. }
  55. private uint numOfEscapedStudent = 0;
  56. public uint NumOfEscapedStudent
  57. {
  58. get => Interlocked.CompareExchange(ref numOfEscapedStudent, 0, 0);
  59. }
  60. private uint numOfNoHpStudent = 0;
  61. public uint NumOfNoHpStudent
  62. {
  63. get => Interlocked.CompareExchange(ref numOfNoHpStudent, 0, 0);
  64. }
  65. private uint numOfRemovedStudent = 0;
  66. public uint NumOfRemovedStudent
  67. {
  68. get => Interlocked.CompareExchange(ref numOfRemovedStudent, 0, 0);
  69. }
  70. public void MapEscapeStudent()
  71. {
  72. if (Interlocked.Increment(ref numOfNoHpStudent) == GameData.numOfStudent)
  73. {
  74. Timer.IsGaming = false;
  75. return;
  76. }
  77. Interlocked.Increment(ref numOfEscapedStudent);
  78. if (Interlocked.Increment(ref numOfRemovedStudent) == GameData.numOfStudent - 1)
  79. OpenEmergencyExit();
  80. }
  81. public void MapDieStudent()
  82. {
  83. if (Interlocked.Increment(ref numOfNoHpStudent) == GameData.numOfStudent)
  84. {
  85. Timer.IsGaming = false;
  86. return;
  87. }
  88. Interlocked.Increment(ref numOfDeceasedStudent);
  89. if (Interlocked.Increment(ref numOfRemovedStudent) == GameData.numOfStudent - 1)
  90. OpenEmergencyExit();
  91. }
  92. public void MapAddictStudent()
  93. {
  94. if (Interlocked.Increment(ref numOfNoHpStudent) == GameData.numOfStudent)
  95. Timer.IsGaming = false;
  96. }
  97. public void MapRescueStudent()
  98. {
  99. Interlocked.Decrement(ref numOfNoHpStudent);
  100. }
  101. private void OpenEmergencyExit()
  102. {
  103. GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
  104. try
  105. {
  106. foreach (EmergencyExit emergencyExit in GameObjDict[GameObjType.EmergencyExit])
  107. if (emergencyExit.CanOpen)
  108. {
  109. emergencyExit.IsOpen = true;
  110. break;
  111. }
  112. }
  113. finally
  114. {
  115. GameObjLockDict[GameObjType.EmergencyExit].ExitReadLock();
  116. }
  117. }
  118. private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
  119. public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
  120. private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
  121. public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
  122. public readonly uint[,] protoGameMap;
  123. public uint[,] ProtoGameMap => protoGameMap;
  124. public PlaceType GetPlaceType(IGameObj obj)
  125. {
  126. try
  127. {
  128. return (PlaceType)protoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
  129. }
  130. catch
  131. {
  132. return PlaceType.Null;
  133. }
  134. }
  135. public PlaceType GetPlaceType(XY pos)
  136. {
  137. try
  138. {
  139. return (PlaceType)protoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
  140. }
  141. catch
  142. {
  143. return PlaceType.Null;
  144. }
  145. }
  146. public bool IsOutOfBound(IGameObj obj)
  147. {
  148. return obj.Position.x >= GameData.lengthOfMap - obj.Radius || obj.Position.x <= obj.Radius || obj.Position.y >= GameData.lengthOfMap - obj.Radius || obj.Position.y <= obj.Radius;
  149. }
  150. public IOutOfBound GetOutOfBound(XY pos)
  151. {
  152. return new OutOfBoundBlock(pos);
  153. }
  154. public Character? FindPlayer(long playerID)
  155. {
  156. Character? player = null;
  157. gameObjLockDict[GameObjType.Character].EnterReadLock();
  158. try
  159. {
  160. foreach (Character person in gameObjDict[GameObjType.Character])
  161. {
  162. if (playerID == person.ID)
  163. {
  164. player = person;
  165. break;
  166. }
  167. }
  168. }
  169. finally
  170. {
  171. gameObjLockDict[GameObjType.Character].ExitReadLock();
  172. }
  173. return player;
  174. }
  175. public Character? FindPlayerToAction(long playerID)
  176. {
  177. Character? player = null;
  178. gameObjLockDict[GameObjType.Character].EnterReadLock();
  179. try
  180. {
  181. foreach (Character person in gameObjDict[GameObjType.Character])
  182. {
  183. if (playerID == person.ID)
  184. {
  185. if (person.CharacterType == CharacterType.TechOtaku)
  186. {
  187. Debugger.Output(person, person.PlayerID.ToString());
  188. foreach (Character character in gameObjDict[GameObjType.Character])
  189. {
  190. if (((UseRobot)person.FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID == character.PlayerID)
  191. {
  192. player = character;
  193. break;
  194. }
  195. }
  196. }
  197. else player = person;
  198. break;
  199. }
  200. }
  201. }
  202. finally
  203. {
  204. gameObjLockDict[GameObjType.Character].ExitReadLock();
  205. }
  206. return player;
  207. }
  208. public bool Remove(GameObj gameObj)
  209. {
  210. GameObj? ToDel = null;
  211. GameObjLockDict[gameObj.Type].EnterWriteLock();
  212. try
  213. {
  214. foreach (GameObj obj in GameObjDict[gameObj.Type])
  215. {
  216. if (gameObj.ID == obj.ID)
  217. {
  218. ToDel = obj;
  219. break;
  220. }
  221. }
  222. if (ToDel != null)
  223. {
  224. GameObjDict[gameObj.Type].Remove(ToDel);
  225. ToDel.TryToRemove();
  226. }
  227. }
  228. finally
  229. {
  230. GameObjLockDict[gameObj.Type].ExitWriteLock();
  231. }
  232. return ToDel != null;
  233. }
  234. public bool RemoveJustFromMap(GameObj gameObj)
  235. {
  236. GameObjLockDict[gameObj.Type].EnterWriteLock();
  237. try
  238. {
  239. if (GameObjDict[gameObj.Type].Remove(gameObj))
  240. {
  241. gameObj.TryToRemove();
  242. return true;
  243. }
  244. return false;
  245. }
  246. finally
  247. {
  248. GameObjLockDict[gameObj.Type].ExitWriteLock();
  249. }
  250. }
  251. public void Add(GameObj gameObj)
  252. {
  253. GameObjLockDict[gameObj.Type].EnterWriteLock();
  254. try
  255. {
  256. GameObjDict[gameObj.Type].Add(gameObj);
  257. }
  258. finally
  259. {
  260. GameObjLockDict[gameObj.Type].ExitWriteLock();
  261. }
  262. }
  263. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  264. {
  265. GameObj? GameObjForInteract = null;
  266. GameObjLockDict[gameObjType].EnterReadLock();
  267. try
  268. {
  269. foreach (GameObj gameObj in GameObjDict[gameObjType])
  270. {
  271. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  272. {
  273. GameObjForInteract = gameObj;
  274. break;
  275. }
  276. }
  277. }
  278. finally
  279. {
  280. GameObjLockDict[gameObjType].ExitReadLock();
  281. }
  282. return GameObjForInteract;
  283. }
  284. public Student? StudentForInteract(XY Pos)
  285. {
  286. Student? GameObjForInteract = null;
  287. GameObjLockDict[GameObjType.Character].EnterReadLock();
  288. try
  289. {
  290. foreach (Character character in GameObjDict[GameObjType.Character])
  291. {
  292. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  293. {
  294. GameObjForInteract = (Student)character;
  295. break;
  296. }
  297. }
  298. }
  299. finally
  300. {
  301. GameObjLockDict[GameObjType.Character].ExitReadLock();
  302. }
  303. return GameObjForInteract;
  304. }
  305. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  306. {
  307. GameObj? GameObjForInteract = null;
  308. GameObjLockDict[gameObjType].EnterReadLock();
  309. try
  310. {
  311. foreach (GameObj gameObj in GameObjDict[gameObjType])
  312. {
  313. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  314. {
  315. GameObjForInteract = gameObj;
  316. break;
  317. }
  318. }
  319. }
  320. finally
  321. {
  322. GameObjLockDict[gameObjType].ExitReadLock();
  323. }
  324. return GameObjForInteract;
  325. }
  326. public GameObj? PartInTheSameCell(XY Pos, GameObjType gameObjType)
  327. {
  328. GameObj? GameObjForInteract = null;
  329. GameObjLockDict[gameObjType].EnterReadLock();
  330. try
  331. {
  332. foreach (GameObj gameObj in GameObjDict[gameObjType])
  333. {
  334. if (GameData.PartInTheSameCell(gameObj.Position, Pos))
  335. {
  336. GameObjForInteract = gameObj;
  337. break;
  338. }
  339. }
  340. }
  341. finally
  342. {
  343. GameObjLockDict[gameObjType].ExitReadLock();
  344. }
  345. return GameObjForInteract;
  346. }
  347. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  348. {
  349. GameObj? GameObjForInteract = null;
  350. GameObjLockDict[gameObjType].EnterReadLock();
  351. try
  352. {
  353. foreach (GameObj gameObj in GameObjDict[gameObjType])
  354. {
  355. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  356. {
  357. GameObjForInteract = gameObj;
  358. break;
  359. }
  360. }
  361. }
  362. finally
  363. {
  364. GameObjLockDict[gameObjType].ExitReadLock();
  365. }
  366. return GameObjForInteract;
  367. }
  368. public bool CanSee(Character player, GameObj gameObj)
  369. {
  370. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  371. XY pos1 = player.Position;
  372. XY pos2 = gameObj.Position;
  373. XY del = pos1 - pos2;
  374. if (del * del > player.ViewRange * player.ViewRange) return false;
  375. if (del.x > del.y)
  376. {
  377. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  378. {
  379. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  380. {
  381. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  382. return false;
  383. }
  384. }
  385. else
  386. {
  387. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  388. {
  389. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  390. return false;
  391. }
  392. }
  393. }
  394. else
  395. {
  396. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  397. {
  398. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  399. {
  400. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  401. return false;
  402. }
  403. }
  404. else
  405. {
  406. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  407. {
  408. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  409. return false;
  410. }
  411. }
  412. }
  413. return true;
  414. }
  415. public Map(uint[,] mapResource)
  416. {
  417. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  418. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  419. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  420. {
  421. if (idx != GameObjType.Null)
  422. {
  423. gameObjDict.Add(idx, new List<IGameObj>());
  424. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  425. }
  426. }
  427. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  428. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  429. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  430. for (int i = 0; i < GameData.rows; ++i)
  431. {
  432. for (int j = 0; j < GameData.cols; ++j)
  433. {
  434. switch (mapResource[i, j])
  435. {
  436. case (uint)PlaceType.Wall:
  437. {
  438. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  439. break;
  440. }
  441. case (uint)PlaceType.Doorway:
  442. {
  443. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  444. break;
  445. }
  446. case (uint)PlaceType.EmergencyExit:
  447. {
  448. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  449. break;
  450. }
  451. case (uint)PlaceType.Generator:
  452. {
  453. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  454. break;
  455. }
  456. case (uint)PlaceType.Chest:
  457. {
  458. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  459. break;
  460. }
  461. case (uint)PlaceType.Door3:
  462. {
  463. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  464. break;
  465. }
  466. case (uint)PlaceType.Door5:
  467. {
  468. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  469. break;
  470. }
  471. case (uint)PlaceType.Door6:
  472. {
  473. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  474. break;
  475. }
  476. case (uint)PlaceType.Window:
  477. {
  478. Add(new Window(GameData.GetCellCenterPos(i, j)));
  479. break;
  480. }
  481. case (uint)PlaceType.BirthPoint1:
  482. case (uint)PlaceType.BirthPoint2:
  483. case (uint)PlaceType.BirthPoint3:
  484. case (uint)PlaceType.BirthPoint4:
  485. case (uint)PlaceType.BirthPoint5:
  486. {
  487. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  488. break;
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. }