Hello. This code will allow you to see which players have been on your screen recently, and optionally alert under certain conditions. The main code is a persistent code, which can be used to build up other codes as well, but my main need was to know who has been on my screen during my hunt...
The HUD is very basic and I would be happy if someone would improve and allow me to share it with the public.
Let's not waste any time, the persistent code:
init start
local counterResetTime = 10 -- How long (in seconds) can creature leave screen before counter is reset?
local waypointSection = 'Spawn' -- Only count while in given waypoint section (or 'any').
local alert = {
enabled = false, -- Alert enabled
screenTime = 5, -- How long does player need to stay on screen to trigger alert?
onlySkull = true, -- Alert only if skull
wptSection = 'Spawn' -- Only play on given waypoint section (or 'any')
}
-- Do not edit below.
waypointSection = waypointSection:lower()
alert.wptSection = alert.wptSection:lower()
init end
auto(500)
SCREEN_CREATURES = SCREEN_CREATURES or {}
foreach creature player 'ps' do
if($id ~= player.id) then
if(waypointSection == 'any' or waypointSection == $wptsection:lower()) then
if(not SCREEN_CREATURES[player.name]) then
SCREEN_CREATURES[player.name] = {firstSeen=os.time(), longestTime=0, currentTime=0, obj=player}
end
SCREEN_CREATURES[player.name].lastSeen = os.time()
local currentTime = SCREEN_CREATURES[player.name].currentTime + 0.5
SCREEN_CREATURES[player.name].currentTime = currentTime
if(currentTime > SCREEN_CREATURES[player.name].longestTime) then
SCREEN_CREATURES[player.name].longestTime = currentTime
end
end
end
end
local playAlert = false
for name, player in pairs(SCREEN_CREATURES) do
local lastSeenSeconds = os.time() - player.lastSeen
if(lastSeenSeconds > counterResetTime) then
player.currentTime = 0
end
if(alert.enabled and player.currentTime >= alert.screenTime and
(not onlySkull or player.obj.skull ~= SKULL_NOSKULL) and
(alert.wptSection == 'any' or alert.wptSection == $wptsection:lower())) then
playAlert = true
end
-- custom code may be added here, to make e.g. ss when player on screen
end
if(playAlert) then
playsound('monster.wav')
end
--Print, if not using HUD, for testing!
--print()
--for name, v in pairs(SCREEN_CREATURES) do
-- print(name, v.firstSeen, v.lastSeen, v.currentTime, v.longestTime)
--end
HUD code:
init start
local timeToDisplay = 20 -- Amount of seconds player has to be on screen max to be displayed
init end
auto(500)
setfontstyle("Verdana", 8, 100)
setfontcolor(0xFF0000)
setfontborder(1, 0x000000)
if(SCREEN_CREATURES) then
local hidden = 0
local amount = 0
for name, player in pairs(SCREEN_CREATURES) do
if(player.longestTime >= timeToDisplay) then
drawtext(name, 13, 12 * amount)
drawtext(math.floor(player.currentTime), 170, 12 * amount)
drawtext(math.floor(player.longestTime), 210, 12 * amount)
amount = amount + 1
else
hidden = hidden + 1
end
end
setfontcolor(0x2277FF)
drawtext("And " .. hidden .. " others...", 13, 12 * amount)
setposition($worldwin.right, $worldwin.bottom - (amount + 1) * 12)
end
timeToDisplay is the amount of seconds the player has to be in your screen to display his name, otherwise he will be shown under "and 4 others" for example.
If you only came for the codes, you may skip below text!
How it works (small intro for scripters who may find other usages of it):
The persistent code will monitor all players that have been on your screen lately, in a table, with the values:
- firstSeen - The first time the player was seen on your screen (os.time())
- lastSeen - The last time the player was seen on your screen ((os.time())
- longestTime - The longest time in seconds the player was on your screen
- currentTime - The amount of time in seconds the player has been in your screen (if he's still there)
- obj - The players userdata object
To obtain the data for a specific player (on any code, action script or whatever), you can do it as such:
local onScreenInfo = SCREEN_CREATURES[playerName]
if(onScreenInfo ~= nil) then
if(onScreenInfo.currentTime ~= 0) then
print("Player \"" .. playerName .. "\" is currently on your screen for " .. onScreenInfo.currentTime .. " seconds!")
end
local firstOnScreen = os.time() - onScreenInfo.firstSeen
print("Player \"" .. playerName .. "\" was first seen on your screen: " .. firstOnScreen .. " seconds ago.")
local lastOnScreen = os.time() - onScreenInfo.lastSeen
print("Player \"" .. playerName .. "\" was last seen on your screen: " .. lastOnScreen .. " seconds ago.")
else
print("Player \"" .. playerName .. "\" has not been on your screen!")
end
The SCREEN_CREATURES variable will be accessible on all of your codes, so it's not limited to only this persistent code!
Enjoy and good luck!