From 3db922fb7fd4bcd80cb1a5299128f043cbcfeab7 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Wed, 17 Feb 2021 20:48:16 -0500 Subject: [PATCH] First round of replacing deprecated stuff This covers only get|set_terrain and terrain_mask --- data/ai/lua/ai_helper.lua | 16 +-- data/ai/lua/battle_calcs.lua | 22 +++-- data/ai/lua/ca_castle_switch.lua | 4 +- data/ai/lua/ca_spread_poison.lua | 2 +- data/ai/lua/generic_recruit_engine.lua | 2 +- data/ai/lua/retreat.lua | 7 +- data/ai/micro_ais/cas/ca_assassin_move.lua | 4 +- .../ai/micro_ais/cas/ca_fast_attack_utils.lua | 4 +- data/ai/micro_ais/cas/ca_goto.lua | 2 +- data/ai/micro_ais/cas/ca_healer_move.lua | 5 +- .../ai/micro_ais/cas/ca_protect_unit_move.lua | 2 +- data/ai/micro_ais/cas/ca_recruit_random.lua | 5 +- .../micro_ais/cas/ca_stationed_guardian.lua | 2 +- data/ai/micro_ais/cas/ca_zone_guardian.lua | 2 +- .../scenarios/04_Spring_of_Reprisal.cfg | 4 +- .../scenarios/13_The_Dwarven_Doors.cfg | 4 +- .../Two_Brothers/ai/ca_muff_toras_move.lua | 2 +- .../lua/campaign/enemy_themed.lua | 5 +- .../lua/game_mechanics/bonus.lua | 2 +- .../lua/game_mechanics/supply.lua | 2 +- .../lua/optional_mechanics/destruction.lua | 98 +++++++++---------- data/lua/wml-tags.lua | 24 +++-- data/lua/wml/find_path.lua | 2 +- data/lua/wml/random_placement.lua | 2 +- data/modifications/pick_advance/main.lua | 2 +- 25 files changed, 117 insertions(+), 109 deletions(-) diff --git a/data/ai/lua/ai_helper.lua b/data/ai/lua/ai_helper.lua index 789ffc6ee219..2ea82e6bc89c 100644 --- a/data/ai/lua/ai_helper.lua +++ b/data/ai/lua/ai_helper.lua @@ -786,10 +786,11 @@ function ai_helper.get_closest_location(hex, location_filter, unit) end local locs = wesnoth.get_locations(loc_filter) + local map = wesnoth.map.get() if unit then for _,loc in ipairs(locs) do - local movecost = unit:movement(wesnoth.get_terrain(loc[1], loc[2])) + local movecost = unit:movement(map:get_terrain(loc[1], loc[2])) if (movecost <= unit.max_moves) then return loc end end else @@ -810,12 +811,13 @@ function ai_helper.get_passable_locations(location_filter, unit) -- All hexes that are not on the map border local all_locs = ai_helper.get_locations_no_borders(location_filter) + local map = wesnoth.map.get() -- If @unit is provided, exclude terrain that's impassable for the unit if unit then local locs = {} for _,loc in ipairs(all_locs) do - local movecost = unit:movement(wesnoth.get_terrain(loc[1], loc[2])) + local movecost = unit:movement(map:get_terrain(loc[1], loc[2])) if (movecost <= unit.max_moves) then table.insert(locs, loc) end end return locs @@ -828,10 +830,11 @@ function ai_helper.get_healing_locations(location_filter) -- Finds all locations matching @location_filter that provide healing, excluding border hexes. local all_locs = ai_helper.get_locations_no_borders(location_filter) + local map = wesnoth.map.get() local locs = {} for _,loc in ipairs(all_locs) do - if wesnoth.get_terrain_info(wesnoth.get_terrain(loc[1],loc[2])).healing > 0 then + if wesnoth.get_terrain_info(map:get_terrain(loc[1],loc[2])).healing > 0 then table.insert(locs, loc) end end @@ -1517,12 +1520,13 @@ function ai_helper.next_hop(unit, x, y, cfg) unit:to_map(old_x, old_y) unit_in_way:to_map() - local terrain = wesnoth.get_terrain(next_hop_ideal[1], next_hop_ideal[2]) + local map = wesnoth.map.get() + local terrain = map:get_terrain(next_hop_ideal[1], next_hop_ideal[2]) local move_cost_endpoint = unit:movement_on(terrain) local inverse_reach_map = LS.create() for _,r in pairs(inverse_reach) do -- We want the moves left for moving into the opposite direction in which the reach map was calculated - local terrain = wesnoth.get_terrain(r[1], r[2]) + local terrain = map:get_terrain(r[1], r[2]) local move_cost = unit:movement_on(terrain) local inverse_cost = r[3] + move_cost - move_cost_endpoint inverse_reach_map:insert(r[1], r[2], inverse_cost) @@ -1727,7 +1731,7 @@ function ai_helper.custom_cost_with_avoid(x, y, prev_cost, unit, avoid_map, ally end local max_moves = unit.max_moves - local terrain = wesnoth.get_terrain(x, y) + local terrain = wesnoth.map.get():get_terrain(x, y) local move_cost = unit:movement_on(terrain) if (move_cost > max_moves) then diff --git a/data/ai/lua/battle_calcs.lua b/data/ai/lua/battle_calcs.lua index 7c6e3b4820ec..47478ad172d2 100644 --- a/data/ai/lua/battle_calcs.lua +++ b/data/ai/lua/battle_calcs.lua @@ -677,8 +677,9 @@ function battle_calcs.battle_outcome(attacker, defender, cfg, cache) if (def_max_hits > att_strikes) then def_max_hits = att_strikes end -- Probability of landing a hit - local att_hit_prob = 1 - defender:defense_on(wesnoth.get_terrain(defender.x, defender.y)) / 100. - local def_hit_prob = 1 - attacker:defense_on(wesnoth.get_terrain(dst[1], dst[2])) / 100. + local map = wesnoth.map.get() + local att_hit_prob = 1 - defender:defense_on(map:get_terrain(defender.x, defender.y)) / 100. + local def_hit_prob = 1 - attacker:defense_on(map:get_terrain(dst[1], dst[2])) / 100. -- Magical: attack and defense, and under all circumstances if att_attack.magical then att_hit_prob = 0.7 end @@ -808,13 +809,15 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache) if (att_stats.slowed ~= 0) then damage = damage + 6 * (att_stats.slowed - att_stats.hp_chance[0]) end + + local map = wesnoth.map.get -- If attack is from a healing location, count that as slightly more than the healing amount - damage = damage - 1.25 * wesnoth.get_terrain_info(wesnoth.get_terrain(dst[1], dst[2])).healing + damage = damage - 1.25 * wesnoth.get_terrain_info(map:get_terrain(dst[1], dst[2])).healing -- Equivalently, if attack is adjacent to an unoccupied healing location, that's bad for xa,ya in H.adjacent_tiles(dst[1], dst[2]) do - local healing = wesnoth.get_terrain_info(wesnoth.get_terrain(xa, ya)).healing + local healing = wesnoth.get_terrain_info(map:get_terrain(xa, ya)).healing if (healing > 0) and (not wesnoth.units.get(xa, ya)) then damage = damage + 1.25 * healing end @@ -865,7 +868,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache) end -- If defender is on a healing location, count that as slightly more than the healing amount - damage = damage - 1.25 * wesnoth.get_terrain_info(wesnoth.get_terrain(defender.x, defender.y)).healing + damage = damage - 1.25 * wesnoth.get_terrain_info(map:get_terrain(defender.x, defender.y)).healing if (damage < 0) then damage = 0. end @@ -908,7 +911,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache) -- If defender is on a village, add a bonus rating (we want to get rid of those preferentially) -- So yes, this is positive, even though it's a plus for the defender -- Note: defenders on healing locations also got a negative damage rating above (these don't exactly cancel each other though) - if wesnoth.get_terrain_info(wesnoth.get_terrain(defender.x, defender.y)).village then + if wesnoth.get_terrain_info(map:get_terrain(defender.x, defender.y)).village then defender_value = defender_value * (1. + 10. / attacker.max_hitpoints) end @@ -924,7 +927,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache) -- We don't need a bonus for good terrain for the attacker, as that is covered in the damage calculation -- However, we add a small bonus for good terrain defense of the _defender_ on the _attack_ hex -- This is in order to take good terrain away from defender on next move, all else being equal - local defender_defense = - (100 - defender:defense_on(wesnoth.get_terrain(dst[1], dst[2]))) / 100. + local defender_defense = - (100 - defender:defense_on(map:get_terrain(dst[1], dst[2]))) / 100. defender_value = defender_value + defender_defense * defense_weight -- Get a very small bonus for hexes in between defender and AI leader @@ -1314,9 +1317,10 @@ function battle_calcs.best_defense_map(units, cfg) if max_moves then unit.moves = unit.max_moves end local reach = wesnoth.find_reach(unit, cfg) if max_moves then unit.moves = old_moves end + local map = wesnoth.map.get() for _,loc in ipairs(reach) do - local defense = unit:defense_on(wesnoth.get_terrain(loc[1], loc[2])) + local defense = unit:defense_on(map:get_terrain(loc[1], loc[2])) if (defense > (defense_map:get(loc[1], loc[2]) or - math.huge)) then defense_map:insert(loc[1], loc[2], defense) @@ -1524,7 +1528,7 @@ function battle_calcs.get_attack_combos_subset(units, enemy, cfg) -- Store information about it in 'loc' and add this to 'locs' -- Want coordinates (dst) and terrain defense (for sorting) loc.dst = xa * 1000 + ya - loc.hit_prob = 100 - unit:defense_on(wesnoth.get_terrain(xa, ya)) + loc.hit_prob = 100 - unit:defense_on(wesnoth.map.get():get_terrain(xa, ya)) table.insert(locs, loc) -- Also mark this hex as usable diff --git a/data/ai/lua/ca_castle_switch.lua b/data/ai/lua/ca_castle_switch.lua index cd097d6c74e7..a926e0e37740 100644 --- a/data/ai/lua/ca_castle_switch.lua +++ b/data/ai/lua/ca_castle_switch.lua @@ -33,7 +33,7 @@ end local function other_units_on_keep(leader) -- if we're on a keep, wait until there are no movable non-leader units on the castle before moving off local leader_score = high_score - if wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep then + if wesnoth.get_terrain_info(wesnoth.map.get():get_terrain(leader.x, leader.y)).keep then local castle = AH.get_locations_no_borders { { "and", { x = leader.x, y = leader.y, radius = 200, @@ -172,7 +172,7 @@ function ca_castle_switch:evaluation(cfg, data, filter_own, recruiting_leader) -- If we're on a keep, -- don't move to another keep unless it's much better when uncaptured villages are present - if best_score > 0 and wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep then + if best_score > 0 and wesnoth.get_terrain_info(wesnoth.map.get():get_terrain(leader.x, leader.y)).keep then local close_unowned_village = (wesnoth.get_villages { { "and", { x = leader.x, diff --git a/data/ai/lua/ca_spread_poison.lua b/data/ai/lua/ca_spread_poison.lua index 4a21fc9e49f9..007ea978e6d9 100644 --- a/data/ai/lua/ca_spread_poison.lua +++ b/data/ai/lua/ca_spread_poison.lua @@ -54,7 +54,7 @@ function ca_spread_poison:evaluation(cfg, data, filter_own) local cant_poison = defender.status.poisoned or defender.status.unpoisonable -- For now, we also simply don't poison units on healing locations (unless standard combat CA does it) - local defender_terrain = wesnoth.get_terrain(defender.x, defender.y) + local defender_terrain = wesnoth.map.get():get_terrain(defender.x, defender.y) local healing = wesnoth.get_terrain_info(defender_terrain).healing -- Also, poisoning units that would level up through the attack or could level on their turn as a result is very bad diff --git a/data/ai/lua/generic_recruit_engine.lua b/data/ai/lua/generic_recruit_engine.lua index 212f3073766b..1baba119dae0 100644 --- a/data/ai/lua/generic_recruit_engine.lua +++ b/data/ai/lua/generic_recruit_engine.lua @@ -343,7 +343,7 @@ return { { "and", params.filter_own } }[1] - if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then + if (not leader) or (not wesnoth.get_terrain_info(wesnoth.map.get():get_terrain(leader.x, leader.y)).keep) then return 0 end diff --git a/data/ai/lua/retreat.lua b/data/ai/lua/retreat.lua index 60e6b27248ec..ed69151c221f 100644 --- a/data/ai/lua/retreat.lua +++ b/data/ai/lua/retreat.lua @@ -21,7 +21,7 @@ function retreat_functions.min_hp(unit) if (caution_factor < 0) then caution_factor = 0 end caution_factor = math.sqrt(caution_factor) * 2 - local hp_per_level = (100 - unit:defense_on(wesnoth.get_terrain(unit.x, unit.y)))/15 * caution_factor + local hp_per_level = (100 - unit:defense_on(wesnoth.map.get():get_terrain(unit.x, unit.y)))/15 * caution_factor local level = unit.level -- Leaders are considered to be higher level because of their value @@ -139,6 +139,7 @@ function retreat_functions.get_retreat_injured_units(healees, regen_amounts, avo local enemy_attack_map = BC.get_attack_map(enemies) local healing_locs = retreat_functions.get_healing_locations() + local map = wesnoth.map.get() local max_rating, best_loc, best_unit = - math.huge for i,u in ipairs(healees) do @@ -150,7 +151,7 @@ function retreat_functions.get_retreat_injured_units(healees, regen_amounts, avo local location_subset = {} for j,loc in ipairs(possible_locations) do if (not avoid_map) or (not avoid_map:get(loc[1], loc[2])) then - local heal_amount = wesnoth.get_terrain_info(wesnoth.get_terrain(loc[1], loc[2])).healing or 0 + local heal_amount = wesnoth.get_terrain_info(map:get_terrain(loc[1], loc[2])).healing or 0 if heal_amount == true then -- handle deprecated syntax -- TODO: remove this when removed from game @@ -212,7 +213,7 @@ function retreat_functions.get_retreat_injured_units(healees, regen_amounts, avo rating = rating - enemy_count * 100000 -- Penalty based on terrain defense for unit - rating = rating - (100 - u:defense_on(wesnoth.get_terrain(loc[1], loc[2])))/10 + rating = rating - (100 - u:defense_on(map:get_terrain(loc[1], loc[2])))/10 if (loc[1] == u.x) and (loc[2] == u.y) and (not u.status.poisoned) then if is_healthy or enemy_count == 0 then diff --git a/data/ai/micro_ais/cas/ca_assassin_move.lua b/data/ai/micro_ais/cas/ca_assassin_move.lua index 4d61d729113d..e9f5e41bb6cc 100644 --- a/data/ai/micro_ais/cas/ca_assassin_move.lua +++ b/data/ai/micro_ais/cas/ca_assassin_move.lua @@ -24,7 +24,7 @@ local function custom_cost(x, y, unit, enemy_rating_map, prefer_map) -- non-preferred hexes rather than a bonus for preferred hexes as the cost function -- must return values >=1 for the a* search to work. - local terrain = wesnoth.get_terrain(x, y) + local terrain = wesnoth.map.get():get_terrain(x, y) local move_cost = unit:movement(terrain) move_cost = move_cost + (enemy_rating_map:get(x, y) or 0) @@ -95,7 +95,7 @@ function ca_assassin_move:execution(cfg) -- Penalties for damage by enemies local enemy_rating_map = LS.create() enemy_damage_map:iter(function(x, y, enemy_damage) - local hit_chance = (100 - unit:defense_on(wesnoth.get_terrain(x, y))) / 100. + local hit_chance = (100 - unit:defense_on(wesnoth.map.get():get_terrain(x, y))) / 100. local rating = hit_chance * enemy_damage rating = rating / unit.max_hitpoints diff --git a/data/ai/micro_ais/cas/ca_fast_attack_utils.lua b/data/ai/micro_ais/cas/ca_fast_attack_utils.lua index 77c71fed7227..b3a22e59d12e 100644 --- a/data/ai/micro_ais/cas/ca_fast_attack_utils.lua +++ b/data/ai/micro_ais/cas/ca_fast_attack_utils.lua @@ -56,7 +56,7 @@ function ca_fast_attack_utils.gamedata_setup() local healing_map = {} for _,loc in ipairs(AH.get_healing_locations {}) do if (not healing_map[loc[1]]) then healing_map[loc[1]] = {} end - healing_map[loc[1]][loc[2]] = wesnoth.get_terrain_info(wesnoth.get_terrain(loc[1], loc[2])).healing + healing_map[loc[1]][loc[2]] = wesnoth.get_terrain_info(wesnoth.map.get():get_terrain(loc[1], loc[2])).healing end gamedata.healing_map = healing_map @@ -157,7 +157,7 @@ function ca_fast_attack_utils.get_unit_defense(unit_copy, x, y, defense_maps) if (not defense_maps[unit_copy.id][x]) then defense_maps[unit_copy.id][x] = {} end if (not defense_maps[unit_copy.id][x][y]) then - local defense = unit_copy:defense_on(wesnoth.get_terrain(x, y)) / 100. + local defense = unit_copy:defense_on(wesnoth.map.get():get_terrain(x, y)) / 100. defense_maps[unit_copy.id][x][y] = { defense = defense } end diff --git a/data/ai/micro_ais/cas/ca_goto.lua b/data/ai/micro_ais/cas/ca_goto.lua index 74f786cdf090..04d912f7220f 100644 --- a/data/ai/micro_ais/cas/ca_goto.lua +++ b/data/ai/micro_ais/cas/ca_goto.lua @@ -7,7 +7,7 @@ local MAISD = wesnoth.require "ai/micro_ais/micro_ai_self_data.lua" local M = wesnoth.map local function custom_cost(x, y, unit, avoid_map, enemy_map, enemy_attack_map, multiplier) - local terrain = wesnoth.get_terrain(x, y) + local terrain = wesnoth.map.get():get_terrain(x, y) local move_cost = unit:movement(terrain) if avoid_map and avoid_map:get(x, y) then diff --git a/data/ai/micro_ais/cas/ca_healer_move.lua b/data/ai/micro_ais/cas/ca_healer_move.lua index 945bf87e0471..a5b64453c07a 100644 --- a/data/ai/micro_ais/cas/ca_healer_move.lua +++ b/data/ai/micro_ais/cas/ca_healer_move.lua @@ -35,13 +35,14 @@ function ca_healer_move:evaluation(cfg, data) { "and", wml.get_child(cfg, "filter_second") } } + local map = wesnoth.map.get() local healees, healees_MP = {}, {} for _,healee in ipairs(all_healees) do -- Potential healees are units without MP that don't already have a healer (also without MP) next to them -- Also, they cannot be on a healing location or regenerate if (healee.moves == 0) then if (not healee:matches { ability = "regenerates" }) then - local healing = wesnoth.get_terrain_info(wesnoth.get_terrain(healee.x, healee.y)).healing + local healing = wesnoth.get_terrain_info(map:get_terrain(healee.x, healee.y)).healing if (healing == 0) then local is_healee = true for _,healer in ipairs(healers_noMP) do @@ -100,7 +101,7 @@ function ca_healer_move:evaluation(cfg, data) rating = rating - enemies_in_reach * 1000 -- All else being more or less equal, prefer villages and strong terrain - local terrain = wesnoth.get_terrain(x, y) + local terrain = map:get_terrain(x, y) local is_village = wesnoth.get_terrain_info(terrain).village if is_village then rating = rating + 2 end diff --git a/data/ai/micro_ais/cas/ca_protect_unit_move.lua b/data/ai/micro_ais/cas/ca_protect_unit_move.lua index 4110daa65aae..345eae2a85b9 100644 --- a/data/ai/micro_ais/cas/ca_protect_unit_move.lua +++ b/data/ai/micro_ais/cas/ca_protect_unit_move.lua @@ -47,7 +47,7 @@ function ca_protect_unit_move:execution(cfg, data) local terrain_defense_map = LS.create() reach_map:iter(function(x, y, data) - terrain_defense_map:insert(x, y, unit:defense_on(wesnoth.get_terrain(x, y))) + terrain_defense_map:insert(x, y, unit:defense_on(wesnoth.map.get():get_terrain(x, y))) end) local goal_distance_map = LS.create() diff --git a/data/ai/micro_ais/cas/ca_recruit_random.lua b/data/ai/micro_ais/cas/ca_recruit_random.lua index 56346cf0111f..843777674c5f 100644 --- a/data/ai/micro_ais/cas/ca_recruit_random.lua +++ b/data/ai/micro_ais/cas/ca_recruit_random.lua @@ -8,10 +8,11 @@ local ca_recruit_random = {} function ca_recruit_random:evaluation(cfg) -- Random recruiting from all the units the side has + local map = wesnoth.map.get() -- Check if leader is on keep local leader = wesnoth.units.find_on_map { side = wesnoth.current.side, canrecruit = 'yes' }[1] - if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then + if (not leader) or (not wesnoth.get_terrain_info(map:get_terrain(leader.x, leader.y)).keep) then return 0 end @@ -30,7 +31,7 @@ function ca_recruit_random:evaluation(cfg) and (xa >= 1) and (xa <= width) and (ya >= 1) and (ya <= height) then - local is_castle = wesnoth.get_terrain_info(wesnoth.get_terrain(xa, ya)).castle + local is_castle = wesnoth.get_terrain_info(map:get_terrain(xa, ya)).castle if is_castle then table.insert(new_hexes, { xa, ya }) diff --git a/data/ai/micro_ais/cas/ca_stationed_guardian.lua b/data/ai/micro_ais/cas/ca_stationed_guardian.lua index e23c8987da74..afdd8ffc705a 100644 --- a/data/ai/micro_ais/cas/ca_stationed_guardian.lua +++ b/data/ai/micro_ais/cas/ca_stationed_guardian.lua @@ -61,7 +61,7 @@ function ca_stationed_guardian:execution(cfg) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) or (unit_in_way == guardian) then - local defense = guardian:defense_on(wesnoth.get_terrain(xa, ya)) + local defense = guardian:defense_on(wesnoth.map.get():get_terrain(xa, ya)) local nh = AH.next_hop(guardian, xa, ya) if nh then if (nh[1] == xa) and (nh[2] == ya) and (defense > best_defense) then diff --git a/data/ai/micro_ais/cas/ca_zone_guardian.lua b/data/ai/micro_ais/cas/ca_zone_guardian.lua index ae1f78ff772b..ab060a234cde 100644 --- a/data/ai/micro_ais/cas/ca_zone_guardian.lua +++ b/data/ai/micro_ais/cas/ca_zone_guardian.lua @@ -46,7 +46,7 @@ function ca_zone_guardian:execution(cfg) if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way)) or (unit_in_way == guardian) then - local defense = guardian:defense_on(wesnoth.get_terrain(xa, ya)) + local defense = guardian:defense_on(wesnoth.map.get():get_terrain(xa, ya)) local nh = AH.next_hop(guardian, xa, ya) if nh then if (nh[1] == xa) and (nh[2] == ya) and (defense > best_defense) then diff --git a/data/campaigns/Descent_Into_Darkness/scenarios/04_Spring_of_Reprisal.cfg b/data/campaigns/Descent_Into_Darkness/scenarios/04_Spring_of_Reprisal.cfg index c9828fe9e52e..f0ba2159660b 100644 --- a/data/campaigns/Descent_Into_Darkness/scenarios/04_Spring_of_Reprisal.cfg +++ b/data/campaigns/Descent_Into_Darkness/scenarios/04_Spring_of_Reprisal.cfg @@ -186,8 +186,8 @@ local loopCounter for loopCounter = 1, snowNeeded do local locationsIndex = wesnoth.random(#locations) local coordinate = locations[locationsIndex] - local terrainCode = wesnoth.get_terrain(coordinate[1], coordinate[2]) - wesnoth.set_terrain(coordinate[1], coordinate[2], (convert[terrainCode] or terrainCode)) + local terrainCode = wesnoth.map.get():get_terrain(coordinate[1], coordinate[2]) + wesnoth.map.get():set_terrain(coordinate[1], coordinate[2], (convert[terrainCode] or terrainCode)) table.remove(locations, locationsIndex) end >> diff --git a/data/campaigns/Heir_To_The_Throne/scenarios/13_The_Dwarven_Doors.cfg b/data/campaigns/Heir_To_The_Throne/scenarios/13_The_Dwarven_Doors.cfg index 428c7b5d9b6c..850e7f7d953f 100644 --- a/data/campaigns/Heir_To_The_Throne/scenarios/13_The_Dwarven_Doors.cfg +++ b/data/campaigns/Heir_To_The_Throne/scenarios/13_The_Dwarven_Doors.cfg @@ -206,8 +206,8 @@ local loopCounter for loopCounter = 1, snowNeeded do local locationsIndex = wesnoth.random(#locations) local coordinate = locations[locationsIndex] - local terrainCode = wesnoth.get_terrain(coordinate[1], coordinate[2]) - wesnoth.set_terrain(coordinate[1], coordinate[2], (convert[terrainCode] or terrainCode)) + local terrainCode = wesnoth.map.get():get_terrain(coordinate[1], coordinate[2]) + wesnoth.map.get():set_terrain(coordinate[1], coordinate[2], (convert[terrainCode] or terrainCode)) table.remove(locations, locationsIndex) end >> diff --git a/data/campaigns/Two_Brothers/ai/ca_muff_toras_move.lua b/data/campaigns/Two_Brothers/ai/ca_muff_toras_move.lua index 69c1916d05f5..bbcc39227350 100644 --- a/data/campaigns/Two_Brothers/ai/ca_muff_toras_move.lua +++ b/data/campaigns/Two_Brothers/ai/ca_muff_toras_move.lua @@ -45,7 +45,7 @@ function muff_toras_move:execution() end -- All else being equal, go with good terrain - local hit_chance = muff_toras:defense(wesnoth.get_terrain(x, y)) + local hit_chance = muff_toras:defense(wesnoth.map.get():get_terrain(x, y)) rating = rating - hit_chance -- Finally, we want to run away from enemies if there are no other factors. diff --git a/data/campaigns/World_Conquest/lua/campaign/enemy_themed.lua b/data/campaigns/World_Conquest/lua/campaign/enemy_themed.lua index 66dce2e074d3..3a430ea3bcf4 100644 --- a/data/campaigns/World_Conquest/lua/campaign/enemy_themed.lua +++ b/data/campaigns/World_Conquest/lua/campaign/enemy_themed.lua @@ -30,8 +30,9 @@ local function wct_map_enemy_themed(race, pet, castle, village, chance) if boss == nil then return end + local map = wesnoth.map.get() --give themed castle - wesnoth.set_terrain(boss.loc, "K" .. castle, "base") + map:set_terrain(boss.loc, "K" .. castle, "base") wesnoth.wml_actions.terrain { terrain="C" .. castle, wml.tag["and"] { @@ -57,7 +58,7 @@ local function wct_map_enemy_themed(race, pet, castle, village, chance) -- extra tweak with trees to elvish castle for i, tile in ipairs(elvish_castle) do if wesnoth.random(10) <= 4 then - wesnoth.set_terrain(tile, "Cv^Fet") + map:set_terrain(tile, "Cv^Fet") end end -- adjacent themed villages diff --git a/data/campaigns/World_Conquest/lua/game_mechanics/bonus.lua b/data/campaigns/World_Conquest/lua/game_mechanics/bonus.lua index e69d5770187e..f6fa9e5905bc 100644 --- a/data/campaigns/World_Conquest/lua/game_mechanics/bonus.lua +++ b/data/campaigns/World_Conquest/lua/game_mechanics/bonus.lua @@ -37,7 +37,7 @@ end function bonus.place_item(x, y, image) if image == "campfire" then - wesnoth.set_terrain(x, y, "*^Ecf", "overlay") + wesnoth.map.get():set_terrain(x, y, "*^Ecf", "overlay") image = nil else image = image or "scenery/lighthouse.png" diff --git a/data/campaigns/World_Conquest/lua/game_mechanics/supply.lua b/data/campaigns/World_Conquest/lua/game_mechanics/supply.lua index 499966081436..be1b6eca220a 100644 --- a/data/campaigns/World_Conquest/lua/game_mechanics/supply.lua +++ b/data/campaigns/World_Conquest/lua/game_mechanics/supply.lua @@ -17,7 +17,7 @@ local supply_images = function wesnoth.wml_actions.wc2_map_supply_village(t) local unit = wesnoth.get_unit(t.x, t.y) local loc = unit.loc - wesnoth.set_terrain(loc, "Kh^Vov", "overlay") + wesnoth.map.get():set_terrain(loc, "Kh^Vov", "overlay") wesnoth.set_village_owner(loc, unit.side, false) diff --git a/data/campaigns/World_Conquest/lua/optional_mechanics/destruction.lua b/data/campaigns/World_Conquest/lua/optional_mechanics/destruction.lua index 6d89e6042423..1da4e9876ed9 100644 --- a/data/campaigns/World_Conquest/lua/optional_mechanics/destruction.lua +++ b/data/campaigns/World_Conquest/lua/optional_mechanics/destruction.lua @@ -18,36 +18,26 @@ local ice = { --replaces terrain fo the wct custom terrain mod. local function wct_map_custom_ruin_village(loc) - local function matches_terrain(filter) - filter.x = cx.x1 - filter.y = cx.y1 - return wesnoth.get_locations({ x = loc[1], y = loc[2], terrain = filter}) > 0 - end + local map = wesnoth.map.get() + loc = wesnoth.map.get_hex(loc) -- TODO: enable once https://github.com/wesnoth/wesnoth/issues/4894 is fixed. if false then - if matches_terrain("*^Vh,*^Vha") then - wesnoth.set_terrain(loc, "*^Vhr", "overlay") + if loc:matches{terrain = "*^Vh,*^Vha"} then + map:set_terrain(loc, "*^Vhr", "overlay") end - if matches_terrain("*^Vhc,*^Vhca") then - wesnoth.set_terrain(loc, "*^Vhr", "overlay") + if loc:matches{terrain = "*^Vhc,*^Vhca"} then + map:set_terrain(loc, "*^Vhr", "overlay") end end end on_event("die", function(cx) - local loc = { cx.x1, cx.y1 } - local function matches(filter) - filter.x = cx.x1 - filter.y = cx.y1 - return #wesnoth.get_locations(filter) > 0 - end - local function matches_terrain(filter) - return #wesnoth.get_locations({ x = cx.x1, y = cx.y1, terrain = filter}) > 0 - end + local map = wesnoth.map.get() + local loc = wesnoth.map.get_hex(cx.x1, cx.y1) if wml.variables.wc2_config_enable_terrain_destruction == false then return end - if not matches_terrain("K*^*,C*^*,*^Fet,G*^F*,G*^Uf,A*,*^B*,Rrc,Iwr,*^Vhh,*^Vh*,*^Fda*") then + if not loc:matches{terrain = "K*^*,C*^*,*^Fet,G*^F*,G*^Uf,A*,*^B*,Rrc,Iwr,*^Vhh,*^Vh*,*^Fda*"} then return end local function item(image) @@ -58,43 +48,43 @@ on_event("die", function(cx) z_order = -10, } end - if matches_terrain("Kh,Kha,Kh^Vov,Kha^Vov") then - wesnoth.set_terrain(loc, "Khr", "base") + if loc:matches{terrain = "Kh,Kha,Kh^Vov,Kha^Vov"} then + map:set_terrain(loc, "Khr", "base") - elseif matches_terrain("Ch,Cha") then - wesnoth.set_terrain(loc, "Chr^Es") + elseif loc:matches{terrain = "Ch,Cha"} then + map:set_terrain(loc, "Chr^Es") -- only without custom activated - elseif matches_terrain("Ch^Vh,Ch^Vhc") then - wesnoth.set_terrain(loc, "Chr", "base") + elseif loc:matches{terrain = "Ch^Vh,Ch^Vhc"} then + map:set_terrain(loc, "Chr", "base") - elseif matches_terrain("Cd") then - wesnoth.set_terrain(loc, "Cdr^Es") + elseif loc:matches{terrain = "Cd"} then + map:set_terrain(loc, "Cdr^Es") - elseif matches_terrain("Cd^Vd") then - wesnoth.set_terrain(loc, "Cdr", "base") + elseif loc:matches{terrain = "Cd^Vd"} then + map:set_terrain(loc, "Cdr", "base") - elseif matches_terrain("Kd") then - wesnoth.set_terrain(loc, "Kdr^Es") + elseif loc:matches{terrain = "Kd"} then + map:set_terrain(loc, "Kdr^Es") - elseif matches_terrain("Gg^Fmf,Gg^Fdf,Gg^Fp,Gg^Uf,Gs^Fmf,Gs^Fdf,Gs^Fp,Gs^Uf") then - wesnoth.set_terrain(loc, "Gll", "base") + elseif loc:matches{terrain = "Gg^Fmf,Gg^Fdf,Gg^Fp,Gg^Uf,Gs^Fmf,Gs^Fdf,Gs^Fp,Gs^Uf"} then + map:set_terrain(loc, "Gll", "base") - elseif matches_terrain("Cv^Fds") then - wesnoth.set_terrain(loc, "Cv^Fdw") + elseif loc:matches{terrain = "Cv^Fds"} then + map:set_terrain(loc, "Cv^Fdw") - elseif matches_terrain("Rr^Fet,Cv^Fet") then - wesnoth.set_terrain(loc, "Rr^Fetd", "overlay") + elseif loc:matches{terrain = "Rr^Fet,Cv^Fet"} then + map:set_terrain(loc, "Rr^Fetd", "overlay") - elseif matches_terrain("Aa") then + elseif loc:matches{terrain = "Aa"} then item(snow[wesnoth.random(#snow)]) - elseif matches_terrain("Ai") then + elseif loc:matches{terrain = "Ai"} then item(ice[wesnoth.random(#ice)]) - elseif matches_terrain("Ww^Bsb|,Ww^Bsb/,Ww^Bsb\\,Wwt^Bsb|,Wwt^Bsb/,Wwt^Bsb\\,Wwg^Bsb|,Wwg^Bsb/,Wwg^Bsb\\") then - wesnoth.set_terrain(loc, "Wwf^Edt") + elseif loc:matches{terrain = "Ww^Bsb|,Ww^Bsb/,Ww^Bsb\\,Wwt^Bsb|,Wwt^Bsb/,Wwt^Bsb\\,Wwg^Bsb|,Wwg^Bsb/,Wwg^Bsb\\"} then + map:set_terrain(loc, "Wwf^Edt") wesnoth.play_sound("water-blast.wav") item("scenery/castle-ruins.png") - elseif matches_terrain("Rrc") then + elseif loc:matches{terrain = "Rrc"} then if wesnoth.variables["bonus.theme"] == "paradise" then wesnoth.wml_actions.remove_item { x = cx.x1, @@ -102,33 +92,33 @@ on_event("die", function(cx) image = "wc2_citadel_leanto" } item("scenery/trash.png") - wesnoth.set_terrain(loc, "Rrc^Edt") + map:set_terrain(loc, "Rrc^Edt") end - elseif matches_terrain("Iwr") then + elseif loc:matches{terrain = "Iwr"} then wesnoth.wml_actions.remove_item { x = cx.x1, y = cx.y1, image = "wc2_dock_ship" } item("scenery/trash.png") - wesnoth.set_terrain(loc, "Iwr^Edt") - elseif matches_terrain("*^Vh,**^Vhc,*^Vha,**^Vhca,*^Fda") then + map:set_terrain(loc, "Iwr^Edt") + elseif loc:matches{terrain = "*^Vh,**^Vhc,*^Vha,**^Vhca,*^Fda"} then wct_map_custom_ruin_village(loc) - if matches_terrain("Ch^V*") then - wesnoth.set_terrain(loc, "Chr", "base") + if loc:matches{terrain = "Ch^V*"} then + map:set_terrain(loc, "Chr", "base") end -- TODO: enable once https://github.com/wesnoth/wesnoth/issues/4894 is fixed. if false then - if matches_terrain("*^Fda") then - wesnoth.set_terrain(loc, "*^Fdw", "overlay") + if loc:matches{terrain = "*^Fda"} then + map:set_terrain(loc, "*^Fdw", "overlay") end end else - if matches_terrain("*^Vhh,*^Vhha") then - wesnoth.set_terrain(loc, "*^Vhhr", "overlay") + if loc:matches{terrain = "*^Vhh,*^Vhha"} then + map:set_terrain(loc, "*^Vhhr", "overlay") end - if matches_terrain("*^Bw|,*^Bw/,*^Bw\\") then - wesnoth.set_terrain(loc, wesnoth.get_terrain(loc) .. "r") + if loc:matches{terrain = "*^Bw|,*^Bw/,*^Bw\\"} then + map:set_terrain(loc, wesnoth.map.get():get_terrain(loc) .. "r") end end end) diff --git a/data/lua/wml-tags.lua b/data/lua/wml-tags.lua index a87eed89d832..7225ef520fa5 100644 --- a/data/lua/wml-tags.lua +++ b/data/lua/wml-tags.lua @@ -418,10 +418,11 @@ function wml_actions.store_locations(cfg) -- the variable can be mentioned in a [find_in] subtag, so it -- cannot be cleared before the locations are recovered local locs = wesnoth.get_locations(cfg) + local map = wesnoth.map.get() local writer = utils.vwriter.init(cfg, "location") for i, loc in ipairs(locs) do local x, y = loc[1], loc[2] - local t = wesnoth.get_terrain(x, y) + local t = map:get_terrain(x, y) local res = { x = x, y = y, terrain = t } if wesnoth.get_terrain_info(t).village then res.owner_side = wesnoth.get_village_owner(x, y) or 0 @@ -509,10 +510,11 @@ end function wml_actions.terrain(cfg) local terrain = cfg.terrain or wml.error("[terrain] missing required terrain= attribute") + local map = wesnoth.map.get() cfg = wml.shallow_parsed(cfg) cfg.terrain = nil for i, loc in ipairs(wesnoth.get_locations(cfg)) do - wesnoth.set_terrain(loc[1], loc[2], terrain, cfg.layer, cfg.replace_if_failed) + map:set_terrain(loc[1], loc[2], terrain, cfg.layer, cfg.replace_if_failed) end end @@ -646,10 +648,11 @@ end function wml_actions.store_starting_location(cfg) local writer = utils.vwriter.init(cfg, "location") + local map = wesnoth.map.get() for _, side in ipairs(wesnoth.sides.find(cfg)) do local loc = side.starting_location if loc then - local terrain = wesnoth.get_terrain(loc[1], loc[2]) + local terrain = map:get_terrain(loc[1], loc[2]) local result = { x = loc[1], y = loc[2], terrain = terrain } if wesnoth.get_terrain_info(terrain).village then result.owner_side = wesnoth.get_village_owner(loc[1], loc[2]) or 0 @@ -662,11 +665,12 @@ end function wml_actions.store_villages( cfg ) local villages = wesnoth.get_villages( cfg ) local writer = utils.vwriter.init(cfg, "location") + local map = wesnoth.map.get() for index, village in ipairs( villages ) do utils.vwriter.write(writer, { x = village[1], y = village[2], - terrain = wesnoth.get_terrain( village[1], village[2] ), + terrain = map:get_terrain( village[1], village[2] ), owner_side = wesnoth.get_village_owner( village[1], village[2] ) or 0 }) end @@ -956,30 +960,32 @@ function wesnoth.wml_actions.store_unit_defense(cfg) wesnoth.deprecated_message("[store_unit_defense]", 3, "1.17.0", "This function returns the chance to be hit, high values represent bad defenses. Using [store_unit_defense_on] is recommended instead.") local unit = wesnoth.units.find_on_map(cfg)[1] or wml.error "[store_unit_defense]'s filter didn't match any unit" + local map = wesnoth.map.get() local terrain = cfg.terrain local defense if terrain then defense = unit:chance_to_be_hit(terrain) elseif cfg.loc_x and cfg.loc_y then - defense = unit:chance_to_be_hit(wesnoth.get_terrain(cfg.loc_x, cfg.loc_y)) + defense = unit:chance_to_be_hit(map:get_terrain(cfg.loc_x, cfg.loc_y)) else - defense = unit:chance_to_be_hit(wesnoth.get_terrain(unit.x, unit.y)) + defense = unit:chance_to_be_hit(map:get_terrain(unit.x, unit.y)) end wml.variables[cfg.variable or "terrain_defense"] = defense end function wesnoth.wml_actions.store_unit_defense_on(cfg) local unit = wesnoth.units.find_on_map(cfg)[1] or wml.error "[store_unit_defense_on]'s filter didn't match any unit" + local map = wesnoth.map.get() local terrain = cfg.terrain local defense if terrain then defense = unit:defense_on(terrain) elseif cfg.loc_x and cfg.loc_y then - defense = unit:defense_on(wesnoth.get_terrain(cfg.loc_x, cfg.loc_y)) + defense = unit:defense_on(map:get_terrain(cfg.loc_x, cfg.loc_y)) else - defense = unit:defense_on(wesnoth.get_terrain(unit.x, unit.y)) + defense = unit:defense_on(map:get_terrain(unit.x, unit.y)) end wml.variables[cfg.variable or "terrain_defense"] = defense end @@ -1018,7 +1024,7 @@ function wml_actions.terrain_mask(cfg) if cfg.mask_file then mask = wesnoth.read_file(cfg.mask_file) end - wesnoth.terrain_mask({x, y}, mask, { + wesnoth.map.get():terrain_mask({x, y}, mask, { is_odd = is_odd, rules = rules, ignore_special_locations = cfg.ignore_special_locations, diff --git a/data/lua/wml/find_path.lua b/data/lua/wml/find_path.lua index 45d8d06fc333..ab4d4aaff87b 100644 --- a/data/lua/wml/find_path.lua +++ b/data/lua/wml/find_path.lua @@ -159,7 +159,7 @@ function wesnoth.wml_actions.find_path(cfg) wml.variables[string.format( "%s.step[%d]", variable, index - 1 )] = { -- this structure takes less space in the inspection window x = path_loc[1], y = path_loc[2], - terrain = wesnoth.get_terrain( path_loc[1], path_loc[2] ), + terrain = wesnoth.map.get():get_terrain( path_loc[1], path_loc[2] ), movement_cost = sub_cost, required_turns = sub_turns } diff --git a/data/lua/wml/random_placement.lua b/data/lua/wml/random_placement.lua index 444d3b885b68..2ed86492d522 100644 --- a/data/lua/wml/random_placement.lua +++ b/data/lua/wml/random_placement.lua @@ -40,7 +40,7 @@ wesnoth.wml_actions.random_placement = function(cfg) wml.variables[variable .. ".x"] = point[1] wml.variables[variable .. ".y"] = point[2] wml.variables[variable .. ".n"] = i - wml.variables[variable .. ".terrain"] = wesnoth.get_terrain(point[1], point[2]) + wml.variables[variable .. ".terrain"] = wesnoth.map.get():get_terrain(point[1], point[2]) if distance < 0 then -- optimisation: nothing to do for distance < 0 elseif distance == 0 then diff --git a/data/modifications/pick_advance/main.lua b/data/modifications/pick_advance/main.lua index e5e1931c5a90..44165b793af0 100644 --- a/data/modifications/pick_advance/main.lua +++ b/data/modifications/pick_advance/main.lua @@ -201,7 +201,7 @@ local function map_has_keeps() local width,height,_ = wesnoth.get_map_size() for x = 1, width do for y = 1, height do - local terr = wesnoth.get_terrain(x, y) + local terr = wesnoth.map.get():get_terrain(x, y) local info = wesnoth.get_terrain_info(terr) if info.keep then return true