Esempio n. 1
0
/*
 * Method: AddAdvert
 *
 * Add an advertisement to the station's bulletin board
 *
 * > ref = station:AddAdvert(description, chatfunc, deletefunc)
 *
 * Parameters:
 *
 *   description - text to display in the bulletin board
 *
 *   chatfunc - function to call when the ad is activated. The function is
 *              passed three parameters: a <ChatForm> object for the ad
 *              conversation display, the ad reference returned by <AddAdvert>
 *              when the ad was created, and an integer value corresponding to
 *              the action that caused the activation. When the ad is initially
 *              selected from the bulletin board, this value is 0. Additional
 *              actions (and thus values) are defined by the script via
 *              <ChatForm.AddAction>.
 *
 *   deletefunc - optional. function to call when the ad is removed from the
 *                bulletin board. This happens when <RemoveAdvert> is called,
 *                when the ad is cleaned up after
 *                <ChatForm.RemoveAdvertOnClose> is called, and when the
 *                <SpaceStation> itself is destroyed (eg the player leaves the
 *                system).
 *
 * Return:
 *
 *   ref - an integer value for referring to the ad in the future. This value
 *         will be passed to the ad's chat function and should be passed to
 *         <RemoveAdvert> to remove the ad from the bulletin board.
 *
 * Example:
 *
 * > local ref = station:AddAdvert(
 * >     "FAST SHIP to deliver a package to the Epsilon Eridani system.",
 * >     function (ref, opt) ... end,
 * >     function (ref) ... end
 * > )
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_spacestation_add_advert(lua_State *l)
{
	LUA_DEBUG_START(l);

	SpaceStation *s = LuaSpaceStation::GetFromLua(1);
	std::string description = luaL_checkstring(l, 2);

	if (!lua_isfunction(l, 3))
		luaL_typerror(l, 3, lua_typename(l, LUA_TFUNCTION));
	
	bool have_delete = false;
	if (lua_gettop(l) >= 4) {
		if (!lua_isnil(l, 4) && !lua_isfunction(l, 4))
			luaL_typerror(l, 4, lua_typename(l, LUA_TFUNCTION));
		have_delete = true;
	}

	int ref = s->AddBBAdvert(description, _create_chat_form);

	lua_getfield(l, LUA_REGISTRYINDEX, "PiAdverts");
	if (lua_isnil(l, -1)) {
		lua_pop(l, 1);
		lua_newtable(l);
		lua_pushvalue(l, -1);
		lua_setfield(l, LUA_REGISTRYINDEX, "PiAdverts");
	}

	lua_pushinteger(l, ref);

	lua_newtable(l);

	lua_pushstring(l, "onChat");
	lua_pushvalue(l, 3);
	lua_settable(l, -3);

	if (have_delete) {
		lua_pushstring(l, "onDelete");
		lua_pushvalue(l, 4);
		lua_settable(l, -3);
	}

	lua_settable(l, -3);
	lua_pop(l, 1);

	LUA_DEBUG_END(l,0);

	_register_for_station_delete(s);

	lua_pushinteger(l, ref);
	return 1;
}