Can threads access variables in a void? - multithreading

I'm currently working on a game bot. It's a very simple bot, but I want to split it up into threads called. Move, search, click. If we have a int y in the thread search with a value of 900 can we get this integers value in click? Please include a code sample also if that's possible.

Related

Loop in a activity diagram

I am trying to design an activity diagram for an image editing application. Let's say that the application has one adjustment to edit an image. that's brightness. When the user opens the application he can change the brightness again and again. Then finally save it. That's really not a loop. but it's a repetitive process. How can I represent such a process. I have found stack answers for looping through documents and for loops. But didn't found a matching scenario for like this.
Thank you!
Oh, but why do you say it is not a loop? It is.
Sorry, no reasonable drawing tool at hand, so this will be textual
Let's look at a pseudocode:
open app (image as attribute)
while decide to continue to brighten the image do
brighten the image
loop
As you can see you do loop and your condition to loop or finish it depends on the decision to brighten or finish working with the app.
The brightening itself can be more complex (e.g. may have some selection of settings like level or method of brightening, it may even have the ability to break the brightenin or undoing) but it is still the loop.
Out of this solution to represent a loop you can use options 2 and 3 easily.

Key and lock code [game programming]?

Metaphorically speaking, I'm learning python in a community college for game programming; and our second assignment is to make a text based game. I'm stuck trying to figure out how to get the code to run if the player has something in their inventory, then display these options or print these options if they don't have that certain item in their inventory.
What you describe is what was a very popular type of game a long time ago. The most difficult portion of this type of game is interpreting user input. You can start with a list of possible commands, iterating through each token of the user's input. If I type
look left
the game can begin by calling a method or function which interprets the available visual data. You will want to look into your database implementation for what is left in relation to the current player's position. The game can respond with
You see a wooden box on the floor. It is locked.
You get the idea. As the user, I will probably want to check my inventory for a key. You can implement a subroutine in your parser for searching, categorizing, and using your inventory. The key to writing anything like the described program is spending at least twice as much time testing and debugging the game over how much time you spent coding the first working version. The second key is to make sure you are thinking ahead. Example: "How can I modularize the navigation subroutine so that maps can be modified or expanded easily?"
Comment if you have any questions. Good luck coding!

Simple ActionListener

So i'm making a little game in which a player can wander around a map and collect things, but may also come across randomly moving bots. I'm trying to make it so that if the player and standing next to the bot and click's 'fight' the bot loses health.
The only way I can think of doing this is by making an ActionListener in the bot class. So I'm trying to make an 'if' statement like, if the Player is on a nextdoor tile, and clicks 'fight', the bot will lose health. However I've never used an ActionListener and despite looking at lots of examples I still don't understand them.
The bot's coordinates on the map are 'ycord' and 'xcord' and the player is represented by 'P'.
Is there a way of saying this...
if(map[ycord+1][xcord].equals("P") && 'fightbutton' is pressed){
bot.health--;
Thanks a lot!
I think you should not use an ActionListener. ActionListeners need to be "registered" on the instance that creates actions, in your case players. So every bot would need to register an ActionListener on every player, which (in my opinion) is a bit messy.
Instead, I would simply create a hitBy(Player) method on Bot which is executed by some superior Game instance. A fighting player would do something like game.madeHit(this) and the game searches for close-by bots and then executes bot.hitBy(player). This can be done by simply iterating over a list containing all bots; this list should be available in Game.

Threading Box2D with pthreads

So I'm essentially trying to implement an AIR Native Extension that does the physics simulation in C with interfaces through Actionscript.
I've gone through quite a few iterations which I'll list below for interest sake and I'm at what I think could be my final attempt at getting this working in a more performant way.
Ultimately I'm looking for help in how I should be setting up a threading environment for running the simulation of Box2D on a separate thread and then polling for state in AS3.
Methods:
Brute Force:
In this method I simply call into C from AS3 and tell it to create a world and pass it some boxes to add to this world. Every frame in AS3, I call into C to tell the world to Step, then loop through all the bodies in the World, get their position and rotation, convert them to actionscript objects and put them in an actionscript array and then send that back to AS3. Once there I loop through the returning array and assign those position and rotation values to my sprites so they visually update.
The results are actually quite decent with about 116 boxes being added before the framerate suffers. This is compared to 30 boxes in a pure AS3 implementation. Note that these stats are in Debug mode. In release mode, they both make it to about 120 boxes. There is little difference between the AS3 implementation and the Native Extension implementation.
ByteArray Sharing
In order to improve performance I decided it would be a good idea to try and limit the amount of data being marshalled across C and AS3. ANE's support sharing a byte array's memory space and so I would send the ByteArray created in AS3 to C and have C simply update the ByteArray. This saves us from having to construct AS3 objects in C and pass them back. Every frame, AS3 simply needs to iterate through it's ByteArray and see what C has written into it and then assign those values to the sprites to set the visual state.
The results here are sadly about the same. Improvements are only marginal.
Direct Object Setting From C
Another thing ANE's are capable of is setting the property of an object that lives in AS3. In this sense I aimed to eliminate the overhead of passing back data to AS3, the looping through the bodies to collect data in C and the looping through in AS3 to assign the values. I directly modified the Box2D code so that when it's values were changed it would write the new x, y, rotation values directly on the corresponding Sprite.
The results are amazing at very low amounts of objects since the call to set these properties is well under a millisecond. The problem is that this scales linearly and around 90 or so objects, the overhead is too severe and things start to slow down.
Threading
At this point I was a bit stumped. There's overhead in marshalling data, there's a cost in C for iterating and constructing the data to return and there's a cost in AS3 for iterating to assign values to the sprites.
Obviously there needs to be a trade-off so my current solution is the best I can come up with for now.
On the AS3 side you call into C to create your world, call in to add a box to that world, and call in to tell C you want a refresh of your data. When boxes are created in AS3 they get a unique id and they are stored in a dictionary with the key being the id.
On the C side, the world is created and a new pthread is spawned to do the Step. Essentially simulating the world on another thread. After it steps, it assembles all the data and writes it into a double array. Then it does so again and again and again. It just simulates forever basically on it's own thread.
When we call in to C to add a new box, I need to create a new box and add it to that world. Since the world is Stepping this could cause problems which means I need to use mutexes I'm pretty sure.
Same thing when we call to get the values refreshed in AIR, I'll want to do a memcpy from the array of doubles into my AS3 bytearray and then loop through the bytearray to set the values on the visual.
The mutexes were giving me trouble so I basically implemented my own which you can see below... and laugh at :)
However it does work, just not as fast as I would like it too. Around 90 we slow down again.
Anyone have any thoughts or pointers? It'd be greatly appreciated!
C Code
The parser was acting up so i've pasted it here:
http://pastebin.com/eBQGuGJX
AS3 Code
Same thing with the parser. I've only included the relevant method dealing with every frame in AS3.
http://pastebin.com/R1Qs2Tyt
I had forgotten I had this question. Fortunately I have figured it out.
The idea of using mutexes etc was over engineered in the first place and unnecessary.
Since we're running in Flash, everything runs in the main thread. Which means for each "frame" flash will natively handle any media, then our client code which we have written, then actually render to the screen and finally do any garbage collection if necessary.
I don't actually need to have the physics sim simulating forever, I simply need to have it be one step ahead of my client code.
So what happens now is when the Client calls into the ANE to setup the world, it creates a new thread that simulates the world and returns immediately back to Flash. Flash will continue to do its work of executing the rest of the client code and then rendering and then GC.
Then on each frame in Flash we can simply call into the ANE to retrieve the results. In the case that the Simulation thread wasn't finished we wait via a join, extract the values and return them to Flash. Making sure to spawn another thread for the next step before returning of course.
In this way we are maximizing our efficiency since the simulation is happening while Flash is busy doing other things we don't have control over (like rendering and GC).
The good news is that performance almost doubles with this approach. Going from approx 90 boxes in a synchronous pure AS3 implementation to approx 170 boxes in a threaded ANE approach.
The bottleneck eventually becomes the iteration through the data coming back from the ANE and assigning those values to the Display Objects.
I hope this helps someone else who was looking for something similar. I'll be giving a talk about it at FITC Toronto at the end of April so there may be more information and material I can post then.
http://www.fitc.ca/events/presentations/presentation.cfm?event=124&presentation_id=1973

How to make a series of dice rolls secure in a client/server scenario?

Imagine the following situation in a game: A series of random numbers is presented to the player. Each number is shown to the player for a short period of time before it goes to the next one. It is the player's aim to pick a high number. He or she just needs to 'click' in the right moment and then the number is chosen.
The question is about how to implement this scenario in a secure way in a client/server scenario.
That means there is a game client which displays the above mentioned scene and there is a server to which the chosen number (in whatever way) needs to be send. The catch is to get this thing secure so that cheating (e.g. by modifying the client) is not possible.
There's really no way to make this completely secure. Even without modifying your client, all someone needs to do is have another program running in the background reading the monitor and looking for a certain number to appear and then send a mouse click event to your client program. This can be done without modifying your client program at all. Even if you somehow managed to make it secure so that they couldn't run any other programs or services at the same time as your program, they could just point a webcam at the screen hooked to a different computer to do the optical digit recognition and send a mouse click event over USB to the computer running your program.
Fortunately, you may be able to get around the problem. Generally if something is to be a "dice roll" you want a random "luck based" outcome. By allowing them to click at a certain time you are making this a skill based game instead of luck based and therefore not really a dice roll. You could make it so there's a slight "delay" from when they click to when the dice stops rolling, so they see it rolling around, click, and it slows down and lands on a number. This way what number was displayed when they actually click does not determine the outcome, but the next (random) number after that one, which would eliminate the possibility of cheating and make this a luck based roll instead of skill based.

Resources