airmash-refugees / game-ideas

A place to keep track of ideas for improvements to AIRMASH
4 stars 2 forks source link

"wonky missiles" #32

Closed ghost closed 4 years ago

ghost commented 4 years ago

This is vaguely a design discussion / idea basically for Wight server, but overlaps with frontend. It's more implementation discussion than anything else..

I hacked the client Mob.js update() basically with this:

        var dir = (this.id & 1) ? -1 : 1;
   ...
            for (t = 0; t < i; t++) {
                    if(this.type == MobType.CarrotMissile) {
                        var r = Math.random();
                        if(Math.random() > 0.5) {
                            r *= dir;
                        }
                        this.speed.x += (r * o) * 0.5;
                        this.speed.y += (r * o) * 0.5;
                    }
       }

Which produced the video on /r/airmash.

So for each frame it perturbs the path somehow. Rather than using Math.random(), could use a PRNG or even just a hash function to generate bits the server also knows.. maybe something like:

var hash = id ^ pos.x ^* pos.y ^ abs(speed.x) ^ abs(speed.y)

Then for each frame,

hash *= 1234;

Or similar. Then right-shift bits off the integer to get 'random' state.

Since server and client both have the same id/pos/speed, this means their wonkiness decisions are in sync.

Is there a simpler way to do this? I thought maybe server could send MOB_UPDATE on each change, but that is a ton of network traffic

ghost commented 4 years ago

~Another possibly simpler option.. just precompute a bunch of paths as a set of arrays, large enough that it's unlikely anyone would remember all the patterns, and use (pos.x^pos.y^id)%length as an index into it~

~This might make the loop much cleaner. Still need to figure a way to represent the path changes that can be applied fractionally.. because the calculation loop can run for partial frames~

Can use Xorshift for it, it's tiny:

function xorshift32(x)
{
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    return x;
}

Just need to cache this.rand = (this.id * this.pos.x * this.pos.y) & 0xffffffff in the mob instance constructor and this.rand = xorshift32(this.rand) every time a new word is needed