Posts Tagged ‘code’

Desert Strife Post-Jam AI (with more code examples!)

Posted by
Wednesday, October 5th, 2016 2:31 am

GIF!

Intro

Took a while (too many projects at the moment; loving it!), but here it is: Part 2. The basis of the system is still a reactive AI using heuristics. It doesn’t plan ahead at all, which is good enough even if it does show every once in a while.

The full AI code can be found here, if you care for that sort of thing!

Heatmaps

Heatmap, the main technique utilized, simply stores values in an extra tilemap (int array!) for spatial information evaluation. In this case the heatmap stores threat and support values used in choosing the AI attack target and movement path.

Pic2Small

Heatmap visualization

The recursive heatmap function is called for each enemy and ally on the map

Threat evaluation

Calculating the threat (and support) values is a cinch: Add up the probable averages of all usable weapon types of your allies and enemies. There is a bit of quess work involved if the enemy ammo counts are not known already.

 

 Nothing out of ordinary here I’d hope

Panic!

New feature alert! Taken that we already have threat values for each group and location data about safe areas, we might as well have a bit of fun with them! Every time a group gets into a fight its support value is compared to the threat level around it. A high enough difference will send the group in panic, running away from enemies and finally fleeing if reaching the level border. Now you can bully your enemies to submission!

PanicCalculation_

Panic also ensues if the total enemy force is overwhelming, but that is checked elsewhere

Conclusions

Simple and seems to work alright. In the next project I’ll be using Monte Carlo Tree Search, no doubt about that. It is much less work with potentially more intelligent results. Working on a generic version at the moment.

Play it here!

PS:

seththefirst suggested taking a look into AI utility functions in part 1 . The threat level comparison turned out to be sufficient so I didn’t implement anything like it, but maybe you (yes, you!) will find it useful.

Desert Strife Jam AI (with code examples!)

Posted by
Tuesday, September 6th, 2016 3:14 am

TL:DR KISS

The Post-Jam version is still far away (I’ve hardly had any time to work on it yet) so I might as well talk about the AI of the Jam version and the plan for the Post-Jam AI.

Version one

The first version of the AI was finished in the morning of the second day. It had one goal and one goal only, EXTERMINATE! In simple terms each unit finds the closest enemy and moves towards it. They also check that the path to the enemy is not blocked, by simply finding a path to it. This could cause slowdown with bigger maps and larger unit counts, but here it is just fine and dandy.

VIEW CODE (embedding doesn’t work unfortunately…)

Version two

So far the AI doesn’t take into consideration the main mechanic: scavenging technology. Implementing such behaviour, the quick and easy way, requires nothing more than adding technology distance checking into the previous code. If there’s technology closer to the unit than enemies, then that is what the unit will go for. On top of this the unit won’t move until it has completely excavated the technology.

VIEW CODE

Version three

I could have stopped here, but there was an annoying deficiency in the artificial thought process. The units don’t prioritize actions; In other words, an attacking unit is sometimes blocked by a scavenging unit.

The fix is slightly more elaborate than the previous step, but by no means complicated: tally up the best actions from all units and choose the most important one based on the target type and distance. Here’s the final AI in action.

NEW STUFF!

VIEW CODE

The future?

There are still a few things I’d like to implement in the AI, namely threat assessment and intelligent positioning. As luck would have it, there’s a single technique which can be used to achieve both of these goals: heatmaps! A heatmap is simply an extra tilemap which contains values based on unit proximity and threat level (if you are lazy you can even reuse the world tilemap objects for this purpose). Heatmaps can be used in selecting the target (lower threat value is preferred) and in pathfinding to choose the safest route to the target.

Conclusion

Keep it simple and you’ll go surprisingly far.

Play Desert Strife here

It’s a clusterfuck!

Posted by (twitter: @Guard13007)
Sunday, December 13th, 2015 4:18 pm

So I haven’t posted anything until now because I’ve been focused on trying to build something cool.

Many many hours of work later, and we have this:

it's a clusterfuck

So the concept for this game is that your two buttons are 1 and 0 because the control systems on this shuttle are fried. You have to execute opcodes in order to fly around, scan, communicate, fire weapons, warp, do whatever. You start out with just those notes on the sides telling you how to control the shuttle. “Growth” has not come into play yet, but the idea is either to build your ship up, or in the future, to build an empire. I won’t get the empire building done within the next few hours, but hopefully I can get some basic scenarios and sector-generation going, and upgrades.

Right now you can travel between sectors which all generate .. pretty much what you started in. I’m working on that! Not much else is done either, but it’s a lot more than I’ve ever put in a game before, so I’m pretty excited!

Code is on GitHub btw.

Oh, and here’s a video (no I’m not live streaming right now cause crunch time) that shows off some of it at an earlier state:

Going in once more!

Posted by (twitter: @Zazanxors)
Thursday, December 10th, 2015 1:33 am

This will be my sixth time and I’m looking forwards to it! Planning to make a smaller game this time around, as I feel my ideas have been too large for me to accomplish in the time frame. As for my tools, I’ll be using Unity (Game Engine), Visual Studio (IDE), Beepbox (Music, some SFX), Chiptone (Other SFX), Paint.NET (Art) and Gimp (Art extras)

As always, I may end up using (and fixing up) some old Unity platformer code I wrote a while back – feel free to use it for your stuff if you want.

Good luck!

Code snippets for everyone to use

Posted by (twitter: @@moongateuk)
Tuesday, December 8th, 2015 6:13 am

Hey everyone,

For my own use I’d been coding up some base code for items and levels. So as of yesterday I’ve put them up over at itch.io and it’s called Basecode Bits.

It’s free to use for anything and will be updated with different little snippets predominantly for game dev and game jams.

This is what’s currently included in the small pack:

  • Level Object.
  • Item Object.
  • Basic Level Generation Code.

If you have any ideas or suggestions then post a comment over on the itch.io page.

For those having trouble with collision detection in Java:

Posted by
Saturday, August 22nd, 2015 6:08 pm

Try putting this in your player class:

public boolean intersects(Tile tile) {
tile = this.tile;
Rectangle r1 = new Rectangle(x, y, width, height);
Rectangle r2 = tile.getRectangle();
if(r1.intersects(r2) || r2.intersects(r1)) {
return true;
} else {
return false;
}

And in your main class, put:

if(player.intersects(tile) {
System.out.println("Intersects!");
}

To get a visual interpretation of the rectangles, put this code in your method where you render:
CDetection
// This is at the top of the class
public boolean meshMode;
// Rendering:
public void render(Graphics2D g) {
player.render(g);
tile.render(g);
if(meshMode) {
// You can of course use any color
g.setColor(Color.GREEN);
// If your ints/doubles aren't public, then make some getters.
g.drawRect(player.x, player.y, player.width, player.height);
g.drawRect(tile.x, tile.y, tile.width, tile.height);
// In Key Listeners:
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_M) {
meshMode = true;
}
if(k == KeyEvent.VK_N) {
meshMode = false;
}
}
}
}

This is a very simple way to calculate collision detection in a 2D Java Game.
Hope this helps a lot :)

I haven’t been here in a long time. So I thought I would share this code I made with you and explain how it works (so people who don’t use Gamemaker can reverse-engineer it). Unfortunately I’m not taking part this time round, but I felt like helping those who are!

Gamemaker Script Code

Simply copy and paste these into two seperate scripts:

Ring Health Bars

Circle Health Bars

To execute the scripts simply use:

scr_health_ring(x,y,radius,thickness,maxsegments,segments,rotation,angle,direction,colour)

or

scr_health_circle(x,y,radius,maxsegments,segments,rotation,angle,direction,colour)

Meet The Variables:

Ring Health bars

x,y = the coordinates of the center of the ring

radius = the distance from the center to the start of the ring

thickness = the thickness of the ring

rotation = at what angle is the zero mark of the ring?

angle = over how many degrees does the ring span?

max segments = how many segments the ring is split in to

segments = how many of those segments are visible

direction = which way is the ring drawn? 1 for anti-clockwise and -1 for clockwise

For the circle health bar the variables are the same except that thickness is not included.

Demo download

A demo (gif):

Circular Health Bars Demo

 

The demo is using two rings; one red and one green to simulate health.

What is this sorcery?

When a computer draws a circle, it is actually just a bunch of triangles arranged in the same way as slices of pie.

To find the coordinates of each triangle to be able to draw them we need a big steaming pile of math.

 

Specifically we need some trigonometry. Let’s start with which way is up:

Ring Health bars angles

Or in this case, which way is right: zero degrees.

A new temporary variable, i, will count our current angle as we sweep around the circle.

After each cycle in the loop it will change by the angle variable divided by the maxsegments.

Ring Health bars sections

 

Add to the mix our variables for radius and thickness and we can draw a triangle to work out our x and y coordinates for our triangles to draw!

Ring Health bars triangle

From this we can get the coordinates for the two points a and b. To get point b we simply add the thickness to the radius:

Ring Health bars triangle ab

 

Then we add the angle change to i, and get the variables c and d. Then we can connect the dots and draw one section!

Ring Health bars triangle cd

Ring Health bars section

Repeating this process for how many sections we want to draw will result in a ring!

This process is the same for the circle, except we only need points a, c, and the center for each triangle.

 

Good luck for LD 32! Hopefully this tutorial will come in handy.

I kinda rushed this. Hopefully you can understand it if you have a basic grip on maths.

Hexagonal Grids!

Posted by (twitter: @theupgradezone)
Wednesday, November 12th, 2014 5:10 am

So, I did the maths on how to draw a perfect hexagon. Now, using the same logic from that
tutorial, in this tutorial I will show you how to make a hexagonal grid!

Maths:
Let’s say we have a hexagon with width a and height b.
(If you used the last tutorial, then the value of a from the last tutorial becomes twice that
value in this tutorial)

HexGrid_001

Now we generate a bunch of hexagons to fill up the screen area.
For this example the point of reference is in the center of the hexagon.

Each hexagon will be 3/4*a to the side of the last one, and b above or below:

HexGrid_002

Remember to keep a and b as whole numbers!

So the screen should look like this: (grey = hexagon, dark grey = overlap)

HexGrid_003

But we need every 2nd column to go up a bit, so that the hexagons interconnect properly.
So we use this equation, where x and y are the coordinates of the center of each hexagon:

HexGrid_004

Now your screen will look like this:

HexGrid_005

Excellent!

Now we can code it!

Pseudo Code:

Initialize:
width = (width of hexagon)
height = (height of hexagon)

Creation Loop:
create hexagon
x = x + (3*width)/4
if edge of screen is reached {
go back to starting edge of screen
y = y + height
}

Creation code for each hexagon:
y = y + mod(sin((90*(x/width))*height/2)

 

 

And there you have a hexagonal grid!

And this is the last of my little group of tutorials :3
Thanks for the comments on how to refine the logic and code of previous tutorials :)

How to draw a perfect Hexagon!

Posted by (twitter: @theupgradezone)
Tuesday, November 11th, 2014 9:55 am

So I did that maths of drawing a perfect octagon. Now, for a perfect hexagon!

Maths:

First, we must imagine a rectangle with width a and height b.
You must now choose which side, a or b, you want to specify.

Hexagon_001

If you specified a, to find b you must use this equation:

Hexagon_002

 

 

If you specified b, to find a you must use this equation:

Hexagon_003

Which can also be written as:

Hexagon_004

Remember to round to the nearest number!

 

Drawing:

Once you have a and b, we need to specify the 6 coordinates.
For this example, the frame of reference is the center of the rectangle.

The top and bottom two are b/2 up or down and a/2 to each side.

Hexagon_005

The two sides are a to the side.

Hexagon_006

Then you draw lines between the coordinates.

Hexagon_007

And there you have a perfect hexagon!

 

Pseudo Code:

Initialize:
// Let’s say we specified the width, a
width = (width of imaginary square)
height = 2 * width * sin(60)

Loop:

coord_1x = x – width/2
coord_1y = y + height/2
coord_2x = x + width/2
coord_2y = y + height/2
coord_3x = x + width
coord_3y = y
coord_4x = x + width/2
coord_4y = y – height/2
coord_5x = x – width/2
coord_5y = y – height/2
coord_6x = x – width
coord_6y = y

Hexagon_008

//draw_line(from_x,from_y,to_x,to_y)
draw_line(coord_1x,coord_1y,coord_2x,coord_2y)
draw_line(coord_2x,coord_2y,coord_3x,coord_3y)
draw_line(coord_3x,coord_3y,coord_4x,coord_4y)
draw_line(coord_4x,coord_4y,coord_5x,coord_5y)
draw_line(coord_5x,coord_5y,coord_6x,coord_6y)
draw_line(coord_6x,coord_6y,coord_1x,coord_1y)

 

And that’s how to draw a perfect hexagon!

Coming soon: Hexagonal grids!

How to draw a Perfect Octagon!

Posted by (twitter: @theupgradezone)
Tuesday, November 11th, 2014 4:45 am

I did some maths while I was bored, and I calculated a way to draw a perfect octagon.
I thought this might come in useful for some of you :3

Warning! Maths ahead!

 

Maths:

Imagine a square, with side length a.

001

Our octagon will fit in this square.

We will call the length of a side of the octagon b.

002

 

Now you put a into this equation:

003

Which can also be written as:

004

And you get b.

Now, round b to the nearest number. We can’t be dealing with fractions of pixels now, can we?

Now you have b, we can draw the octagon.

 

Drawing:

For this example, our reference point will be in the center of the octagon, which is also the center of our imaginary square.

005

Our first 2 co-ordinates will be a/2 up and b/2 to either side.

006

007

For the sides, it will be a/2 to the side and b/2 up or down.

008

Then, draw lines between the co-ordinates to finish the octagon.

009

And there you have a regular octagon!

 

Pseudo code:

Initialize:

size = (length of side of square)
oct_side = round((size*sqrt(2))/(2+sqrt(2)))

Loop:

coord_1x = x – oct_side/2
coord_1y = y + size/2
coord_2x = x + oct_side/2
coord_2y = y + size/2
coord_3x = x + size/2
coord_3y = y + oct_side/2
coord_4x = x + size/2
coord_4y = y – oct_side/2
coord_5x = x + oct_side/2
coord_5y = y – size/2
coord_6x = x – oct_side/2
coord_6y = y – size/2
coord_7x = x – size/2
coord_7y = y – oct_side/2
coord_8x = x – size/2
coord_8y = y + oct_side/2

010

//draw_line(from_x,from_y,to_x,to_y)

draw_line(coord_1x,coord_1y,coord_2x,coord_2y)
draw_line(coord_2x,coord_2y,coord_3x,coord_3y)
draw_line(coord_3x,coord_3y,coord_4x,coord_4y)
draw_line(coord_4x,coord_4y,coord_5x,coord_5y)
draw_line(coord_5x,coord_5y,coord_6x,coord_6y)
draw_line(coord_6x,coord_6y,coord_7x,coord_7y)
draw_line(coord_7x,coord_7y,coord_8x,coord_8y)
draw_line(coord_8x,coord_8y,coord_1x,coord_1y)

 

And that’s how to draw a perfect octagon!

Coming soon: How to draw a perfect hexagon!

 

To those interested in the maths involved to get the equation:

Maths

 

I originally calculated this to be able to draw the Aperture Science logo by hand, but I also saw the use of it on the computer!

Base code update

Friday, December 13th, 2013 4:04 pm

I’ve written a little base code project I’m going to start with.

It’s fairly simple and is available here.

To recap, it uses libgdx. If I fancy some postprocessing shader stuff, I’ll use the PostProcessor class from here, and if I need to do some pathfinding, I have my own simple A* implementation I wrote which is available right here.

Adios!

Hi everyone

****I posted new questions about compilers and editors in the comments please read them :) ****

I am an experienced programmer and know a few languages.

I want to start making games with a windows 7 computer for any operating system (linux, Mac and Windows).

I do not know which is the best coding language to use for that that is free But has a good compiler and runs well. I also do not want to use software like gamemaker or construct. Something like c++ or java or love2D so please take that into consideration!

Also what libraries would I need for 2D games for now to make it with.

Thanks so much for your help in advance! I really want to become some kind of game devloper so this is key.

Hope you all are having fun in the ludum dare! Expect me to be ready for the next one!

Hope my questions make sense!

Please comment!

Thanks

Swipefaststudios

Yesterday my stream peaked at 126 people watching me program! In order to make it a true 53 hour marathon, I just left the stream going overnight and pointed the camera at my bed. When I woke up, there were still 44 people in the stream who greeted me as I got back on! Aha, ha ah!

More coding action today at twitch.tv/dvcolgan! I’m making a massively multiplayer online capture the flag turn based real time game using Django and Coffeescript.  I’m hoping to have a playable game today.  Stay tuned! #ld48

Capture

Huldo LD

Posted by
Thursday, April 25th, 2013 1:23 pm

Signing up for the competition seems to be in order. It’s my first one, so hopefully it goes swimmingly.

I’m going to use my old XNA framework engine, but I’ll utilize ANX to get rid of the framework dependency. I tested it a few times and it seems to work mostly fine, except for Rendertargets which leak memory bad and crash the whole thing! Apparently fullscreen support is missing as well which is a bit of a bummer.

I might just ship two versions then eh, when/if I get something done of course.

Here is the codebase for those interested.

Have a nice day.

Chevy Ray’s Codebase

Posted by (twitter: @xMrPhil)
Friday, August 24th, 2012 7:36 am

I took Chevy Ray‘s code that he released as part of his keynote for LD23 and put it up on github.  It is written for his FlashPunk engine.   I used it to make my LD23 entry Seeds of Destruction and it worked out really well for me.

PS Chevy if you happen to see this, I’ll happily transfer the repository to you.

Code on GitHub

Posted by (twitter: @attrition0)
Tuesday, April 24th, 2012 9:14 pm

While more for myself than anything else, I’ve placed my Unity project on GitHub. Maybe somone can make it.

Game: Earth Is Dying!

GitHub: TinyWorld

Till next time!

[cache: storing page]