Author Topic: Getting number of players in game  (Read 1754 times)

0 Members and 1 Guest are viewing this topic.

Getting number of players in game
Hello, how can you get the number of human players currently in-game via lua script? Is this possible at all?
Thanks.

 

Offline m!m

  • 211
Re: Getting number of players in game
As far as I can tell from the code it is not possible to get the currently active players in a multiplayer game.

 
Re: Getting number of players in game
Well that's what I was thinking anyway ^^
It would be nice to have a way to test if a ship is AI or player controlled and to know who is the host in a MP game for scripting purpose.
I know MP is not very attractive for many players out there so I can undestand why this was not implemented.

Thanks for the quick answer!!

 

Offline m!m

  • 211
Re: Getting number of players in game
This feature would likely be part of the scripting communication channel feature you already requested on GitHub. I looked through the multiplayer data structures and it does have references to the game objects for every player so it should be possible to determine which objects are controlled by a player and which are controlled by the AI.

 
Re: Getting number of players in game
Well until then (if it gets in the code on day who knows) I have another question.

Is there a limit to the number of mission sexp variables?
I'm trying to get around in multiplayer by using sexp var instead of lua var but as every ship on a mission may have several variables attached I wonder if it could cause FSO to crash if too many of them are set in a particular mission?

 

Offline m!m

  • 211
Re: Getting number of players in game
The maximum number of SEXP variables is currently 250.

 
Re: Getting number of players in game
Ok thanks I should not go above this as only players need sexp variables (there's currently 14 of them) to be set so that the "host" can acces these variables too in the script.

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Getting number of players in game
I'm pretty certain there is a SEXP solution to this issue. But I'm not in front of my PC to check. Look at the multiplayer status SEXPs

Even if you can't check the number of players you can definitely loop through starting wings and test if they are players.
« Last Edit: December 22, 2017, 10:39:56 am by karajorma »
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 
Re: Getting number of players in game
I'm using a script on gameplay start to hook the player then set the sexp variables corresponding to that player and a variable "isplayer" to true.

Then the main script looks for all sexp variables and puts them in a table so that the script can acces them easily (the table is updated when a sexp var may change) and is set to run with no restriction on single player (currently testing it seems to work fine) but will only run on Alpha 1's computer during a MP game to avoid conflicts (the script runs sexp that affect players and ai). I still need to test the MP side though.

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Getting number of players in game
I'm sure there is a quicker way to do that, but yeah, that should work.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 
Re: Getting number of players in game
I'm doing it that way because a part of the script runs on frame and it must check for certain values on ships that belong to some classes. And as i don't want to sexp this in every mission i made a script (i'm not very good at scripting but i'm learning from other's work)

  

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Getting number of players in game
I understand that. But even given that you are using scripts to call SEXPs. I still think there is an easier way to do this.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 
Re: Getting number of players in game
As i said i'm not really skilled for efficient scripting would you mind taking a look at the script if i post it here to see how i may do this in a better way?

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Getting number of players in game
I'm not skilled at scripting at all. But I was right that there is an easier solution. What's wrong with calling the num-players SEXP from your script?
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 
Re: Getting number of players in game
Well indeed it's my fault because in fact I mislabeled the topic: the question was rather how can I determine if a ship currently in mission is a player ship or an ai ship during a MP game (and not just getting the number of players)

Edit: in fact my initial problem was to find a way to transmit variables between the players through the network until i realized that i could use mission sexp variables ^^
« Last Edit: December 22, 2017, 11:52:11 pm by krevett62 »

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Getting number of players in game
Okay. That makes a little bit more sense.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 
Re: Getting number of players in game
Well it seems my solution isn't working as expected  :P
Single player is working fine.
Host is working fine.
Client crash on gameplay start (when the launch counter reaches 0) with this log:

I laughed at "get a stacktrace, a coder, an old priest and a young priest." ^^

Code: [Select]
Assert: "static_cast<size_t>(idx) < values.size()"
File: globalincs/flagset.h
Line: 83
Invalid value passed to flagset::set(), get a stacktrace, a coder, an old priest and a young priest.

ntdll.dll! ZwWaitForSingleObject + 10 bytes
KERNELBASE.dll! WaitForSingleObjectEx + 156 bytes
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
fs2_open_3_8_0_x64_AVX-FASTDBG.exe! <no symbol>
kernel32.dll! BaseThreadInitThunk + 13 bytes
ntdll.dll! RtlUserThreadStart + 33 bytes
[...]
[\Code]

 
Re: Getting number of players in game
After multiple tests I'm still unable to find what is causing the client to crash (as I said earlier I'm lacking serious scripting skill) so i'm posting the script in its current state if someone wish to give a helping hand ^^
As it is the script works when playing in SP or for the host in MP. The client will crash with an "invalid bitset(N)" as soon as gameplay starts. I tried to comment some parts of the script but was unable to find what is really causing the crash.
I tried to put some comments in the script, the main functions that are problematic are InitPlayerVariables(name), RunHost() ad RunClient()

Code: [Select]
#Conditional Hooks
$Application: FS2_Open
$On Game Init:
[
debug = true
cloak_ships = {["Shroud"] = true,["Hades#Stealth"] = true,["Basilisk"] = true,["Kamov#Stealth"] = true}
spectral_ships = {["Crusader"] = true,["Naginata"] = true,["Shroud"] = true,["Tempest"] = true,["Azan"] = true,["Haidar"] = true}
ecm_ships = {["Grendel"] = true,["Loki"] = true,["Mirage"] = true,["Phoenix"] = true,["Wolverine"] = true,["Kossac"] = true,["Sailin"] = true}
blindfire_ships = {["Coyote"] = true,["Patriot"] = true,["Phoenix"] = true,["Predator"] = true,["Reaper"] = true,["Sai"] = true,["Shroud"] = true,["Karak"] = true,["Lagg"] = true}
reverse_ships = {["Phoenix"] = true,["Sai"] = true,["Shroud"] = true,["Wolverine"] = true,["Basilisk"] = true,["Sabre"] = true,["Saracen"] = true}
Settings = {}

-- Various settings (modified by difficulty setting)
-- player_difficulty_factor is a bonus/malus in seconds to the duration of special features (base duration is for medium difficulty)
-- ai_difficulty_factor is a bonus/malus in percent to the duration of special features (base duration is for medium difficulty)
Settings.player_cloak_duration = 30
Settings.player_spectral_duration = 30
Settings.player_ecm_duration = 30
Settings.player_difficulty_factor = 2.0
Settings.ai_cloak_duration = 20
Settings.ai_decloak_duration = 16
Settings.ai_spectral_duration = 12
Settings.ai_nospectral_duration = 20
Settings.ai_ecm_duration = 12
Settings.ai_noecm_duration = 20
Settings.ai_random_active = 8
Settings.ai_random_inactive = 6
Settings.ai_random_chance = 65
Settings.ai_difficulty_factor = 0.2

-- Keys settings (change to your liking)
Settings.cloak_key = "Alt-Shift-F"
Settings.spectral_key = "Alt-Shift-G"
Settings.ecm_key = "Alt-Shift-Q"

function isHost()
local state = ba.getCurrentMPStatus(NIL)
if debug then gr.drawString(state) end
if state == "SINGLEPLAYER" or state == "MULTIPLAYER_MASTER" then
return true
else
return false
end
end

function SetGameVar()
if ba.getCurrentMPStatus(NIL) ==  "SINGLEPLAYER" then
local level = ba.getGameDifficulty()
Settings.factor_up = 1 + (level - 3) * Settings.ai_difficulty_factor
Settings.factor_down = 1 - (level - 3) * Settings.ai_difficulty_factor
Settings.player_factor = (3 - level) * Settings.player_difficulty_factor
else
Settings.factor_up = 1
Settings.factor_down = 1
Settings.player_factor = 0
end
end

function SetPlayerVar(name)
mn.SEXPVariables[name .. "_cloakremaining"] = Settings.player_cloak_duration + Settings.player_factor
mn.SEXPVariables[name .. "_spectralremaining"] = Settings.player_spectral_duration + Settings.player_factor
mn.SEXPVariables[name .. "_ecmremaining"] = Settings.player_ecm_duration + Settings.player_factor
mn.SEXPVariables[name .. "_cloaked"] = "false"
mn.SEXPVariables[name .. "_cloaktime"] = 0
mn.SEXPVariables[name .. "_decloaktime"] = 0
mn.SEXPVariables[name .. "_spectral"] = "false"
mn.SEXPVariables[name .. "_spectraltime"] = 0
mn.SEXPVariables[name .. "_nospectraltime"] = 0
mn.SEXPVariables[name .. "_ecm"] = "false"
mn.SEXPVariables[name .. "_ecmtime"] = 0
mn.SEXPVariables[name .. "_noecmtime"] = 0
mn.SEXPVariables[name .. "_isplayer"] = "true"
if debug then ba.print("Initialized player " .. name) end
end

function StorePlayerVariables(name)
mn.SEXPVariables[name .. "_cloakremaining"] = shipvar[name .. "_cloakremaining"]
mn.SEXPVariables[name .. "_spectralremaining"] = shipvar[name .. "_spectralremaining"]
mn.SEXPVariables[name .. "_ecmremaining"] = shipvar[name .. "_ecmremaining"]
mn.SEXPVariables[name .. "_cloaked"] = shipvar[name .. "_cloaked"]
mn.SEXPVariables[name .. "_cloaktime"] = shipvar[name .. "_cloaktime"]
mn.SEXPVariables[name .. "_decloaktime"] = shipvar[name .. "_decloaktime"]
mn.SEXPVariables[name .. "_spectral"] = shipvar[name .. "_spectral"]
mn.SEXPVariables[name .. "_spectraltime"] = shipvar[name .. "_spectraltime"]
mn.SEXPVariables[name .. "_nospectraltime"] = shipvar[name .. "_nospectraltime"]
mn.SEXPVariables[name .. "_ecm"] = shipvar[name .. "_ecm"]
mn.SEXPVariables[name .. "_ecmtime"] = shipvar[name .. "_ecmtime"]
mn.SEXPVariables[name .. "_noecmtime"] = shipvar[name .. "_noecmtime"]
end

function AIcloak(name)
math.randomseed(os.time())
local time = mn.getMissionTime()
local rand = math.random(1, 100)
local f1 = math.random(0, Settings.ai_random_active)
local f2 = math.random(0, Settings.ai_random_inactive)
if debug then gr.drawString("Running AIcloak on " .. name) end

if not shipvar[name .. "_decloaktime"] then
if rand <= Settings.ai_random_chance then
shipvar[name .. "_decloaktime"] = Settings.factor_up * Settings.ai_cloak_duration + f1
else
shipvar[name .. "_decloaktime"] = 0
end
end
if not shipvar[name .. "_cloaktime"] then
if rand <= Settings.ai_random_chance then
shipvar[name .. "_cloaktime"] = Settings.factor_up * Settings.ai_cloak_duration + Settings.factor_down * Settings.ai_decloak_duration + f1 + f2
else
shipvar[name .. "_cloaktime"] = Settings.factor_down * Settings.ai_decloak_duration + f2
end
end

if time <= shipvar[name .. "_decloaktime"] then
mn.runSEXP(" (ship-stealthy !" .. name .. "!) ")
elseif time > shipvar[name .. "_decloaktime"] and time <= shipvar[name .. "_cloaktime"] then
mn.runSEXP(" (ship-unstealthy !" .. name .. "!) ")
else
mn.runSEXP(" (ship-stealthy !" .. name .. "!) ")
shipvar[name .. "_cloaktime"] = shipvar[name .. "_cloaktime"] + Settings.factor_up * Settings.ai_cloak_duration + Settings.factor_down * Settings.ai_decloak_duration + f1 + f2
shipvar[name .. "_decloaktime"] = shipvar[name .. "_decloaktime"] + Settings.factor_up * Settings.ai_cloak_duration + Settings.factor_down * Settings.ai_decloak_duration + f1 + f2
end
end

function AIspectral(name)
math.randomseed(os.time())
local time = mn.getMissionTime()
local rand = math.random(1, 100)
local f1 = math.random(0, Settings.ai_random_active)
local f2 = math.random(0, Settings.ai_random_inactive)
if debug then gr.drawString("Running AIspectral on " .. name) end

if not shipvar[name .. "nospectraltime"] then
if rand <= Settings.ai_random_chance then
shipvar[name .. "nospectraltime"] = Settings.factor_up * Settings.ai_spectral_duration + f1
else
shipvar[name .. "nospectraltime"] = 0
end
end
if not shipvar[name .. "spectraltime"] then
if rand <= Settings.ai_random_chance then
shipvar[name .. "spectraltime"] = Settings.factor_up * Settings.ai_spectral_duration + Settings.factor_down * Settings.ai_nospectral_duration + f1 + f2
else
shipvar[name .. "spectraltime"] = Settings.factor_down * Settings.ai_nospectral_duration + f2
end
end

if time <= shipvar[name .. "nospectraltime"] then
mn.runSEXP(" (set-armor-type !" .. name .. "! !True! !SL-Spectral Shield! !Shields!) ")
elseif time > shipvar[name .. "nospectraltime"] and time <= shipvar[name .. "spectraltime"] then
mn.runSEXP(" (set-armor-type !" .. name .. "! !True! !SL-Standard Shield! !Shields!) ")
else
mn.runSEXP(" (set-armor-type !" .. name .. "! !True! !SL-Spectral Shield! !Shields!) ")
shipvar[name .. "spectraltime"] = shipvar[name .. "spectraltime"] + Settings.factor_up * Settings.ai_spectral_duration + Settings.factor_down * Settings.ai_nospectral_duration + f1 + f2
shipvar[name .. "nospectraltime"] = shipvar[name .. "nospectraltime"] + Settings.factor_up * Settings.ai_spectral_duration + Settings.factor_down * Settings.ai_nospectral_duration + f1 + f2
end
end

function AIecm(name)
math.randomseed(os.time())
local time = mn.getMissionTime()
local rand = math.random(1, 100)
local f1 = math.random(0, Settings.ai_random_active)
local f2 = math.random(0, Settings.ai_random_inactive)
if debug then gr.drawString("Running AIecm on " .. name) end

if not shipvar[name .. "noecmtime"] then
if rand <= Settings.ai_random_chance then
shipvar[name .. "noecmtime"] = Settings.factor_up * Settings.ai_ecm_duration + f1
else
shipvar[name .. "noecmtime"] = 0
end
end
if not shipvar[name .. "ecmtime"] then
if rand <= Settings.ai_random_chance then
shipvar[name .. "ecmtime"] = Settings.factor_up * Settings.ai_ecm_duration + Settings.factor_down * Settings.ai_noecm_duration + f1 + f2
else
shipvar[name .. "ecmtime"] = Settings.factor_down * Settings.ai_noecm_duration + f2
end
end

if time <= shipvar[name .. "noecmtime"] then
mn.runSEXP(" (turret-protect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !flak! !" .. name .. "!) ")
elseif time > shipvar[name .. "noecmtime"] and time <= shipvar[name .. "ecmtime"] then
mn.runSEXP(" (turret-unprotect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !flak! !" .. name .. "!) ")
else
mn.runSEXP(" (turret-protect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !flak! !" .. name .. "!) ")
shipvar[name .. "ecmtime"] = shipvar[name .. "ecmtime"] + Settings.factor_up * Settings.ai_ecm_duration + Settings.factor_down * Settings.ai_noecm_duration + f1 + f2
shipvar[name .. "noecmtime"] = shipvar[name .. "noecmtime"] + Settings.factor_up * Settings.ai_ecm_duration + Settings.factor_down * Settings.ai_noecm_duration + f1 + f2
end
end

function Playercloak(name)
local time = mn.getMissionTime()
if debug then gr.drawString("Running Playercloak on " .. name) end

if shipvar[name .. "_cloaked"] == "true" and tonumber(shipvar[name .. "_cloakremaining"]) > 0 then
mn.runSEXP(" (ship-stealthy !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !flak! !" .. name .. "!) ")
shipvar[name .. "_cloakremaining"] = tonumber(shipvar[name .. "_cloaktime"]) + tonumber(shipvar[name .. "_cloakremaining"]) - time
shipvar[name .. "_cloaktime"] = time
elseif shipvar[name .. "_cloaked"] == "true" and tonumber(shipvar[name .. "_cloakremaining"]) <= 0 then
mn.runSEXP(" (ship-unstealthy !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !flak! !" .. name .. "!) ")
shipvar[name .. "_cloakremaining"] = 0
shipvar[name .. "_decloaktime"] = time
shipvar[name .. "_cloaked"] = "false"
else
mn.runSEXP(" (ship-unstealthy !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !flak! !" .. name .. "!) ")
if tonumber(shipvar[name .. "_cloakremaining"]) < Settings.player_cloak_duration + Settings.player_factor then
shipvar[name .. "_cloakremaining"] = time + tonumber(shipvar[name .. "_cloakremaining"]) - tonumber(shipvar[name .. "_decloaktime"])
shipvar[name .. "_decloaktime"] = time
elseif tonumber(shipvar[name .. "_cloakremaining"]) >= Settings.player_cloak_duration + Settings.player_factor then
shipvar[name .. "_cloakremaining"] = Settings.player_cloak_duration + Settings.player_factor
end
end
StorePlayerVariables(name)
end

function Playerspectral(name)
local time = mn.getMissionTime()
if debug then gr.drawString("Running Playerspectral on " .. name) end

if shipvar[name .. "_spectral"] == "true" and tonumber(shipvar[name .. "_spectralremaining"]) > 0 then
mn.runSEXP(" (set-armor-type !" .. name .. "! !True! !SL-Spectral Shield! !Shields!) ")
shipvar[name .. "_spectralremaining"] = tonumber(shipvar[name .. "_spectraltime"]) + tonumber(shipvar[name .. "_spectralremaining"]) - time
shipvar[name .. "_spectraltime"] = time
elseif shipvar[name .. "_spectral"] == "true" and tonumber(shipvar[name .. "_spectralremaining"]) <= 0 then
mn.runSEXP(" (set-armor-type !" .. name .. "! !True! !SL-Standard Shield! !Shields!) ")
shipvar[name .. "_spectralremaining"] = 0
shipvar[name .. "_nospectraltime"] = time
shipvar[name .. "_spectral"] = "false"
else
mn.runSEXP(" (set-armor-type !" .. name .. "! !True! !SL-Standard Shield! !Shields!) ")
if tonumber(shipvar[name .. "_spectralremaining"]) < Settings.player_spectral_duration + Settings.player_factor then
shipvar[name .. "_spectralremaining"] = time + tonumber(shipvar[name .. "_spectralremaining"])  - tonumber(shipvar[name .. "_nospectraltime"])
shipvar[name .. "_nospectraltime"] = time
elseif tonumber(shipvar[name .. "_spectralremaining"]) >= Settings.player_spectral_duration + Settings.player_factor then
shipvar[name .. "_spectralremaining"] = Settings.player_spectral_duration + Settings.player_factor
end
end
StorePlayerVariables(name)
end

function Playerecm(name)
local time = mn.getMissionTime()
if debug then gr.drawString("Running Playerecm on " .. name) end

if shipvar[name .. "_ecm"] == "true" and tonumber(shipvar[name .. "_ecmremaining"]) > 0 then
mn.runSEXP(" (turret-protect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-protect-ship !flak! !" .. name .. "!) ")
shipvar[name .. "_ecmremaining"] = tonumber(shipvar[name .. "_ecmtime"]) + tonumber(shipvar[name .. "_ecmremaining"]) - time
shipvar[name .. "_ecmtime"] = time
elseif shipvar[name .. "_ecm"] == "true" and tonumber(shipvar[name .. "_ecmremaining"]) <= 0 then
mn.runSEXP(" (turret-unprotect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !flak! !" .. name .. "!) ")
shipvar[name .. "_ecmremaining"] = 0
shipvar[name .. "_noecmtime"] = time
shipvar[name .. "_ecm"] = "false"
else
mn.runSEXP(" (turret-unprotect-ship !beam! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !laser! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !missile! !" .. name .. "!) ")
mn.runSEXP(" (turret-unprotect-ship !flak! !" .. name .. "!) ")
if tonumber(shipvar[name .. "_ecmremaining"]) < Settings.player_ecm_duration + Settings.player_factor then
shipvar[name .. "_ecmremaining"] = time + tonumber(shipvar[name .. "_ecmremaining"])  - tonumber(shipvar[name .. "_noecmtime"])
shipvar[name .. "_noecmtime"] = time
elseif tonumber(shipvar[name .. "_ecmremaining"]) >= Settings.player_ecm_duration + Settings.player_factor then
shipvar[name .. "_ecmremaining"] = Settings.player_ecm_duration + Settings.player_factor
end
end
StorePlayerVariables(name)
end

function SetCloak(name)
if not shipvar[name .. "_isplayer"] then
AIcloak(name)
else
Playercloak(name)
end
end

function SetSpectral(name)
if not shipvar[name .. "_isplayer"] then
AIspectral(name)
else
Playerspectral(name)
end
end

function SetECM(name)
if not shipvar[name .. "_isplayer"] then
AIecm(name)
else
Playerecm(name)
end
end

-- Main function (Runs only on host)
function RunHost()
local ship
local class
local name
local var
local i

for i = 1, #mn.SEXPVariables do
var = mn.SEXPVariables[i]
if var:isValid() then
shipvar[tostring(var)] = tostring(var.Value)
if debug then
gr.drawString(tostring(var) .. ": " .. tostring(var.Value))
end
end
end

for i = 1, #mn.Ships do
ship = mn.Ships[i]

if ship:isValid() and ship:hasShipExploded() == 0 then
class = ship.Class

if cloak_ships[class.Name] then
name = ship.Name
SetCloak(name)
end

if spectral_ships[class.Name] then
name = ship.Name
SetSpectral(name)
end

if ecm_ships[class.Name] then
name = ship.Name
SetECM(name)
end

if class == tb.ShipClasses['Mine'] then
name = ship.Name
mn.runSEXP(" (when (> (distance !" .. name .. "! !<any friendly>!) !500!) (ship-stealthy !" .. name .. "!)) ")
mn.runSEXP(" (when (<= (distance !" .. name .. "! !<any friendly>!) !500!) (ship-unstealthy !" .. name .. "!) (alter-ship-flag !hidden-from-sensors! !True! !False! !" .. name .. "!)) ")
mn.runSEXP(" (when (<= (distance !" .. name .. "! !<any friendly>!) !100!) (self-destruct !" .. name .. "!)) ")
end

end
end
if debug then gr.drawString("Running RunHost()") end
end

-- HUD display (Runs for everyone)
function RunClient()
local player = hv.Player
local name
local class
local display

if player:isValid() and player:getBreedName() == "Ship" then
name = player.Name
class = player.Class
display = 0

if cloak_ships[class.Name] then
display = math.floor(tonumber(mn.SEXPVariables[name .. "_cloakremaining"].Value))
if mn.SEXPVariables[name .. "_cloaked"].Value == "true" then
mn.runSEXP(" (hud-set-custom-gauge-active !false! !cloak!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !cloakactive!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !cloaktime!) ")
else
mn.runSEXP(" (hud-set-custom-gauge-active !true! !cloak!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !false! !cloakactive!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !cloaktime!) ")
end
mn.runSEXP(" (hud-set-text !cloaktime! !" .. display .. "!) ")
end

if spectral_ships[class.Name] then
display = math.floor(tonumber(mn.SEXPVariables[name .. "_spectralremaining"].Value))
if mn.SEXPVariables[name .. "_spectral"].Value == "true" then
mn.runSEXP(" (hud-set-custom-gauge-active !false! !spectral!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !spectralactive!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !spectraltime!) ")
else
mn.runSEXP(" (hud-set-custom-gauge-active !true! !spectral!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !false! !spectralactive!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !spectraltime!) ")
end
mn.runSEXP(" (hud-set-text !spectraltime! !" .. display .. "!) ")
end

if ecm_ships[class.Name] then
display = math.floor(tonumber(mn.SEXPVariables[name .. "_ecmremaining"].Value))
if mn.SEXPVariables[name .. "_ecm"].Value == "true" then
mn.runSEXP(" (hud-set-custom-gauge-active !false! !ecm!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !ecmactive!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !ecmtime!) ")
else
mn.runSEXP(" (hud-set-custom-gauge-active !true! !ecm!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !false! !ecmactive!) ")
mn.runSEXP(" (hud-set-custom-gauge-active !true! !ecmtime!) ")
end
mn.runSEXP(" (hud-set-text !ecmtime! !" .. display .. "!) ")
end

if blindfire_ships[class.Name] then
mn.runSEXP(" (hud-set-custom-gauge-active !true! !blindfire!) ")
end

if reverse_ships[class.Name] then
mn.runSEXP(" (hud-set-custom-gauge-active !true! !reverse!) ")
end
end
if debug then gr.drawString("Running RunClient()") end
end
]


$On Mission Start:
[
--Reset variables
shipvar = {}
SetGameVar()
]


$On Gameplay Start:
[
local player = hv.Player
if player:isValid() and player:getBreedName() == "Ship" then SetPlayerVar(player.Name) end
]


$State: GS_STATE_GAME_PLAY
$On Frame:
[
if isHost() then RunHost() end
RunClient()
]


$State: GS_STATE_GAME_PLAY
$On Key Pressed:
[
--------------------------------------------------
--Cloak/ECM/Spectral shields script by krevett62--
--------------------------------------------------
local player = hv.Player
local name
local class
local time

if player:isValid() and player:getBreedName() == "Ship" then
name = player.Name
class = player.Class

if hv.Key == Settings.cloak_key and cloak_ships[class.Name] then
time = mn.getMissionTime()

if mn.SEXPVariables[name .. "_cloaked"].Value == "false" then
mn.SEXPVariables[name .. "_cloaktime"] = time
mn.SEXPVariables[name .. "_cloaked"] = "true"
else
mn.SEXPVariables[name .. "_decloaktime"] = time
mn.SEXPVariables[name .. "_cloaked"] = "false"
end
end

if hv.Key == Settings.spectral_key and spectral_ships[class.Name] then
time = mn.getMissionTime()

if mn.SEXPVariables[name .. "_spectral"].Value == "false" then
mn.SEXPVariables[name .. "_spectraltime"] = time
mn.SEXPVariables[name .. "_spectral"] = "true"
else
mn.SEXPVariables[name .. "_nospectraltime"] = time
mn.SEXPVariables[name .. "_spectral"] = "false"
end
end

if hv.Key == Settings.ecm_key and ecm_ships[class.Name] then
time = mn.getMissionTime()

if mn.SEXPVariables[name .. "_ecm"].Value == "false" then
mn.SEXPVariables[name .. "_ecmtime"] = time
mn.SEXPVariables[name .. "_ecm"] = "true"
else
mn.SEXPVariables[name .. "_noecmtime"] = time
mn.SEXPVariables[name .. "_ecm"] = "false"
end
end
end
]

#End
« Last Edit: December 24, 2017, 03:04:01 am by krevett62 »

 
Re: Getting number of players in game
Hello again ^^
Can someone tell me the difference between the variable hv.Player and the function ba.getCurrentPlayer()?

 

Offline m!m

  • 211
Re: Getting number of players in game
hv.Player is a handle for the current player object while ba.getCurrentPlayer() returns a player handle for the currently selected player. The first one is only relevant in-mission but the second contains non-mission specific data.