Putting the 'role' back in role-playing games since 2002.
Donate to Codex
Good Old Games
  • Welcome to rpgcodex.net, a site dedicated to discussing computer based role-playing games in a free and open fashion. We're less strict than other forums, but please refer to the rules.

    "This message is awaiting moderator approval": All new users must pass through our moderation queue before they will be able to post normally. Until your account has "passed" your posts will only be visible to yourself (and moderators) until they are approved. Give us a week to get around to approving / deleting / ignoring your mundane opinion on crap before hassling us about it. Once you have passed the moderation period (think of it as a test), you will be able to post normally, just like all the other retards.

I shot myself in the foot and blew my whole leg. Actually...

desocupado

Magister
Joined
Nov 17, 2008
Messages
1,802
...it was machine gun fire.

START OF WALL OF TEXT, NOT NEEDED FOR HELP WITH THE ISSUE, SKIP IT IF YOU WANT

First shot was that I did not put all my units and abilities on paper before starting.
"Duh, that's so obvious, you moron!"
Well, it's obvious to me too. NOW, not before. :)

The second shot was that my functions got too big. They do not do a small simple thing, like it was preferable. The function to get a target, not only get's a target, but checks if it's a valid target, and ignores clicks on empty squares. At first, it looked good, and contained, but now I see it limits the functions usefulness. Halfway through where I am right now, I realized that, and started doing minimalistic functions, but the damage was done on the old ones.

The third shot was consequence of the first, I ended up not preparing the code for shit I would have to implement, so my attack code ended up a MESS, with checks for special cases, which would not be a problem if I had one or two, but noooooo... I had to give 2 weapons for almost all units, make some units ignore cover, make infantry do no damage to armored targets, ah, but anti-tank infantry DOES damage to armored targets, so what do I do?
A special check inside a special check? He, I did not do that, but solving the issue was not pretty.

Which leads me to the fourth and last shot, which is consequence of the second, my functions are not AI code friendly. The AI doesn't need to ignore clicks, nor it needs to receive a message when it tries to attack it's own units. But my attack and move functions already include that, so I need to write new functions for the AI.

Although I can barely look at the mess I made, I have right now, 8 working units, 1 pending to be implemented (troop transport), and it's not much trouble to implement more. The game already supports cover, I just need a way to put maps and cover info in it, shouldn't be a problem.

So, I want to finish it. And need help with the (not yet implemented) AI.

----------------------------END OF WALL OF TEXT----------------------------

How do I make an AI move in a vector direction?

Once the (future) AI picks one of it's units, it will move it in the direction of it's (the unit) preferred target. I can make the direction a vector, by subtracting the target position from the unit, and maybe make it a unitary vector (in math, it has no value, only a direction, has a special name, but I don't know it in english)

I want the AI to move in that direction but I can't think of a way to do that. Especially since the AI will not be able to choose exactly the direction of the vector, because of the square based map.

I'm using x + y = move_range, so I need to make the AI pick a position within range, but in the vector's direction, impassable terrain is not a problem, there's none.

Any thoughts? What those jap fuckers do to make their units move in those FFT or Tactics Ogre games, because that's exactly what I need, movement-wise.
 

Deneidez

Educated
Joined
Oct 17, 2010
Messages
75
Location
Finland, Joensuu
Re: I shot myself in the foot and blew my whole leg. Actuall

desocupado said:
How do I make an AI move in a vector direction?

Once the (future) AI picks one of it's units, it will move it in the direction of it's (the unit) preferred target. I can make the direction a vector, by subtracting the target position from the unit, and maybe make it a unitary vector (in math, it has no value, only a direction, has a special name, but I don't know it in english)

I want the AI to move in that direction but I can't think of a way to do that. Especially since the AI will not be able to choose exactly the direction of the vector, because of the square based map.

I'm using x + y = move_range, so I need to make the AI pick a position within range, but in the vector's direction, impassable terrain is not a problem, there's none.
Err... You only need point A and point B. If you want to use vectors somehow, you can always check the point where movement should start and end. And because its square based map you could use A*(I know its not so good when there is no impassable terrain, but when there will be you will be able to modify it easily to take those into account too.).
http://en.wikipedia.org/wiki/A*

Or you could just use simple line drawing algorithm. (There are some better ones, but I think A* would be simpler and more useful.)
http://en.wikipedia.org/wiki/Line_drawing_algorithm
 

desocupado

Magister
Joined
Nov 17, 2008
Messages
1,802
It's not that I want to use vectors, they were my first (and only) thought, didn't know about any other way. Gonna read the links.

EDIT: Made an algorithm myself, but can't make the program walk in diagonals. It only walks in straight lines. Fuck me sideways...
 

Shemar

Educated
Joined
Oct 16, 2010
Messages
260
If I am reading correct and your movement is in squares then you have the simple solution (pseudocode):

if (Xe>Xs) X=Xs+1
else if (Xe<Xs) X=Xs-1
else X=Xs

if (Ye>Ys) Y=Ys+1
else if (Ye<Ys) Y=Ys-1
else Y=Ys

With Xs,Ys the unit's current square, Xe,Ye the unit's desired end position and X,Y the next step square.

You can put that in a loop that checks for Xs=Xe and Ys=Ye with Xs=X; Ys=Y at the end of the loop and you have basic movement.

Of course that is not straight, it will first do all the diagonals of the movement and then the straight part, but it is using the least amount of movement squares.

To emulate more straight movement on a square grid is a bit more complicated, but I have C++ code that does it. PM me is you are interested.
 

bonescraper

Guest
Shemar said:
If I am reading correct and your movement is in squares then you have the simple solution (pseudocode):

if (Xe>Xs) X=Xs+1
else if (Xe<Xs) X=Xs-1
else X=Xs

if (Ye>Ys) Y=Ys+1
else if (Ye<Ys) Y=Ys-1
else Y=Ys

With Xs,Ys the unit's current square, Xe,Ye the unit's desired end position and X,Y the next step square.

You can put that in a loop that checks for Xs=Xe and Ys=Ye with Xs=X; Ys=Y at the end of the loop and you have basic movement.

Of course that is not straight, it will first do all the diagonals of the movement and then the straight part, but it is using the least amount of movement squares.

To emulate more straight movement on a square grid is a bit more complicated, but I have C++ code that does it. PM me is you are interested.
:what:
 

SCO

Arcane
In My Safe Space
Joined
Feb 3, 2009
Messages
16,320
Shadorwun: Hong Kong
bonescraper said:
Shemar said:
If I am reading correct and your movement is in squares then you have the simple solution (pseudocode):

if (Xe>Xs) X=Xs+1
else if (Xe<Xs) X=Xs-1
else X=Xs

if (Ye>Ys) Y=Ys+1
else if (Ye<Ys) Y=Ys-1
else Y=Ys

With Xs,Ys the unit's current square, Xe,Ye the unit's desired end position and X,Y the next step square.

You can put that in a loop that checks for Xs=Xe and Ys=Ye with Xs=X; Ys=Y at the end of the loop and you have basic movement.

Of course that is not straight, it will first do all the diagonals of the movement and then the straight part, but it is using the least amount of movement squares.

To emulate more straight movement on a square grid is a bit more complicated, but I have C++ code that does it. PM me is you are interested.
:what:

Yeah baby this is what programming is


Recognize your ignorance peasant!
 

desocupado

Magister
Joined
Nov 17, 2008
Messages
1,802
Shemar said:
If I am reading correct and your movement is in squares then you have the simple solution (pseudocode):

if (Xe>Xs) X=Xs+1
else if (Xe<Xs) X=Xs-1
else X=Xs

if (Ye>Ys) Y=Ys+1
else if (Ye<Ys) Y=Ys-1
else Y=Ys

With Xs,Ys the unit's current square, Xe,Ye the unit's desired end position and X,Y the next step square.

You can put that in a loop that checks for Xs=Xe and Ys=Ye with Xs=X; Ys=Y at the end of the loop and you have basic movement.

Of course that is not straight, it will first do all the diagonals of the movement and then the straight part, but it is using the least amount of movement squares.

To emulate more straight movement on a square grid is a bit more complicated, but I have C++ code that does it. PM me is you are interested.

That's beautiful, thank you.
 

Shemar

Educated
Joined
Oct 16, 2010
Messages
260
SCO said:
He has two useless branches there. Quick question if something is not smaller or larger than something else what is it?

Undefined... if you are using C/C++ or any other language that does not auto-initialize variables. Granted, inside the proposed loop the "else X=Xs" branches are useless (assuming one time initialization), but as independent code used to take Start/End coordinates and give next step, without assumption as to the prior value of X,Y, they are necessary.
 

Raghar

Arcane
Vatnik
Joined
Jul 16, 2009
Messages
22,791
Re OP.

Movement on a map isn't AI. When you know a target you simply take a position from a list of possible positions and place a piece on that position. Look at check board, or Go board, and play with it a little.

Do you want to draw movement, or place unit somewhere? These are two different actions.

You might like to look at a flooding algorithm. While it's deathly slow it's intuitive and working. MaxUnitMovement^2 * units that's still quite fast when max movement is 10.

Just you wait when you arrive at a problem when one unit blocks movement of another unit.

make infantry do no damage to armored targets, ah, but anti-tank infantry DOES damage to armored targets
A generic weapon object compared to a generic armor object?

First shot was that I did not put all my units and abilities on paper before starting.
I talked about design document...
 

desocupado

Magister
Joined
Nov 17, 2008
Messages
1,802
Raghar said:
Re OP.

1 - Movement on a map isn't AI. When you know a target you simply take a position from a list of possible positions and place a piece on that position. Look at check board, or Go board, and play with it a little.

Do you want to draw movement, or place unit somewhere? These are two different actions.

You might like to look at a flooding algorithm. While it's deathly slow it's intuitive and working. MaxUnitMovement^2 * units that's still quite fast when max movement is 10.

Just you wait when you arrive at a problem when one unit blocks movement of another unit.

make infantry do no damage to armored targets, ah, but anti-tank infantry DOES damage to armored targets
2 - A generic weapon object compared to a generic armor object?

First shot was that I did not put all my units and abilities on paper before starting.
3 - I talked about design document...

1 - Well, the algorithm will look at all the players units, and search for the preferred target, and then move in their direction. If within weapon range it will cease movement and proceed to attack. If there's cover it should get inside cover before attacking, even if already inside weapon range.

But that's all theory, I haven't implemented that. I'm kinda demotivated facing the mistakes I made, and the difficulty of the project. No promises, but I will try to end it. So far, the algorithm finds it's correct target and moves in it's direction, although it's not using diagonals (I already identified the why of that, and will try to implement the code the guy above suggested, it should correct it.) But it ceases the movement in the wrong range, and I have no clue on the why is that. The attack/cover is also pending.

2 - I'm doing this thing to actually learn the stuff. I'm not a full fledged programmer, not even by a long shot, so my attempt at class implementation didn't work out. I think I learned something in the process, though.

3 - Yes, you did, and I should have made one.
 

Aikanaro

Liturgist
Joined
Feb 3, 2004
Messages
142
On the wall of text:
Don't fall into the trap of thinking that you're stuck with shit code forever and ever. Don't just leave it to grow worse - rewrite it, or you're not going to make it much further. Break the megafunctions into smaller ones, rewrite the attack code to be generalised. It might feel like a lot of work, but this sort of rewriting is common and a lot faster and easier than not doing it in the long run.

It probably won't even take that much time because now you know what you need to do.
 

SCO

Arcane
In My Safe Space
Joined
Feb 3, 2009
Messages
16,320
Shadorwun: Hong Kong
desocupado said:
SCO said:
He has two useless branches there. Quick question if something is not smaller or larger than something else what is it?

This is cool:
http://keithphw.freehostia.com/LineOfSi ... Sight.jnlp

Holy crap, that is, indeed, cool!

The path finding nodes in that are placed manually?

No it's all dynamic, there isn't even a grid, it autocreates it.
BTW the pathfinding is using A*.

I believe the code is here:
http://code.google.com/p/straightedge/
If you want to copy something. You'll have to adapt it, since you're using python, but that is good training anyway.

Edit: I should have said, the code is not there yet. Guy only created the project 32 hours ago, didn't commit yet.
 

ecliptic

Liturgist
Joined
Feb 11, 2003
Messages
915
Check out a book called Code Complete for good tips on how to program _well_. Most programming books just talk about how to write code in a specific language or environment, whereas this one actually tells you how to write maintainable, readable, and well written code.

Also, there's lots of game oriented path finding algorithm tutorials out there.
 

zenbitz

Scholar
Joined
Feb 2, 2009
Messages
295
Aikanaro said:
On the wall of text:
Don't fall into the trap of thinking that you're stuck with shit code forever and ever. Don't just leave it to grow worse - rewrite it, or you're not going to make it much further. Break the megafunctions into smaller ones, rewrite the attack code to be generalised. It might feel like a lot of work, but this sort of rewriting is common and a lot faster and easier than not doing it in the long run.

It probably won't even take that much time because now you know what you need to do.

I just wanted to quote this for emphasis. Programming IS rewriting bad code. Not just hacking the next hacky solution on to the previous pile of hacks.
 

As an Amazon Associate, rpgcodex.net earns from qualifying purchases.
Back
Top Bottom