Implementing State Machine for Unity3D NPC characters - c#

I can't seem to wrap my head around this topic and i need a bit of a push forward, maybe an example.
I'm currently developing a project including simulating a large number of automated characters trying to fulfill their "needs".
As for now the characters have their need levels stored in their scripts as simple floating point <0,1> value and check every frame if it exceeds a given value and then try to move to a defined point and satisfy their need.
The problem is one character can have many needs and that leads to a situation when an character moves to satisfy one need and it drops just below the threshold it moves to satisfy the next need. Assuming that later i would like the needs to "rise" over time this will be a big problem.
I think i should implement an state machine to transition from idle -> move to satisfy -> wait to satisfy -> idle but as i said i cant quite understand the whole state machine thing. The closest to understanding for me was this: https://github.com/nblumhardt/stateless but I still cant wrap my mind around it.
Would be grateful for any help, tutorials, examples, anything.

Related

When using Dijkstra's algorithm, is there a good way to keep track of a variable that changes with the path?

I'm writing a scheduling program using C# in which I construct a graph of people's availabilities and run Dijkstra's algorithm from every start node to every end node to determine the optimal path.
I would like to use the number of times a specific person has worked as an edge weight (i.e if the algorithm has to choose between John who has worked 2 times and Sue who has worked 1, it will choose Sue because she has worked fewer times).
The problem with this is that this number will be ever changing depending on which vertex I'm currently viewing. I think I can create a class, lets call it TimesWorked, that keeps a list of people and the number of times they have worked and then add an object for said class to my vertex class. From there on each edge from vertex1 to vertex2, assuming this edge creates a better path to vertex2 than current, I can deep copy TimesWorked from vertex1, make the required change to the copy, and assign that to vertex2. This will require me to check all paths into any vertex before checking the paths out of the vertex which should be possible because I can guarantee this is a directed acyclic graph.
I haven't written any code for the algorithm yet, otherwise I would paste it, but does anyone have any insight into whether or not my idea would work or possibly have a better idea?
here is an example graph, I made it in paint so it wasn't super easy to add arrows, but it is directed and always goes from left to right. start and end reflect the start time and end time of an availability.

Frequencies and amplitudes of sound file in C#

I'm trying to create a program which gets the various "notes" in a sound file (WAV or MP3) and can get the frequency and amplitude of each. I've been searching around for this, and of course there is the problem of distinguishing individual "notes" in a music file which isn't a MIDI, but it seems that something along these lines can be done with NAudio or DirectSound. Any ideas?
Thanks!
What you are asking to do is extremely difficult.
Step one would be to convert your audio from a time domain to a frequency domain. That is, you take a number of samples, and do a Fourier transform (implemented in your software as FFT).
Next, you begin deciding what you call a note or not. This is as not as simple as picking out the loudest of the frequencies! Different instruments have different timbre, which is created by various harmonics. If you had a song of nothing but sine waves, this would be much simpler. However, you'll find that you'll start seeing notes where your ear tells you they don't exist.
Now, psychoacoustics come into play. It is entirely possible for humans to "hear" notes that do not even have a fundamental. This is particularly true in a musical context. If I were to take a trombone and start playing a scale downward, at some point, the fundamental disappears or is mostly gone. However, you will still perceive that scale as going downward, when in fact the fundamental sound has all-but disappeared. Things get really tricky at this point.
To answer your question, start with an FFT. Maybe this is sufficient for your needs. If not, begin reading the significant amount of technical literature on the subject.

Go into one side of screen and come out on other side?

Could someone give me advice on how to have the player go through one side of the screen, and come out the opposite side in XNA C#?
If you need an example, just look at Super Mario Bros. 2, if you go into the right side you'll come out the left, for example.
I am going to respond at the highest level, there is a lot more to this than I am letting on.
Normally you stop the character from moving past the barrier of the screen, if you are using such a system you need to instead track them outside of the viewport, and know when to wrap them around. This could either be when the character completely disappears off the screen, or the moment any part of him disappears from the screen.
Then it becomes handling the special cases of drawing when he is partially off the screen, and moving him over to the other side once the transition has completed (or any time in between if that makes sense for your implementation, for instance the character could be considered on the other side once 50% moved off the screen).
Also note that I focus on characters, but any visible elements will have to know about these situations, even if they can't travel the same way. For instance if you are half on both sides you may be vulnerable on both sides. All of these are design decisions you need to make.
With regards to the technical implementation, that is above and beyond what we can do for you here, refinement is something we can do, but creation should come from you. (And is so heavily dependent on your other code that anything we made would just be an example and not directly usable).

How do I use .NET to find an orange ball in an image?

I'm getting images from a C328R camera attached to a small arduino robot. I want the robot to drive towards orange ping-pong balls and pick them up. I'm using the C# code supplied by funkotron76 at http://www.codeproject.com/KB/recipes/C328R.aspx.
Is there a library I can use to do this, or do I need to iterate over every pixel in the image looking for orange? If so, what kind of tolerance would I need to compensate for various lighting conditions?
I could probably test to figure out these numbers, but I'm hoping someone out there knows the answers.
Vision can be surprisingly difficult, especially as you try to tolerate varying conditions. A few good things to research include Blob Finding (searching for contiguous pixels matching certain criteria, usually a threshold of brightness), Image Segmentation (can you have multiple balls in an image?) and general theory on Hue (most vision algorithms work with grayscale or binary images, so you'll first need to transform the image in a way that highlights the orangeness as the criteria for selection.)
Since you are presumably tracking these objects in real time as you move toward them, you might also be interested in learning about tracking models, such as the Kalman filter. It's overkill for what you're doing, but it's interesting and the basic ideas are helpful. Since you presumably know that the object should not be moving very quickly, you can use that fact to filter out false positives that could otherwise lead you to move away from the object. You can put together a simpler version of this kind of filtering by simply ignoring frames that have moved too far from the previous accepted frame (with a few boundary conditions to avoid getting stuck ignoring the object.)

Keeping sync in multiplayer RTS game that uses floating point arithmetic

I'm writing a 2D space RTS game in C#. Single player works. Now I want to add some multiplayer functionality. I googled for it and it seems there is only one way to have thousands of units continuously moving without a powerful net connection: send only the commands through the network while running the same simulation at every player.
And now there is a problem the entire engine uses doubles everywhere. And floating point calculations are depends heavily on compiler optimalizations and cpu architecture so it is very hard to keep things syncronized.
And it is not grid based at all, and have a simple phisics engine to move the space-ships (space ships have impulse and angular-momentum...). So recoding the entire stuff to use fixed point would be quite cumbersome (but probably the only solution).
So I have 2 options so far:
Say bye to the current code and restart from scratch using integers
Make the game LAN only where there is enough bandwidth to have 8 players with thousands of units and sending the positions and orientation etc in (almost) every frame...
So I looking for better opinions, (or even tips on migrating the code to fixed-point without messing everything up...)
Surely all your client will be using the same binary, so compiler optimisations have no effect on synchronisation issues.
Also, if you are only planning on targeting one architecture (or are at least only allowing people to play against each other if they are on the same architecture) then that doesn't matter either.
I've done exactly the same thing using floating points in C# developing games for the iPhone and desktop, and they both give the same results, even though the iPhone is ARM and desktop is x86.
Just make sure the game does exactly the same calculations and you will be fine.
If all else fails, just replace all instances of float in your game to a standard fixed-point arithmetic class. That way you can be 100% sure that your calculations are deterministic across architectures, although the nature of fixed-point arithmetic may adversely effect your game.
I'm a little late responding to this, but from a Game Security point of view the simulation should only be running on the server/host (i.e.: don't trust the clients, they could be cheating):
The clients should only be sending their movements/commands to the server (which discards bad inputs or clamps them within game limits, so a client saying "I'm running at 10,000m/s" gets clamped by the server to say 10m/s).
The server/host only tells clients about things happening within their field of view (i.e.: a player at co-ordinates 0,0 doesn't get told about two AIs fighting each other at 200,0 if he can only see a radius of 50 units around him/herself).
It's the second part that saves the bandwidth - the simulation on the server/host may have thousands of objects to manage but the clients only need to know about 100 or 200 things within their own field of view.
The only wrinkle in the situation is things like dynamic fire (bullets, missiles, etc) whose range may be greater than a client's view radius. The server/host tells the clients their origin and initial trajectory/target object, the clients then simulate their path according to the same rules, but the kills are only valid in the simulation on the server/host.
Serializing the client-specific world state and compressing it before transmission can also be a huge win, especially if your class properties are only Public where needed. (I normally avoid XML, but we significantly improved compression ratios in one application by serializing to XML and compressing it versus serializing to a binary format and compressing that. I suspect the limited range of ASCII characters used had a hand in it, YMMV.)
A common technique is to have all clients describe their current state to the other clients, periodically.
When two computers disagree about the state of an object, presumably due to floating point error, the game has some rule to determine which is correct, and all clients adjust to match it.
What are you using doubles for specifically? Could you use decimal instead?
Generaly the server would store the state(position/oriantaion/type) of all players units.
When player1 moves a unit
either... the instuction to move is sent to the server
or... the updated state is sent to the server
When the player client needs to render the scene the server sends back state info on the location of all the units within a requested scope.

Categories