widelands-dev team mailing list archive
-
widelands-dev team
-
Mailing list archive
-
Message #09929
Re: [Merge] lp:~widelands-dev/widelands/bug-1675179-lua-economy into lp:widelands
Where does a lua programme "find" that flag to fetch the economy from?
would it not be easier to get some "default" economy for the headquarter
(like you do in that test :-). Or some warehouses?
Can we have a test for a bare flag? Is there an ecomony for every
disconnected flag?
For the tutotrial this may be enough, but is this stable enoough
for general usage. Can wee add some test where the economies change?
See my inline comments, too
will compile this and let the test run.
Diff comments:
> === modified file 'src/scripting/lua_map.cc'
> --- src/scripting/lua_map.cc 2017-01-30 14:40:12 +0000
> +++ src/scripting/lua_map.cc 2017-03-26 11:16:28 +0000
> @@ -3342,6 +3342,134 @@
> */
>
> /* RST
> +Economy
> +-------
> +.. class:: LuaEconomy
> +
> + Provides access to an economy. A player can have multiple economies;
> + you can get an economy from a :class:`Flag`.
> +*/
> +const char LuaEconomy::className[] = "Economy";
> +const MethodType<LuaEconomy> LuaEconomy::Methods[] = {
> + METHOD(LuaEconomy, ware_target_quantity),
> + METHOD(LuaEconomy, worker_target_quantity),
> + METHOD(LuaEconomy, set_ware_target_quantity),
> + METHOD(LuaEconomy, set_worker_target_quantity),
> + {nullptr, nullptr},
> +};
> +const PropertyType<LuaEconomy> LuaEconomy::Properties[] = {
> + {nullptr, nullptr, nullptr},
> +};
> +
> +void LuaEconomy::__persist(lua_State* L) {
/** store the economy based on player number and economy number. */
> + const Widelands::Economy* economy = get();
> + const Widelands::Player& player = economy->owner();
> + PERS_UINT32("player", player.player_number());
> + PERS_UINT32("economy", player.get_economy_number(economy));
> +}
> +
> +void LuaEconomy::__unpersist(lua_State* L) {
/** restore the dynamic economy based on player number and economy number. */
Mhh, is the economy_number number stable. e.g. If I load a LuaEconomy much later after the game has progressed?
Can this fail? and what will happen in lua, then?
> + Widelands::PlayerNumber player_number;
> + size_t economy_number;
> + UNPERS_UINT32("player", player_number);
> + UNPERS_UINT32("economy", economy_number);
> + const Widelands::Player& player = get_egbase(L).player(player_number);
> + set_economy_pointer(player.get_economy_by_number(economy_number));
> +}
> +
> +/* RST
> + .. method:: ware_target_quantity(warename)
> +
> + Returns the amount of the given ware that should be kept in stock for this economy.
> +
> + :arg warename: the name of the ware.
> + :type warename: :class:`string`
> +*/
> +int LuaEconomy::ware_target_quantity(lua_State* L) {
> + const std::string warename = luaL_checkstring(L, 2);
> + const Widelands::DescriptionIndex index = get_egbase(L).tribes().ware_index(warename);
> + if (get_egbase(L).tribes().ware_exists(index)) {
> + const Widelands::Economy::TargetQuantity& quantity = get()->ware_target_quantity(index);
> + lua_pushinteger(L, quantity.permanent);
> + } else {
> + report_error(L, "There is no ware '%s'.", warename.c_str());
> + }
> + return 1;
> +}
> +
> +/* RST
> + .. method:: worker_target_quantity(workername)
> +
> + Returns the amount of the given worker that should be kept in stock for this economy.
> +
> + :arg workername: the name of the worker.
> + :type workername: :class:`string`
> +*/
> +int LuaEconomy::worker_target_quantity(lua_State* L) {
> + const std::string workername = luaL_checkstring(L, 2);
> + const Widelands::DescriptionIndex index = get_egbase(L).tribes().worker_index(workername);
> + if (get_egbase(L).tribes().worker_exists(index)) {
> + const Widelands::Economy::TargetQuantity& quantity = get()->worker_target_quantity(index);
> + lua_pushinteger(L, quantity.permanent);
> + } else {
> + report_error(L, "There is no worker '%s'.", workername.c_str());
> + }
> + return 1;
> +}
> +
> +/* RST
> + .. method:: set_ware_target_quantity(warename)
> +
> + Sets the amount of the given ware type that should be kept in stock for this economy.
> +
> + :arg warename: the name of the ware type.
> + :type warename: :class:`string`
> +
> + :arg amount: the new target amount for the ware. Needs to be >= 0.
> + :type amount: :class:`integer`
> +*/
> +int LuaEconomy::set_ware_target_quantity(lua_State* L) {
> + const std::string warename = luaL_checkstring(L, 2);
> + const Widelands::DescriptionIndex index = get_egbase(L).tribes().ware_index(warename);
> + if (get_egbase(L).tribes().ware_exists(index)) {
> + const int quantity = luaL_checkinteger(L, 3);
> + if (quantity < 0) {
> + report_error(L, "Target ware quantity needs to be >= 0 but was '%d'.", quantity);
> + }
> + get()->set_ware_target_quantity(index, quantity, get_egbase(L).get_gametime());
> + } else {
> + report_error(L, "There is no ware '%s'.", warename.c_str());
> + }
> + return 1;
> +}
> +
> +/* RST
> + .. method:: set_worker_target_quantity(workername)
> +
> + Sets the amount of the given worker type that should be kept in stock for this economy.
> +
> + :arg workername: the name of the worker type.
> + :type workername: :class:`string`
> +
> + :arg amount: the new target amount for the worker. Needs to be >= 0.
> + :type amount: :class:`integer`
> +*/
> +int LuaEconomy::set_worker_target_quantity(lua_State* L) {
> + const std::string workername = luaL_checkstring(L, 2);
> + const Widelands::DescriptionIndex index = get_egbase(L).tribes().worker_index(workername);
> + if (get_egbase(L).tribes().worker_exists(index)) {
> + const int quantity = luaL_checkinteger(L, 3);
> + if (quantity < 0) {
> + report_error(L, "Target worker quantity needs to be >= 0 but was '%d'.", quantity);
> + }
> + get()->set_worker_target_quantity(index, quantity, get_egbase(L).get_gametime());
> + } else {
> + report_error(L, "There is no worker '%s'.", workername.c_str());
> + }
> + return 1;
> +}
> +
> +/* RST
> MapObject
> ---------
>
--
https://code.launchpad.net/~widelands-dev/widelands/bug-1675179-lua-economy/+merge/321008
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug-1675179-lua-economy into lp:widelands.
References