Named Game of the Year by over 50 publications, Valve's début title blends action and adventure with award-winning technology to create a frighteningly realistic world where players must think to survive. Also includes an exciting multiplayer mode that allows you to play against friends and enemies around the world.

Post tutorial Report RSS Half-life1 Coding: 4digits on HUD and 9999 ammo on clip

Second part of the tutorial, that includes the modification of the HUD to show 4 digits on ammo, and the modificacion on the client-server message for the weapon magazine.

Posted by on - Intermediate Client Side Coding

first read the first tutorial, as the half of this is the same as the other but over SelAmmo data, is very recomendable to implement it


Start using the "find" function and look for "SelAmmo" in the Entire Solution
in player.cpp we need to edit the info inside the function LinkUserMessages

curweapon1

we need to change that 3 to a -1

curweapon2

then the message information inside CBasePlayer::RemoveAllItems

curweapon3

we change that bye by a short

curweapon4

and at last the message information inside CBasePlayer::Killed

curweapon5

and again

curweapon6

next in the list you should have weapon.cpp
we need to go inside the function CBasePlayerWeapon::UpdateClientData and find

curweapon7

we change that again for a short

curweapon8

and at last we open ammo.cpp
and inside CHudAmmo::MsgFunc_CurWeapon
we find

curweapon9

we change the read_byte to read_short

curweapon91

we that we can have 999 of ammo in the clip of the gun

now lets add more digits to the HUD

first lets opend hud.h and at the top find that constant definition

hud1 1

then we add our new one for the new 4digits HUD

hud2

now open hud_redraw.cpp and go to the very bottom of the file and find
inside the CHud::GetNumWidth and we add this if statment

hud3

in the same function go down find this

hud4

and edit it to look like this

hud5

now open hud_redraw.cpp and find the function CHud :: DrawHudNumber
this function is the one that arrange the numbers in positions in the hud
we need to change this line, this line trys to take the third digit of the number and put the correct HUD number, but as we are going to
add a 4th digit for the numbers we need to change this line

hud6

now this will make the third digit to not have a problem when we add the 4th digit

hud7

now we add the fourth digit on top

hud8

inside the same function in the bottom we find this chunk of code for the spacing of the digits

hud9

we need to ADD another one for our 4th digit(WE DO NOT REMOVE ANYTHING HERE)

hud91

done with hud_redraw.cpp now we can use 4th digits on hud(for health,armor, ammo, and weapona ammo)

at this point I recommend you again to have implemented the first tutorial for increasing the AMMO type capacity

so we open ammo.cpp, find the function int CHudAmmo::Draw(float flTime)
inside of it we need to find all the x = gHUD.DrawHudNumber() and change one of the variable inside of this

hud92

we need to change the old DHN_3DIGITS to out new DHN_4DIGITS

hud93

we do the same with this 2 more inside the function (look for the valve comment "GL Seems to need this" if you are confuse)

hud94

we change those to this

hud95

and one more inside if ((pw->iAmmo2Type != 0) && (gWR.CountAmmo(pw->iAmmo2Type) > 0)) {

hud96

now with that change you can have up to 9999 of ammo in the weapon magazine and be displayed on HUD + all the HUD stuff that use numbers
(armor,health) can be modified to use 4digits

one more thing if you did the first tutorial and now this you should change from player.cpp a line from the first tutorial
inside CBasePlayer::SendAmmoUpdate
the line WRITE_SHORT ( max( min( m_rgAmmo[i], 999 ), 0 ) );
should be even extended to have 9999 as max number of ammo sent to the client

hud97

then you can go to weapon.h and modify the capacity of ammo and magazine

hud98

and if everything goes well you can have this

20160417083009 1

I dont think we need that much ammo to end half-life1

Post comment Comments
Gunship_Mark_II
Gunship_Mark_II - - 4,864 comments

Beautiful tutorial, very well written and nicely illustrated. I'm sure that a lot of people find it very useful, thank you for your work!

Reply Good karma Bad karma+2 votes
Foxtrop Author
Foxtrop - - 1,007 comments

well thanks!, I felt that the other tutorial was kind of incomplete without the hud

Reply Good karma+1 vote
FanProgrammer
FanProgrammer - - 426 comments

Very nice

Reply Good karma Bad karma+1 vote
zenav
zenav - - 8 comments

waht this program to open this file

Reply Good karma Bad karma+1 vote
Foxtrop Author
Foxtrop - - 1,007 comments

visual studio 2008

Reply Good karma+1 vote
ghostboyed
ghostboyed - - 4 comments

You can also use Notepad++

Reply Good karma Bad karma+1 vote
ghostboyed
ghostboyed - - 4 comments

When im compiling client dll it gives me error : Could not load library */valve/client.dll Help plz

Reply Good karma Bad karma+1 vote
Foxtrop Author
Foxtrop - - 1,007 comments

first of all you shouldnt have in your proyect the load of .dll because thats what you are trying to compile, so it could be that the directory of the sdk is wrong for some reason, be sure that your sdk is compatible with your compiling program, before following any tutorial you should try to compile a clean SDK without modifications

Reply Good karma+1 vote
ghostboyed
ghostboyed - - 4 comments

Error: User Msg AmmoX 4 bytes written, expected 2
and when im changing ammox event from 2 to -1 still no work (then ammo is 0 but u can reload)

Reply Good karma Bad karma+1 vote
Foxtrop Author
Foxtrop - - 1,007 comments

did you implement the first tutorial? Moddb.com
Look in the code for "ammox"
in player.cpp you need to change the reg_user_msg("ammox",2) to a (ammox,-1)
then in function SendAmmoUpdate, you need to change
WRITE_BYTE( i );
WRITE_BYTE( max( min( m_rgAmmo[i], 254 ), 0 ) );
to
WRITE_SHORT ( i );
WRITE_SHORT ( max( min( m_rgAmmo[i], 9999 ), 0 ) );
and then in the client side, on ammo.cpp fin this function
int CHudAmmo::MsgFunc_AmmoX(const char *pszName, int iSize, void *pbuf)
{
BEGIN_READ( pbuf, iSize );

int iIndex = READ_BYTE();
int iCount = READ_BYTE();

gWR.SetAmmo( iIndex, abs(iCount) );

return 1;
}
and change
int iIndex = READ_SHORT();
int iCount = READ_SHORT();

It should work I build it up on a clean SDK

Reply Good karma+1 vote
Guest
Guest - - 689,338 comments

hi sir do you know where can i change the weapon firing speed i know it's possible i found a mod that had the mp5 and some other weapons fire stupidly fast

Reply Good karma Bad karma0 votes
Foxtrop Author
Foxtrop - - 1,007 comments

weapons use diferent methods but the majority as the mp5 uses

m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.1;

where +0.1 is the time between shots, a smaller number a faster shoot a bigger number a slower

Reply Good karma+1 vote
Guest
Guest - - 689,338 comments

This comment is currently awaiting admin approval, join now to view.

ghostboyed
ghostboyed - - 4 comments

Is There Difference about setting MESSAGE Size To Exact one or -1 ?
Will It Affect Performance ?

REG_USER_MSG( szMessage , Size )

Reply Good karma Bad karma+1 vote
Foxtrop Author
Foxtrop - - 1,007 comments

as far I understand -1 means that the size is variable, it would be better to know how much data you want to send to know the right size, in the case of the message "curweapon" we changed the size from 3 to -1, but if we use size of 4 it should work too!
as the message sends 3 data package
write_byte=size 1
write_byte=size 1
write_short= size 2
I dont test it, but it should work
these message would definitly had some impact on online games as this are data between client and servers, how much will affect performance I dont know

Reply Good karma+1 vote
NapoleonII
NapoleonII - - 126 comments

Nice tutorial.

One more thing that needs to be fixed:

player.cpp, under void CBasePlayer::SendAmmoUpdate(void)

ASSERT( m_rgAmmo[i] < 255 ); ---> should be changed to 999 if you have followed the first tutorial, or should be changed to 9999 if you have followed the first and the second tutorial.

E.G.:
ASSERT( m_rgAmmo[i] < 999 ); // we have 3 digits, more than 254

or

ASSERT( m_rgAmmo[i] < 9999 ); // we have 4 digits, up to 9999

Other than that, everything works really good.

Reply Good karma Bad karma+1 vote
gmodfan643
gmodfan643 - - 8 comments

hello i followed you're first tutorial and now this and for some reason the hl.dll file size is 3,222 KB and when i put the dll file in the dlls folder then ingame well i dont even see my hud or use the HEV Suit anymore.

Reply Good karma Bad karma+1 vote
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: