No Mans Sky–Spreadsheet of Desire

Still playing it.   I’m actually finding the number of story missions .. relentless.  Like, stop bugging me, lemme go free play for a bit dangit.   OTOH, its definitely .. hey I have an hour to play .. lets go knock something out .. 5 things later .. oh, I never got around to the one I was going to do.

I do not have a head for remembering detail (anymore).   So, I came up with this – I tried it in Google Sheets first, but they don’t have good visualization of formulas, so Excel wins this round:

image

The plan is that the Calcs stuff (Column I onwards) is a bunch of rows where I can say “if I want 1 metal plate, you gotta have 50 ferrite dust”.   Unfortunately, in Excel, i can’t sort this .. not the way it is.  This would be better done as a quick app.   Anyhoo, I can type in my inventory on things, and then it gives me how much I need of what, and using the visual tree above I can figure out the build order.

If I did it as an app, I could probably also factor in stuff like “how much carbon to run the refiner” and “how much carbon to run the mining laser”, although the latter would need some “how good is your equipment” estimates.   Could probably also try to calculate “how much time would this take” and optimization of which fuels to use “by cost” vs “by time to acquire”.

If I did it as an app, I would load it with a text file which has a one-line language for describing things and what they are dependent on, for example “Warp Cell|Craft|Antimatter Housing:1,Antimatter:1”.  That way the 2nd thing, if it were “Refine”, could then bring in additional costs associated with that.

No Mans Sky

Started playing with the 1.5 update.   Fun so far..   went back because I was feeling lost, and mapped out the starting part of the game. Here it is, probably 85% consistent, till the point where you can leave the surface.   I was trying to do solid line = component, and dashed = influence, but that got a bit tangled.  (Thanks to draw.io for the auto-layout).

NMS Intro Build

Its a pretty large game.  I still need to re-build a base, and then possibly get to hyperdrive.  Haven’t done much archeology yet, nor have I done much stuff in space. 

Don’t know that I’ll play it too much longer, we’ll see.

dotnetmud: spacemud: optimizing network traffic

I spent some time getting the network load produced by the game down.     The rest of this post, I test out the changes to see how much better everything became.  

Methodology

Two ships connected; I’m going to leave their starting locations as random.  I’m going to have them turn in circles and constantly fire.  This will yield some explosions against the planet, and others against each other, but most of the missiles will be flying out into space.

Running against a deployed web server in the cloud

Using WIFI and my home internet connection.

Collecting data via portal.azure.com’s App Service Monitoring graph set to minutes.  (This didn’t work, I had to go with my local wifi connection)

image

https://github.com/sunnywiz/dotnetmud2015/tree/chatty1

This is where we started at.    Sample packet and network load:

imageimage

 

https://github.com/sunnywiz/dotnetmud2015/tree/chatty2

The main change was to give custom JsonProperty names, as well as to reduce the number of decimal places being transmitted.

imageimage

I use Decimal  instead of Double because in double’s, there’s a chance that the underlying representation isn’t quite so digit friendly; decimals are exact for every digit.  However, decimals are harder on the processor for doing math – supposedly.   I haven’t tested that.

The result:  Not too much better.   5.5 MB came down to 3.1MB on the receive side.

 

imageimage  

https://github.com/sunnywiz/dotnetmud2015/tree/chatty4

chatty3, which I’m skipping, changed the method signatures to two-character method names.  Not too much saved there.

chatty4 added “blanking out” data that doesn’t change much.  It does this by keeping track of what it believes the client thinks the state is, and doing a diff:

image image

imageimage

In order to pull this off, I had to go to a dictionary of objects to render, rather than an array.   I also kept a “full” frame every 10 frames, similar to MPEG encoding G vs I frames.

I thought I’d get fancy and send nulls if various values (DX, DY) had not changed (for example, they do not change for bullets) – however, JSON sends a “:null” which can be longer than just sending the value.  The more advanced version of “don’t include the property if it hasn’t changed” is possible, but it got too complicated, so I ignored that for now.   So this version only does the image and name attributes, but that’s enough to get a nice reduction in size:

 

imageimage

The savings:  another 30%.  Its still a lot of data.  Not quite the gains I was hoping for.

At this point, an average entity on the screen is taking 100 bytes or so, instead of the 500 they were before. 

Network Optimization: Where to go from here?

I’m going to call this good enough for now.  The directions to go from here for network optimization:

  • Instead of using the system JSON serialization, write my own serializer.   This could do things like “If It hasn’t changed, don’t send it”.
    • so instead of: {“DR”:0.0,”DX”:17.109,”DY”:299.512,”ID”:0,”IM”:null,”N”:null,”R”:446.7,”RA”:3.0,”X”:37.0,”Y”:1474.8}
    • “dx17.109dy299.512id341r446.7x37y1474.8”   that’s about half the size.   But needs a lot more code to massage it.
    • Could use a binary packing method as well.   Quick search doesn’t find anything that is happy in both C# and Javascript.
  • Put some logic in as to whether something needs to be sent every time or not.  For example,
    • bullets pretty much go in a straight line once fired.  They don’t need to be sent every time.   In fact, if the current X,Y is approximately where the previous update X,Y + DX,DY would put it, then there’s no need to send those values.
    • Planets currently don’t move. (That will change).      This is a broader case of “nothing has changed in this object from what the client expects, so just acknowledge the object still exists, nothing more”.

What’s next?

I think that authorization / identity of clients (and dealing with disconnection better) is in order.  

Then I can do the “single person logged in, multiple clients” code

And then we can launch a spaceship from the text game.

And then we can design an actual playable game.   (oooo!)

dotnetmud: spacegame: Timing Loops 1: Client / Server timing loop

imageI feel stalled in my project.  It might be that I need to write about the important stuff done so far before I continue to redo/optimize the game; setting the stage to understand the need for some of those optimizations.

Reference code for this post (tag): https://github.com/sunnywiz/dotnetmud2015/tree/blog20160302.

The playable game (which might be updated, not frozen to this post) is: http://dotnetmud.azurewebsites.net/Home/SpaceClient

Game state at this time:   Multiplayer, can shoot missiles, missiles hit things, score is kept.

There are 3 interacting timing loops:

Client / Server Update Loop

I made the client “in charge” of this.  Starting in Javascript, the client requests an update from the server:

image

image

Along with the request for game data, the client is also sending the state of controls for the user.  I went with “how many ms has the thrust key been pressed” as my input – there’s a separate set of handlers which watch key-up and key-down events and long how long keys have been pressed for.

This then travels up through Signal/R, and shows up at the server 20-50ms (an eternity) later:

image

The SIgnal/R side of the hub takes the connection, looks up which server-game-object that connection is tied to, and asks that game object (which happens to be a Ship.cs) to craft what it thinks the world looks like.

The Ship has several things it has to do. 

First is to look at the keypresses and turn those into thrust amounts.  I went with a % of thrust:

image

There is similar code for “can I fire another missile yet” (not pictured).

Then, it makes a copy of all the stuff that the client needs to draw the world:

image

It records itself, all the other 2D objects in space, some scores, and what the current “Server Time” is (and rate of change of server time, which is a YAGNIY for when the server gets overloaded and has to go into bullet time).

It packages all that into a return (of type object, which gets JSON-serialized), and SpaceHub happily sends it back to the client:

image

Then, 25-50ms later (another eternity), the client receives the game-update:

image

  • Green – the client updates what it knows about what’s going on on the server.   I tried to keep this in its own “object space”, to indicate that the server is the authority here.
  • Blue – the client updates various things that are of interest to the client
  • Purple – there’s some bulk copying going on that isn’t very sexy and is very network-inefficient.
  • Red – and the client immediately requests another packet.

Places to Improve: Network Traffic.

The server sends ALL the data, ALL the time.   A network packet looks like this (raw, pretty printed):

image image

There is a LOT of stuff in there that is needed once (like image URL’s), and then does not change.   Also, many property names, while nice for humans, is not optimal for network traffic (words like “ClientREquestsPollFromServer”) for example.

“No Big Deal?”  — Doing a test, holding down the fire button and shooting the planet, for a few minutes, in Azure Data:

image

Doing some math:

image

If I have a successful game, and there’s about 1 combat going all the time, we’re looking at $18 per month.

The solution for this is to only send diffs across the network.  That’s on my list for pretty soon.  But it will require some hand-crafting detail.   And some before/after testing.  This will be its own blog post.

Places to Improve: Network Lag

This model is similar to how Kermit used to work – there is a LOT of dead time between updates.   

One way to improve this might be to have the client keep track of the lag between server updates, and then, in its request to the server, ask “please give me additional packets at the following times, until you hear from me again”.    So, the server could send 2 game-updates for every 1 client-inputs-update.     Its on my list, sometime after sending diffs.   Have to be careful and mark the intermediate frames as intermediate, so that only the major frames cause the cycle to continue.

Another direction we could go is completely decouple the client-input from the server-response.   I’m not going to do that, because I like how the client has to constantly remind the server of its existence – if the client goes haywire, I don’t need to write additional server code to detect that and shut down the server sending stuff to the client.   I’m also lazy. Smile 

In Conclusion

So, there’s the ugliest timing loop in the game so far.   The other two are relatively easy, may not get written about soon. 

dotNetMud: Its Alive! (on azure)

The link:  http://dotnetmud.azurewebsites.net/

From there you can either get into the old-fashioned text-and-chat mud:

image

(Valid commands are who, look, shout, say, and east and west)

 

Or you can get into the space-mud side of things:

image

(Valid controls are W for thrust, A and D for rotate)

Neither of these are really interesting until you have more than one client (browser, tab) logged in.  In the case of the latter, perhaps from different computers, with different network speeds.

Corresponding source code:  approximately here (as of these screenshots):  https://github.com/sunnywiz/dotnetmud2015/tree/blog20160220

Where to go from here?

I could try to make the space game more fun – adding missiles and lasers.   More realistic game.   You can’t have a space game without combat.. can you?

I could try to make the space game more single-player interesting – adding gravity, multiple planets. (your mission: try to get into orbit around a planet).  This lends itself to “more realistic server side computational load”.

I could try to make the space game more efficient – optimizing network traffic (right now, VERY large messages).

I could try to make both clients work better – by having “session” be the unifying factor, and the text game launches the space game, both in the context of the same player object, somehow.    This would probably involve hooking up to a database for persistence of user information as well.    This is complicated…  this is probably where I should go if I’m focusing on the “sample game” side of things.

I could try to make space more interesting – by voronoi’ing it, so that it can have 10000 or more objects, but its very easy to find the objects that are within, say, 100000 units of you.   So that I can do hyperdrive between star systems that you can drop out of and be in the middle of nowhere.  Also complicated.. probably comes after multiple planets.

I could try to stress test it – get a bunch of computers attached to it, see how it performs on the azure side.  I have it running on a Free web-node at the moment, and its surprisingly happy – 1.9% CPU.

StoneHearth (Alpha 14)

image

I have been playing a very cute game:  StoneHearth.   I paid $24 for it on Steam, its in Very Early stages, and I don’t regret the purchase.

However, be aware that, as it stands right now, its not very challenging once you learn how to stay alive.

I put together one farmer (prevent starvation) and one footman (prevent death by first attacker), and then mostly left the game alone after that, and … everybody lived.  Overnight.  I’m sure they would have died of cold eventually as the seasons changed, if that were implemented.   But as it is, the game settles into a sustainable state, doesn’t devolve into chaos. 

Comparatively, I left City : Skylines running overnight and it did descend into chaos.  How?  by the one simple rule of “no-auto-bulldoze-condemned.”  It’s the one thing that left unmonitored, will destabilize everything.

Also, if you do silly things like dig tunnels to then fill with water, some algorithms crash (you can see some of that above) and stuff and things generally go haywire and people stop picking up stuff etc.  Performance and Bugs.

The concept, though, is solid.   I love watching the little hearthlings do their little lives.. I guess this is what the Sims was like.

The build tree took a little getting used to.  I couldn’t find a nice graphic on the web, so I tried making one in LucidCharts:

However, LucidCharts (Pro) does not appear to have an auto-re-flow option, and if I started getting the level of detail I wanted .. it would not have worked.

What other options do I have?   A little google searching found:  http://bl.ocks.org/d3noob/5141528, So I downloaded that code, and plugged in my own CSV file.   But ran into cross-origin problems.

So then I went with http://jsfiddle.net/skysigal/k3kdeu9L/ which I forked into my own .. http://jsfiddle.net/roLmyovw/2  :

image

Which is just not quite pretty.  Not as pretty as this kitten, anyway:

image

Which is my Princess Fifi Toupee.  She kept trying to step on the keyboard as I wrote this post.  https://www.instagram.com/p/BB27Zh1Fvko/?taken-by=mollymcmahan

So, I think I’m giving up on this game for now.    I played it for 8-9 hours.    Maybe not the best return, but I suspect I’ll play it again in the future.

dotnetmud: Hitting a wall on clients, hubs, and games

Till now, I’ve had this pretty simple layout:

The browser fires up a signal/R client, which connects to a signal/R server, which talks to a driver, which talks to a game-specific engine.

One signal/R client maps to a user in the game somewhere.

This Situation is not Covered:

You are standing in a spaceport

You see:

a ship

>> board ship

You are inside the ship.  you can ‘launch’ to launch the ship.

>> launch

 

At this point, something happens that tells the client “hey, start this other client up” – possibly in a new tab.    Maybe it leaves the current tab in place. The new client connects up to its own hub, with its own mudlib, and does its own kinds of commands. The user can then fly the ship around in space.  The space stuff doesn’t know that the ship is also a “mudlib” object.

This could also be used with Shop Transactions, or a Game of Uno, or whatever – custom client for custom situation, in the same user’s session.

What this Means

a) I need a different sort of Auth mechanism to bind the user to an Identity.  I think I’ll probably go ahead and use the stock website’s authentication methods to get myself an actual UserIdentity;  I don’t know if that would flow down into any signal/R client or not.

b) Every user can have MULTIPLE clients going on at any point in time.   

This is making my head hurt.  I’ll let it percolate.   For now, no cross-jumping between clients.

HOWEVER, I do want to move the <T> that I currently have on the Driver so that the Driver stands alone, and the Signal/R Hub is the one that knows about the mudlib GameSpecific. 

(dotnetmud) spacegame: accelerating to a planet

I shook some dust off some old braincells, and re-remembered some algebra.

My quandry, as hinted by previous post:  GIven that I’m 42000 units away from the planet, and I have a max acceleration of 100 units/(sec^2), how much do I accelerate in the direction of the planet?

Some googling finds this:   https://van.physics.illinois.edu/qa/listing.php?id=116

d = vt + (1/2)at2

I know my distance, 42000.  And I know my a, 100.   What I need to calculate is v.  But not the one in the equation – that’s starting velocity.

Reverse it.  At the planet, starting at v=0, what’s my final velocity when I get to distance d?

The velocity is v = at.  So if I can solve for t, then I can plug that in.

d = (1/2) att

tt = 2d/a

t = Math.sqrt(2*d/a)

v = a * Math.sqrt(2*d/a).

Check the numbers in excel.   I set it up like this:

image

And plug in the formula to see how well it calculates speed:

image

The drift is due to the coarse 1 second time interval on the calculations, I’m pretty sure.

 

So, the answer is, if I’m 42000 u away from the planet, and I can thrust at 100 u/s^2, I want to be going 100 * Math.Sqrt(2*42000/100) = 2895 units/sec in the direction of the planet.

  • If I’m not going this speed, I need to boost in the direction of the planet.
  • If I’m going faster than this speed, I need to boost in the other direction.

To make up for inaccuracies, I’ll set the target velocity to match, say, 90% of my actual available acceleration.

I’m not accounting for my initial velocity at all, even if its not in the right direction.  The point is, at any point in space I can caluate what my velocity should be, and what direction it should be in, for the fastest approach to the planet.  Whatever I’m doing, change to match that.   It should be a nice curve, followed by some nice acceleration, followed by a turnaround, and then some deceleration with some waggles. 

Looking forward to implementing it.  No time tonight, so I just blogged about my resurrection of Physics 101 instead.   Super-excited.

DotNetMud: SpaceGame: Autopilot

After an aborted run at Rares Trading in Elite Dangerous (idea: pick up rares, go to Fehu, sell them, and then pick up missions there.  Problem: takes many hours to get enough worth selling), I took a look at writing an autopilot in my javascript space game (the part that is not yet Mud-like, but I intend for it to be).

Nice: Javascript has a Math.atan2(y,x) which _possibly_ avoids the x=0 problem that normal Math.atan() solutions do.  (arctangent gets you the angle to something so you know where to point the ship).

However, my code doesn’t work.  My ship very often points AWAY from the target, and applies FULL THRUST FOREVER.  This is NOT how you go somewhere.

There’s another aspect – I’m simulating “keyboard control”, ie, IsThrusting, IsLeft, IsRight.   For the ship to feel responsive when playing the game, need to have good numbers for these.  But with autopilot, there’s a lot of jigger (LRLRLRLR) because it continually overshoots its target.

So, here’s what I have to do for the relatively final solution:

  • Set up the ship so that there’s a finer gradient of control.
    • What I’ve seen other games do is, the longer you have it pressed, the more thrust you get, up to max.
      • This means I need to swap out my current keyboard handler (keyup, keydown) to change to a dictionary of “when was it pressed”, so that I can calculate how long its been held.
    • Or, I could go with W = 12.5% thrust, Ctrl-W = 25%, shift-W= 50%, and ctrl/shift = 100%.  Almost 8 stops.  More instantaneous full thrust available.
      • I want to avoid something where the autopilot can take a shortcut that’s not really available to a player.   So if I did the slow build up, I’d have to have the autopilot also do a slow build up.
  • Change how I do the maths for the autopilot.   Inputs to autopilot:  Who to go to, and what distance to orbit them at when we get there.
    • I see a few major layers.
    • First one is “close the distance”.   This would use the calculation of … http://physics.info/motion-equations/  … ouch, my brain hurts.   Basically, I need to find a solution for:
      • If I start at the orbit distance, and accelerate away at 75% thrust, what velocity am I at when I get to my current distance away?   Reverse that.  That’s the velocity towards the object that I want.   Call that V1.  I want it to shut off as I get close to the destination so that orbital stuff can kick in.
    • Second one is orbital force.   Skip this if I’m more than 2x the desired distance.
      • Figure out if I’m going clockwise or counter clockwise from the destination.  OR, just assume one direction for now.  clockwise.
      • Figure out the vector I need to be in a perfect circular orbit around the object at the desired distance.   Magnitude is known (previous post on orbital mechanics has link to the article), and Direction is 90 degrees from where the destination is.
      • That’s my desired vector V2.
    • Combine V1 and V2.  This is my desired vector V3
    • Look at my current vector V4 and figure out the change that needs to happen V5 = V3-V4.
      • Do a left/right thrust as needed to point me at that vector (do a smoothing function here).
      • if I’m pointing in relatively the right direction, do a thrust along that vector (do a smoothing function here).

This could be really fun.

Image: wikipedia

When Maths goes right

I found out I had been doing gravity wrong.

This is what I was getting:

image

There seemed to be gravity, but it was messy.

I had been using the good old (G*m1*m2)/(r^2) equation.   However, in an effort to be smart, I calculated the (x2-x1) * (y2-y1) to be the (r^2) value..

WHICH IS TRUE.

And when you apply that force to an object of mass m1, the m1 disappears (F=ma; a=F/m) and you get the acceleration, the pull of gravity.

But, the kicker is, I then have to scale the normal vector out by that.   And the normal vector is…  just the x component .. is .. (x2-x1)/r. 

When you do the whole thing, the acceleration felt is proportional to 1/r^3, not r^2.

So I did that.  And I pulled out the gravitational constant, G, to use elsewhere (turns out you can calculate orbital velocities and periods as well). 

The result:

image

No matter where you start, no matter your initial velocity and direction (within reason), you get a nice orbit.

What’s more, I could use the maths where “velocity at distance r” can be calculated, as well as “radians per unit time” can be calculated, for a circular orbit.

This means, I could write an autopilot to get you in a circular orbit around a star or planet at a particular distance pretty easily.  I think.

However, I also found out that due to gravitational effects of planets,  it was very easy to get swept into the sun.. and then gravitationally flung out of the solar system.

So, I changed it when you get within the radius of a planet, gravity is about constant – like it would be at the surface.  “Airbrakes”, as it were.  

THIS HAS PROMISE!  I spent probably an half hour flying around in this simulation, gravity made it so interesting. 

source: https://github.com/sunnywiz/dotnetmud2015/blob/e5db82581382e43a721f0abc4a5c45710ed7fee4/canvasTest/solarSystemFlight.html

it needs one image from the same directory.  Otherwise, just run that file.