source: binary-improvements2/7dtd-server-fixes/src/LandClaimList.cs@ 394

Last change on this file since 394 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes {
6 public static class LandClaimList {
7 public delegate bool OwnerFilter (Player _owner);
8
9 public delegate bool PositionFilter (Vector3i _position);
10
11 public static Dictionary<Player, List<Vector3i>> GetLandClaims (OwnerFilter[] _ownerFilters,
12 PositionFilter[] _positionFilters) {
13 Dictionary<Vector3i, PersistentPlayerData> d = GameManager.Instance.GetPersistentPlayerList ().m_lpBlockMap;
14 Dictionary<Player, List<Vector3i>> result = new Dictionary<Player, List<Vector3i>> ();
15
16 if (d == null) {
17 return result;
18 }
19
20 Dictionary<PersistentPlayerData, List<Vector3i>> owners =
21 new Dictionary<PersistentPlayerData, List<Vector3i>> ();
22 foreach ((Vector3i claimPos, PersistentPlayerData owner) in d) {
23 bool allowed = true;
24 if (_positionFilters != null) {
25 foreach (PositionFilter pf in _positionFilters) {
26 if (pf (claimPos)) {
27 continue;
28 }
29
30 allowed = false;
31 break;
32 }
33 }
34
35 if (!allowed) {
36 continue;
37 }
38
39 if (!owners.ContainsKey (owner)) {
40 owners.Add (owner, new List<Vector3i> ());
41 }
42
43 owners [owner].Add (claimPos);
44 }
45
46 foreach ((PersistentPlayerData owner, List<Vector3i> claimPositions) in owners) {
47 Player p = PersistentContainer.Instance.Players [owner.UserIdentifier, false] ?? new Player (owner.UserIdentifier);
48
49 bool allowed = true;
50 if (_ownerFilters != null) {
51 foreach (OwnerFilter of in _ownerFilters) {
52 if (of (p)) {
53 continue;
54 }
55
56 allowed = false;
57 break;
58 }
59 }
60
61 if (!allowed) {
62 continue;
63 }
64
65 result.Add (p, new List<Vector3i> ());
66 foreach (Vector3i v in claimPositions) {
67 result [p].Add (v);
68 }
69 }
70
71 return result;
72 }
73
74 public static OwnerFilter UserIdFilter (PlatformUserIdentifierAbs _userId) {
75 return _p => _p.PlatformId.Equals (_userId);
76 }
77
78 public static PositionFilter CloseToFilter2dRect (Vector3i _position, int _maxDistance) {
79 return _v => Math.Abs (_v.x - _position.x) <= _maxDistance && Math.Abs (_v.z - _position.z) <= _maxDistance;
80 }
81
82 public static OwnerFilter OrOwnerFilter (OwnerFilter _f1, OwnerFilter _f2) {
83 return _p => _f1 (_p) || _f2 (_p);
84 }
85 }
86}
Note: See TracBrowser for help on using the repository browser.