62public abstract class FarmingPatch {
63 private static final double CLEARING_EXPERIENCE = 4;
65 protected Plant plant;
72 protected boolean fullyGrown;
75 protected final Player player;
83 this.boundaries = boundaries;
91 stage = stage.getPreviousStage();
92 zone.sendPatchConfigs(player);
101 int elapsed = (int) timer.elapsedTime(TimeUnit.SECONDS) / 12;
102 int growthStates = plant.getEndingState() - plant.getStartingState();
103 int growthTime = plant.getGrowthTime();
105 if (elapsed > growthTime) {
106 elapsed = growthTime;
109 int completed = growthStates * elapsed / growthTime;
111 if (growth != completed) {
112 fullyGrown = completed == growthStates;
117 zone.sendPatchConfigs(player);
121 private void simulate(
long elapsed) {
124 if (plant ==
null || isDead()) {
128 int growthStates = plant.getEndingState() - plant.getStartingState();
129 int growthTime = plant.getGrowthTime();
131 if (elapsed > growthTime) {
132 elapsed = growthTime;
135 int completed = (int) (growthStates * elapsed / growthTime);
137 for (
int i = growth; i <= completed; i++) {
138 fullyGrown = i == growthStates;
149 protected void onGrowth() {}
151 public boolean clickObject(
int opcode) {
152 if (opcode == 1 && (needsWeeding() || isDead())) {
163 public boolean itemOnObject(Item item,
int index) {
168 return putCompost(item.getId(), index) || plantSeed(item.getId(), index);
171 private void clearPatch() {
172 int animation, delay;
175 player.dialogueFactory.sendStatement(
"This patch is already empty.").execute();
181 player.dialogueFactory.sendStatement(
"You need a spade to clear this patch.").execute();
189 player.dialogueFactory.sendStatement(
"You need a rake to clear this patch.").execute();
197 player.action.execute(createClearingAction(player, animation, delay));
200 private boolean plantSeed(
int seedId,
int slot) {
201 if (player.locking.locked()) {
205 Plant toPlant = plantForSeed(seedId);
207 if (toPlant ==
null) {
212 player.message(
"You can't plant a seed here.");
216 if (toPlant.getLevelRequired() > player.skills.getLevel(
Skill.
FARMING)) {
217 player.dialogueFactory.sendStatement(
"You need a farming level of " + toPlant.getLevelRequired() +
" to plant this seed.").execute();
222 player.dialogueFactory.sendStatement(
"You need a seed dibber to plant seeds here.").execute();
226 if (!player.inventory.contains(toPlant.getSeedId(), toPlant.getSeedAmount())) {
227 player.dialogueFactory.sendStatement(
"You need at least " + toPlant.getSeedAmount() +
" seeds to plant here.").execute();
235 player.locking.lock(3 * 3 / 5);
237 player.inventory.remove(
new Item(seedId, toPlant.getSeedAmount()), slot,
true);
240 player.resetAnimation();
242 zone.sendPatchConfigs(player);
247 private boolean putCompost(
int itemId,
int slot) {
248 if (player.locking.locked()) {
258 if (needsWeeding() || isComposted()) {
259 player.message(
"This patch doesn't need compost.");
263 player.locking.lock(7 * 3 / 5);
267 player.message(
"You pour some " + type.name().toLowerCase() +
" on the patch.");
273 private void inspect() {
275 player.dialogueFactory.sendStatement(
"This plant is diseased. Use a plant cure on it to cure it, ",
"or clear the patch with a spade.").execute();
277 }
else if (isDead()) {
278 player.dialogueFactory.sendStatement(
"This plant is dead. You did not cure it while it was diseased.",
"Clear the patch with a spade.").execute();
282 String patch = getClass().getSimpleName().replace(
"Patch",
"").toLowerCase();
285 if (needsWeeding()) {
286 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has not been treated.",
"The patches needs weeding.").execute();
287 }
else if (isWatered() && isEmpty()) {
289 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with water",
"and compost. The patches is empty and weeded.").execute();
291 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with water",
"and supercompost. The patches is empty and weeded.").execute();
293 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with water",
"and ultracompost. The patches is empty and weeded.").execute();
295 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with water.",
"The patches is empty and weeded.").execute();
297 }
else if (isComposted() && isEmpty()) {
299 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with compost.",
"The patches is empty and weeded.").execute();
301 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with supercompost.",
"The patches is empty and weeded.").execute();
303 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has been treated with ultracompost.",
"The patches is empty and weeded.").execute();
305 }
else if (isEmpty()) {
306 player.dialogueFactory.sendStatement(
"This is " + patch +
" patch. The soil has not been treated.",
"The patches is empty and weeded.").execute();
308 player.locking.lock(5);
309 player.message(
"You bend down and start to inspect the patch...");
310 player.animate(1331);
314 public void execute() {
315 int index = growth % plant.getInspectMessages().length;
316 if (growth > index) index = growth - index - 1;
317 player.dialogueFactory.sendStatement(plant.getInspectMessages()[index]).execute();
322 public void onCancel(
boolean logout) {
323 player.animate(1332);
329 public int getConfig() {
330 if (isEmpty() || needsWeeding()) {
331 return stage.ordinal();
333 return (state.ordinal() << 6) | (plant.getStartingState() + growth);
336 protected void resetPatch() {
346 public abstract Plant plantForSeed(
int seedId);
348 public void setNormal() {
352 public void setWatered() {
356 public void setDiseased() {
360 public void setDead() {
364 public void setEmpty() {
369 public boolean isNormal() {
373 public boolean isWatered() {
377 public boolean isDiseased() {
381 public boolean isDead() {
385 public boolean isEmpty() {
389 public boolean isGrowing() {
390 return !isEmpty() && !needsWeeding();
393 public boolean isComposted() {
397 public boolean needsWeeding() {
401 public boolean within(
Position position) {
403 if (
Utility.inside(boundary, position))
412 protected void onSchedule() {
413 player.animate(animation);
417 public void execute() {
418 if (needsWeeding()) {
419 stage = stage.getNextStage();
420 player.inventory.add(6055, 1);
426 player.animate(animation);
428 zone.sendPatchConfigs(player);
431 player.message(
"You clear the patch.");
439 public void onCancel(
boolean logut) {
440 player.resetAnimation();
449 public String getName() {
450 return "Farming Clear";
455 public JsonObject toJson() {
456 JsonObject
object =
new JsonObject();
457 object.addProperty(
"stage", stage.name());
458 object.addProperty(
"state", state.name());
459 object.addProperty(
"compost", compost.name());
460 object.addProperty(
"timer", timer.getCachedTime());
461 object.addProperty(
"fully-grown", fullyGrown);
462 object.addProperty(
"growth", growth);
464 object.addProperty(
"plant", plant.getSeedId());
469 public void fromJson(JsonObject
object) {
470 stage =
FarmingStage.valueOf(
object.
get(
"stage").getAsString());
471 state =
FarmingState.valueOf(
object.
get(
"state").getAsString());
472 compost =
CompostType.valueOf(
object.
get(
"compost").getAsString());
473 fullyGrown =
object.get(
"fully-grown").getAsBoolean();
474 growth =
object.get(
"growth").getAsInt();
476 if (
object.has(
"plant")) {
477 plant = plantForSeed(
object.
get(
"plant").getAsInt());
480 long cached =
object.get(
"timer").getAsLong();
481 simulate(TimeUnit.SECONDS.convert(System.nanoTime() - cached, TimeUnit.NANOSECONDS) / 12);
482 timer.setCachedTime(cached);