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

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

Major refactoring/cleanup

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