RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Viewport.java
1package com.osroyale.game.world.entity.mob;
2
3import com.osroyale.game.world.entity.mob.npc.Npc;
4import com.osroyale.game.world.entity.mob.player.Player;
5
6import java.util.LinkedList;
7import java.util.List;
8import java.util.concurrent.atomic.AtomicInteger;
9
50
51public final class Viewport {
52
54 public static final int ADD_THRESHOLD = 15;
55
57 public static final int CAPACITY = 100;
58
60 public static final int VIEW_DISTANCE = 15;
61
63 private final List<Player> playersInViewport = new LinkedList<>();
64
66 private final List<Npc> npcsInViewport = new LinkedList<>();
67
69 private final AtomicInteger playerViewingDistance = new AtomicInteger(VIEW_DISTANCE);
70
72 private final AtomicInteger npcViewingDistance = new AtomicInteger(VIEW_DISTANCE);
73
77 private final Player player;
78
79 public Viewport(Player player) {
80 this.player = player;
81 }
82
88 public boolean add(Mob other) {
89 if (!canAdd(other)) {
90 return false;
91 }
92
93 if (other.isPlayer()) {
94 playersInViewport.add(other.getPlayer());
95 other.updateFlags.add(UpdateFlag.APPEARANCE);
96 } else if (other.isNpc()) {
97 npcsInViewport.add(other.getNpc());
98 other.updateFlags.add(UpdateFlag.FACE_COORDINATE); // doing this for players for some reason crashes the players client
99 }
100 return true;
101 }
102
108 private boolean canAdd(Mob other) {
109 if (shouldRemove(other)) {
110 return false;
111 }
112
113 if (other.isPlayer()) {
114 Player player = other.getPlayer();
115 return !playersInViewport.contains(player);
116 } else if (other.isNpc()) {
117 Npc npc = other.getNpc();
118 return !npcsInViewport.contains(npc);
119 }
120
121 return false;
122 }
123
129 public boolean shouldRemove(Mob other) {
130 boolean sameEntity = player == other;
131 boolean notValid = !other.isValid();
132 boolean notVisible = !other.isVisible();
133 boolean notSameInstance = player.instance != other.instance;
134 boolean notInDistance = !player.getPosition().isWithinDistance(other.getPosition().copy(), other.isPlayer() ? player.viewport.getPlayerViewingDistance() : player.viewport.getNpcViewingDistance());
135 boolean positionChanged = other.positionChange || other.teleportRegion;
136 boolean regionChanged = other.regionChange && other.teleporting;
137 return sameEntity || notValid || notVisible || notSameInstance || notInDistance || regionChanged || positionChanged;
138 }
139
144 if (playersInViewport.size() >= Viewport.CAPACITY) {
145 if (playerViewingDistance.decrementAndGet() < 1) {
146 playerViewingDistance.set(1);
147 }
148 } else {
149 if (playerViewingDistance.incrementAndGet() > VIEW_DISTANCE) {
150 playerViewingDistance.set(VIEW_DISTANCE);
151 }
152 }
153
154 if (npcsInViewport.size() >= Viewport.CAPACITY) {
155 if (npcViewingDistance.decrementAndGet() < 1) {
156 npcViewingDistance.set(1);
157 }
158 } else {
159 if (npcViewingDistance.incrementAndGet() > VIEW_DISTANCE) {
160 npcViewingDistance.set(VIEW_DISTANCE);
161 }
162 }
163 }
164
168 public List<Player> getPlayersInViewport() {
169 return playersInViewport;
170 }
171
175 public List<Npc> getNpcsInViewport() {
176 return npcsInViewport;
177 }
178
183 return playerViewingDistance.get();
184 }
185
190 return npcViewingDistance.get();
191 }
192
193}