Skip to content

Commit

Permalink
Create creature spawner interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jun 22, 2024
1 parent 0d5a3f8 commit d55a075
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 28 deletions.
38 changes: 30 additions & 8 deletions src/games/stendhal/server/entity/creature/Creature.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import games.stendhal.server.entity.item.Corpse;
import games.stendhal.server.entity.item.Item;
import games.stendhal.server.entity.item.StackableItem;
import games.stendhal.server.entity.mapstuff.spawner.CreatureRespawnPoint;
import games.stendhal.server.entity.mapstuff.spawner.CreatureSpawner;
import games.stendhal.server.entity.npc.NPC;
import games.stendhal.server.entity.player.Player;
import games.stendhal.server.entity.slot.EntitySlot;
Expand Down Expand Up @@ -122,7 +122,7 @@ public class Creature extends NPC {
private int corpseWidth = 1;
private int corpseHeight = 1;

private CreatureRespawnPoint point;
private CreatureSpawner spawner;

/** Respawn time in turns */
private int respawnTime;
Expand Down Expand Up @@ -549,18 +549,40 @@ public Map<String, String> getAIProfiles() {
return aiProfiles;
}

public void setRespawnPoint(final CreatureRespawnPoint point) {
this.point = point;
/**
* Registers this creature with a spawner.
*
* @param spawner
* Spawner instance.
*/
public void setSpawner(final CreatureSpawner spawner) {
this.spawner = spawner;
setRespawned(true);
}

@Deprecated
public void setRespawnPoint(final CreatureSpawner spawner) {
setSpawner(spawner);
}

/**
* Gets the spawner of this creature.
*
* @return
* Spawner instance.
*/
public CreatureSpawner getRespawner() {
return spawner;
}

/**
* gets the respan point of this create
*
* @return CreatureRespawnPoint
*/
public CreatureRespawnPoint getRespawnPoint() {
return point;
@Deprecated
public CreatureSpawner getRespawnPoint() {
return getRespawner();
}

/**
Expand Down Expand Up @@ -685,8 +707,8 @@ public void onDead(final Killer killer, final boolean remove) {

notifyRegisteredObjects();

if (this.point != null) {
this.point.notifyDead(this);
if (this.spawner != null) {
this.spawner.onRemoved(this);
}

super.onDead(killer, remove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import games.stendhal.common.Rand;
import games.stendhal.server.core.engine.SingletonRepository;
import games.stendhal.server.core.engine.StendhalRPZone;
import games.stendhal.server.core.events.TurnListener;
import games.stendhal.server.core.rp.StendhalRPAction;
import games.stendhal.server.entity.creature.Creature;
import games.stendhal.server.util.Observer;
Expand All @@ -38,12 +37,7 @@
* pattern is used; the <i>prototypeCreature</i> will be copied to create new
* creatures.
*/
public class CreatureRespawnPoint implements TurnListener {
/** longest possible respawn time in turns. half a year - should be longer than the
* server is up in one phase */
private static final int MAX_RESPAWN_TIME = 200 * 60 * 24 * 30 * 6;
/** minimum respawn time in turns. about 10s */
private static final int MIN_RESPAWN_TIME = 33;
public class CreatureRespawnPoint implements CreatureSpawner {

/** the logger instance. */
private static final Logger logger = Logger.getLogger(CreatureRespawnPoint.class);
Expand Down Expand Up @@ -143,23 +137,34 @@ public void setRespawnTime(final int respawnTime) {
this.respawnTime = respawnTime;
}

/**
* Notifies this respawn point about the death of a creature that was
* spawned here.
*
* @param dead
* The creature that has died
*/
public void notifyDead(final Creature dead) {
@Override
public void onSpawned(Creature spawned) {
spawned.init();
spawned.setRespawnPoint(this);
creatures.add(spawned);
}

@Override
public void onRemoved(Creature removed) {
if (!respawning) {
// start respawning a new creature
respawning = true;
SingletonRepository.getTurnNotifier().notifyInTurns(
calculateNextRespawnTurn(), this);
}
creatures.remove(removed);
}

creatures.remove(dead);
/**
* Notifies this respawn point about the death of a creature that was
* spawned here.
*
* @param dead
* The creature that has died
*/
@Deprecated
public void notifyDead(final Creature dead) {
onRemoved(dead);
}

/**
Expand Down Expand Up @@ -226,6 +231,7 @@ public void setPrototypeCreature(final Creature creature) {
* add observer to observers list
* @param observer - observer to add
*/
@Override
public void addObserver(final Observer observer) {
observers.add(observer);
}
Expand All @@ -234,6 +240,7 @@ public void addObserver(final Observer observer) {
* remove observer from list
* @param observer - observer to remove
*/
@Override
public void removeObserver(final Observer observer) {
observers.remove(observer);
}
Expand All @@ -256,10 +263,7 @@ protected void respawn() {
newentity.registerObjectsForNotification(observers);

if (StendhalRPAction.placeat(zone, newentity, x, y)) {
newentity.init();
newentity.setRespawnPoint(this);

creatures.add(newentity);
onSpawned(newentity);
} else {
// Could not place the creature anywhere.
// Treat it like it just had died.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/***************************************************************************
* Copyright © 2024 - Faiumoni e. V. *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
package games.stendhal.server.entity.mapstuff.spawner;

import games.stendhal.server.core.events.TurnListener;
import games.stendhal.server.entity.creature.Creature;
import games.stendhal.server.util.Observer;


public interface CreatureSpawner extends TurnListener {

/** Longest possible respawn time in turns (half a year - should be longer than the server is up
* in one phase). */
static final int MAX_RESPAWN_TIME = 200 * 60 * 24 * 30 * 6;
/** Minimum respawn time in turns (about 10s) */
static final int MIN_RESPAWN_TIME = 33;


void addObserver(Observer observer);

void removeObserver(Observer observer);

/**
* Notifies this spawner that a creature was spawned.
*
* @param spawned
* The new creature.
*/
void onSpawned(Creature spawned);

/**
* Notifies this spawner about the death of a creature that was spawned with it.
*
* @param removed
* The creature that was removed.
*/
void onRemoved(Creature removed);
}

0 comments on commit d55a075

Please sign in to comment.