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

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