lua: move code related to getting unit types to its own file
This commit is contained in:
parent
2aca986bcb
commit
a669f25255
7 changed files with 121 additions and 50 deletions
|
@ -973,6 +973,7 @@ set(wesnoth-main_SRC
|
|||
scripting/lua_rng.cpp
|
||||
scripting/lua_team.cpp
|
||||
scripting/lua_types.cpp
|
||||
scripting/lua_unit_type.cpp
|
||||
scripting/mapgen_lua_kernel.cpp
|
||||
scripting/plugins/context.cpp
|
||||
scripting/plugins/manager.cpp
|
||||
|
|
|
@ -546,6 +546,7 @@ wesnoth_sources = Split("""
|
|||
scripting/lua_rng.cpp
|
||||
scripting/lua_team.cpp
|
||||
scripting/lua_types.cpp
|
||||
scripting/lua_unit_type.cpp
|
||||
scripting/mapgen_lua_kernel.cpp
|
||||
scripting/plugins/context.cpp
|
||||
scripting/plugins/manager.cpp
|
||||
|
|
|
@ -79,6 +79,7 @@
|
|||
#include "scripting/lua_gui2.hpp" // for show_gamestate_inspector
|
||||
#include "scripting/lua_team.hpp"
|
||||
#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc
|
||||
#include "scripting/lua_unit_type.hpp"
|
||||
#include "sdl/utils.hpp" // for surface
|
||||
#include "side_filter.hpp" // for side_filter
|
||||
#include "sound.hpp" // for commit_music_changes, etc
|
||||
|
@ -199,33 +200,6 @@ namespace {
|
|||
};
|
||||
}//unnamed namespace for queued_event_context
|
||||
|
||||
/**
|
||||
* Gets some data on a unit type (__index metamethod).
|
||||
* - Arg 1: table containing an "id" field.
|
||||
* - Arg 2: string containing the name of the property.
|
||||
* - Ret 1: something containing the attribute.
|
||||
*/
|
||||
static int impl_unit_type_get(lua_State *L)
|
||||
{
|
||||
char const *m = luaL_checkstring(L, 2);
|
||||
lua_pushstring(L, "id");
|
||||
lua_rawget(L, 1);
|
||||
const unit_type *utp = unit_types.find(lua_tostring(L, -1));
|
||||
if (!utp) return luaL_argerror(L, 1, "unknown unit type");
|
||||
unit_type const &ut = *utp;
|
||||
|
||||
// Find the corresponding attribute.
|
||||
return_tstring_attrib("name", ut.type_name());
|
||||
return_int_attrib("max_hitpoints", ut.hitpoints());
|
||||
return_int_attrib("max_moves", ut.movement());
|
||||
return_int_attrib("max_experience", ut.experience_needed());
|
||||
return_int_attrib("cost", ut.cost());
|
||||
return_int_attrib("level", ut.level());
|
||||
return_int_attrib("recall_cost", ut.recall_cost());
|
||||
return_cfgref_attrib("__cfg", ut.get_cfg());
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets some data on a race (__index metamethod).
|
||||
* - Arg 1: table containing an "id" field.
|
||||
|
@ -3670,16 +3644,7 @@ game_lua_kernel::game_lua_kernel(const config &cfg, CVideo * video, game_state &
|
|||
cmd_log_ << lua_team::register_metatable(L);
|
||||
|
||||
// Create the gettype metatable.
|
||||
cmd_log_ << "Adding gettype metatable...\n";
|
||||
|
||||
lua_pushlightuserdata(L
|
||||
, gettypeKey);
|
||||
lua_createtable(L, 0, 2);
|
||||
lua_pushcfunction(L, impl_unit_type_get);
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_pushstring(L, "unit type");
|
||||
lua_setfield(L, -2, "__metatable");
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
cmd_log_ << lua_unit_type::register_metatable(L);
|
||||
|
||||
//Create the getrace metatable
|
||||
cmd_log_ << "Adding getrace metatable...\n";
|
||||
|
@ -3834,26 +3799,20 @@ void game_lua_kernel::initialize()
|
|||
// Create the unit_types table.
|
||||
cmd_log_ << "Adding unit_types table...\n";
|
||||
|
||||
lua_settop(L, 0);
|
||||
lua_getglobal(L, "wesnoth");
|
||||
lua_pushlightuserdata(L
|
||||
, gettypeKey);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_newtable(L);
|
||||
BOOST_FOREACH(const unit_type_data::unit_type_map::value_type &ut, unit_types.types())
|
||||
{
|
||||
lua_createtable(L, 0, 1);
|
||||
lua_pushstring(L, ut.first.c_str());
|
||||
lua_setfield(L, -2, "id");
|
||||
lua_pushvalue(L, -3);
|
||||
lua_setmetatable(L, -2);
|
||||
luaW_pushunittype(L, ut.first);
|
||||
lua_setfield(L, -2, ut.first.c_str());
|
||||
}
|
||||
lua_setfield(L, -3, "unit_types");
|
||||
lua_pop(L, 2);
|
||||
lua_setfield(L, -2, "unit_types");
|
||||
|
||||
//Create the races table.
|
||||
cmd_log_ << "Adding races table...\n";
|
||||
|
||||
lua_settop(L, 0);
|
||||
lua_getglobal(L, "wesnoth");
|
||||
lua_pushlightuserdata(L
|
||||
, getraceKey);
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
/* Dummy pointer for getting unique keys for Lua's registry. */
|
||||
static char const v_executeKey = 0;
|
||||
static char const v_gettypeKey = 0;
|
||||
static char const v_getraceKey = 0;
|
||||
static char const v_getunitKey = 0;
|
||||
static char const v_unitvarKey = 0;
|
||||
|
@ -24,7 +23,6 @@ static char const v_ustatusKey = 0;
|
|||
|
||||
|
||||
luatypekey const executeKey = static_cast<void *>(const_cast<char *>(&v_executeKey));
|
||||
luatypekey const gettypeKey = static_cast<void *>(const_cast<char *>(&v_gettypeKey));
|
||||
luatypekey const getraceKey = static_cast<void *>(const_cast<char *>(&v_getraceKey));
|
||||
luatypekey const getunitKey = static_cast<void *>(const_cast<char *>(&v_getunitKey));
|
||||
luatypekey const unitvarKey = static_cast<void *>(const_cast<char *>(&v_unitvarKey));
|
||||
|
|
|
@ -20,7 +20,6 @@ typedef void* luatypekey;
|
|||
// i dont want to cast to void* each time ....
|
||||
// a drawback is, that these are now normal static variables wich are initialised at initialisation time (so you shoudn't use these at/before initialisation time).
|
||||
extern luatypekey const executeKey;
|
||||
extern luatypekey const gettypeKey;
|
||||
extern luatypekey const getraceKey;
|
||||
extern luatypekey const getunitKey;
|
||||
extern luatypekey const unitvarKey;
|
||||
|
|
79
src/scripting/lua_unit_type.cpp
Normal file
79
src/scripting/lua_unit_type.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "scripting/lua_unit_type.hpp"
|
||||
|
||||
#include "lua/lua.h"
|
||||
#include "lua/lauxlib.h"
|
||||
#include "scripting/lua_common.hpp"
|
||||
#include "unit_types.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Implementation for a lua reference to a unit_type.
|
||||
*/
|
||||
|
||||
// Registry key
|
||||
static const char * UnitType = "unit type";
|
||||
|
||||
/**
|
||||
* Gets some data on a unit type (__index metamethod).
|
||||
* - Arg 1: table containing an "id" field.
|
||||
* - Arg 2: string containing the name of the property.
|
||||
* - Ret 1: something containing the attribute.
|
||||
*/
|
||||
static int impl_unit_type_get(lua_State *L)
|
||||
{
|
||||
char const *m = luaL_checkstring(L, 2);
|
||||
lua_pushstring(L, "id");
|
||||
lua_rawget(L, 1);
|
||||
const unit_type *utp = unit_types.find(lua_tostring(L, -1));
|
||||
if (!utp) return luaL_argerror(L, 1, "unknown unit type");
|
||||
unit_type const &ut = *utp;
|
||||
|
||||
// Find the corresponding attribute.
|
||||
return_tstring_attrib("name", ut.type_name());
|
||||
return_int_attrib("max_hitpoints", ut.hitpoints());
|
||||
return_int_attrib("max_moves", ut.movement());
|
||||
return_int_attrib("max_experience", ut.experience_needed());
|
||||
return_int_attrib("cost", ut.cost());
|
||||
return_int_attrib("level", ut.level());
|
||||
return_int_attrib("recall_cost", ut.recall_cost());
|
||||
return_cfgref_attrib("__cfg", ut.get_cfg());
|
||||
return 0;
|
||||
}
|
||||
|
||||
namespace lua_unit_type {
|
||||
std::string register_metatable(lua_State * L)
|
||||
{
|
||||
luaL_newmetatable(L, UnitType);
|
||||
|
||||
lua_pushcfunction(L, impl_unit_type_get);
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_pushstring(L, "unit type");
|
||||
lua_setfield(L, -2, "__metatable");
|
||||
|
||||
return "Adding unit type metatable...\n";
|
||||
}
|
||||
}
|
||||
|
||||
void luaW_pushunittype(lua_State *L, const std::string & id)
|
||||
{
|
||||
lua_createtable(L, 0, 1);
|
||||
lua_pushstring(L, id.c_str());
|
||||
lua_setfield(L, -2, "id");
|
||||
luaL_setmetatable(L, UnitType);
|
||||
}
|
34
src/scripting/lua_unit_type.hpp
Normal file
34
src/scripting/lua_unit_type.hpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef LUA_UNIT_TYPE_HPP_INCLUDED
|
||||
#define LUA_UNIT_TYPE_HPP_INCLUDED
|
||||
|
||||
struct lua_State;
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* This namespace contains bindings for lua to hold a reference to a
|
||||
* unit type and access its stats.
|
||||
*/
|
||||
namespace lua_unit_type {
|
||||
std::string register_metatable(lua_State *);
|
||||
} //end namespace lua_team
|
||||
|
||||
/// Create a lua object containing a reference to a unittype, and a
|
||||
/// metatable to access the properties.
|
||||
void luaW_pushunittype(lua_State *, const std::string &);
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue