AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Get user weapons on death (https://forums.alliedmods.net/showthread.php?t=245132)

Flick3rR 07-28-2014 18:54

Get user weapons on death
 
Hey, guys! I'm currently trying to save somehow player's weapons when he dies. The purpose is to respawn him with the same weapons as he died (if it's even possible). So, what I tried:
- Hooking CurWeapon and looping trough get_user_weapons.
- Geting user weapons on death (probably didn't work because he is already dead...)
The problem is that I don't know how to save those weapon indexes and later give them on spawn.
Any help is appereciated.
P.S.: Took a look here, some of the information may be useful: https://forums.alliedmods.net/showthread.php?t=182970

mottzi 07-29-2014 00:00

Re: Get user weapons on death
 
You'd do something like:

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <fun>

new iWeapons[33][32]

public 
plugin_init() 
{
    
RegisterHam(Ham_Killed"player""eventPlayerKilled"false)
    
RegisterHam(Ham_Spawn"player""eventPlayerSpawned"true)
}

public 
eventPlayerKilled(id)
{
    new 
iNum
    get_user_weapons
(idiWeapons[id], iNum)
}

public 
eventPlayerSpawned(id)
{
    if(!
is_user_alive(id))
    {
        return 
HAM_IGNORED
    
}
    
    new 
szWeaponName[45]
    
    for(new 
032i++)
    {
        if(
iWeapons[id][i])
        {
            
get_weaponname(iWeapons[id][i], szWeaponNamecharsmax(szWeaponName))
            
            
give_item(idszWeaponName)
        }
    }
    
    
arrayset(iWeapons[id], 032)
    
    return 
HAM_IGNORED



Flick3rR 07-30-2014 15:13

Re: Get user weapons on death
 
Well, that works, but only sometimes... :D
I mean, it seems like it's not the best way to do the job, since sometimes it doesn't give anything, sometimes it gives only primaries, etc.
Here is my code for now, with the mentioned bugs:
PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <hamsandwich>
#include <fakemeta>
#include <cstrike>
#include <fun>

const PH PLUGIN_HANDLED
const PC PLUGIN_CONTINUE

const TimeToRespawnAfterChoose 5

new player_lives[33]
new 
player_weapons[33][32]
new 
player_bpammo[33][32]
new 
g_flDeadOrigin[33][3]
new 
g_iMaxPlayers

public plugin_init() {
    
RegisterHam(Ham_Killed"player""onPlayerKilled")
    
    
g_iMaxPlayers get_maxplayers()
}

public 
onPlayerKilled(victimkillershouldgib)
{
    
    
pev(victimpev_origing_flDeadOrigin[victim])
    
set_task(1.0"taskAskForRespawn"victim)
    
    new 
num
    get_user_weapons
(killerplayer_weapons[killer], num)
        
    for(new 
inumi++)
        
cs_get_user_bpammo(killerplayer_bpammo[killer][player_weapons[killer][i]])    
}

public 
taskAskForRespawn(id)
{
    if(
is_user_alive(id))
        return 
PH
    
    
new szTitle[64]
    
formatex(szTitlecharsmax(szTitle), "You have\r %i\y extra lives^nDo you want to use one?"player_lives[id])
    
    new 
menu menu_create(szTitle"handleRespawnMenu")
    
    
menu_additem(menu"Yes"""0)
    
menu_additem(menu"No"""0)
    
    
menu_display(idmenu0)
    
    return 
PC
}

public 
handleRespawnMenu(idmenuitem)
{
    if(
item == MENU_EXIT)
    {
        
menu_destroy(menu)
        return 
PH
    
}
    
    if(
is_user_alive(id))
    {
        
client_print(idprint_chat"You are already alive!")
        return 
PH
    
}
    
    switch(
item)
    {
        case 
0:
        {
            
client_print(idprint_chat"You will be respawned in %d seconds!"TimeToRespawnAfterChoose)
            
set_task(float(TimeToRespawnAfterChoose), "respawnPlayer"id)
        }
        default: return 
PH
    
}    
    
    return 
PH
}

public 
respawnPlayer(id)
{
    if(
is_user_alive(id))
        return 
PH
    
    ExecuteHamB
(Ham_CS_RoundRespawnid)
    
    
set_pev(idpev_origing_flDeadOrigin[id])
    
    new 
szWeaponName[32]
    for(new 
1<= g_iMaxPlayersi++)
    {
        if(
is_user_connected(id) && player_weapons[id][i])
        {
            
get_weaponname(player_weapons[id][i], szWeaponNamecharsmax(szWeaponName))
            
            
strip_user_weapons(id)
            
give_item(id"weapon_knife")
            
            
give_item(idszWeaponName)
            
cs_set_user_bpammo(idplayer_weapons[id][i], player_bpammo[id][i])
        }
    }
    
    return 
PC


I guess this is not the right way to set the BPAmmo, too, but it's the same with or without it. I'm currently waiting for better and improved suggestions.
Thanks!

NikKOo31 07-30-2014 15:33

Re: Get user weapons on death
 
Just edited a few things :bee:

PHP Code:

/* Plugin generated by AMXX-Studio */ 

#include <amxmodx> 
#include <hamsandwich> 
#include <fakemeta> 
#include <cstrike> 
#include <fun> 

#define task_respawn    49839483

const PH PLUGIN_HANDLED 
const PC PLUGIN_CONTINUE 

const TimeToRespawnAfterChoose 

new player_lives[33
new 
player_weapons[33][32
new 
player_bpammo[33][32
new 
g_flDeadOrigin[33][3

public 
plugin_init() { 
    
//don't forget to register plugin
    
RegisterHam(Ham_Killed"player""onPlayerKilled"
     


public 
onPlayerKilled(victim

     
    
pev(victimpev_origing_flDeadOrigin[victim]) 
    
set_task(1.0"taskAskForRespawn"victim task_respawn
     
    new 
num 
    get_user_weapons
(victimplayer_weapons[victim], num
    
player_weapons[victim][num] = 0
    
for(new inumi++) 
        
cs_get_user_bpammo(victimplayer_bpammo[victim][player_weapons[victim][i]])     


public 
taskAskForRespawn(id

    
id -= task_respawn
    
if(is_user_alive(id)) 
        return 
PH 
     
    
new szTitle[64
    
formatex(szTitlecharsmax(szTitle), "You have\r %i\y extra lives^nDo you want to use one?"player_lives[id]) 
     
    new 
menu menu_create(szTitle"handleRespawnMenu"
     
    
menu_additem(menu"Yes"""0
    
menu_additem(menu"No"""0
     
    
menu_display(idmenu0
     
    return 
PH 


public 
handleRespawnMenu(idmenuitem

    if(
item == MENU_EXIT
    { 
        
menu_destroy(menu
        return 
PH 
    

     
    if(
is_user_alive(id)) 
    { 
        
client_print(idprint_chat"You are already alive!"
        return 
PH 
    

     
    switch(
item
    { 
        case 
0
        { 
            
client_print(idprint_chat"You will be respawned in %d seconds!"TimeToRespawnAfterChoose
            
set_task(float(TimeToRespawnAfterChoose), "respawnPlayer"id task_respawn
        } 
        default: return 
PH 
    
}     
    
    
menu_destroy(menu
    return 
PH 


public 
respawnPlayer(id

    
id -= task_respawn
    
if(is_user_alive(id)) 
        return 
PH 
     
    ExecuteHamB
(Ham_CS_RoundRespawnid
     
    
set_pev(idpev_origing_flDeadOrigin[id]) 

    
strip_user_weapons(id

    new 
szWeaponName[32] , i
    
while(player_weapons[id][i]) 
    { 
        
get_weaponname(player_weapons[id][i], szWeaponNamecharsmax(szWeaponName)) 
        
give_item(idszWeaponName
        
cs_set_user_bpammo(idplayer_weapons[id][i], player_bpammo[id][i]) 
        
player_weapons[id][i] = 0
        i
++
    } 
     
    return 
PC 



Flick3rR 07-30-2014 16:13

Re: Get user weapons on death
 
The result is the same... Sometimes gives nothing, sometimes gives only primary, even once I was freezed?! No errors in the log for now. Well, I gues I will search for an alternative.
The question is currently staying.
Thanks!

mottzi 07-30-2014 16:53

Re: Get user weapons on death
 
Did you debug the array holding the weapon ids in the kill-event and in the respawn function?

Your for loop is wrong ( for(new i = 1; i <= g_iMaxPlayers; i++) ), have another look at my example. Stripping the players weapon is enough, you dont need to give him a knife, as tat would give him the knife twice (because it's already included in the weapons-array), then, you are not destroying the menus in the handler sometimes.

Edit: Btw, you are saving the killers weapons and not the victims...
Edit2: Both of you guys do the bpammo thing wrong; take this as a base and add your origin stuff:
PHP Code:

#include <amxmodx> 
#include <amxmisc> 
#include <hamsandwich> 
#include <fun> 
#include <cstrike> 

new iWeapons[33][32
new 
iWeaponBackPack[33][32]
new 
iDeadOrigin[33][3]

public 
plugin_init()  

    
RegisterHam(Ham_Killed"player""eventPlayerKilled"false


public 
eventPlayerKilled(id

    new 
iNum 
    get_user_weapons
(idiWeapons[id], iNum
    
    for(new 
0iNumi++)
    {
        if(
iWeapons[id][i]) 
        {
            if(
iWeapons[id][i] != CSW_KNIFE)
            {
                
iWeaponBackPack[id][i] = cs_get_user_bpammo(idiWeapons[id][i])  
            }
        }
        else
        {
            break
        }
    }
    
    
get_user_origin(idiDeadOrigin[id])
    
    new 
menu menu_create("Respawn ?""menuRespawnHandler")
    
    
menu_additem(menu"Yes")
    
menu_additem(menu"No")
    
    
menu_display(idmenu)


public 
menuRespawnHandler(idmenuitem

    if(!
is_user_connected(id) || is_user_alive(id))
    {
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED    
    
}
    
    if(
item == 0)
    {
        
ExecuteHamB(Ham_CS_RoundRespawnid
        
set_user_origin(idiDeadOrigin[id])
        
        new 
szWeaponName[45
        
        for(new 
032i++) 
        { 
            if(
iWeapons[id][i]) 
            { 
                if(
iWeapons[id][i] == CSW_KNIFE)
                {
                    
give_item(id"weapon_knife"
                }
                else
                {
                    
get_weaponname(iWeapons[id][i], szWeaponNamecharsmax(szWeaponName)) 
                    
                    
give_item(idszWeaponName
                    
cs_set_user_bpammo(idiWeapons[id][i], iWeaponBackPack[id][i]) 
                }
            } 
            else
            {
                break
            }
        } 
    }
    
    
arrayset(iWeapons[id], 032
    
arrayset(iWeaponBackPack[id], 032
    
    
menu_destroy(menu)
    return 
PLUGIN_HANDLED


Note: You could avoid useless loop cycles using break when iWeapons[id][i] == 0...

Edit3: Added the origin stuff and testet, should work o.k.

Flick3rR 07-30-2014 18:33

Re: Get user weapons on death
 
Lol, mottzi, thanks for the note. Really dumb - getting killer's weapons. Now it's all okay, thank you all guys!

^SmileY 07-30-2014 18:43

Re: Get user weapons on death
 
hook curWeapon event, and save the weapons into global var.

after, use csx to hook the death and check the global var...

:)

Flick3rR 07-30-2014 19:01

Re: Get user weapons on death
 
I think it's not needed to hook that event, since I can get the weapons once on user's death and store them into the variable. The great example by mottzi is working (made little changes for the code). I think the case is closed! Thanks to all!

mottzi 07-30-2014 20:28

Re: Get user weapons on death
 
Quote:

Originally Posted by ^SmileY (Post 2176444)
hook curWeapon event, and save the weapons into global var.

after, use csx to hook the death and check the global var...

:)

using CurWeapon would probably be the poorest way to go...


All times are GMT -4. The time now is 13:02.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.