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

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