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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 && person.FindIActiveSkill(ActiveSkillType.UseRobot).IsBeingUsed)
  190. {
  191. Debugger.Output(person, person.PlayerID.ToString());
  192. foreach (Character character in gameObjDict[GameObjType.Character])
  193. {
  194. Debugger.Output(character, character.PlayerID.ToString());
  195. if (person.PlayerID + GameData.numOfPeople == character.PlayerID)
  196. {
  197. player = character;
  198. break;
  199. }
  200. }
  201. }
  202. else player = person;
  203. break;
  204. }
  205. }
  206. }
  207. finally
  208. {
  209. gameObjLockDict[GameObjType.Character].ExitReadLock();
  210. }
  211. return player;
  212. }
  213. public bool Remove(GameObj gameObj)
  214. {
  215. GameObj? ToDel = null;
  216. GameObjLockDict[gameObj.Type].EnterWriteLock();
  217. try
  218. {
  219. foreach (GameObj obj in GameObjDict[gameObj.Type])
  220. {
  221. if (gameObj.ID == obj.ID)
  222. {
  223. ToDel = obj;
  224. break;
  225. }
  226. }
  227. if (ToDel != null)
  228. GameObjDict[gameObj.Type].Remove(ToDel);
  229. }
  230. finally
  231. {
  232. GameObjLockDict[gameObj.Type].ExitWriteLock();
  233. }
  234. return ToDel != null;
  235. }
  236. public bool RemoveJustFromMap(GameObj gameObj)
  237. {
  238. GameObjLockDict[gameObj.Type].EnterWriteLock();
  239. bool flag;
  240. try
  241. {
  242. flag = GameObjDict[gameObj.Type].Remove(gameObj);
  243. }
  244. finally
  245. {
  246. GameObjLockDict[gameObj.Type].ExitWriteLock();
  247. }
  248. return flag;
  249. }
  250. public void Add(GameObj gameObj)
  251. {
  252. GameObjLockDict[gameObj.Type].EnterWriteLock();
  253. try
  254. {
  255. GameObjDict[gameObj.Type].Add(gameObj);
  256. }
  257. finally
  258. {
  259. GameObjLockDict[gameObj.Type].ExitWriteLock();
  260. }
  261. }
  262. public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
  263. {
  264. GameObj? GameObjForInteract = null;
  265. GameObjLockDict[gameObjType].EnterReadLock();
  266. try
  267. {
  268. foreach (GameObj gameObj in GameObjDict[gameObjType])
  269. {
  270. if (GameData.ApproachToInteract(gameObj.Position, Pos))
  271. {
  272. GameObjForInteract = gameObj;
  273. break;
  274. }
  275. }
  276. }
  277. finally
  278. {
  279. GameObjLockDict[gameObjType].ExitReadLock();
  280. }
  281. return GameObjForInteract;
  282. }
  283. public Student? StudentForInteract(XY Pos)
  284. {
  285. Student? GameObjForInteract = null;
  286. GameObjLockDict[GameObjType.Character].EnterReadLock();
  287. try
  288. {
  289. foreach (Character character in GameObjDict[GameObjType.Character])
  290. {
  291. if (!character.IsGhost() && GameData.ApproachToInteract(character.Position, Pos))
  292. {
  293. GameObjForInteract = (Student)character;
  294. break;
  295. }
  296. }
  297. }
  298. finally
  299. {
  300. GameObjLockDict[GameObjType.Character].ExitReadLock();
  301. }
  302. return GameObjForInteract;
  303. }
  304. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  305. {
  306. GameObj? GameObjForInteract = null;
  307. GameObjLockDict[gameObjType].EnterReadLock();
  308. try
  309. {
  310. foreach (GameObj gameObj in GameObjDict[gameObjType])
  311. {
  312. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  313. {
  314. GameObjForInteract = gameObj;
  315. break;
  316. }
  317. }
  318. }
  319. finally
  320. {
  321. GameObjLockDict[gameObjType].ExitReadLock();
  322. }
  323. return GameObjForInteract;
  324. }
  325. public GameObj? PartInTheSameCell(XY Pos, GameObjType gameObjType)
  326. {
  327. GameObj? GameObjForInteract = null;
  328. GameObjLockDict[gameObjType].EnterReadLock();
  329. try
  330. {
  331. foreach (GameObj gameObj in GameObjDict[gameObjType])
  332. {
  333. if (GameData.PartInTheSameCell(gameObj.Position, Pos))
  334. {
  335. GameObjForInteract = gameObj;
  336. break;
  337. }
  338. }
  339. }
  340. finally
  341. {
  342. GameObjLockDict[gameObjType].ExitReadLock();
  343. }
  344. return GameObjForInteract;
  345. }
  346. public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
  347. {
  348. GameObj? GameObjForInteract = null;
  349. GameObjLockDict[gameObjType].EnterReadLock();
  350. try
  351. {
  352. foreach (GameObj gameObj in GameObjDict[gameObjType])
  353. {
  354. if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
  355. {
  356. GameObjForInteract = gameObj;
  357. break;
  358. }
  359. }
  360. }
  361. finally
  362. {
  363. GameObjLockDict[gameObjType].ExitReadLock();
  364. }
  365. return GameObjForInteract;
  366. }
  367. public bool CanSee(Character player, GameObj gameObj)
  368. {
  369. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  370. XY pos1 = player.Position;
  371. XY pos2 = gameObj.Position;
  372. XY del = pos1 - pos2;
  373. if (del * del > player.ViewRange * player.ViewRange) return false;
  374. if (del.x > del.y)
  375. {
  376. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  377. {
  378. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  379. {
  380. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  381. return false;
  382. }
  383. }
  384. else
  385. {
  386. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  387. {
  388. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  389. return false;
  390. }
  391. }
  392. }
  393. else
  394. {
  395. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  396. {
  397. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  398. {
  399. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  400. return false;
  401. }
  402. }
  403. else
  404. {
  405. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  406. {
  407. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  408. return false;
  409. }
  410. }
  411. }
  412. return true;
  413. }
  414. public Map(uint[,] mapResource)
  415. {
  416. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  417. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  418. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  419. {
  420. if (idx != GameObjType.Null)
  421. {
  422. gameObjDict.Add(idx, new List<IGameObj>());
  423. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  424. }
  425. }
  426. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  427. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  428. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  429. for (int i = 0; i < GameData.rows; ++i)
  430. {
  431. for (int j = 0; j < GameData.cols; ++j)
  432. {
  433. switch (mapResource[i, j])
  434. {
  435. case (uint)PlaceType.Wall:
  436. {
  437. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  438. break;
  439. }
  440. case (uint)PlaceType.Doorway:
  441. {
  442. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  443. break;
  444. }
  445. case (uint)PlaceType.EmergencyExit:
  446. {
  447. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  448. break;
  449. }
  450. case (uint)PlaceType.Generator:
  451. {
  452. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  453. break;
  454. }
  455. case (uint)PlaceType.Chest:
  456. {
  457. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  458. break;
  459. }
  460. case (uint)PlaceType.Door3:
  461. {
  462. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  463. break;
  464. }
  465. case (uint)PlaceType.Door5:
  466. {
  467. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  468. break;
  469. }
  470. case (uint)PlaceType.Door6:
  471. {
  472. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  473. break;
  474. }
  475. case (uint)PlaceType.Window:
  476. {
  477. Add(new Window(GameData.GetCellCenterPos(i, j), mapResource[i - 1, j] == (uint)PlaceType.Wall));
  478. break;
  479. }
  480. case (uint)PlaceType.BirthPoint1:
  481. case (uint)PlaceType.BirthPoint2:
  482. case (uint)PlaceType.BirthPoint3:
  483. case (uint)PlaceType.BirthPoint4:
  484. case (uint)PlaceType.BirthPoint5:
  485. {
  486. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  487. break;
  488. }
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }