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

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