-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ground items kotlin #372
Open
rmcmk
wants to merge
1
commit into
kotlin-experiments
Choose a base branch
from
ground-items-kotlin
base: kotlin-experiments
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ground items kotlin #372
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
plugin { | ||
name = "ground_item" | ||
packageName = "org.apollo.game.plugin.entity" | ||
authors = ["Ryley"] | ||
} |
13 changes: 13 additions & 0 deletions
13
game/plugin/entity/ground-item/src/ground_item_action.plugin.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import org.apollo.game.message.impl.InventoryItemMessage | ||
|
||
on { InventoryItemMessage::class } | ||
.where { option == 5 } | ||
.then { | ||
// This is just a stub, for now. | ||
// Several other things need to be done here: | ||
// - Items may only be dropped from your inventory | ||
// - Items dropped must be removed from your inventory | ||
// - Items must be checked to ensure they have a 'drop' option | ||
val item = it.inventory.get(slot) | ||
it.addGroundItem(item, it.position) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import org.apollo.game.GameConstants | ||
import org.apollo.game.model.entity.GroundItem | ||
import org.apollo.game.scheduling.ScheduledTask | ||
|
||
/** | ||
* A [ScheduledTask] that manages the globalization and expiration of [GroundItem]s. | ||
*/ | ||
class GroundItemSynchronizationTask(private val groundItem: GroundItem) : ScheduledTask(DELAY, false) { | ||
|
||
companion object { | ||
|
||
/** | ||
* The delay between executions of this task, in pulses. | ||
*/ | ||
const val DELAY = 1 | ||
|
||
/** | ||
* The amount of time, in pulses, in which this [GroundItem] will be globally visible. | ||
*/ | ||
const val TRADABLE_TIME_UNTIL_GLOBAL = 60000 / GameConstants.PULSE_DELAY | ||
|
||
/** | ||
* The amount of time, in pulses, in which this [GroundItem] will expire and be removed from the [World]. | ||
*/ | ||
const val UNTRADABLE_TIME_UNTIL_EXPIRE = 180000 / GameConstants.PULSE_DELAY | ||
|
||
/** | ||
* The amount of time, in pulses, in which this [GroundItem] will expire and be removed from the [World]. | ||
*/ | ||
const val TIME_UNTIL_EXPIRE = 180000 / GameConstants.PULSE_DELAY | ||
} | ||
|
||
/** | ||
* The amount of game pulses this [ScheduledTask] has been alive. | ||
*/ | ||
private var pulses = 0 | ||
|
||
override fun execute() { | ||
val world = groundItem.world | ||
val owner = world.playerRepository[groundItem.ownerIndex] | ||
val untradable = false // TODO: item.getDefinition().isTradable(); | ||
|
||
if (!groundItem.isAvailable) { | ||
stop() | ||
return | ||
} | ||
|
||
// Untradable items never go global | ||
if (untradable) { | ||
if (pulses >= UNTRADABLE_TIME_UNTIL_EXPIRE) { | ||
world.removeGroundItem(owner, groundItem) | ||
stop() | ||
} | ||
return | ||
} | ||
|
||
if (groundItem.isGlobal) { | ||
if (pulses >= TIME_UNTIL_EXPIRE) { | ||
world.removeGroundItem(owner, groundItem) | ||
stop() | ||
} | ||
} else { | ||
if (pulses >= TRADABLE_TIME_UNTIL_GLOBAL) { | ||
groundItem.globalize() | ||
world.addGroundItem(owner, groundItem) | ||
} | ||
} | ||
|
||
pulses++ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import org.apollo.game.message.impl.RemoveTileItemMessage | ||
import org.apollo.game.message.impl.SendTileItemMessage | ||
import org.apollo.game.model.Item | ||
import org.apollo.game.model.Position | ||
import org.apollo.game.model.World | ||
import org.apollo.game.model.entity.GroundItem | ||
import org.apollo.game.model.entity.Player | ||
|
||
/** | ||
* Spawns a new local [GroundItem] for this Player at the specified [Position]. | ||
*/ | ||
fun Player.addGroundItem(item: Item, position: Position) { | ||
world.addGroundItem(this, GroundItem.dropped(world, position, item, this)) | ||
} | ||
|
||
internal fun World.addGroundItem(player: Player, item: GroundItem) { | ||
val region = regionRepository.fromPosition(item.position) | ||
|
||
if (item.isGlobal) { | ||
region.addEntity(item, true) | ||
return | ||
} | ||
|
||
groundItems.computeIfAbsent(player.encodedName, { HashSet<GroundItem>() }) += item | ||
|
||
val offset = region.getPositionOffset(item) | ||
player.send(SendTileItemMessage(item.item, offset)) | ||
|
||
schedule(GroundItemSynchronizationTask(item)) | ||
} | ||
|
||
internal fun World.removeGroundItem(player: Player, item: GroundItem) { | ||
val region = regionRepository.fromPosition(item.position) | ||
|
||
if (item.isGlobal) { | ||
region.removeEntity(item) | ||
} | ||
|
||
val items = groundItems[player.encodedName] ?: return | ||
items -= item | ||
|
||
val offset = region.getPositionOffset(item) | ||
player.send(RemoveTileItemMessage(item.item, offset)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be better done as a single task that polls GroundItem's from a queue every tick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind explaining how that queue should work? I don't think to queue them makes much sense here as all this task is doing is keeping the ground items state in sync with the client at the appropriate time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So there's one GroundItemSyncTask at all times and new ones are just added to its queue, rather than creating a new task for each item. Downside of this is we have to implement what is essentially scheduling logic in the task itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of defeats the purpose of the task scheduler, does it not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Partially, yes; that is the downside