User:Phenomenon: Difference between revisions

From WolfWiki
Jump to navigation Jump to search
Line 15: Line 15:
===== Installation =====
===== Installation =====


*Download the LUA script ''[http://www.force-opponents.org/lua/playsoundcmd.lua playsoundcmd.lua]'' to your etpro folder
*Download the LUA script ''[http://www.force-opponents.de/lua/playsoundcmd.lua playsoundcmd.lua]'' to your etpro folder


*Add the following command to your server.cfg to call the script on server start
*Add the following command to your server.cfg to call the script on server start

Revision as of 17:55, 25 July 2006

personal profile

personal bla bla later ...

lua script - playsoundcmd.lua

Converted "playsound" functionality from Tony J. White's ETPub Mod.

Requirements
Installation
  • Add the following command to your server.cfg to call the script on server start
set lua_modules "playsoundcmd.lua"
  • Restart your server
Source
------------------------------------------------------------------------
--
-- playsoundcmd.lua    -- Version 0.1
-- (c) 2006 Phenomenon -- phenomenon@force-opponents.de
--
------------------------------------------------------------------------
--
-- Converted "playsound" functionality from Tony J. White's ETPub Mod.
--
-- Usage: playsound [sound]                  -- GLOBAL SOUND
--        playsound [name|slot#] [sound]     -- CLIENT ONLY SOUND
--        playsound_env [name|slot#] [sound] -- SURROUNDING CLIENT SOUND
--
------------------------------------------------------------------------

MODULE_VERSION = "0.1"
EV_GENERAL_SOUND = 51
EV_GLOBAL_CLIENT_SOUND = 54
CON_DISCONNECTED = 0

function et_InitGame(leveltime, randomseed, restart)
	et.RegisterModname("playsoundcmd-"..MODULE_VERSION)
end

function et_ConsoleCommand()
	local command = string.lower(et.trap_Argv(0))
	
	if command == "playsound" or command == "playsound_env" then
		playsound_command()
		return 1
	end
	
	return 0
end

function playsound_command()
	if et.trap_Argc() < 2 then
		et.G_Print("^ousage: ^7playsound ^7[^fname^7|^fslot#^7] ^7[^fsound^7]\n")
		return
	end
	
	if et.trap_Argc() > 2 then
		local playerlist = client_numbers_from_string(et.trap_Argv(1))
		
		if table.getn(playerlist) ~= 1 then
			et.G_Print(string.format("^oplaysound: %s", match_one_player(playerlist)))
			return
		end
		
		local soundindex = et.G_SoundIndex(et.trap_Argv(2))
		
		if string.lower(et.trap_Argv(0)) == "playsound_env" then
			et.G_AddEvent(playerlist[1], EV_GENERAL_SOUND, soundindex)
		else
			local tempentity = et.G_TempEntity(et.gentity_get(playerlist[1], "r.currentOrigin"), EV_GLOBAL_CLIENT_SOUND)
			et.gentity_set(tempentity, "s.teamNum", playerlist[1])
			et.gentity_set(tempentity, "s.eventParm", soundindex)
		end
	else
		et.G_globalSound(et.trap_Argv(1))
	end
end

function client_numbers_from_string(partofname)
	local playerlist, index, maxclients = {}, 1, tonumber(et.trap_Cvar_Get("sv_maxclients"))
	
	table.setn(playerlist, 0)
	
	-- if a number is provided, it might be a slot#
	if tonumber(partofname) then
		local playernum = tonumber(partofname)
		
		if playernum >= 0 and playernum < maxclients then
			if et.gentity_get(playernum, "pers.connected") ~= CON_DISCONNECTED then
				playerlist[index] = playernum
				table.setn(playerlist, index)
				return playerlist
			end
		end
	end
	
	-- now look for name matches
	local cleanpartofname = sanitize_string(partofname)
	
	if string.len(cleanpartofname) < 1 then
		return playerlist
	end
	
	for playernum = 0, maxclients - 1, 1 do
		if et.gentity_get(playernum, "pers.connected") ~= CON_DISCONNECTED then
			if string.find(sanitize_string(et.gentity_get(playernum, "pers.netname")), cleanpartofname) then
				playerlist[index] = playernum
				table.setn(playerlist, index)
				index = index + 1
			end
		end
	end
	
	return playerlist
end

function sanitize_string(str)
	return trim(string.lower(et.Q_CleanStr(str)))
end

function trim(str)
	str = string.gsub(str, "^%s+", "")
	str = string.gsub(str, "%s+$", "")
	return str
end

function match_one_player(playerlist)
	if table.getn(playerlist) == 0 then
		return "^7no ^7connected ^7player ^7by ^7that ^7name ^7or ^7slot#\n"
	end
	
	if table.getn(playerlist) > 1 then
		local message = "^7more ^7than ^7one ^7player ^7name ^7matches. ^7be ^7more ^7specific ^7or ^7use ^7the ^7slot#:\n"
		
		for index, playernum in ipairs(playerlist) do
			message = string.format("%s^7%2i ^7- ^7%s\n", message, playernum, fix_ending_caret(et.gentity_get(playernum, "pers.netname")))
		end
		
		return message
	end
end

function fix_ending_caret(str)
	return string.gsub(str, "%^$", "^^ ") -- fix ending caret for printing
end