A website for every gema-assaultcube fan
 
HomeHome  CalendarCalendar  FAQFAQ  SearchSearch  MemberlistMemberlist  UsergroupsUsergroups  RegisterRegister  Log in  

Share | 
 

 Newbie in lua

View previous topic View next topic Go down 
AuthorMessage
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Newbie in lua   Sat Aug 04, 2012 9:39 pm

Hi, I found out about this lua extension today, so I tried to write a little script. Can you tell me if my script is correct, and answer my questions ? I can't host my own server so I couldn't test it :/ I used the script of the zombie mode to help me programming.

It's a tag game : there are 2 teams, and when you touch someone of the other team he changes team. A round ends when everyone is on the same team. Teams are shuffled before every round. You get 1 point everytime you touch someone of the other team. Touching a teammate does nothing. By "touch", I mean "knife" xD. Of course don't let your children play with knives >.>

Code:

PLUGIN_NAME = "TagGame"
PLUGIN_AUTHOR = "Baruch"
PLUGIN_VERSION = "1.0" -- 5 aug 2012

----------------

include("ac_server")

tag_mode = false

function onPlayerSayText(cn, text)
    if text == "!tag" and isadmin(cn) then
        if tag_mode then
            stop()
        else
            start()
        end
    end
end

function start()
    tag_mode = true
    cla = 0 -- can I initialize cla and rvsf here ? cla and rvsf are the number of players in each team for this round
    rvsf = 0
    setautoteam(false)
    changemap(getmapname(), GM_TSURV, 15)
    shuffleteams()
    clientprint(-1, "\f2TAG GAME BEGINS")
end

function stop()
    tag_mode = false
    setautoteam(true)
    shuffleteams()
    clientprint(-1, "\f2TAG GAME IS OVER")
end

-- everyone starts with 1 hp, but you cant get injured, cf onPlayerDamage

function onPlayerSpawn(cn)
    if not tag_mode then return end
    spawn(cn, 1, 0, 0, 0, GUN_KNIFE, GM_TSURV, GUN_KNIFE) -- can i set GUN_KNIFE as a primary weapon ?
    if getteam(cn) == TEAM_CLA then
        cla = cla + 1
    else
        rvsf = rvsf + 1
    end
end

function onPlayerDamage(actor_cn, target_cn, damage, actor_gun, gib)
    if not tag_mode then return end
    sethealth(target_cn, 1) -- is that sufficient to be sure that the target won't die ?
    local team_target = getteam(target_cn)
    if getteam(actor_cn) ~= team_target then
        if team_target = TEAM_CLA then
            setteam(target_cn, TEAM_RVSF) -- does the target instantly change team, or has to die first and wait 5 seconds ?
            cla = cla - 1
            rvsf = rvsf + 1
            if cla == 0 then
                forcearenawin(actor_cn)
        else
            setteam(target_cn, TEAM_CLA)
            rvsf = rvsf - 1
            cla = cla + 1
            if rvsf == 0 then
                forcearenawin(actor_cn)
        end
        setscore(actor_cn, getscore(actor_cn) + 1)
    end
end

function onPlayerCallVote(cn, type, text, number)
    if not tag_mode then return end
    if (type == SA_AUTOTEAM) or (type == SA_FORCETEAM) or (type == SA_SHUFFLETEAMS) then
        voteend(VOTE_NO)
    end
end

function onMapEnd() -- do I have to specify onMapChange too ?
    if not tag_mode then return end
    stop()
end

function onArenaWin()
    if not tag_mode then return end
    shuffleteams()
    cla = 0
    rvsf = 0
end

function onPlayerDisconnect(cn, reason)
    if not tag_mode then return end
    local team = getteam(cn)
    if team == TEAM_CLA then
        cla = cla - 1
    elseif team == TEAM_RVSF then
        rvsf = rvsf - 1
    end
end

function onPlayerDeath(target_cn, actor_cn, gib, gun) -- same thing as onPlayerDisconnect
    if not tag_mode then return end
    local team = getteam(target_cn)
    if team == TEAM_CLA then
        cla = cla - 1
    elseif team == TEAM_RVSF then
        rvsf = rvsf - 1
    end
end

Did I forget something ? Can you please answer my questions in the comments ? That would be really helpful Smile

Edit : I can't post a link to download the file because I'm a new member.
Back to top Go down
View user profile
Chidori



Messages : 4
Joined/Date d'inscription : 2012-07-31

PostSubject: Re: Newbie in lua   Sat Aug 04, 2012 11:36 pm

fix line 55: "if team_target == TEAM_CLA then"
close if's on line 60 and line 65
and onArenaWin should be like function onArenaWin(alive_cn)

1) can I initialize cla and rvsf here ? cla and rvsf are the number of players in each team for this round
You can

2) is that sufficient to be sure that the target won't die ?
Actually you make player die because of this command. Damage applies after this handler and player has 1 hp left. Set at least 51 hp to let player stay alive because knife damage is 50 - he will have 1 hp after hit.

3) does the target instantly change team, or has to die first and wait 5 seconds ?
With changing sethealth - instantly, doesn't have to wait.

4) do I have to specify onMapChange too ?
No. This handler is calling when intermission starts. onMapChange is calling after it.

5) same thing as onPlayerDisconnect.
Do not duplicate code, make function to use same code.

shuffleteams doesn't work - players stay in the same team when new round starts. Remove shuffleteams() from onAreaWin and move it to onPlayerDamage(), place it before forcearenawin(actor_cn)

Well, code is kinda buggy - it works but there is a problem with cla and rvsf counts, sometimes it counts wrong and new round doesn't start. You said that you can't host server - it's wrong. Maybe there is a problem with ms registration but u can connect locally and test script.
Back to top Go down
View user profile
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Sun Aug 05, 2012 5:49 am

Thank you a lot !

Quote :
2) is that sufficient to be sure that the target won't die ?
Actually you make player die because of this command. Damage applies after this handler and player has 1 hp left. Set at least 51 hp to let player stay alive because knife damage is 50 - he will have 1 hp after hit.

Ok, but then if the player is hit 2 times, he will die. Is there a way to be sure that players can't die ?

Quote :
5) same thing as onPlayerDisconnect.
Do not duplicate code, make function to use same code.

Can you tell me how ? Surprised
Something like this ?

Code:
function onPlayerDeath(target_cn, actor_cn, gib, gun)
      onPlayerDisconnect(tardet_cn, blablabla)
end

Quote :
shuffleteams doesn't work - players stay in the same team when new round starts. Remove shuffleteams() from onAreaWin and move it to onPlayerDamage(), place it before forcearenawin(actor_cn)

Ok, do you know why moving it will make it work ?

Quote :
You said that you can't host server - it's wrong. Maybe there is a problem with ms registration but u can connect locally and test script.

Oh yeah i had forgotten that. I can't create my own public server because I can't forward my ports.
To create a local server, i use -m localhost in additional server switches, but the server wizard fails, can you tell me how I should proceed ?

Thanks



Back to top Go down
View user profile
Park



Messages : 266
Joined/Date d'inscription : 2011-11-04

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Sun Aug 05, 2012 6:36 am

yo
1) 51 hp not needed. just enter return PLUGIN_BLOCK to deny the damage.
\t that would be

function onPlayerDamage(actor_cn, target_cn, damage, actor_gun, gib)
if not tag_mode then return end
-- sethealth(target_cn, 1) -- is that sufficient to be sure that the target won't die ?
local team_target = getteam(target_cn)
if getteam(actor_cn) ~= team_target then
if team_target = TEAM_CLA then
setteam(target_cn, TEAM_RVSF) -- does the target instantly change team, or has to die first and wait 5 seconds ?
cla = cla - 1
rvsf = rvsf + 1
if cla == 0 then
forcearenawin(actor_cn)
else
setteam(target_cn, TEAM_CLA)
rvsf = rvsf - 1
cla = cla + 1
if rvsf == 0 then
forcearenawin(actor_cn)
end
setscore(actor_cn, getscore(actor_cn) + 1)
end
-- THIS:
return PLUGIN_BLOCK
end

2) for a local server, the server.bat or server.sh made by the idiot DEVs of ac should be enough. you dont need to bother about ms if you dont forward ports it doesnt matter...
Back to top Go down
View user profile http://mgpmax.dyndns.org
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Sun Aug 05, 2012 7:15 am

Ah ok, can you explain what exactly does PLUGIN_BLOCK ?

If it's supposed to block the default action, why can I still say text with this code :

function onPlayerSayText(cn, text)
return PLUGIN_BLOCK
end

Edit : I managed to create my server Smile


Last edited by Spirée on Sun Aug 05, 2012 7:55 am; edited 1 time in total
Back to top Go down
View user profile
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Sun Aug 05, 2012 7:49 am

Another thing : when i put a script in the lua directory, and then remove it, the server seems to still read the script... How can I remove it ?
Back to top Go down
View user profile
Park



Messages : 266
Joined/Date d'inscription : 2011-11-04

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Sun Aug 05, 2012 1:30 pm

1) all scripts should be in lua/scripts/
if you remove them, they will not be loaded.
i think you dont remove them right.

1) return PLUGIN_BLOCK
blocks the default action, on which the handler is loaded.
look for handlers with B at the far right on sveark.info/ac/lua
2) you will always see your own message. this is client-sided.
Back to top Go down
View user profile http://mgpmax.dyndns.org
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Sun Aug 05, 2012 5:16 pm

Ok, so that means that this code prevents other players from seeing what the player(cn) says ?

Code:

function onPlayerSayText(cn, text)
return PLUGIN_BLOCK
end

Btw, what "setprotocol(-getdefaultprotocol())" is for ?
Back to top Go down
View user profile
Chidori



Messages : 4
Joined/Date d'inscription : 2012-07-31

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 3:01 am

Park wrote:
look for handlers with B at the far right on sveark.info/ac/lua
Ahah, now i know what is this "B" for Smile
Spirée wrote:
Ok, so that means that this code prevents other players from seeing what the player(cn) says ?

Code:

function onPlayerSayText(cn, text)
return PLUGIN_BLOCK
end
Absolutely right. This is used for !pm command e.g.
Back to top Go down
View user profile
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 4:09 pm

Could you tell me what return the functions getvelocity() and getangle() ?

I guess getangle() returns the direction the player is facing, but why isn't there setangle() ? (because there is a setvelocity())

Can you explain to me what is socket type ? And hostname ? ^^

Thanks !
Back to top Go down
View user profile
Sveark



Messages : 124
Joined/Date d'inscription : 2011-05-02
Age : 23
Location/Localisation : Russia

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:12 pm

1. getvelocity and setvelocity have no ties to each other, except for the name.
The setvelocity's signature is:
setvelocity (integer target_cn, real x, real y, real z, integer power)
Calling this function causes the player with CN=target_cn to move faster: each coordinate of the velocity vector is increased by 3*power*(x or y or z)/100 (cubes per millisecond I suppose cubes per second).

The getvelocity's signature is:
getvelocity (integer player_cn) = (real, real, real)
It returns three values each of which shows the instantaneous velocity (cubes/second) in three absolute directions (x, y and z - the 3 directions made by the OpenGL). The fact is that the player's velocity is operated client-side. I added the code which performs the velocity calculation server-side.

2. getangle returns 2 values. 1-st is the player's rotation (in the horizontal plane, X0Y), 2-nd is the player's slope (in the vertical plane, Z0Y). All the values are in angular degrees.

In AC, player's position is controlled client-side. The server cannot neither make a player to turn around nor move him somewhere. So there's no setangle. But I'll add this function in the version 1.4; it will control a virtual player (aka bot), like other such functions: setpos, setskin and so on.

3. Socket type is where the player connected from: whether from the Internet (ST_TCPIP) or LAN (ST_LOCAL). ST_EMPTY says that the player disconnected.

4. Hostname is the computer name. But in AC it coincides with the IP address. However, it may be "local" if the player connected from LAN.


Last edited by Sveark on Tue Aug 28, 2012 11:57 pm; edited 2 times in total
Back to top Go down
View user profile http://sveark.info/ac
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:16 pm

1. Oh nice ! But if the velocity calculation is server-side, how can you make the player go faster ? Surprised

2. Ok, but you can still teleport the player with setpos() no ?

Thanks for your answers Smile
Back to top Go down
View user profile
Sveark



Messages : 124
Joined/Date d'inscription : 2011-05-02
Age : 23
Location/Localisation : Russia

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:23 pm

It's an AC feature. A server can push players (it does this when a grenade explodes).

No, in AC, player's position is controlled client-side. setpos will only work for bots.
Back to top Go down
View user profile http://sveark.info/ac
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:28 pm

Ok ! So for now the lua extension doesn't allow bots ? (it will come with v1.4 ?)
Back to top Go down
View user profile
Sveark



Messages : 124
Joined/Date d'inscription : 2011-05-02
Age : 23
Location/Localisation : Russia

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:32 pm

For now, the Lua mod allows to make bots. But they will move like stone statues albino, cause there's no setangle.

Edit: wow, post #777 xD
Back to top Go down
View user profile http://sveark.info/ac
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:37 pm

Ah ok. Is that possible to create an AI for bots in a lua script ? To create a bot, I have to use the initclient function ? For now I can only make them move ? (no shoot, no angle, etc.)

Edit : JACKPOT !


Last edited by Spirée on Mon Aug 06, 2012 6:54 pm; edited 1 time in total
Back to top Go down
View user profile
Sveark



Messages : 124
Joined/Date d'inscription : 2011-05-02
Age : 23
Location/Localisation : Russia

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:53 pm

This is possible, but yes, bots won't shoot. To create a bot, you call addclient. It returns you the CN of the created bot. You set his name (setname) and preferably hostname (sethostname; it'll be shown in the logs). Next, you call initclient and do some stuff like setteam, spawn, setpos, dodamage. And to remove the bot, you call delclient.

Edit: also, it's desirable to do setsockettype(cn, ST_LOCAL) after you created your bot.


Last edited by Sveark on Mon Aug 06, 2012 7:00 pm; edited 1 time in total
Back to top Go down
View user profile http://sveark.info/ac
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Mon Aug 06, 2012 6:58 pm

Ok. And can I... (sorry for all these questions xD. I try to know what I can do and what I cannot)

Thank you for the answers ! I'm trying to get my own modded server hosted, to test some scripts in multiplayer Very Happy
Back to top Go down
View user profile
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Wed Aug 08, 2012 10:53 am

Hmmm I can't get the setvelocity function to work properly :/. Nothing seems to happen.

Quote :

setvelocity (integer target_cn, real x, real y, real z, integer power)
Calling this function causes the player with CN=target_cn to move faster: each coordinate of the velocity vector is increased by 3*power*(x or y or z)/100 (cubes per millisecond I suppose).

You mean that the velocity in the x direction in increased by 3*power*x/100 , and same for y and z ?

I used the command line setvelocity(0,1,1,1,10000) (my cn is 0), and nothing changes (I still run at the same velocity). I tried other numbers than 10000, same result.

What am I doing wrong ? ^^

Back to top Go down
View user profile
Park



Messages : 266
Joined/Date d'inscription : 2011-11-04

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Wed Aug 08, 2012 1:19 pm

1 is pretty low, try > 5...
Back to top Go down
View user profile http://mgpmax.dyndns.org
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Wed Aug 08, 2012 1:45 pm

Ah ok, I understood. I thought setvelocity was changing the speed at which the player is running.
Back to top Go down
View user profile
Sveark



Messages : 124
Joined/Date d'inscription : 2011-05-02
Age : 23
Location/Localisation : Russia

PostSubject: Re: Newbie in lua   Thu Aug 09, 2012 12:38 am

Spirée wrote:
You mean that the velocity in the x direction in increased by 3*power*x/100 , and same for y and z ?
Yes.

Spirée wrote:
I used the command line setvelocity(0,1,1,1,10000) (my cn is 0), and nothing changes (I still run at the same velocity). I tried other numbers than 10000, same result.

What am I doing wrong ? ^^
Take a look at the following implementation of the onPlayerSayText handler:
Code:
function onPlayerSayText(cn, text)
  setvelocity(cn, 1,1,1, tonumber(text))
end
Works fine for me. It tells the server to push a player with the power equal to what he said converted to a number. The vector of the push points to the right bottom corner of the minimap.

Spirée wrote:
Ah ok, I understood. I thought setvelocity was changing the speed at which the player is running.
You thought right. setvelocity changes the instantaneous velocity.
Back to top Go down
View user profile http://sveark.info/ac
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Thu Aug 09, 2012 7:27 am

I tried to use the setvelocity function to change the player's speed :

To slow a player, this code works :

Code:

slow = false

function onPlayerSayText(cn, text)
    if text == "!slow" then
        slow = true
    end
end

function LuaLoop()
    x,y,z = getvelocity(0)
    if slow then setvelocity(0,-x/5,-y/5,0,1) end
end

But when you jump, you dont move in directions x and y.

In the formula -x/a, the more a is close to 1, the more the player is slowed, and the more you have a trembling effect when the player doesn't move. However, if a is too close to 1, the player is trembling, and then you get a snowball effect (the player trembles more and more, until he hits a wall).

To remove the trembling effect, you can use something like that :

Code:

function LuaLoop()
    x,y,z = getvelocity(0)
    speed = math.sqrt(x*x + y*y)
    if slow and speed > 0.9 then setvelocity(0,-x/5,-y/5,0,1) end
end

To get bigger movement speed with this method doesn't work properly, because of the snowball effect :

Move
-> velocity increased
-> setvelocity pushes you
-> velocity is increased even more
-> setvelocity pushes you even more, etc.

So you just rush onto the first wall you hit.

Back to top Go down
View user profile
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Fri Aug 10, 2012 8:16 am

I've made a function that makes a player rush into the direction he's looking at (pushed by an invisible nade ^^)

Code:

function rush(cn, power)
    a, b = getangle(cn)
    a = a * math.pi / 180
    b = b * math.pi / 180
    setvelocity(cn, math.sin(a) * math.cos(b), - math.cos(a) * math.cos(b), math.sin(b) , power)
end
Back to top Go down
View user profile
Baruch



Messages : 105
Joined/Date d'inscription : 2012-08-04
Age : 25
Location/Localisation : France

Gema stats
farthest jump:

PostSubject: Re: Newbie in lua   Mon Sep 03, 2012 5:32 pm

Sveark, would it be possible to make a function that gives a player stealth ? (no one can see him, or maybe not everybody). I know it's not possible with the current version, but can't you make the clients believe a player is disconnected ?
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Newbie in lua   Today at 11:15 am

Back to top Go down
 
Newbie in lua
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Topdogs 2000pt Newbie Knights
» Help HH newbie
» A guide for MH2(dos)
» Newbie question
» Newbie SYM VTS200

Permissions in this forum:You cannot reply to topics in this forum
gema :: International :: Lua-
Jump to: