RuneHive-Game
Loading...
Searching...
No Matches
RegionManager.java
Go to the documentation of this file.
1package com.runehive.game.world.region;
2
3import com.google.common.base.Preconditions;
4import com.runehive.game.world.entity.mob.Mob;
5import com.runehive.game.world.entity.mob.npc.Npc;
6import com.runehive.game.world.entity.mob.player.Player;
7import com.runehive.game.world.position.Position;
8import com.runehive.util.Utility;
9import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
10import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
11
12import java.util.*;
13import java.util.function.Consumer;
14
15/**
16 * Manages the world regions.
17 *
18 * @author Graham Edgecombe
19 */
20public class RegionManager {
21
22 /** The active (loaded) region map. */
23 private final Int2ObjectMap<Region> activeRegions = new Int2ObjectOpenHashMap<>();
24
25 /**
26 * Sends an action to {@link Mob} instance which is within a {@code
27 * distance}.
28 *
29 * @param action action consumer.
30 */
31 public static void forNearbyPlayer(Mob mob, int distance, Consumer<Player> action) {
32 mob.getRegion().getSurroundingRegions().ifPresent(regions -> {
33 for (Region region : regions) {
34 for (Player nearby : region.getPlayers(mob.getHeight())) {
35 if (nearby == null) continue;
36 if (Utility.getDistance(nearby, mob) > distance) continue;
37 if (nearby.getCurrentHealth() <= 0 || nearby.isDead()) continue;
38 action.accept(nearby);
39 }
40 }
41 });
42 }
43
44 /**
45 * Sends an action to {@link Mob} instance which is within a {@code
46 * distance}.
47 *
48 * @param action action consumer.
49 */
50 public static void forNearbyPlayer(Position position, int distance, Consumer<Player> action) {
51 position.getRegion().getSurroundingRegions().ifPresent(regions -> {
52 for (Region region : regions) {
53 for (Player other : region.getPlayers(position.getHeight())) {
54 if (other == null) continue;
55 if (Utility.getDistance(other, position) > distance) continue;
56 if (other.getCurrentHealth() <= 0 || other.isDead()) continue;
57 action.accept(other);
58 }
59 }
60 });
61 }
62
63 /**
64 * Gets the local players around an entity.
65 *
66 * @param mob The entity.
67 * @return The collection of local players.
68 */
69 public List<Player> getLocalPlayers(Mob mob) {
70 Preconditions.checkArgument(mob != null, "mob is null");
71 List<Player> localPlayers = new LinkedList<>();
73 for (Player player : region.getPlayers(mob.getHeight())) {
74 if (mob.getPosition().isWithinDistance(player.getPosition(), Region.VIEW_DISTANCE)) {
75 localPlayers.add(player);
76 }
77 }
78 }
79 return localPlayers;
80 }
81
82 /**
83 * Gets the local npcs around an entity.
84 *
85 * @param mob The entity.
86 * @return The collection of local npcs.
87 */
88 public List<Npc> getLocalNpcs(Mob mob) {
89 List<Npc> localNpcs = new LinkedList<>();
91 for (Npc npc : region.getNpcs(mob.getHeight())) {
92 if (mob.getPosition().isWithinDistance(npc.getPosition(), Region.VIEW_DISTANCE)) {
93 localNpcs.add(npc);
94 }
95 }
96 }
97 return localNpcs;
98 }
99
100 /**
101 * Gets the regions surrounding a position.
102 *
103 * @param position The position.
104 * @return The regions surrounding the position.
105 */
107 Region target = getRegion(position.getX(), position.getY());
108
109 if (target.getSurroundingRegions().isPresent()) {
110 return target.getSurroundingRegions().get();
111 }
112
113 Set<Region> surrounding = new HashSet<>();
114 int chunkX = position.getChunkX();
115 int chunkY = position.getChunkY();
116
117 for (int y = -1; y <= 1; y++) {
118 for (int x = -1; x <= 1; x++) {
119 int xx = (chunkX + x * 8 + 6) << 3;
120 int yy = (chunkY + y * 8 + 6) << 3;
121 Region region = getRegion(xx, yy);
122
123 if (!surrounding.contains(region)) {
124 surrounding.add(region);
125 }
126 }
127 }
128
129 Region[] x = surrounding.toArray(new Region[surrounding.size()]);
130 target.setSurroundingRegions(Optional.of(x));
131 return target.getSurroundingRegions().get();
132 }
133
134 /**
135 * Gets a region by position.
136 *
137 * @param position The position.
138 * @return The region.
139 */
141 return getRegion(position.getX(), position.getY());
142 }
143
144 /**
145 * Gets a region by its x and y coordinates.
146 *
147 * @param x The x coordinate.
148 * @param y The y coordinate.
149 * @return The region.
150 */
151 public Region getRegion(final int x, final int y) {
152 final int hash = hash(x, y);
153
154 final Region region = activeRegions.get(hash);
155 if (region != null) {
156 return region;
157 }
158
159 final Region newRegion = new Region(x, y);
160 activeRegions.put(hash, newRegion);
161 return newRegion;
162 }
163
164 private static int hash(int x, int y) {
165 return ((x >> 6) << 8) + (y >> 6);
166 }
167
168}
Handles the mob class.
Definition Mob.java:66
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
Represents a single tile on the game world.
Definition Position.java:14
boolean isWithinDistance(Position other, int radius)
Checks if this location is within range of another.
Represents a single region.
Definition Region.java:21
void setSurroundingRegions(Optional< Region[]> surroundingRegions)
Definition Region.java:100
Optional< Region[]> getSurroundingRegions()
Definition Region.java:96
static void forNearbyPlayer(Position position, int distance, Consumer< Player > action)
Sends an action to Mob instance which is within a distance.
List< Player > getLocalPlayers(Mob mob)
Gets the local players around an entity.
Region getRegion(final int x, final int y)
Gets a region by its x and y coordinates.
Region getRegion(Position position)
Gets a region by position.
static void forNearbyPlayer(Mob mob, int distance, Consumer< Player > action)
Sends an action to Mob instance which is within a distance.
final Int2ObjectMap< Region > activeRegions
The active (loaded) region map.
List< Npc > getLocalNpcs(Mob mob)
Gets the local npcs around an entity.
Region[] getSurroundingRegions(Position position)
Gets the regions surrounding a position.
Handles miscellaneous methods.
Definition Utility.java:27
static int getDistance(Interactable source, Position target)
Definition Utility.java:363