RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
FlowerPoker.java
1package com.osroyale.content.gambling.impl;
2
3import com.osroyale.content.gambling.Flowers;
4import com.osroyale.content.gambling.Gamble;
5import com.osroyale.content.gambling.GambleStage;
6import com.osroyale.game.task.Task;
7import com.osroyale.game.world.World;
8import com.osroyale.game.world.entity.mob.player.Player;
9import com.osroyale.game.world.object.CustomGameObject;
10import com.osroyale.game.world.position.Position;
11import com.osroyale.util.Utility;
12
13import java.util.ArrayList;
14import java.util.Collections;
15import java.util.HashMap;
16import java.util.Map;
17
18import static com.osroyale.content.gambling.GambleManager.GAMBLING_ZONE;
19
54
55public class FlowerPoker extends Gamble {
56
57 public FlowerPoker(Player host, Player opponent) {
58 super(host, opponent);
59 }
60
61 @Override
62 public String toString() {
63 return "Flower Poker";
64 }
65
66 @Override
67 public void gamble() {
68 getHost().getGambling().getFlowers().clear();
69 getOpponent().getGambling().getFlowers().clear();
70
71 getHost().getGambling().getGameFlowers().clear();
72 getOpponent().getGambling().getGameFlowers().clear();
73
74 for (int i = 0; i < 5; i++) {
75 Flowers hostFlower = Flowers.values()[Utility.random(Flowers.values().length - 1)];
76 Flowers opponentFlower = Flowers.values()[Utility.random(Flowers.values().length - 1)];
77
78 while (hostFlower == Flowers.BLACK || hostFlower == Flowers.WHITE) {
79 hostFlower = Flowers.values()[Utility.random(Flowers.values().length - 1)];
80 }
81
82 while (opponentFlower == Flowers.BLACK || hostFlower == Flowers.WHITE) {
83 opponentFlower = Flowers.values()[Utility.random(Flowers.values().length - 1)];
84 }
85
86 getHost().getGambling().getFlowers().add(hostFlower);
87 getOpponent().getGambling().getFlowers().add(opponentFlower);
88 }
89 final Ranking hostResult = getRank(getHost());
90 final Ranking opponentResult = getRank(getOpponent());
91
92 System.out.println("Looking for place to play");
93 boolean[] canPlay = { false, false };
94 Position positionOne, positionTwo;
95 do {
96 int maxX = GAMBLING_ZONE.getMaximumX() - GAMBLING_ZONE.getMinimumX();
97 int maxY = GAMBLING_ZONE.getMaximumY() - GAMBLING_ZONE.getMinimumY();
98 System.out.println("maxX: " + maxX + ", " + maxY);
99 positionOne = new Position(GAMBLING_ZONE.getMinimumX() + Utility.random(maxX), GAMBLING_ZONE.getMinimumY() + Utility.random(maxY));
100 positionTwo = positionOne.create(positionOne.getX() + 1, positionOne.getY());
101 canPlay[0] = getHost().getGambling().canPlayFlowerPokerAtPositon(getHost(), positionTwo);
102 canPlay[1] = getHost().getGambling().canPlayFlowerPokerAtPositon(getHost(), positionOne);
103
104 System.out.println(canPlay[0] + " - " + canPlay[1]);
105 } while(canPlay[0] == false || canPlay[1] == false);
106
107 System.out.println("Found a place to play");
108
109 Position finalPositionOne = positionOne;
110 Position finalPositionTwo = positionTwo;
111 World.schedule(new Task(1) {
112 int time = 0;
113
114 @Override
115 protected void execute() {
116 if(getHost().getGambling().getStage() != GambleStage.IN_PROGRESS) {
117 cancel();
118 return;
119 }
120 switch (time) {
121 case 0:
122 getHost().move(finalPositionOne);
123 getOpponent().move(finalPositionTwo);
124 break;
125 case 1:
126 plant(this);
127 break;
128 case 29:
129 getHost().speak("I got " + hostResult.name().toLowerCase().replaceAll("_", " "));
130 getOpponent().speak("I got " + opponentResult.name().toLowerCase().replaceAll("_", " "));
131 break;
132 case 30:
133 getHost().getGambling().finish(getHost(), getOpponent(), hostResult.ordinal(), opponentResult.ordinal());
134 cancel();
135 break;
136 }
137 time++;
138 }
139 });
140 }
141
142 private void plant(Task gambleTask) {
143 World.schedule(new Task(1) {
144 int time = 0;
145 int type = 0;
146 Flowers hostFlower = getHost().getGambling().getFlowers().get(type);
147 Flowers opponentFlower = getOpponent().getGambling().getFlowers().get(type);
148
149 Player[] players = new Player[] { getHost(), getOpponent() };
150
151 @Override
152 protected void execute() {
153 if(getHost().getGambling().getStage() != GambleStage.IN_PROGRESS) {
154 cancel();
155 return;
156 }
157
158 switch (time) {
159 case 1:
160 for(Player player : players)
161 player.animate(827);
162 break;
163 case 2:
164 hostFlower = getHost().getGambling().getFlowers().get(type);
165 opponentFlower = getOpponent().getGambling().getFlowers().get(type);
166
167 if (hostFlower == null || opponentFlower == null) {
168 cancel();
169 break;
170 }
171
172 for(Player player : players) {
173 Flowers flowers = player.equals(getHost()) ? hostFlower : opponentFlower;
174
175 final CustomGameObject gameFlower = new CustomGameObject(flowers.getId(), player.getPosition().copy());
176 gameFlower.register();
177 player.getGambling().getGameFlowers().add(gameFlower);
178
179 player.movement.walkTo(player.getPosition().south());
180 }
181
182 break;
183 case 3:
184 for(Player player : players) {
185 Flowers flowers = player.equals(getHost()) ? hostFlower : opponentFlower;
186 player.speak("I planted a " + Utility.formatText(flowers.name().toLowerCase()) + " flower.");
187 }
188 break;
189 case 4:
190
191 if(hostFlower.name().equalsIgnoreCase("WHITE") || hostFlower.name().equalsIgnoreCase("BLACK") ||
192 opponentFlower.name().equalsIgnoreCase("WHITE") || opponentFlower.name().equalsIgnoreCase("BLACK")) {
193 getHost().getGambling().finish(getHost(), getOpponent(), 0, 0);
194 gambleTask.cancel();
195 cancel();
196 break;
197 }
198
199 if (type == 4) {
200 cancel();
201 break;
202 }
203
204 time = 0;
205 type++;
206 break;
207 }
208 time++;
209 }
210 });
211 }
212
213private enum Ranking {
214 NOTHING,
215 PAIR,
216 TWO_PAIR,
217 THREE_OF_KIND,
218 FULL_HOUSE,
219 FOUR_OF_KIND,
220 ROYAL_KIND,
221 }
222
223 private static Ranking getRank(Player player) {
224 ArrayList<Flowers> flowers = new ArrayList<Flowers>();
225 flowers.addAll(player.getGambling().getFlowers());
226 Collections.sort(flowers);
227 Map<Integer, Integer> pairs = getPairs(flowers);
228 for (int i = 0; i < pairs.size(); i++) {
229 if (pairs.get(i) == null) {
230 continue;
231 }
232 if (pairs.get(i).intValue() == 5) {
233 return Ranking.ROYAL_KIND;
234 }
235 }
236 if (pairs.size() == 2) {
237 if ((pairs.get(0).intValue() == 3 && pairs.get(1).intValue() == 2) || (pairs.get(1).intValue() == 3 && pairs.get(0).intValue() == 2)) {
238 return Ranking.FULL_HOUSE;
239 }
240 }
241 int totalPairs = 0;
242 for (int i = 0; i < pairs.size(); i++) {
243 if (pairs.get(i) == null) {
244 continue;
245 }
246 if (pairs.get(i).intValue() == 3) {
247 return Ranking.THREE_OF_KIND;
248 }
249 if (pairs.get(i).intValue() == 2) {
250 totalPairs++;
251 }
252 }
253 if (totalPairs == 2) {
254 return Ranking.TWO_PAIR;
255 }
256 if (totalPairs == 1) {
257 return Ranking.PAIR;
258 }
259 return Ranking.NOTHING;
260 }
261
262 private static Map<Integer, Integer> getPairs(ArrayList<Flowers> list) {
263 Map<Integer, Integer> finalPairs = new HashMap<Integer, Integer>();
264 int[] pairs = new int[14];
265 for (Flowers flower : list) {
266 pairs[flower.ordinal()]++;
267 }
268 int slot = 0;
269 for (int i = 0; i < pairs.length; i++) {
270 if (pairs[i] >= 2) {
271 finalPairs.put(slot, pairs[i]);
272 slot++;
273 }
274 }
275 return finalPairs;
276 }
277
278}
synchronized final void cancel()
Definition Task.java:147
static void schedule(Task task)
Definition World.java:284
static Position create(int x, int y, int z)