Beispiel #1
0
void LuaChatForm::OnOptionClicked(int option)
{
    SetMoney(1000000000);

	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	lua_getfield(l, LUA_REGISTRYINDEX, "PiAdverts");
	assert(lua_istable(l, -1));

	lua_pushinteger(l, GetAdvert()->ref);
	lua_gettable(l, -2);
	assert(!lua_isnil(l, -1));

	lua_getfield(l, -1, "onChat");
	assert(lua_isfunction(l, -1));

	LuaObject<LuaChatForm>::PushToLua(this);
	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushinteger(l, option);
	pi_lua_protected_call(l, 3, 0);

	lua_pop(l, 2);

	LUA_DEBUG_END(l, 0);
}
Beispiel #2
0
void LuaChatForm::Sold(Equip::Type t) {
	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	_get_trade_function(l, GetAdvert()->ref, "sold");

	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushstring(l, EnumStrings::GetString("EquipType", t));
	pi_lua_protected_call(l, 2, 0);

	LUA_DEBUG_END(l, 0);
}
Beispiel #3
0
void LuaChatForm::Bought(Equip::Type t) {
	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	_get_trade_function(l, GetAdvert()->ref, "bought");

	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushstring(l, LuaConstants::GetConstantString(l, "EquipType", t));
	pi_lua_protected_call(l, 2, 0);

	LUA_DEBUG_END(l, 0);
}
Beispiel #4
0
Sint64 LuaChatForm::GetPrice(Equip::Type t) const {
	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	_get_trade_function(l, GetAdvert()->ref, "getPrice");

	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushstring(l, EnumStrings::GetString("EquipType", t));
	pi_lua_protected_call(l, 2, 1);

	Sint64 price = Sint64(lua_tonumber(l, -1) * 100.0);
	lua_pop(l, 1);

	LUA_DEBUG_END(l, 0);

	return price;
}
Beispiel #5
0
int LuaChatForm::GetStock(Equip::Type t) const {
	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	_get_trade_function(l, GetAdvert()->ref, "getStock");

	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushstring(l, EnumStrings::GetString("EquipType", t));
	pi_lua_protected_call(l, 2, 1);

	int stock = lua_tointeger(l, -1);
	lua_pop(l, 1);

	LUA_DEBUG_END(l, 0);

	return stock;
}
Beispiel #6
0
bool LuaChatForm::DoesSell(Equip::Type t) const {
	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	_get_trade_function(l, GetAdvert()->ref, "canTrade");

	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushstring(l, EnumStrings::GetString("EquipType", t));
	pi_lua_protected_call(l, 2, 1);

	bool can_trade = lua_toboolean(l, -1) != 0;
	lua_pop(l, 1);

	LUA_DEBUG_END(l, 0);

	return can_trade;
}
Beispiel #7
0
void LuaChatForm::OnClose() {
	StationAdvertForm::OnClose();

	lua_State *l = Lua::manager->GetLuaState();
	int ref = GetAdvert()->ref;

	LUA_DEBUG_START(l);

	if (m_commodityTradeWidget) {
		lua_getfield(l, LUA_REGISTRYINDEX, "PiAdverts");
		assert(lua_istable(l, -1));

		lua_pushinteger(l, ref);
		lua_gettable(l, -2);
		assert(!lua_isnil(l, -1));

		lua_pushstring(l, "tradeWidgetFunctions");
		lua_pushnil(l);
		lua_settable(l, -3);

		lua_pop(l, 2);
	}

	if (!AdTaken()) return;

	lua_getfield(l, LUA_REGISTRYINDEX, "PiAdverts");
	assert(lua_istable(l, -1));

	lua_pushinteger(l, ref);
	lua_gettable(l, -2);
	assert(!lua_isnil(l, -1));

	lua_getfield(l, -1, "onDelete");
	if (!lua_isnil(l, -1)) {
		lua_pushinteger(l, ref);
		pi_lua_protected_call(l, 1, 0);
	}
	else
		lua_pop(l, 1);

	lua_pop(l, 1);

	lua_pushinteger(l, ref);
	lua_pushnil(l);
	lua_settable(l, -3);

	lua_pop(l, 1);

	LUA_DEBUG_END(l, 0);
}
Beispiel #8
0
void LuaChatForm::OnClickSell(int t) {
	lua_State *l = Lua::manager->GetLuaState();

	LUA_DEBUG_START(l);

	_get_trade_function(l, GetAdvert()->ref, "onClickSell");

	lua_pushinteger(l, GetAdvert()->ref);
	lua_pushstring(l, EnumStrings::GetString("EquipType", t));
	pi_lua_protected_call(l, 2, 1);

	bool allow_sell = lua_toboolean(l, -1) != 0;
	lua_pop(l, 1);

	LUA_DEBUG_END(l, 0);

	if (allow_sell) {
		if (BuyFrom(Pi::player, static_cast<Equip::Type>(t), true)) {
			Pi::Message(stringf(Lang::SOLD_1T_OF, formatarg("commodity", Equip::types[t].name)));
		}
		m_commodityTradeWidget->UpdateStock(t);
	}
}