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

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