RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
CannonManager.java
1package com.osroyale.content.combat.cannon;
2
3import com.osroyale.game.world.items.Item;
4import com.osroyale.game.world.position.Area;
5import com.osroyale.net.packet.out.SendMessage;
6import com.osroyale.game.Animation;
7import com.osroyale.game.Projectile;
8import com.osroyale.game.world.entity.mob.npc.Npc;
9import com.osroyale.game.world.entity.mob.player.Player;
10import com.osroyale.game.world.World;
11import com.osroyale.game.world.region.Region;
12import com.osroyale.util.Utility;
13
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.Map;
17
41
42public class CannonManager {
43
44 static Map<String, Cannon> ACTIVE_CANNONS = new HashMap<>();
45
46public enum Setup {
47 NO_CANNON,
48 BASE,
49 STAND,
50 BARRELS,
51 FURNACE,
52 COMPLETE_CANNON
53 }
54
55 public enum Rotation {
56 NORTH,
57 NORTH_EAST,
58 EAST,
59 SOUTH_EAST,
60 SOUTH,
61 SOUTH_WEST,
62 WEST,
63 NORTH_WEST
64 }
65
66 public static void test(Player player) {
67 drop(player, new Cannon(player.getName(), player.getPosition()));
68 }
69
70 public static void drop(Player player, Cannon cannon) {
71 if (ACTIVE_CANNONS.containsKey(player.getName())) {
72 player.send(new SendMessage("You already have a cannon active!"));
73 return;
74 }
75 if(Area.inCatacombs(player)) {
76 player.send(new SendMessage("You cannot set up a cannon here!"));
77 return;
78 }
79
80 if (cannon.getStage().ordinal() != 0) {
81 player.send(new SendMessage("You have already started setting up a cannon!"));
82 return;
83 }
84
85 for (Cannon other : ACTIVE_CANNONS.values()) {
86 if (other.getPosition().isWithinDistance(player.getPosition(), 5)) {
87 player.send(new SendMessage("You are trying to build too close to another cannon!"));
88 return;
89 }
90 }
91
92 World.schedule(new CannonBuild(player, cannon));
93 }
94
95 public static void pickup(Player player) {
96 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
97
98 if (cannon == null) {
99 player.send(new SendMessage("This is not your cannon!"));
100 return;
101 }
102
103 if (!cannon.getOwner().equalsIgnoreCase(player.getName())) {
104 player.send(new SendMessage("This is not your cannon!"));
105 return;
106 }
107
108 player.animate(new Animation(827));
109
110 if(cannon.getAmmunition() != 0)
111 player.inventory.add(new Item(2, cannon.getAmmunition()));
112
113 int[] ids = { 6, 8, 10, 12 };
114 for(int index = 0; index < ids.length; index++)
115 player.inventory.add(new Item(ids[index]));
116
117 cannon.unregister();
118 ACTIVE_CANNONS.remove(player.getName());
119 }
120
121 public static void empty(Player player) {
122 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
123
124 if (cannon == null) {
125 player.send(new SendMessage("This is not your cannon!"));
126 return;
127 }
128
129 if (!cannon.getOwner().equalsIgnoreCase(player.getName())) {
130 player.send(new SendMessage("This is not your cannon!"));
131 return;
132 }
133
134 if(cannon.getAmmunition() != 0)
135 player.inventory.add(new Item(2, cannon.getAmmunition()));
136
137 cannon.setFiring(false);
138 cannon.setAmmunition(0);
139 }
140
141 public static void logout(Player player) {
142 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
143
144 if(cannon == null) return;
145
146
147 if(cannon.getAmmunition() != 0)
148 player.inventory.add(new Item(2, cannon.getAmmunition()));
149
150 int[] ids = { 6, 8, 10, 12 };
151 for(int index = 0; index < ids.length; index++)
152 player.inventory.add(new Item(ids[index]));
153
154 cannon.unregister();
155 ACTIVE_CANNONS.remove(player.getName());
156 }
157
158 public static void load(Player player) {
159 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
160
161 if (cannon == null) {
162 player.send(new SendMessage("This is not your cannon!"));
163 return;
164 }
165
166 if (!cannon.getOwner().equalsIgnoreCase(player.getName())) {
167 player.send(new SendMessage("This is not your cannon!"));
168 return;
169 }
170
171 if (!player.inventory.contains(2)) {
172 player.send(new SendMessage("You do not have any Cannon balls."));
173 return;
174 }
175
176 int needed = 30 - cannon.getAmmunition();
177
178 if (needed == 0) {
179 player.send(new SendMessage("Your cannon is full."));
180 return;
181 }
182
183 int cannon_balls = player.inventory.computeAmountForId(2);
184
185 if (cannon_balls <= needed) {
186 player.inventory.remove(2, cannon_balls);
187 player.send(new SendMessage("You load the last of your cannon balls"));
188 cannon.setAmmunition(cannon.getAmmunition() + cannon_balls);
189 } else {
190 player.inventory.remove(2, needed);
191 player.send(new SendMessage("You load " + needed + " balls into the cannon."));
192 cannon.setAmmunition(cannon.getAmmunition() + needed);
193 }
194
195 if (cannon.isFiring()) {
196 player.send(new SendMessage("The cannon is already firing!"));
197 return;
198 }
199
200 cannon.setFiring(true);
201 World.schedule(new CannonFireAction(player, cannon));
202 }
203
204 public static Projectile getCannonFire() {
205 Projectile p = new Projectile(53);
206 p.setStartHeight(50);
207 p.setEndHeight(50);
208 return p;
209 }
210
211 public static Npc[] getNpc(Cannon cannon) {
212 ArrayList<Npc> attack = new ArrayList<>();
213
214 for (Npc npc : World.getNpcs()) {
215 if (npc == null) {
216 continue;
217 }
218
219 if (!Utility.withinDistance(npc, cannon, Region.VIEW_DISTANCE)) {
220 continue;
221 }
222
223 if (!npc.definition.isAttackable()) {
224 continue;
225 }
226
227 attack.add(npc);
228 }
229
230 Npc[] npc = new Npc[attack.size()];
231
232 for (int i = 0; i < npc.length; i++) {
233 npc[i] = attack.get(i);
234 }
235
236 return npc;
237 }
238
239 public static void rotate(Cannon cannon) {
240 switch (cannon.getRotation()) {
241 case NORTH:
242 World.sendObjectAnimation(516, cannon.getObject());
243 break;
244 case NORTH_EAST:
245 World.sendObjectAnimation(517, cannon.getObject());
246 break;
247 case EAST:
248 World.sendObjectAnimation(518, cannon.getObject());
249 break;
250 case SOUTH_EAST:
251 World.sendObjectAnimation(519, cannon.getObject());
252 break;
253 case SOUTH:
254 World.sendObjectAnimation(520, cannon.getObject());
255 break;
256 case SOUTH_WEST:
257 World.sendObjectAnimation(521, cannon.getObject());
258 break;
259 case WEST:
260 World.sendObjectAnimation(514, cannon.getObject());
261 break;
262 case NORTH_WEST:
263 World.sendObjectAnimation(515, cannon.getObject());
264 break;
265 }
266 }
267
268}
static void sendObjectAnimation(int animation, GameObject object)
Definition World.java:321
static void schedule(Task task)
Definition World.java:284