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 20 kB

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