PDA

View Full Version : Code review: Supplier (50%) and WeaponFiding optimization.



Higor
05-11-2012, 09:34 PM
Playes and Bots have an inventory chain, players owns one piece of inventory, and this piece of inventory owns the next inventory and so we have a chained list.

Weaponfinding routines loop through this chain, it's naturally faster if weapons are arranged in front of the list isn't it?

This code modifies SiegeGI.AddDefaultInventory
It performs a run on the player's inventory chain and puts weapons first.
This makes weapon switching faster (1 millisecond is sometimes appreciated :chuncky:)
And is half of the planned optimization on the Suppliers, these perform 10 weaponfinding checks every 0.2 seconds per player, so yeah...

There's no edition actually, we're adding code at the end of the function, it looks like this:

function AddDefaultInventory(Pawn playerPawn)
{
local int i;
local Weapon newWeapon;
local inventory inv, previnv;

if ( PlayerPawn.IsA('Spectator') || (bRequireReady && (CountDown > 0)) )
return;

GivePlayerWeapon(playerPawn, WeaponClasses[12]);

for ( i = 0; i < 12; i++ )
if ( WeaponClasses[i] != None )
GivePlayerWeapon(playerPawn, WeaponClasses[i]);

//CACUS HACK, rearrange inventory chain so supplied weapons can be found faster
if ( playerPawn.Inventory == none )
return; //BUGHERE, no inventory given?

inv = playerPawn.Inventory;

While ( inv.Inventory != none )
{
previnv = inv;
inv = inv.Inventory;

//This is a valid suppliable weapon (90% chance), let's put it in front of the list
if ( (Weapon(Inv) != none) && (Weapon(Inv).AmmoType != none) )
{
previnv.Inventory = inv.Inventory; //Remove from middle of chain
inv.Inventory = playerPawn.Inventory; //Appear on front
playerPawn.Inventory = inv;
}
}
}

If you wonder what the hell WEAPON_SEARCH is, it's my iterator, it provides more control by jumping between code sections inside a function.

You can even use jumps to enter or leave another For, While, etc iterator.

I haven't tested it since I don't have Siege installed, but can check that the inventory chain isn't broken or anything when testing it if you can see the weapons and ammo amounts on your HUD...

.seVered.][
05-11-2012, 10:10 PM
Since your builds are also in your players inventory, can't we put an ICON up for Nuke's that you have built or are finished building.

Higor
05-11-2012, 10:26 PM
Builds are in inventory when you pick them up.

One way of adding onscreen icons is by creating an extra, hidden inventory item that checks serverside for the player's nukes, saves the remaining time and health %.
The client receives both values on the 'n' nukes (can be up to 4) and then draws icons onscreen like relics do.

Another way of doing that is adding a general checker for both teams and let the player's hud find it clientside, but that would provide a good hacker a way to read the opposing team's nuke status, sure that can be prevented but doesn't fit in a paragraph of explanations. I'll leave that for another time.

Feralidragon
05-12-2012, 12:12 PM
Goto? Are you showing a "goto" for algorithmic understanding purposes or are you actually saying a goto should be used?
I ask this because if it's the latter, you certainly don't want to use goto's, you use either "for" or "while" cicles ("while" would be the one to use here), never goto's outside state code.

Don't get me wrong, but the process of optimization has to go hand-in-hand with good algorithmic structure to keep the code readable, understandable, stable and "debug-able".

Higor
05-12-2012, 11:41 PM
Changed loop type.
Removed unneeded comments.