PDA

View Full Version : The Netcode Guide


Somedude
August 5th, 2005, 09:00 PM
Saved from the FA forums.

I've put this off too long… Over 4000 words and its done (or rather ive got bored of adding to it, youll have to do without the troubleshooting section). It would be nice if you could move this to the support forum once it becomes inactive. (well looky where it is!)

This is my long-awaited netcode guide for Half-Life in general and Firearms in particular. It is not specifically designed for a single connection type, nor will I give you any specific values, since the best value depends on almost every aspect of your system and connection. I will, however, often refer to modem or broadband since these are so wildly different despite their many types. ISDN users will just have to consider themselves as being between the two :P I also a section of tweaks for server admins. And no, I wont tell you how to produce fake lag (its easy to do though).

And anyone else who has a cached LAN, you're buggered. Just accept it, theres not much you can do except act like you have ISDN. Or bypass the cache and invalidate your terms of use and run the risk of having your connection removed.

Remember that Half-Life is a single player game, and was designed as such. Anything that seems a stupid way for a multiplayer game to work probably is, but is there because it is a good way for a single player game to work. So this time, please no ‘they would never do it that way’ comments.


phides really got the idea here, and thats a philosophy youll do well to follow.

Many of you will wish to skip the next few paragraphs, which explain how the game works, and go straight to the cvars later on. However, reading the explanation will make it easier to understand what you are doing, and will help to find the best values. And you will have to follow the cvars in order, as most of the tweaks I give wont help unless the previous ones have been done.

In a single player game, there is no point in the game working out what is happening between frames, since the gamer will only be affected by what they can see and hear. Half-Life therefore proceed in ‘game frames’. In single player these are simply the same as the graphical frames (as in fps - frames per second). Whilst the graphics card renders the next frame, the processor calculates what is happening in the game (damage levels, movement, AI etc.), and the next frame does not start until both tasks have been completed.

In a multiplayer game, this works slightly differently depending on whether a listen or dedicated server is being used (dedicated servers are indicated by a small server icon next to them in the server browser, others are listen servers). For a listen server, the network frames (the frames in which the network game proceeds) are the same as the hosts game frames, and therefore the same as their graphical frames. For a dedicated server (and all good servers are dedicated) the rendering task is stupidly easy, as a dedicated server merely displays the console, which can be rendered extremely quickly as there are no 3d calculations to be processed. Therefore the servers frame rate will depend only on how fast it can process the game information (calculating movement etc.). Dedicated servers typically run at 80 fps or higher, and during lulls in the fighting often hit the HL framerate limit of 100fps. They would run at 100fps all the time except for having to handle all the requests for data and client updates. Try setting up your own server, once a few players join then your framerate will drop markedly.

In the current version of Half-Life, Valve claim that network and graphical framerates are completely separate, allowing modem users to run at 100fps without degrading their network performance. In a way this is true, so long as graphical framerates are far (at least 3 times) higher than network framerates. However, a new network frame still cannot be started until the concurrent graphical frame is finished. This means a graphical framerate which is close to the network framerate can cause irregular network frames.

Because of the way in which both the internet and Half-Life work, network problems such as high latency and packet loss are minimised by having a regular stream of updates. This is because of synchronising with the servers framerate, setting up virtual circuits on the PoPs, and modem time-switching algorithms, amongst others. Thus irregular network frames lead to the things we all hate, the several phenomenon which are categorised as ‘lag’.

I feel I had better describe what these are before I tell you how to minimise them.

Ping - [Packet Information Groper (they worked out what the accronym would be before deciding the terms). This is the amount of time (in milliseconds, or thousandths or a second) between a packet being sent by your computer to the server and the reply (the ‘pong’) being received. This is the one which is most dependant on your connection type, most modems will take around 150ms to even get to their ISP, so pings rarely get below 200. An ADSL user will probably only have 10ms or so to their ISP, so they start hitting the fundamental speed limits of their part of the internet, and these are the players who are most affected by server location (if you can call the difference between 30 and 80ms an effect).

Packet Loss - This is probably the most important and most tweakable effect, but one which is often ignored in favour of ping. A lost packet is one which at some stage of its journey is discarded, either because it is too old (the most usual one) or because bandwidth limitations prevent it being forwarded. Those annoying Connection Problems? Those are when your packet loss hits 100% (ie nothing is getting through) for a few seconds at a time, or even permanently (which is why you drop after a while if your not tweaked correctly when these awful problems occur).

Choke - This is the average time (in milliseconds) between a packet being generated on your computer and it being sent to the server. It is one of the major goals during tweaking to get this to 0, or at least as close as possible.

‘Broadband Slowdown’ - High pingers DO NOT cause lag. How that stupid myth got around I don't know, but it’s complete nonsense. In fact, as you probably now guessed, broadband connections slow servers. Broadbanders ask for many updates a second, each of larger packet size, whilst sending large numbers of updates themselves, again each of large packet size. All that bandwidth gets used up (the server bandwidth used by a single broadband connection could support five to ten modemers) and all the packets being sent place a drain on the servers processor and RAM usage. The most unfair part of this is that its actually the modemers who feel the attendant drop in performance the most, in a way paying for the sins of the broadbanders. Maybe that's why the myth grew, whenever there's a slow server, any modemers will have a high ping, so broadbanders just assume that is the cause of the slowdown, not its affect. Not only is it vastly unfair to kick modemers when a server begins to slow down, it also will probably make no noticeable difference.

It is also worth bearing in mind that all of these are affected by the netcode of the mod being played, for example CS and to a lesser degree DoD are built to get the players pings as low as possible, without even thinking about packet loss or choke. This is all well and good for those with broadband and good graphics cards, but the teams obviously don't give two shits about anyone with a sub-standard graphics card or a modem. Thankfully, Firearms is built with a good mix of these, with such things as client side accuracy immensely helping reduce network traffic and therefore helping those with restricted bandwidth (ie, modemers).

How do you find out what these values are for you? Well, Half-Life has a wonderful little tool called the netgraph. You turn it on by typing net_graph 1 in the console. Values 1-5 are valid, each showing slightly different things, whilst 0 turns it off. I use 3 personally, but find the one which you use the most. The netgraph can cause a strain on graphical fps, but I find it minimal. Only those with significantly higher graphical fps than network fps should see a big drop. If so, you can decrease its size or turn it off altogether once you have tweaked your netcode. The following is an explanation of net_graph 1:

<see attachment for the pic>
1. FPS Counter - Your current frame rate.
2. Network Latency - This is your current network latency. (‘ping’)
3. Your downstream bandwidth use.
4. Your upstream bandwidth use.
5. This is an animated graph displaying your ever-changing ping. The higher your ping, the higher the green/yellow/orange/red line gets. This also displays your lost packets in red, and mismatched entities in blue.
6. Your current server update (incoming) rate.
7. Your current client update (outgoing) rate.

A CVar is a ‘client variable’ (or a server variable), a value governing some operation of the engine which can be changed by the client (or server administrator). For example con_color can be used to enter an RGB value for the colour of the text in the console. I am going to examine each relevant cvar, explain its use, and give guidelines on its best settings. I will inevitably miss some of them (there are hundreds, although most don't really affect the network performance). In each case, x is a number, and they should be entered in the console (although they can also be entered in the command line).

Somedude
August 5th, 2005, 09:02 PM
Continued from above.

Netgraph CVars

net_graph x - as noted above, enabled the netgraph. 0 is off, whilst 1-5 display different combinations of information. 0 is default. I like 3.

net_graphpos x - determines the position of the netgraph on the screen. 1 is bottom right, 2 bottom centre and 3 bottom left. 1 is default. Im happy with 1, but some set it to 2 and then move the compass to the top of the screen so they can see their ammo count better.

net_graphwidth x - the width of the graph in pixels. One pixel is used in the graph part for each packet sent. 192 is default. Remember to keep it large enough for the text to be readable.

Net_graphheight x - the height of the graph in pixels. 64 is default. Again, keep it large enough to be readable.

Ever wondered why the ping displayed on your netgraph is usually less than the latency displayed on the scoreboard? Well, the netgraphs ping is purely the network ping time. The latency in the scoreboard is more representative of the time obstacles against the player, as it includes the time each packet takes to be rendered and displayed on the clients computer. They are both useful, but in different ways.

FPS Related CVars

fps_max x - Sets the maximum graphical fps. Any integer between 1 and 100 is valid, whilst 72 is the default. Half-Life will attempt to divide each second evenly into this many ‘slices’. If the rendering process ends before the slice, then it will begin rendering the next, but it will never go more than one frame ahead, to conserve memory usage and stick to the framerate. If the frame isn't rendered before the slice ends, then the next slice is allocated to that same frame. No-one ever believes me, but this is the single most important variable for adjusting the netcode. Since everything in Half-Life is linked to frames, and all frames depend on the graphical frames, this is extremely important. The advice I see all the time is to set this to 100 (the maximum value), so that as many frames as possible are rendered meaning the minimum time between a packet being received and it being rendered. This is completely and utterly WRONG. As noted earlier, netcode revolves around predictability, and regularity is the most important thing. It is better to have a regular fps of 20 than one which jumps between 20 and 30. In fact, often setting a lower fps_max can raise the average framerate, because the renderings are fitting nicely into the time slices, which eliminates the inevitable wasted time when a frame requires just over one slice to render. In fact a value which is close but not quite right is worse than one wildly off, since each slice is bigger, wasting half a slice is more harmful. You need to gradually decrease this value until you find a point at which your fps is almost constant at the limit. There will be areas of some maps or large firefights in which it may drop, but it needs to be constant in normal play. If it isn't, drop it. It may start to look bad as you get closer, but when you have the correct value it will look better than you would've thought, since the frames will be regular and therefore easy for the brain to interpolate to a moving image. This is why TV looks better than a game running at an average of 24fps, since TV is always regular. As detailed below, correctly adjusting this single setting can reduce ping, packet loss and choke all in one. Still don't believe me? Then stop reading this guide, the rest of it is pointless if you haven't done this step. If you end up with a framerate which is too low (ie under 20) then you should consider dropping detail levels to improve rendering speed, some tweaks for which are included at the end of this guide (Actually they arent, i never got around to writing them. be patient, i may do at some time). Its also rather pointless to have a value of fps_max that is higher than your monitors refresh rate (60Hz or 75Hz are normal) unless you are running a server on it, because the extra frames simply wont be displayed.

fps_modem x - don't be mislead by the name, this is equally valid for all connection types. Sets the maximum number of graphical frames per second for an internet game. This or fps_max is used, whichever is lower. Any number between 1 and 100, 100 is default. This is only of any use for those who play both on LANs and the internet, since they will run at fps_max during their LAN game, and at this in their internet games. If you didn't think fps could affect network performance, then why would there be different cvars for different network types? I bet some people still don't believe me… It is probably worth reducing detail levels and getting a higher fps during LAN games, to take advantage of the extra bandwidth. If your running over 50fps anyway its not worth it though.

fps_lan x - sets the maximum number of graphical frames per second for a LAN game. This or fps_max is used, whichever is lower. Any number between 1 and 100, 100 is the default. This is irrelevant for almost any HL mod, since it only matters if the mod is being played in single player (where fps_max will be used), LAN (fps_lan) and internet (fps_modem) play. I know of no such mod. The only other situation I can think of this being used is if the same machine is sometimes used for running a dedicated server, and at other times playing on both a LAN and the internet. In this case, the fps_lan and fps_modem values would set the values when playing, whilst fps_max would be set at 100 to allow the dedicated server to run as quickly as possible. Again, rather unlikely.

Idiotic Lag-Compensation CVars
I say idiotic, because I fail to understand why anyone would want to change them, but when lag comp first came out everyone on broadband wanted to know them so some people must somewhere.

cl_lc x - 1 or 0, 1 being default. 1 tells Half-Life to use the lag compensation (assuming the mod and server support it), whilst 0 turns it of. Lag compensation is the system whereby you hit what you were aiming at when you fired, instead of having to fire ahead of the target. Why you would want it off is beyond me, unless you have a ping of less than 10. Maybe on a fast LAN, where it would reduced required bandwidth, but since LANs have tons of bandwidth to spare anyway there's really no point. Leave it on.

cl_lw x - 1 or 0, 1 being default. 1 tells Half-Life to use client side accuracy and recoil. If turned off, when you click fire you wont see the gun fire for a pause equal to your ping time, because it will wait for the server to tell it how much recoil there was and where the bullet went. This may make a bit of sense in mods which have server side accuracy and recoil (CS being the most obvious and heinous one), but since Firearms uses client side prediction for these anyway, and the server lines up with what the clients tell it rather than the other way around, there's no point and it actually makes the game harder, since you can't adjust for the recoil. It makes no difference to bandwidth usage, since 1 sends it to the server, whilst 0 sends the same data the other way. Leave it on.

cl_lb x - 1 or 0, 1 being default (0 used to be, and is listed in some guides). 1 tells Half-Life to place blood sprites if the client thinks you’ve hit your target, whilst 0 will wait for confirmation from the server. This can look weird, and only serves to indicate if you really did score a hit with highly inaccurate guns in server based accuracy and recoil mods, like CS. In Firearms its pointless. Leave it on.

Direct Netcode CVars
These cvars directly affect the network packets, so can make a large difference to performance. Setting them wrongly can make the game unplayable, or even lead to constant connection problems once you join a server. Make sure you make a note of your previous values before changing them (to find this, type the cvar without a number after it. Half-Life will list the current value).

cl_updaterate x - Any integer value between 1 and 100. 20 is default. This is the number of updates the client wants to receive from the server each second. Barring something going wrong with the server (eg its fps dropping right down due to a large amount of fighting, or too many broadbanders being connected) the server will indeed send this many. The client doesn't actually send this many requests, just when you join the server (or change the value) it sends the server a packet saying ‘please send me this many updates a second’. The server will attempt to send these. The best value is equal to fps_max, but don't go over about 30 (even 25 is pushing it). If your fps_max is higher than this, then choose a value which is a simple multiple of it. Eg. if fps_max is 42 then use 21. The game only really begins to look jerky around 13, so don't be afraid to try values down to about 15. This sort of value will only be noticeable when things move very quickly ingame, eg grenades from the m79, gp25 or m203. Since it’s only telling you what’s already happened on the server, there's no need to have a really high value, higher values only place unnecessary strains on the server, leading to broadband slowdown. Oh and set this to 50 for LAN games.

cl_cmdrate x - Any integer between 1 and 100. 30 is default. Basically the opposite of cl_updaterate, this sets the number of outgoing packets sent each second. There is absolutely NO reason to set this any higher than fps_max, since it will just be sending the same data twice during some frames. Since outgoing bandwidth varies less than incoming bandwidth, you can generally get away with values up to about 40 (modems up to 30). Set this to the same as fps_max, or a simple divisor (eg. half of it) if your fps_max is higher than these limits.

cl_rate x or just rate x – Any integer from 0 to 99999. Default is set by which connection u chose on install. This is that amount of data in bytes allowed to be sent every second (total of up and down stream). Basically there is a trade-off here between bandwidth and smoothness. The higher this is, the more data gets send each second, and the better the client can keep track of what is happening on the server. However, at the same time it takes up more bandwidth, both on the client and the server. As a client you will want to set this as high as you can on your connection, for optimum smoothness. Setting this too high will result in package loss, as there physically will not be enough bandwidth to send some of the packets. Try a bit higher than the values below and then drop it until your packet loss disappears (or reaches a minimum, it may be the server). When changing this setting, it is important to realise that it makes a difference to connection reliability, so you should try each setting for 5-10 minutes before deciding whether to drop it again.

These are approximate values for the rate, although since this is the most variable setting of all your connection could be wildly different.

56k modem or 64k ISDN – 2000-3000
128k ISDN – 3500-5000
cable modem – 5000-8000
xDSL or T1 – 7000-10000
LAN – 10000

cl_resend x – Any integer between 0 and 16. 6 is default. cl_backup also does the same thing. This sets the maximum number of times to resend any lost packets. If you have followed the above tweaks u should really get much pl, but setting this to 1 will reduce pl without affecting performance and flush too much. This is an IMPORTANT setting, putting it higher than necessary can really mess things up.

cl_timeout x – Any integer between 0 and 1000. 30 is default. Sets the number of seconds after which the connection is given up for dead if no packets have been received. Often it is recoverable, especially if you can see the in and out rates fluctuating. Setting this to 90 works for me. That way I can decide when to give up on the connection.

Somedude
August 5th, 2005, 09:04 PM
Still more...

Download CVars

cl_allowupload x - 0 or 1, 1 default. When set to 1 this will upload any custom spray-paint you may be using. Since this causes hardly any lag, only for the first 20-30secs, and since it lets other players see your spray-paint, you should leave it on.

cl_allowdownload x – 0 or 1, 1 is default. When set to 1, HL will download any sounds or maps form the server needed for the next round. For sounds this is best left on, since they probably aren't available in a pack, and if they are by the time you’ve found them you would have downloaded them already. However, you should NEVER download a map this way! The map wont be zipped, and the HL connection is slower than an ftp, so you are probably trebling the time it would take you to download the map anyway. Take a look in the mapping forum here, there’s a ‘map releases’ thread that has links to all the custom maps I’ve ever needed. (seems to have been unsticked, try a search for it, or use www.disciplex.com or www.doomsdaywarriors)

cl_download_ingame x – 0 or 1, 1 is default. When set to 1, this will allow HL to download other players spray-paints during the game. For the same reason as cl_allowupload, you should leave this on. However, the images downloaded are stored in a file called custom.hpk in your firearms folder, and I recommend deleting this periodically (every two weeks is about right) since it can get quite big and take up a large chunk of your RAM and loading time. HL will create a new one as necessary anyway.

For Server Admins
These are tweaks a server admin can use to optimize the networks performance whilst playing on their server. I’m not absolutely sure about any of these, since I haven’t had a server to test them on. However, this part of the guide HAS been updated with feedback from server admins. Place these in autoexec.cfg or config.cfg for them to work, most cant be changed while the server is running.

sv_maxrate x – Any integer from 0 to 99999. 99999 is default. Sets the maximum rate for any one client. This multiplied by the max players should be about two thirds of your total bandwidth (as its not 100% efficient and you want to leave some over for joining players, spray downloads etc). However I often see it higher than the max bandwidth, which is stupid because everyone starts having their rate automatically reduced. This not only messes up carefully set up netcodes, but it also means the modemers suffer at the expensive of broadbanders and causes broadband slowdown. I would actually like to see a server which sets this to 3000 so everyone gets the same performance, no matter their connection type, but I don’t think it will happen. Dont set this TOO low (below about 1500) as players will start to get packet loss or be prevented from joining the server due to timeouts.

sv_minrate x - Same as sv_maxrate, but sets the minimum rate instead of the maximum one. A bit pointless, people should be allowed to set their rate as low as they like. Set to 0.

sv_dlmax x – Any integer from 0 to 99999. 99999 is default. Sets the maximum volume of downloads to send to any client in kB. MAKE SURE YOU SET THIS CORRECTLY! The absolute best value is 500. This is large enough for the client to get any sounds, but smaller than almost all maps. This mean the clients will not be able to DL maps off your server, which is horribly inefficient and really lags the server badly, especially if they are on broadband.

FPS Tweaking
for adjusting fps BEFORE setting caps and suchlike, try THIS (http://zona-counter.com/downfil/hl-ves_1.0.zip) program (thanks to gregor to bringing it to my attention). Dont even think about touching the netcode sections of it though.

Automated Netcode Tweaking
dont want to do all this by hand? FAPCM (http://www.fireabend-clan.de/files/tools/FAPCM26180FULL.exe) has good netcode support


Well thats all i can be bothered to do, its possible ill edit in a troubleshooting section and some more admin stuff. Sorry it took so long people

fuzion
August 26th, 2005, 06:27 AM
um the FAPCM link is down and so is the fps tweaking link to the thread.

thanks for all this btw

Modest Genius
August 26th, 2005, 11:44 PM
yay! well done somedude!

Heruhyarmen
October 14th, 2005, 10:30 PM
Wow! Very cool!