AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] Custom Weapon Hud Sprites + Slot Redirection (https://forums.alliedmods.net/showthread.php?t=175632)

Arkshine 01-06-2012 15:07

[TUT] Custom Weapon Hud Sprites + Slot Redirection
 
2 Attachment(s)
Custom Weapon Hud Sprites + Slot Redirection
Updated January 6th, 2012


http://dl.dropbox.com/u/27827560/Scr...6_19.54.38.png

Ever wanted to show your new weapon in the hud ?
It's actually and surprisely easy to show a new sprite.

The following method have these features :
  • Can change few sprites types - weapon, weapon_s (select), ammo, crosshair, zoom
  • Can redirect slots - you can decide how to arrange your weapons list
  • Dynamic - you don't need to restart, waiting, or something
  • Per player - you can handle new weapons per player with conditions
The drawbacks are :
  • Must be based on an original weapon - Meaning you replace an existing weapon, you can't add
  • Therefore you can have only 29 new weapons at once - which is actually good and you could handle different set of weapons per player
  • Slot redirection might not work always properly - Discovered while writing the tutorial, it doesn't work well

I wanted to wait by making an API before posting, but that's not really useful. Though, I will do it, some people may find that useful.

Such functionnality would deserve to be integrated in a plugin which manages custom weapon, like WeaponMod, so for each new weapon, an original weapon is distribuated to be used as replacement.
Purpose would be to avoid multiples plugins using different method for creating a new weapon, using the same original weapon.

Important : While writing the tutorial and testing, I've noticed the slot redirection doesn't work well for all weapons. For example, ak47 doesn't like to be redirected, or knife doesn't want to go on slot 1/2. I have no idea why, still need to be figured out. I have not tested much and don't have much time, so it will be up to you people to give feebacks and therefore improving the way of altering hud. Even without the redirection, you might encounter issues. Please understand that's something which would be needed to test. I've posted even though I'm not sure it will work properly all the time, for the sake of knowledge and because the more people looks at that, the better chance to get a more robust method.
Please keep in mind the tutorial has the purpose only for now to show the basis of the method.




Contents :



How it works ? top
The main key is the game event : WeaponList.
This message basically configures the hud weapon list.
That's something you should send with the right values when a weapon is added to the player's inventory.
Values you can find here.

The structure looks like :

Code:

string        WeaponName
byte        PrimaryAmmoID
byte        PrimaryAmmoMaxAmount
byte        SecondaryAmmoID
byte        SecondaryAmmoMaxAmount
byte        SlotID
byte        NumberInSlot
byte        WeaponID
byte        Flags

The second key is WeaponName and SlotID.
The first argument is used for example when you select a weapon, it's the same as entering in the console the weapon name.
It's there you change it by your new weapon name, like weapon_flare.
To select the weapon, you will need to hook this weapon name in order to redirect it to the original weapon.

The latter argument will help you to change the slot of a weapon.
It will need some extra code to work properly.
The third key is the first argument which refers also to the file named weapon_*.txt (* = name) in the /sprites directory.
It contains the sprites to use for each types.
A full list of types would be : weapon, weapon_s, ammo, ammo2, autoaim, crosshair, zoom, zoom_aim.
(Though I'm not sure ammo2, autoaim or zoom_aim could be used.)

So, in this file you can define your new sprites.
Here the format of such file :
Code:

numberOfSprites
<type> <resolution> <spriteFile> <shiftX> <shiftY> <width> <height>
...


Example with weapon_knife.txt :
Code:

10
weapon                        320 320hud1        0        0        80        20
weapon_s                320 320hud1        0        20        80        20
ammo                        320 320hud2        0        16        18        18
crosshair                320 crosshairs        24        0        24        24
autoaim                        320 crosshairs        0        72        24        24
weapon                        640 640hud10        0        135        170        45
weapon_s                640 640hud11        0        135        170        45
ammo                        640 640hud7        0        72        24        24
crosshair                640 crosshairs        24        0        24        24
autoaim                        640 crosshairs        0        72        24        24


I think it's self-explanatory. Just look at a sprite file and see.
The fourth key is to precache the required files.
Both .txt and .spr/.tga files are needed on the client.
They can be precached without problem using precache_generic().

Don't hesitate to make elaborate name for the weapon name, so file will be somewhow unique.
Because if there is another plugin using the same weapon name, the existing file on the client won't be overwrite.
Also, for the sprites, you may want to create custom folders.

Coding example in steps top
Let's say you want to replace the knife with a flare, and you want to change its slot too.

So, you have a weapon_ArkFlare.txt file with as content :
Code:

2
weapon                        640 640hud19        0        0        170        45
weapon_s                640 640hud20        0        0        170        45


It will use sprites/640hud19.spr and sprites/640hud20.spr files.

http://dl.dropbox.com/u/27827560/640hud19.gif http://dl.dropbox.com/u/27827560/640hud20.gif

Precaching your files top
All you need is to make the client downloading the files. That's precache_generic is used.
Code:
public plugin_precache() {     precache_generic( "sprites/weapon_ArkFlare.txt" );     precache_generic( "sprites/640hud19.spr" );     precache_generic( "sprites/640hud20.spr" ); }
Hooking when player got a weapon top
We need to hook when a player gets a knife.
So, we can send a WeaponList message to tell the client to use "weapon_ArkFlare".

We can the use forward Ham_Item_AddToPlayer for that.
Code:
RegisterHam( Ham_Item_AddToPlayer, "weapon_knife", "OnAddToPlayerKnife", .Post = true );

Then we can send our message.
Code:
new MsgIndexWeaponList; public plugin_init() {     RegisterHam( Ham_Item_AddToPlayer, "weapon_knife", "OnAddToPlayerKnife", .Post = true );     MsgIndexWeaponList = get_user_msgid( "WeaponList" ); } public OnAddToPlayerKnife( const item, const player ) {     if( pev_valid( item ) && is_user_alive( player ) ) // just for safety.     {         message_begin( MSG_ONE, MsgIndexWeaponList, .player = player );         {             write_string( "weapon_ArkFlare" );    // WeaponName             write_byte( -1 );                   // PrimaryAmmoID             write_byte( -1 );                   // PrimaryAmmoMaxAmount             write_byte( -1 );                   // SecondaryAmmoID             write_byte( -1 );                   // SecondaryAmmoMaxAmount             write_byte( 2 );                    // SlotID (0...N)             write_byte( 1 );                    // NumberInSlot (1...N)             write_byte( CSW_KNIFE );            // WeaponID             write_byte( 0 );                    // Flags         }         message_end();     } }

Result :

http://dl.dropbox.com/u/27827560/Scr...6_07.32.21.png

Hooking when player got a weapon and redirecting a slot top
More or less the same code. You would need to modify the slot in the message.
Also, hooking Ham_Item_ItemSlot because the values stored in CKnife::GetItemInfo() function
are not changed and when ItemSlot() will be called, it will return the original value. Therefore we
need to change the return value.
Code:
RegisterHam( Ham_Item_ItemSlot, "weapon_knife", "OnItemSlotKnife" );

Let's say we want to redirect slot to the 5th.
Be careful, slot value in the message starts from 0, while in ItemSlot from 1.
Code:
new MsgIndexWeaponList; public plugin_init() {     RegisterHam( Ham_Item_AddToPlayer, "weapon_knife", "OnAddToPlayerKnife", .Post = true );     RegisterHam( Ham_Item_ItemSlot, "weapon_knife", "OnItemSlotKnife" );         MsgIndexWeaponList = get_user_msgid( "WeaponList" ); } public OnAddToPlayerKnife( const item, const player ) {     if( pev_valid( item ) && is_user_alive( player ) ) // just for safety.     {         message_begin( MSG_ONE, MsgIndexWeaponList, .player = player );         {             write_string( "weapon_ArkFlare" );    // WeaponName             write_byte( -1 );                   // PrimaryAmmoID             write_byte( -1 );                   // PrimaryAmmoMaxAmount             write_byte( -1 );                   // SecondaryAmmoID             write_byte( -1 );                   // SecondaryAmmoMaxAmount             write_byte( 4 );                    // SlotID (0...N)    <== Changed here (was 2)             write_byte( 1 );                    // NumberInSlot (1...N)             write_byte( CSW_KNIFE );            // WeaponID             write_byte( 0 );                    // Flags         }         message_end();     } } public OnItemSlotKnife( const item ) {     SetHamReturnInteger( 5 );     return HAM_SUPERCEDE; }

Result :

http://dl.dropbox.com/u/27827560/Scr...6_15.32.58.png

Hooking weapon selection top
Now, you have changed the weapon name, you would need to hook when you select it.
The reason is when you will tell the client you select this weapon, "weapon_ArkFlare" will be used.
Which means you won't be able to select it since such weapon name is not known.

Solution is simple : hooking the weapon name and redirecting to the original name.
Code:
public plugin_init() {     register_clcmd( "weapon_ArkFlare", "ClientCommand_SelectFlare" ); } public ClientCommand_SelectFlare( const client ) {     engclient_cmd( client, "weapon_knife" ); }
Full code of the example top
As it is, it would be really a basic example.
Purpose was to show you only the way without any elaborated code.
PHP Code:

#include <amxmodx> 
#include <hamsandwich> 
#include <fakemeta> 

new MsgIndexWeaponList

public 
plugin_precache() 

    
precache_generic"sprites/weapon_ArkFlare.txt" ); 
    
precache_generic"sprites/640hud19.spr" ); 
    
precache_generic"sprites/640hud20.spr" ); 


public 
plugin_init() 

    
RegisterHamHam_Item_AddToPlayer"weapon_knife""OnAddToPlayerKnife", .Post true ); 
    
RegisterHamHam_Item_ItemSlot"weapon_knife""OnItemSlotKnife" ); 

    
register_clcmd"weapon_ArkFlare""ClientCommand_SelectFlare" ); 

    
MsgIndexWeaponList get_user_msgid"WeaponList" ); 


public 
ClientCommand_SelectFlare( const client 

    
engclient_cmdclient"weapon_knife" ); 


public 
OnAddToPlayerKnife( const item, const player 

    if( 
pev_validitem ) && is_user_aliveplayer ) ) // just for safety. 
    

        
message_beginMSG_ONEMsgIndexWeaponList, .player player ); 
        { 
            
write_string"weapon_ArkFlare" );  // WeaponName 
            
write_byte( -);                   // PrimaryAmmoID 
            
write_byte( -);                   // PrimaryAmmoMaxAmount 
            
write_byte( -);                   // SecondaryAmmoID 
            
write_byte( -);                   // SecondaryAmmoMaxAmount 
            
write_byte);                    // SlotID (0...N) 
            
write_byte);                    // NumberInSlot (1...N) 
            
write_byteCSW_KNIFE );            // WeaponID 
            
write_byte);                    // Flags 
        

        
message_end(); 
    } 


public 
OnItemSlotKnife( const item 

    
SetHamReturnInteger); 
    return 
HAM_SUPERCEDE


Advanced examples top
Coming... Probably.
API top
Coming... Probably.

xPaw 01-06-2012 15:18

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Amazing!

Devil259 01-06-2012 15:42

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
o_o Really good job!

bibu 01-06-2012 15:57

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Awesome!

PsYChOPaTiQuE 01-06-2012 16:00

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
OMFG !!!
Good job ;)

bibu 01-06-2012 16:09

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Could you attach your sprites and txt file for the test plugin?

drekes 01-06-2012 16:12

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Great work.

Arkshine 01-06-2012 16:16

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Quote:

Originally Posted by bibu (Post 1626871)
Could you attach your sprites and txt file for the test plugin?

I can attach the basic example. The sprites used are actually the default ones. So no need to download. (It was just more easy/fast for me).

[EDIT: done]

bboygrun 01-06-2012 17:33

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Really nice job man ! You are the best !

:crab:

xakintosh 01-07-2012 03:44

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Wicked thanks.

kiki33hun 01-07-2012 05:03

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Good job

pacheco 01-07-2012 10:24

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Awesome! Nice Arkshine! Thanks

liinuus 01-07-2012 12:42

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Great Job, will be very usefull :D

nikhilgupta345 01-07-2012 13:35

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Nice. Didn't think this was possible.

m0skVi4a 01-07-2012 15:18

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Quote:

Originally Posted by nikhilgupta345 (Post 1627474)
Nice. Didn't think this was possible.

I also :shock:
Thanks

hleV 01-07-2012 17:25

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Nice, but are you completely sure that you can only replace the already made weapons and not create new ones? In ESF (which is a mod and possibly uses some different weapon system) you can actually create new weapons by registering a command (like "weapon_new") to hook when the weapon is selected (or force the player to select it), writing certain messages and setting weapon's new ID (if the last ID for ESF is like 34, then for the new weapon it would be 35) in certain messages and of course setting up the weapon_new.txt file.

Destro- 01-07-2012 17:55

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Today: -1
within 2 month:+10
;(

pd:the truth +10 :|.

mabaclu 01-07-2012 17:58

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Can this be used to place a sprite in the player's screen? That way we could show an image without editing hud.txt

NiHiLaNTh 01-07-2012 18:15

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Arkshine, I think that knife can be placed in first slot. I passed SlotID as 0 and NumberInSlot 1 and it worked, while setting both to 0 didnt drawed sprite in HUD.

Edit. Is it possible to change crosshair ?
I mean I passed it in .txt file and precached the sprite.But the original CS crosshair appeared. I was trying to hide crosshair with HideWeapon event but I failed.

Edit2. Maybe you explain what each parameter does? Especially, someone maybe confused with flags parameter(since you can add some flags to the weapon).

bboygrun 01-07-2012 20:03

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Quote:

Originally Posted by NiHiLaNTh (Post 1627635)
Edit. Is it possible to change crosshair ?
I mean I passed it in .txt file and precached the sprite.But the original CS crosshair appeared. I was trying to hide crosshair with HideWeapon event but I failed.

I saw that the crosshair can be changed when you zoom with a weapon ( Awp, scout ... ) but it doesn't work like you if i don't zoom or if i have a weapon without a zoom.

K.K.Lv 01-07-2012 21:30

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
awesome man, that should be useful for me !

lucas_7_94 01-08-2012 03:40

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
i will say this , its fucking awesome , great work arkshine.

Arkshine 01-08-2012 04:25

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Quote:

Originally Posted by hleV (Post 1627613)
Nice, but are you completely sure that you can only replace the already made weapons and not create new ones? In ESF (which is a mod and possibly uses some different weapon system) you can actually create new weapons by registering a command (like "weapon_new") to hook when the weapon is selected (or force the player to select it), writing certain messages and setting weapon's new ID (if the last ID for ESF is like 34, then for the new weapon it would be 35) in certain messages and of course setting up the weapon_new.txt file.

I don't know. The weapon system is the same as HL, thus hardcoded as hell. I don't think you can, but I've posted so people could test and finding better method. Will try something to make sure.

Quote:

Originally Posted by mabaclu (Post 1627630)
Can this be used to place a sprite in the player's screen? That way we could show an image without editing hud.txt

No, sadly. I've tried and it doesn't work. I've also check the client dll, and it checks the keywords listed in the tuto only. Others will be ignored.

Quote:

Originally Posted by NiHiLaNTh (Post 1627635)
Arkshine, I think that knife can be placed in first slot. I passed SlotID as 0 and NumberInSlot 1 and it worked, while setting both to 0 didnt drawed sprite in HUD.

Edit. Is it possible to change crosshair ?
I mean I passed it in .txt file and precached the sprite.But the original CS crosshair appeared. I was trying to hide crosshair with HideWeapon event but I failed.

Edit2. Maybe you explain what each parameter does? Especially, someone maybe confused with flags parameter(since you can add some flags to the weapon).

Playing with the position could make the trick, but I've noticed also it doesn't work well if you modify the position. The whole thing sounds tricky for some weapon. Keep up testing.

About crosshair, it seems the keyword "crosshair" is a second crosshair you can added and not replacing the original.

Will explain the parameters another time. Thanks.

NiHiLaNTh 01-08-2012 06:11

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
OK, will do some testing in there.

P.S. You can also pass ammo2 keyword for the secondary ammo type.Using it together with

Code:

byte  SecondaryAmmoID    byte  SecondaryAmmoMaxAmount
you draw secondary ammo type above primary.But there is one bug, secondary HUD ammo over-draws the money HUD.The only way how to fix that is to hide money HUD. :(

wangningyu 01-08-2012 08:19

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
thanks for share !

pRoxxxDD 01-09-2012 14:59

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
It was actualy one year ago. :)

pRoxxxDD 01-09-2012 16:24

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Quote:

Originally Posted by bboygrun (Post 1627668)
I saw that the crosshair can be changed when you zoom with a weapon ( Awp, scout ... ) but it doesn't work like you if i don't zoom or if i have a weapon without a zoom.

You answer on your question, just set FoV 89 (Standart no Zoom is 90) to player, and you'll see the new crossfire.:)

To Arkshine, C.S. sends Weapon List as MSG_INIT (This mean message sends one time on connect), if you want change sprite for ever just send your message one time on connect, otherwise you must send
it every time you want change it. =)

Arkshine 01-09-2012 17:11

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
I'm well aware this message is sent one time but that's would lose the purpose to be dynamic & per player, which would be what want coder for most of them. But it's true I could add a section for the situation all players all the time.

Kiske 01-12-2012 13:08

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
I need to show icon if player has hud_fastswitch 1, is that possible?

dias 01-13-2012 18:49

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Does it work on client-side ?

jc980 01-14-2012 02:28

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Can you make it possible to replace the "deathmsg" event on the upper-right side of the screen? where it shows whom you killed and who are killed, by changing its "weapon spr"?

ex:

jc980 WEAPON_SPRITE bot01

Arkshine 01-14-2012 04:16

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Quote:

Originally Posted by dias (Post 1631128)
Does it work on client-side ?

Not sure to understand your question. Hud is client-side.

Quote:

Originally Posted by jc980 (Post 1631361)
Can you make it possible to replace the "deathmsg" event on the upper-right side of the screen? where it shows whom you killed and who are killed, by changing its "weapon spr"?

ex:

jc980 WEAPON_SPRITE bot01

If it was possible, I would have wrote it in my tutorial. Such sprites has to be defined in hud.txt. But there is no easy way like for the weapon hud. You can consider as impossible.

dFF 01-27-2012 08:34

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Hi. Can anybody to explain what's line to change to use for example 'Law Grenade' or 'Gas mask' ?

Arkshine 01-27-2012 08:55

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
If you have read the tutorial, you should have seen the height of each weapon in this sprite is 45. So, all you have to do is to change shift x.

0 - Flare
45 - CS Grenade
90 - Melee
135 - Law Grenade
190 - Gaz mask

dFF 01-30-2012 02:36

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Thanks for reply.

Edit :: Problem solved! The crash is caused by another part of my code.
For now everything is ok with You're tutorial. After some tests I will see what will be happen.

DeviLzRO 02-03-2012 15:54

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Good job man, very helpful !

t3hNox 02-06-2012 17:28

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Very useful tutorial. Looking forward to seeing API :)

Xalus 02-09-2012 11:20

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Awesome!
And without Orphue, nice :D

dias 03-06-2012 07:12

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
what about multi custom weapon hud ?
Example: In Slot 0 (Primary Weapon Slot) I have Mp5 and P90. So how to make custom hud for that two. I try, and fail

Carlen20 03-06-2012 08:47

Re: [TUT] Custom Weapon Hud Sprites + Slot Redirection
 
Gj!


All times are GMT -4. The time now is 14:29.

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