Skip to content

Commit

Permalink
First round of replacing deprecated stuff
Browse files Browse the repository at this point in the history
This covers only get|set_terrain and terrain_mask
  • Loading branch information
CelticMinstrel committed Feb 18, 2021
1 parent 4fa92c7 commit 3db922f
Show file tree
Hide file tree
Showing 25 changed files with 117 additions and 109 deletions.
16 changes: 10 additions & 6 deletions data/ai/lua/ai_helper.lua
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions data/ai/lua/battle_calcs.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions data/ai/lua/ca_castle_switch.lua
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion data/ai/lua/ca_spread_poison.lua
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion data/ai/lua/generic_recruit_engine.lua
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions data/ai/lua/retreat.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_assassin_move.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_fast_attack_utils.lua
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_goto.lua
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions data/ai/micro_ais/cas/ca_healer_move.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_protect_unit_move.lua
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions data/ai/micro_ais/cas/ca_recruit_random.lua
Expand Up @@ -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

Expand All @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_stationed_guardian.lua
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_zone_guardian.lua
Expand Up @@ -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
Expand Down
Expand Up @@ -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
>>
Expand Down
Expand Up @@ -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
>>
Expand Down
2 changes: 1 addition & 1 deletion data/campaigns/Two_Brothers/ai/ca_muff_toras_move.lua
Expand Up @@ -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.
Expand Down

0 comments on commit 3db922f

Please sign in to comment.