1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/scripting/lua/api/netpacks/SetResources.cpp

160 lines
3.0 KiB
C++
Raw Normal View History

/*
* api/netpacks/SetResources.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "SetResources.h"
#include "../../LuaStack.h"
#include "../Registry.h"
namespace scripting
{
namespace api
{
namespace netpacks
{
VCMI_REGISTER_SCRIPT_API(SetResourcesProxy, "netpacks.SetResources");
const std::vector<SetResourcesProxy::CustomRegType> SetResourcesProxy::REGISTER_CUSTOM =
{
2021-02-16 16:07:30 +02:00
{"new", &Wrapper::constructor, true},
{"getAbs", &SetResourcesProxy::getAbs, false},
{"setAbs", &SetResourcesProxy::setAbs, false},
{"getPlayer", &SetResourcesProxy::getPlayer, false},
{"setPlayer", &SetResourcesProxy::setPlayer, false},
{"setAmount", &SetResourcesProxy::setAmount, false},
{"getAmount", &SetResourcesProxy::getAmount, false},
{"clear", &SetResourcesProxy::clear, false},
{"toNetpackLight", &PackForClientProxy<SetResourcesProxy>::toNetpackLight, false}
};
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::getAbs(lua_State * L)
{
2021-02-16 16:07:30 +02:00
LuaStack S(L);
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
return LuaStack::quickRetBool(L, object->abs);
}
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::setAbs(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
bool value = false;
2021-02-16 16:07:30 +02:00
if(S.tryGet(2, value))
object->abs = value;
return S.retVoid();
}
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::getPlayer(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
S.clear();
S.push(object->player);
return 1;
}
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::setPlayer(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
PlayerColor value;
2021-02-16 16:07:30 +02:00
if(S.tryGet(2, value))
object->player = value;
return S.retVoid();
}
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::getAmount(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
Res::ERes type = Res::ERes::INVALID;
2021-02-16 16:07:30 +02:00
if(!S.tryGet(2, type))
return S.retVoid();
S.clear();
const TQuantity amount = vstd::atOrDefault(object->res, static_cast<size_t>(type), 0);
S.push(amount);
return 1;
}
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::setAmount(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
Res::ERes type = Res::ERes::INVALID;
2021-02-16 16:07:30 +02:00
if(!S.tryGet(2, type))
return S.retVoid();
int typeIdx = static_cast<int>(type);
if(typeIdx < 0 || typeIdx >= object->res.size())
return S.retVoid();
TQuantity amount = 0;
2021-02-16 16:07:30 +02:00
if(!S.tryGet(3, amount))
return S.retVoid();
object->res.at(typeIdx) = amount;
return S.retVoid();
}
2021-02-16 16:07:30 +02:00
int SetResourcesProxy::clear(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
std::shared_ptr<SetResources> object;
if(!S.tryGet(1, object))
return S.retVoid();
object->res.amin(0);
object->res.positive();
return S.retVoid();
}
}
}
}