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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 = 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 = 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. Debugger.Output(person, person.PlayerID.ToString());
  222. foreach (Character character in gameObjDict[GameObjType.Character])
  223. {
  224. if (((UseRobot)person.FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID == character.PlayerID)
  225. {
  226. player = character;
  227. break;
  228. }
  229. }
  230. }
  231. else player = person;
  232. break;
  233. }
  234. }
  235. }
  236. finally
  237. {
  238. gameObjLockDict[GameObjType.Character].ExitReadLock();
  239. }
  240. return player;
  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(Student AStudent)
  264. {
  265. GameObjLockDict[GameObjType.Character].EnterReadLock();
  266. try
  267. {
  268. foreach (Character character in GameObjDict[GameObjType.Character])
  269. {
  270. if (!character.IsGhost() && character != AStudent && GameData.ApproachToInteract(character.Position, AStudent.Position))
  271. {
  272. return (Student)character;
  273. }
  274. }
  275. }
  276. finally
  277. {
  278. GameObjLockDict[GameObjType.Character].ExitReadLock();
  279. }
  280. return null;
  281. }
  282. public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
  283. {
  284. GameObj? GameObjForInteract = null;
  285. GameObjLockDict[gameObjType].EnterReadLock();
  286. try
  287. {
  288. foreach (GameObj gameObj in GameObjDict[gameObjType])
  289. {
  290. if (GameData.IsInTheSameCell(gameObj.Position, Pos))
  291. {
  292. GameObjForInteract = gameObj;
  293. break;
  294. }
  295. }
  296. }
  297. finally
  298. {
  299. GameObjLockDict[gameObjType].ExitReadLock();
  300. }
  301. return GameObjForInteract;
  302. }
  303. public GameObj? PartInTheSameCell(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.PartInTheSameCell(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? OneForInteractInACross(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.ApproachToInteractInACross(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 bool CanSee(Character player, GameObj gameObj)
  346. {
  347. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  348. XY pos1 = player.Position;
  349. XY pos2 = gameObj.Position;
  350. XY del = pos1 - pos2;
  351. if (del * del > player.ViewRange * player.ViewRange) return false;
  352. if (del.x > del.y)
  353. {
  354. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  355. {
  356. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  357. {
  358. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  359. return false;
  360. }
  361. }
  362. else
  363. {
  364. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  365. {
  366. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  367. return false;
  368. }
  369. }
  370. }
  371. else
  372. {
  373. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  374. {
  375. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  376. {
  377. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  378. return false;
  379. }
  380. }
  381. else
  382. {
  383. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  384. {
  385. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  386. return false;
  387. }
  388. }
  389. }
  390. return true;
  391. }
  392. public bool Remove(GameObj gameObj)
  393. {
  394. GameObj? ToDel = null;
  395. GameObjLockDict[gameObj.Type].EnterWriteLock();
  396. try
  397. {
  398. foreach (GameObj obj in GameObjDict[gameObj.Type])
  399. {
  400. if (gameObj.ID == obj.ID)
  401. {
  402. ToDel = obj;
  403. break;
  404. }
  405. }
  406. if (ToDel != null)
  407. {
  408. GameObjDict[gameObj.Type].Remove(ToDel);
  409. ToDel.TryToRemove();
  410. }
  411. }
  412. finally
  413. {
  414. GameObjLockDict[gameObj.Type].ExitWriteLock();
  415. }
  416. return ToDel != null;
  417. }
  418. public bool RemoveJustFromMap(GameObj gameObj)
  419. {
  420. GameObjLockDict[gameObj.Type].EnterWriteLock();
  421. try
  422. {
  423. if (GameObjDict[gameObj.Type].Remove(gameObj))
  424. {
  425. gameObj.TryToRemove();
  426. return true;
  427. }
  428. return false;
  429. }
  430. finally
  431. {
  432. GameObjLockDict[gameObj.Type].ExitWriteLock();
  433. }
  434. }
  435. public void Add(GameObj gameObj)
  436. {
  437. GameObjLockDict[gameObj.Type].EnterWriteLock();
  438. try
  439. {
  440. GameObjDict[gameObj.Type].Add(gameObj);
  441. }
  442. finally
  443. {
  444. GameObjLockDict[gameObj.Type].ExitWriteLock();
  445. }
  446. }
  447. public Map(uint[,] mapResource)
  448. {
  449. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  450. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  451. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  452. {
  453. if (idx != GameObjType.Null)
  454. {
  455. gameObjDict.Add(idx, new List<IGameObj>());
  456. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  457. }
  458. }
  459. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  460. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  461. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  462. for (int i = 0; i < GameData.rows; ++i)
  463. {
  464. for (int j = 0; j < GameData.cols; ++j)
  465. {
  466. switch (mapResource[i, j])
  467. {
  468. case (uint)PlaceType.Wall:
  469. {
  470. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  471. break;
  472. }
  473. case (uint)PlaceType.Doorway:
  474. {
  475. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  476. break;
  477. }
  478. case (uint)PlaceType.EmergencyExit:
  479. {
  480. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  481. break;
  482. }
  483. case (uint)PlaceType.Generator:
  484. {
  485. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  486. break;
  487. }
  488. case (uint)PlaceType.Chest:
  489. {
  490. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  491. break;
  492. }
  493. case (uint)PlaceType.Door3:
  494. {
  495. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  496. break;
  497. }
  498. case (uint)PlaceType.Door5:
  499. {
  500. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  501. break;
  502. }
  503. case (uint)PlaceType.Door6:
  504. {
  505. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  506. break;
  507. }
  508. case (uint)PlaceType.Window:
  509. {
  510. Add(new Window(GameData.GetCellCenterPos(i, j)));
  511. break;
  512. }
  513. case (uint)PlaceType.BirthPoint1:
  514. case (uint)PlaceType.BirthPoint2:
  515. case (uint)PlaceType.BirthPoint3:
  516. case (uint)PlaceType.BirthPoint4:
  517. case (uint)PlaceType.BirthPoint5:
  518. {
  519. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  520. break;
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }