Skip to content

Commit

Permalink
Handle prayer activation and deactivation separately
Browse files Browse the repository at this point in the history
  • Loading branch information
mbpolan committed Oct 24, 2023
1 parent 18dad6f commit b018cb0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
22 changes: 13 additions & 9 deletions internal/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,16 +1618,18 @@ func (g *Game) handleGameUpdate() error {
if pe.player.PrayerDrainCounter > pe.player.PrayerDrainResistance() {
pe.player.PrayerDrainCounter -= pe.player.PrayerDrainResistance()

// deduct from the player's prayer points (stat level) and send the client an update
prayerStatLevel := max(pe.player.Skills[model.SkillTypePrayer].StatLevel-1, 0)
pe.player.Skills[model.SkillTypePrayer].StatLevel = prayerStatLevel

pe.DeferSendSkills([]model.SkillType{model.SkillTypePrayer})

// if the player has no more prayer points, deactivate all prayers
if prayerStatLevel == 0 {
clear(pe.player.ActivePrayers)
pe.player.PrayerDrainCounter = 0
pe.DeferSendServerMessage("You have run out of prayer points, you can recharge at an altar.")

// TODO: this should call out to scripts to handle deactivation
}
}
}
Expand Down Expand Up @@ -2232,14 +2234,16 @@ func (g *Game) handleDelayCurrentAction(pe *playerEntity, tickDuration int) {
pe.DeferActionCompletion(tickDuration)
}

// handleTogglePrayer enables or disables a prayer.
func (g *Game) handleTogglePrayer(pe *playerEntity, prayerID, drain int) {
_, ok := pe.player.ActivePrayers[prayerID]
if ok {
delete(pe.player.ActivePrayers, prayerID)
} else {
pe.player.ActivePrayers[prayerID] = drain
}
// handleActivatePrayer enables a prayer.
// Concurrency requirements: (a) game state may be locked and (b) this player should be locked.
func (g *Game) handleActivatePrayer(pe *playerEntity, prayerID, drain int) {
pe.player.ActivePrayers[prayerID] = drain
}

// handleDeactivatePrayer disables a prayer.
// Concurrency requirements: (a) game state may be locked and (b) this player should be locked.
func (g *Game) handleDeactivatePrayer(pe *playerEntity, prayerID int) {
delete(pe.player.ActivePrayers, prayerID)
}

// handlePlayerSwapInventoryItem handles moving an item from one slot to another in a player's inventory.
Expand Down
6 changes: 4 additions & 2 deletions internal/game/game_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type ScriptHandler interface {
// handleDelayCurrentAction blocks the player from performing other actions until a set amount of game ticks have
// elapsed.
handleDelayCurrentAction(pe *playerEntity, tickDuration int)
// handleTogglePrayer enables or disables a prayer.
handleTogglePrayer(pe *playerEntity, prayerID, drain int)
// handleActivatePrayer enables a prayer.
handleActivatePrayer(pe *playerEntity, prayerID, drain int)
// handleDeactivatePrayer disables a prayer.
handleDeactivatePrayer(pe *playerEntity, prayerID int)
}
19 changes: 17 additions & 2 deletions internal/game/script_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,27 @@ func (s *ScriptManager) registerPlayerModel(l *lua.LState) {
s.handler.handleTeleportPlayer(pe, model.Vector3D{X: x, Y: y, Z: z})
return 0
},
"toggle_prayer": func(state *lua.LState) int {
"has_prayer_active": func(state *lua.LState) int {
pe := state.CheckUserData(1).Value.(*playerEntity)
prayerID := state.CheckInt(2)

_, ok := pe.player.ActivePrayers[prayerID]
state.Push(lua.LBool(ok))
return 1
},
"activate_prayer": func(state *lua.LState) int {
pe := state.CheckUserData(1).Value.(*playerEntity)
prayerID := state.CheckInt(2)
drain := state.CheckInt(3)

s.handler.handleTogglePrayer(pe, prayerID, drain)
s.handler.handleActivatePrayer(pe, prayerID, drain)
return 0
},
"deactivate_prayer": func(state *lua.LState) int {
pe := state.CheckUserData(1).Value.(*playerEntity)
prayerID := state.CheckInt(2)

s.handler.handleDeactivatePrayer(pe, prayerID)
return 0
},
}))
Expand Down
11 changes: 9 additions & 2 deletions scripts/interfaces/prayers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ function interface_5608_on_action(player, interface)
return
end

-- TODO: check if player meets level requirements
-- determine if this prayer is to be activated or deactivated
local activate = player:has_prayer_active(prayer_id) == false

-- ensure the player has at least one prayer point before activating a prayer
if activate and player:stat_level(SKILL_PRAYER) == 0 then
player:server_message("You need to recharge your Prayer at an altar.")
return
end

if prayer_id == PRAYER_THICK_SKIN then
prayer_thick_skin(player)
prayer_thick_skin(player, activate)
else
player:server_message("This prayer is not yet available!")
end
Expand Down
14 changes: 12 additions & 2 deletions scripts/prayers/thick_skin.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
--- Handles activating the thick spin prayer.
-- @param player The player who activated the prayer
function prayer_thick_skin(player)
player:toggle_prayer(PRAYER_THICK_SKIN, 3)
-- @param activate true if the prayer should be activated, false if deactivated
function prayer_thick_skin(player, activate)
if activate then
local ok = skill_level_minimum(player, SKILL_PRAYER, 1, "You need prayer level 1 to use this prayer.")
if not ok then
return true
end

player:activate_prayer(PRAYER_THICK_SKIN, 3)
else
player:deactivate_prayer(PRAYER_THICK_SKIN)
end
end

0 comments on commit b018cb0

Please sign in to comment.