source: binary-improvements/7dtd-server-fixes/src/LandClaimList.cs@ 336

Last change on this file since 336 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 2.2 KB
RevLine 
[253]1using System;
2using System.Collections.Generic;
3using AllocsFixes.PersistentData;
4
[325]5namespace AllocsFixes {
[253]6 public class LandClaimList {
7 public delegate bool OwnerFilter (Player owner);
8
9 public delegate bool PositionFilter (Vector3i position);
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>> ();
22 foreach (KeyValuePair<Vector3i, PersistentPlayerData> kvp in d) {
23 bool allowed = true;
24 if (_positionFilters != null) {
25 foreach (PositionFilter pf in _positionFilters) {
26 if (!pf (kvp.Key)) {
27 allowed = false;
28 break;
[253]29 }
30 }
31 }
32
[326]33 if (allowed) {
34 if (!owners.ContainsKey (kvp.Value)) {
35 owners.Add (kvp.Value, new List<Vector3i> ());
[253]36 }
37
[326]38 owners [kvp.Value].Add (kvp.Key);
39 }
40 }
41
42 foreach (KeyValuePair<PersistentPlayerData, List<Vector3i>> kvp in owners) {
43 Player p = PersistentContainer.Instance.Players [kvp.Key.PlayerId, false];
44 if (p == null) {
45 p = new Player (kvp.Key.PlayerId);
46 }
47
48 bool allowed = true;
49 if (_ownerFilters != null) {
50 foreach (OwnerFilter of in _ownerFilters) {
51 if (!of (p)) {
52 allowed = false;
53 break;
[253]54 }
55 }
[326]56 }
[253]57
[326]58 if (allowed) {
59 result.Add (p, new List<Vector3i> ());
60 foreach (Vector3i v in kvp.Value) {
61 result [p].Add (v);
[253]62 }
63 }
64 }
[325]65
[253]66 return result;
67 }
68
69 public static OwnerFilter SteamIdFilter (string _steamId) {
70 return p => p.SteamID.Equals (_steamId);
71 }
72
73 public static PositionFilter CloseToFilter2dRect (Vector3i _position, int _maxDistance) {
74 return v => Math.Abs (v.x - _position.x) <= _maxDistance && Math.Abs (v.z - _position.z) <= _maxDistance;
75 }
76
77 public static OwnerFilter OrOwnerFilter (OwnerFilter _f1, OwnerFilter _f2) {
78 return p => _f1 (p) || _f2 (p);
79 }
80 }
[325]81}
Note: See TracBrowser for help on using the repository browser.