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

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