added name= attribute to [item]

name= has no effect but can be used to delete the item. I also changed
[item] to automatically create an id (in case no id was given) and
return that (only usable if called from lua).

I used the id= field from overlay class for this, it was previously only
used by the editor. Not sure if it is a good idea to reuse the id field.
This commit is contained in:
gfgtdf 2016-01-31 02:06:13 +01:00
parent a885c2bdf1
commit 08236f8953
6 changed files with 21 additions and 11 deletions

View file

@ -3,7 +3,7 @@ local wml_actions = wesnoth.wml_actions
local game_events = wesnoth.game_events
local scenario_items = {}
local next_item_name = 0
local function add_overlay(x, y, cfg)
wesnoth.add_tile_overlay(x, y, cfg)
local items = scenario_items[x * 10000 + y]
@ -11,7 +11,7 @@ local function add_overlay(x, y, cfg)
items = {}
scenario_items[x * 10000 + y] = items
end
table.insert(items, { x = x, y = y, image = cfg.image, halo = cfg.halo, team_name = cfg.team_name, visible_in_fog = cfg.visible_in_fog, redraw = cfg.redraw })
table.insert(items, { x = x, y = y, image = cfg.image, halo = cfg.halo, team_name = cfg.team_name, visible_in_fog = cfg.visible_in_fog, redraw = cfg.redraw, name = cfg.name })
end
local function remove_overlay(x, y, name)
@ -21,7 +21,7 @@ local function remove_overlay(x, y, name)
if name then
for i = #items,1,-1 do
local item = items[i]
if item.image == name or item.halo == name then
if item.image == name or item.halo == name or item.name == name then
table.remove(items, i)
end
end
@ -39,6 +39,7 @@ function game_events.on_save()
table.insert(custom_cfg, { "item", w })
end
end
table.insert(custom_cfg, { "next_item_name", { next_item_name = next_item_name } })
return custom_cfg
end
@ -51,6 +52,9 @@ function game_events.on_load(cfg)
local v2 = v[2]
add_overlay(v2.x, v2.y, v2)
table.remove(cfg, i)
elseif v[1] == "next_item_name" then
next_item_name = v[2].next_item_name or next_item_name
table.remove(cfg, i)
else
i = i + 1
end
@ -61,6 +65,10 @@ end
function wml_actions.item(cfg)
local locs = wesnoth.get_locations(cfg)
cfg = helper.parsed(cfg)
if not cfg.name then
cfg.name = "item_" .. tostring(next_item_name)
next_item_name = next_item_name + 1
end
if not cfg.image and not cfg.halo then
helper.wml_error "[item] missing required image= and halo= attributes."
end
@ -70,6 +78,8 @@ function wml_actions.item(cfg)
local redraw = cfg.redraw
if redraw == nil then redraw = true end
if redraw then wml_actions.redraw {} end
if cfg.write_name then wesnoth.set_variable(write_name, cfg.name) end
return cfg.name
end
function wml_actions.remove_item(cfg)

View file

@ -99,13 +99,13 @@ void display::parse_team_overlays()
}
void display::add_overlay(const map_location& loc, const std::string& img, const std::string& halo,const std::string& team_name, bool visible_under_fog)
void display::add_overlay(const map_location& loc, const std::string& img, const std::string& halo, const std::string& team_name, const std::string& item_id, bool visible_under_fog)
{
if (halo_man_) {
const halo::handle halo_handle = halo_man_->add(get_location_x(loc) + hex_size() / 2,
get_location_y(loc) + hex_size() / 2, halo, loc);
const overlay item(img, halo, halo_handle, team_name, visible_under_fog);
const overlay item(img, halo, halo_handle, team_name, item_id, visible_under_fog);
overlays_->insert(overlay_map::value_type(loc,item));
}
}
@ -134,7 +134,7 @@ void display::remove_single_overlay(const map_location& loc, const std::string&
std::pair<Itor,Itor> itors = overlays_->equal_range(loc);
while(itors.first != itors.second) {
//If image or halo of overlay struct matches toDelete, remove the overlay
if(itors.first->second.image == toDelete || itors.first->second.halo == toDelete) {
if(itors.first->second.image == toDelete || itors.first->second.halo == toDelete || itors.first->second.id == toDelete) {
iteratorCopy = itors.first;
++itors.first;
//Not needed because of RAII --> halo_man_->remove(iteratorCopy->second.halo_handle);

View file

@ -147,7 +147,7 @@ public:
* One tile may have multiple overlays.
*/
void add_overlay(const map_location& loc, const std::string& image,
const std::string& halo="", const std::string& team_name="",
const std::string& halo="", const std::string& team_name="",const std::string& item_id="",
bool visible_under_fog = true);
/** remove_overlay will remove all overlays on a tile. */

View file

@ -75,7 +75,7 @@ editor_action* mouse_action_item::click_left(editor_display& disp, int x, int y)
}
const overlay& item = item_palette_.selected_fg_item();
disp.add_overlay(start_hex_, item.image, item.halo, "", true);
disp.add_overlay(start_hex_, item.image, item.halo, "", "", true);

View file

@ -21,8 +21,8 @@ struct overlay
{
overlay(const std::string& img, const std::string& halo_img,
halo::handle handle, const std::string& overlay_team_name, const bool fogged) : image(img), halo(halo_img),
team_name(overlay_team_name), halo_handle(handle) , visible_in_fog(fogged)
halo::handle handle, const std::string& overlay_team_name, const std::string& item_id, const bool fogged) : image(img), halo(halo_img),
team_name(overlay_team_name), id(item_id), halo_handle(handle) , visible_in_fog(fogged)
{}

View file

@ -3465,7 +3465,7 @@ int game_lua_kernel::intf_add_tile_overlay(lua_State *L)
if (game_display_) {
game_display_->add_overlay(map_location(x, y), cfg["image"], cfg["halo"],
cfg["team_name"], cfg["visible_in_fog"].to_bool(true));
cfg["team_name"], cfg["name"], cfg["visible_in_fog"].to_bool(true));
}
return 0;
}