From 0d1773ef9142b5735258c6bbd374c0d1f90ace26 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Wed, 25 May 2022 21:55:52 +0200 Subject: [PATCH] rpg - fix xp bug --- maps/mountain_fortress_v3/locomotive.lua | 3 +- modules/rpg/functions.lua | 53 ++++++++++++++++++++ modules/rpg/main.lua | 10 +++- modules/rpg/spells.lua | 61 +++++++++++++++--------- 4 files changed, 103 insertions(+), 24 deletions(-) diff --git a/maps/mountain_fortress_v3/locomotive.lua b/maps/mountain_fortress_v3/locomotive.lua index 36e989aa..71ae5039 100644 --- a/maps/mountain_fortress_v3/locomotive.lua +++ b/maps/mountain_fortress_v3/locomotive.lua @@ -113,7 +113,7 @@ local function give_passive_xp(data) if not validate_player(player) then return end - if player.afk_time < 200 then + if player.afk_time < 200 and not RPG.get_last_spell_cast(player) then if Math2D.bounding_box.contains_point(area, player.position) or player.surface.index == loco_surface.index then if player.surface.index == loco_surface.index then PermissionGroups.add_player_to_permission_group(player, 'limited') @@ -135,6 +135,7 @@ local function give_passive_xp(data) } rpg[player.index].xp_since_last_floaty_text = 0 rpg[player.index].last_floaty_text = game.tick + visuals_delay + RPG.set_last_spell_cast(player, player.position) if player.gui.screen[rpg_main_frame] then local f = player.gui.screen[rpg_main_frame] local d = Gui.get_data(f) diff --git a/modules/rpg/functions.lua b/modules/rpg/functions.lua index bb9dc85c..fa9556fb 100644 --- a/modules/rpg/functions.lua +++ b/modules/rpg/functions.lua @@ -6,6 +6,7 @@ local BiterHealthBooster = require 'modules.biter_health_booster_v2' local P = require 'utils.player_modifiers' local Token = require 'utils.token' local Alert = require 'utils.alert' +local Math2D = require 'math2d' local level_up_floating_text_color = {0, 205, 0} local visuals_delay = Public.visuals_delay @@ -368,6 +369,56 @@ function Public.validate_player(player) return true end +function Public.set_last_spell_cast(player, position) + if not player or not player.valid then + return false + end + if not position then + return false + end + + if not type(position) == 'table' then + return false + end + + local rpg_t = Public.get_value_from_player(player.index) + + rpg_t.last_spell_cast = position + + return true +end + +function Public.get_last_spell_cast(player) + if not player or not player.valid then + return false + end + + local rpg_t = Public.get_value_from_player(player.index) + + if not rpg_t then + return + end + + if not rpg_t.last_spell_cast then + return false + end + + local position = player.position + local cast_radius = 1 + local cast_area = { + left_top = {x = rpg_t.last_spell_cast.x - cast_radius, y = rpg_t.last_spell_cast.y - cast_radius}, + right_bottom = {x = rpg_t.last_spell_cast.x + cast_radius, y = rpg_t.last_spell_cast.y + cast_radius} + } + + if rpg_t.last_spell_cast then + if Math2D.bounding_box.contains_point(cast_area, position) then + return true + else + return false + end + end +end + function Public.remove_mana(player, mana_to_remove) local rpg_extra = Public.get('rpg_extra') local rpg_t = Public.get_value_from_player(player.index) @@ -991,6 +1042,7 @@ function Public.rpg_reset_player(player, one_time_reset) bonus = rpg_extra.breached_walls or 1, rotated_entity_delay = 0, last_mined_entity_position = {x = 0, y = 0}, + last_spell_cast = {x = 0, y = 0}, show_bars = false, stone_path = false, aoe_punch = false, @@ -1032,6 +1084,7 @@ function Public.rpg_reset_player(player, one_time_reset) bonus = 1, rotated_entity_delay = 0, last_mined_entity_position = {x = 0, y = 0}, + last_spell_cast = {x = 0, y = 0}, show_bars = false, stone_path = false, aoe_punch = false, diff --git a/modules/rpg/main.lua b/modules/rpg/main.lua index 1324a470..deba8d40 100644 --- a/modules/rpg/main.lua +++ b/modules/rpg/main.lua @@ -611,6 +611,10 @@ local function on_player_changed_position(event) return end + if Public.get_last_spell_cast(player) then + return + end + if random(1, 64) ~= 1 then return end @@ -675,6 +679,7 @@ local function on_pre_player_mined_item(event) if rpg_t.last_mined_entity_position.x == entity.position.x and rpg_t.last_mined_entity_position.y == entity.position.y then return end + rpg_t.last_mined_entity_position.x = entity.position.x rpg_t.last_mined_entity_position.y = entity.position.y @@ -970,7 +975,10 @@ local function on_player_used_capsule(event) rpg_t = rpg_t } - spell.callback(data) + local cast_spell = spell.callback(data) + if not cast_spell then + return + end rpg_t.last_spawned = game.tick + spell.cooldown Public.update_mana(player) diff --git a/modules/rpg/spells.lua b/modules/rpg/spells.lua index b1173fbe..06631c4f 100644 --- a/modules/rpg/spells.lua +++ b/modules/rpg/spells.lua @@ -179,6 +179,17 @@ local function create_entity(data) local force = data.force local tame_unit_effects = data.tame_unit_effects + local last_spell_cast = rpg_t.last_spell_cast + + if last_spell_cast then + if Public.get_last_spell_cast(player) then + Public.cast_spell(player, true) + return false + end + end + + Public.set_last_spell_cast(player, position) + if self.biter then local e = surface.create_entity({name = self.entityName, position = position, force = force}) tame_unit_effects(player, e) @@ -197,6 +208,9 @@ local function create_entity(data) local e = surface.create_entity({name = self.entityName, position = pos, force = force}) e.direction = player.character.direction Public.remove_mana(player, self.mana_cost) + else + Public.cast_spell(player, true) + return false end end end @@ -205,6 +219,9 @@ local function create_entity(data) local e = surface.create_entity({name = self.entityName, position = position, force = force}) e.direction = player.character.direction Public.remove_mana(player, self.mana_cost) + else + Public.cast_spell(player, true) + return false end end @@ -231,7 +248,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/stone-wall', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -245,7 +262,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/wooden-chest', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -259,7 +276,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/iron-chest', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -273,7 +290,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/steel-chest', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -287,7 +304,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/transport-belt', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -301,7 +318,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/fast-transport-belt', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -315,7 +332,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/express-transport-belt', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -329,7 +346,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/underground-belt', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -343,7 +360,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/fast-underground-belt', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -357,7 +374,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/express-underground-belt', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -371,7 +388,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/pipe', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -385,7 +402,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'recipe/pipe-to-ground', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -399,7 +416,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'entity/tree-05', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -413,7 +430,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'entity/sand-rock-big', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -427,7 +444,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'entity/small-biter', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -441,7 +458,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'entity/small-spitter', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -455,7 +472,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'entity/medium-biter', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -469,7 +486,7 @@ spells[#spells + 1] = { enabled = true, sprite = 'entity/medium-spitter', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -484,7 +501,7 @@ spells[#spells + 1] = { log_spell = true, sprite = 'entity/biter-spawner', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -499,7 +516,7 @@ spells[#spells + 1] = { log_spell = true, sprite = 'entity/spitter-spawner', callback = function(data) - create_entity(data) + return create_entity(data) end } @@ -770,7 +787,7 @@ spells[#spells + 1] = { sprite = 'entity/tank', special_sprite = 'entity=tank', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = { @@ -788,7 +805,7 @@ spells[#spells + 1] = { sprite = 'entity/spidertron', special_sprite = 'entity=spidertron', callback = function(data) - create_entity(data) + return create_entity(data) end } spells[#spells + 1] = {