How to Improve Algorithm for the "Loot" Process

maplestory2mesos Date: Jul/27/16 12:36:40 Views: 1155

I've heard about the rumours that items that drop from mobs will only stay for 30 seconds instead of the normal 180 seconds (or somewhere around that I'm not sure).

 

maplestory2_mesos

 


What's really funny about this is that people are saying this was the solution to the massive "loot lag".
Okay, okay. So assuming the normal duration was 180s, and assuming for that duration you constantly kill mobs at the same rate, the max amount of items on the ground will be reduced to 30/180=1/6. Does this help at all?
I honestly don't agree. Pet loot already lags while picking up 10 items from a total possible items on map of 20. It may depend on your hardware and network however.

Regardless of whether this so-called solution fixes the loot lag, everyone should agree that this is THE typical Nexon-approach of fixing problems: fix the problem on the surface, break every related AND unrelated components in game while significantly nerfing game experience. Think of what they did to Kanna and Kishin lately. Think of what they do when they nerf training maps because it was too popular, instead of buffing other unpopular/outdated maps so people can spread while not having their exp gain rate reduced, and then reduce the exp required to reach to Lv200 because it's too hard to level with all the maps nerfed. Really? This is the most stupid approach of every problem. Of course balancing every maps in the game is impossible and impractical, so one map will always be better than others. Of course Nexon will then nerf that map. Infinite loop of stupidity?

Back to the topic. Assuming from players' experiences that you lag when you pick up items while there are a lot of items on the ground in the whole map, I can only say that Nexon developers just can't be bothered to actually write some decent code, and just focus on adding more broken buggy contents.

Okay back to the topic again. Assuming from players' experiences that you lag when you pick up items while there are a lot of items on the ground in the whole map, I guess the looting algorithm is the most basic one that everyone can think of: "yea just loop through all the items in the map and then do a collision detection with the pet, and if it collides, while still inside the loop send a request packet to the server that the pet is trying to pick up ONE item".

If that is the way it is actually implemented, please do consider my suggestion.
Please, instead of storing all items in the map in an array, use a tile-based hashtable.
It works like this:
Assume a pet as a hitbox of m x n pixels. Let w = m/2 and h = n/2. Now each tile has the dimension of w x h. Note that this dimension is just a suggestion.
When items are dropped from monsters, they are placed on the map, right? Each map should have a hashtable (not a 2D array please MS client is already using over 1.2GB of ram), and the item should be added to a virtual "tile" according to its coordinates. For example, if it was dropped onto (67, 43) and the tile is 20x20, the item will be added to the array `items[3, 2]`. Just in case, of course all this is being done on the client.
Then when the pet is moving, check for the "tiles" it is colliding with. Since m*n = (2*w)*(2*h), the maximum number of tiles it can collide is (2+1)*(2+1)=9. Now, think. What do you think performs faster, looping trough 500 items or just doing 9 hashtable lookups? Heck 9 could even be decrease to 4 if you make w = m and h = n.
Now about sending the packets. Ignore any null entries in the hashtable (meaning out-of-bounds or no items placed), and you're left with 9 or less arrays of items. Send a pick-up request packet of all items in the 9 arrays (pick-up-able ones, of course) in one batch. No more one-by-one request because it-works-well-on-my-computer-because-it's-sitting-right-next-to-the-server. Please make sure to GC empty arrays and if not re-use it instead of assigning a new one becuase I'm sick of memory leak - as for the latter, I'm assuming unmanaged context of course.

Because:
1. The KMS public servers (not KMST) didn't get the 30-second expiration of all drops. They only had their event drops, which used to expire in 15 or 20 seconds, extended to last 30 seconds.
Since GMS rejected the fast-expiring event drops last summer (we got it by mistake and after two months it was restored to normal), we have good reason to hope our drop duration will not be affected by V update.

2. You are misdiagnosing the problem.

The problem of many items on the map has nothing to do with loot. It's just many items on the map that constantly have to be re-displayed.

"Loot lag" is due to the client checking every item picked up (including maplestory meso) against every active quest, to see if this item is required by that quest.
We know this is the problem because people who have a completely empty quest list (due to a glitch) have zero loot lag. And people who cleaned out their In Progress quest tab as much as possible (by completing or forfeiting quests; can't completely empty it due to the Maple Rewards quests, however) notice a great improvement in their lag.
Another clue is that people who have no pet out at all and are not looting themselves, but are partied with someone who is looting (including maplestory mesos, which are shared with the party), suffer loot lag as well.

So if you want to suggest a new data structure to improve loot lag, focus on that quests<->items relationship.
Obviously scanning the entire quest list for every item is the most inefficient way to do it.
Some kind of hash table or indexing by item ID (which then points to a list of quests that need this item ID).