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

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