mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 16:49:04 +00:00
Add API plugin with common functionality
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package org.apollo.game.plugins.api
|
package org.apollo.game.plugins.api
|
||||||
|
|
||||||
|
import org.apollo.game.model.entity.Player
|
||||||
import org.apollo.game.model.entity.Skill
|
import org.apollo.game.model.entity.Skill
|
||||||
import org.apollo.game.model.entity.SkillSet
|
import org.apollo.game.model.entity.SkillSet
|
||||||
|
|
||||||
|
val Player.skills: SkillSet get() = skillSet
|
||||||
val SkillSet.attack: Skill get() = getSkill(Skill.ATTACK)
|
val SkillSet.attack: Skill get() = getSkill(Skill.ATTACK)
|
||||||
val SkillSet.defence: Skill get() = getSkill(Skill.DEFENCE)
|
val SkillSet.defence: Skill get() = getSkill(Skill.DEFENCE)
|
||||||
val SkillSet.strength: Skill get() = getSkill(Skill.STRENGTH)
|
val SkillSet.strength: Skill get() = getSkill(Skill.STRENGTH)
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import java.util.*
|
||||||
|
|
||||||
|
val RAND = Random()
|
||||||
|
|
||||||
|
fun rand(bounds: Int): Int {
|
||||||
|
return RAND.nextInt(bounds)
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import org.apollo.game.model.Position
|
||||||
|
import org.apollo.game.model.World
|
||||||
|
import org.apollo.game.model.area.Region
|
||||||
|
import org.apollo.game.model.entity.Entity
|
||||||
|
import org.apollo.game.model.entity.EntityType
|
||||||
|
import org.apollo.game.model.entity.EntityType.DYNAMIC_OBJECT
|
||||||
|
import org.apollo.game.model.entity.EntityType.STATIC_OBJECT
|
||||||
|
import org.apollo.game.model.entity.obj.DynamicGameObject
|
||||||
|
import org.apollo.game.model.entity.obj.GameObject
|
||||||
|
import org.apollo.game.model.entity.obj.StaticGameObject
|
||||||
|
import org.apollo.game.scheduling.ScheduledTask
|
||||||
|
import java.lang.IllegalArgumentException
|
||||||
|
import java.util.*
|
||||||
|
import java.util.stream.Stream
|
||||||
|
|
||||||
|
fun <T : Entity> Region.find(position: Position, pred: (T) -> Boolean, vararg types: EntityType): Stream<T> {
|
||||||
|
val result = this.getEntities<T>(position, *types)
|
||||||
|
|
||||||
|
return result.stream()
|
||||||
|
.filter(pred::invoke)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Region.findObjects(position: Position, id: Int): Stream<GameObject> {
|
||||||
|
return find<GameObject>(position, { it.id == id }, DYNAMIC_OBJECT, STATIC_OBJECT)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Region.findObject(position: Position, id: Int): Optional<GameObject> {
|
||||||
|
return find<GameObject>(position, { it.id == id }, DYNAMIC_OBJECT, STATIC_OBJECT)
|
||||||
|
.findFirst()
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExpireObjectTask(
|
||||||
|
val world: World,
|
||||||
|
val obj: GameObject,
|
||||||
|
val replacement: GameObject,
|
||||||
|
val respawnDelay: Int
|
||||||
|
) : ScheduledTask(0, true) {
|
||||||
|
|
||||||
|
private var respawning: Boolean = false
|
||||||
|
|
||||||
|
override fun execute() {
|
||||||
|
val region = world.regionRepository.fromPosition(obj.position)
|
||||||
|
|
||||||
|
if (!respawning) {
|
||||||
|
world.spawn(replacement)
|
||||||
|
respawning = true
|
||||||
|
setDelay(respawnDelay)
|
||||||
|
} else {
|
||||||
|
region.removeEntity(replacement, false)
|
||||||
|
world.spawn(obj)
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun World.expireObject(obj: GameObject, replacement: Int, respawnDelay: Int) {
|
||||||
|
val replacementObj = DynamicGameObject.createPublic(this, replacement, obj.position, obj.type, obj.orientation)
|
||||||
|
val respawnedObj = DynamicGameObject.createPublic(this, obj.id, obj.position, obj.type, obj.orientation)
|
||||||
|
|
||||||
|
schedule(ExpireObjectTask(this, obj, replacementObj, respawnDelay))
|
||||||
|
}
|
||||||
@@ -335,12 +335,22 @@ public final class Region {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes an {@link Entity} from this Region.
|
* Removes an {@link Entity} from this Region and notifies listeners.
|
||||||
*
|
*
|
||||||
* @param entity The Entity.
|
* @param entity The Entity.
|
||||||
* @throws IllegalArgumentException If the Entity does not belong in this Region, or if it was never added.
|
* @throws IllegalArgumentException If the Entity does not belong in this Region, or if it was never added.
|
||||||
*/
|
*/
|
||||||
public void removeEntity(Entity entity) {
|
public void removeEntity(Entity entity) {
|
||||||
|
removeEntity(entity, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an {@link Entity} from this Region.
|
||||||
|
*
|
||||||
|
* @param entity The Entity.
|
||||||
|
* @throws IllegalArgumentException If the Entity does not belong in this Region, or if it was never added.
|
||||||
|
*/
|
||||||
|
public void removeEntity(Entity entity, boolean notifyListeners) {
|
||||||
EntityType type = entity.getEntityType();
|
EntityType type = entity.getEntityType();
|
||||||
if (type.isTransient()) {
|
if (type.isTransient()) {
|
||||||
throw new IllegalArgumentException("Tried to remove a transient Entity (" + entity + ") from " +
|
throw new IllegalArgumentException("Tried to remove a transient Entity (" + entity + ") from " +
|
||||||
@@ -356,7 +366,9 @@ public final class Region {
|
|||||||
throw new IllegalArgumentException("Entity (" + entity + ") belongs in (" + this + ") but does not exist.");
|
throw new IllegalArgumentException("Entity (" + entity + ") belongs in (" + this + ") but does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyListeners(entity, EntityUpdateType.REMOVE);
|
if (notifyListeners) {
|
||||||
|
notifyListeners(entity, EntityUpdateType.REMOVE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import kotlinx.coroutines.experimental.CommonPool
|
|||||||
import kotlinx.coroutines.experimental.Job
|
import kotlinx.coroutines.experimental.Job
|
||||||
import kotlinx.coroutines.experimental.channels.Channel
|
import kotlinx.coroutines.experimental.channels.Channel
|
||||||
import kotlinx.coroutines.experimental.launch
|
import kotlinx.coroutines.experimental.launch
|
||||||
import kotlinx.coroutines.experimental.selects.select
|
import kotlinx.coroutines.experimental.runBlocking
|
||||||
|
|
||||||
class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspend () -> Unit) {
|
class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspend () -> Unit) {
|
||||||
var job: Job? = null
|
var job: Job? = null
|
||||||
@@ -27,10 +27,10 @@ class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspe
|
|||||||
val action = actionSupplier.invoke()
|
val action = actionSupplier.invoke()
|
||||||
|
|
||||||
job = launch(CommonPool) {
|
job = launch(CommonPool) {
|
||||||
select {
|
run {
|
||||||
pulseChannel.onReceive {
|
while (action.isRunning) {
|
||||||
|
pulseChannel.receive()
|
||||||
callback()
|
callback()
|
||||||
action.stop()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,12 +45,14 @@ class AsyncActionRunner(val actionSupplier: () -> Action<*>, val callback: suspe
|
|||||||
pulseChannel.close()
|
pulseChannel.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun wait(pulses: Int = 1) {
|
suspend fun wait(pulses: Int = 1, pulseCallback: (() -> Unit)? = null) {
|
||||||
var remainingPulses = pulses
|
var remainingPulses = pulses
|
||||||
|
|
||||||
while (remainingPulses > 0) {
|
while (remainingPulses > 0) {
|
||||||
val numPulses = pulseChannel.receive()
|
val numPulses = pulseChannel.receive()
|
||||||
remainingPulses -= numPulses
|
remainingPulses -= numPulses
|
||||||
|
|
||||||
|
pulseCallback?.invoke()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package org.apollo.game.action
|
|||||||
interface AsyncActionTrait {
|
interface AsyncActionTrait {
|
||||||
val runner: AsyncActionRunner
|
val runner: AsyncActionRunner
|
||||||
|
|
||||||
suspend fun wait(pulses: Int = 1) {
|
suspend fun wait(pulses: Int = 1, pulseCallback: (() -> Unit)? = null) {
|
||||||
runner.wait(pulses)
|
runner.wait(pulses, pulseCallback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,12 +15,18 @@ abstract class AsyncDistancedAction<T : Mob> : DistancedAction<T>, AsyncActionTr
|
|||||||
|
|
||||||
abstract suspend fun executeActionAsync()
|
abstract suspend fun executeActionAsync()
|
||||||
|
|
||||||
|
open protected fun start() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
override fun stop() {
|
override fun stop() {
|
||||||
super.stop()
|
super.stop()
|
||||||
runner.stop()
|
runner.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun executeAction() {
|
override fun executeAction() {
|
||||||
|
start()
|
||||||
|
|
||||||
if (!runner.started()) {
|
if (!runner.started()) {
|
||||||
runner.start()
|
runner.start()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user