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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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, Student AStudent)
  243. {
  244. GameObjLockDict[GameObjType.Character].EnterReadLock();
  245. try
  246. {
  247. foreach (Character character in GameObjDict[GameObjType.Character])
  248. {
  249. if (!character.IsGhost() && character != AStudent && GameData.ApproachToInteract(character.Position, Pos))
  250. {
  251. return (Student)character;
  252. }
  253. }
  254. }
  255. finally
  256. {
  257. GameObjLockDict[GameObjType.Character].ExitReadLock();
  258. }
  259. return null;
  260. }
  261. public GameObj? OneInTheSameCell(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.IsInTheSameCell(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 GameObj? PartInTheSameCell(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.PartInTheSameCell(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? OneForInteractInACross(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.ApproachToInteractInACross(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 bool CanSee(Character player, GameObj gameObj)
  325. {
  326. if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
  327. XY pos1 = player.Position;
  328. XY pos2 = gameObj.Position;
  329. XY del = pos1 - pos2;
  330. if (del * del > player.ViewRange * player.ViewRange) return false;
  331. if (del.x > del.y)
  332. {
  333. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  334. {
  335. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  336. {
  337. if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
  338. return false;
  339. }
  340. }
  341. else
  342. {
  343. for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
  344. {
  345. if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
  346. return false;
  347. }
  348. }
  349. }
  350. else
  351. {
  352. if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
  353. {
  354. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  355. {
  356. if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
  357. return false;
  358. }
  359. }
  360. else
  361. {
  362. for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
  363. {
  364. if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
  365. return false;
  366. }
  367. }
  368. }
  369. return true;
  370. }
  371. public bool Remove(GameObj gameObj)
  372. {
  373. GameObj? ToDel = null;
  374. GameObjLockDict[gameObj.Type].EnterWriteLock();
  375. try
  376. {
  377. foreach (GameObj obj in GameObjDict[gameObj.Type])
  378. {
  379. if (gameObj.ID == obj.ID)
  380. {
  381. ToDel = obj;
  382. break;
  383. }
  384. }
  385. if (ToDel != null)
  386. {
  387. GameObjDict[gameObj.Type].Remove(ToDel);
  388. ToDel.TryToRemove();
  389. }
  390. }
  391. finally
  392. {
  393. GameObjLockDict[gameObj.Type].ExitWriteLock();
  394. }
  395. return ToDel != null;
  396. }
  397. public bool RemoveJustFromMap(GameObj gameObj)
  398. {
  399. GameObjLockDict[gameObj.Type].EnterWriteLock();
  400. try
  401. {
  402. if (GameObjDict[gameObj.Type].Remove(gameObj))
  403. {
  404. gameObj.TryToRemove();
  405. return true;
  406. }
  407. return false;
  408. }
  409. finally
  410. {
  411. GameObjLockDict[gameObj.Type].ExitWriteLock();
  412. }
  413. }
  414. public void Add(GameObj gameObj)
  415. {
  416. GameObjLockDict[gameObj.Type].EnterWriteLock();
  417. try
  418. {
  419. GameObjDict[gameObj.Type].Add(gameObj);
  420. }
  421. finally
  422. {
  423. GameObjLockDict[gameObj.Type].ExitWriteLock();
  424. }
  425. }
  426. public Map(uint[,] mapResource)
  427. {
  428. gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
  429. gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
  430. foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
  431. {
  432. if (idx != GameObjType.Null)
  433. {
  434. gameObjDict.Add(idx, new List<IGameObj>());
  435. gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
  436. }
  437. }
  438. protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
  439. Array.Copy(mapResource, protoGameMap, mapResource.Length);
  440. birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
  441. for (int i = 0; i < GameData.rows; ++i)
  442. {
  443. for (int j = 0; j < GameData.cols; ++j)
  444. {
  445. switch (mapResource[i, j])
  446. {
  447. case (uint)PlaceType.Wall:
  448. {
  449. Add(new Wall(GameData.GetCellCenterPos(i, j)));
  450. break;
  451. }
  452. case (uint)PlaceType.Doorway:
  453. {
  454. Add(new Doorway(GameData.GetCellCenterPos(i, j)));
  455. break;
  456. }
  457. case (uint)PlaceType.EmergencyExit:
  458. {
  459. Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
  460. break;
  461. }
  462. case (uint)PlaceType.Generator:
  463. {
  464. Add(new Generator(GameData.GetCellCenterPos(i, j)));
  465. break;
  466. }
  467. case (uint)PlaceType.Chest:
  468. {
  469. Add(new Chest(GameData.GetCellCenterPos(i, j)));
  470. break;
  471. }
  472. case (uint)PlaceType.Door3:
  473. {
  474. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
  475. break;
  476. }
  477. case (uint)PlaceType.Door5:
  478. {
  479. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
  480. break;
  481. }
  482. case (uint)PlaceType.Door6:
  483. {
  484. Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
  485. break;
  486. }
  487. case (uint)PlaceType.Window:
  488. {
  489. Add(new Window(GameData.GetCellCenterPos(i, j)));
  490. break;
  491. }
  492. case (uint)PlaceType.BirthPoint1:
  493. case (uint)PlaceType.BirthPoint2:
  494. case (uint)PlaceType.BirthPoint3:
  495. case (uint)PlaceType.BirthPoint4:
  496. case (uint)PlaceType.BirthPoint5:
  497. {
  498. birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
  499. break;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }