Sub's coding pool party

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
So every tile has

std::list<Unit*> UnitsOnTile

It's a collection of pointers to units. That are on the tile. It's not even doing anything at the moment other than being used to retrieve a unit list occasionally. As far as I can tell units are only added at the proper times. Yet when I step through the code, it sometimes gives me some absurdly high number of objects in containers that aren't empty with garbage values. When I go to remove items from the container, it also seems to not actually remove, as far as I can tell from stepping through the code. The values will be correct before the line that removes them, and after the line that removes them, the container is still the same size, except that the element removed is just replaced with garbage values. I've been trying to figure this out all day, it's been driving me mad. I'm just calling UnitsOnTile.remove(UnitToRemove), unit to remove being a pointer that points to the same object as one of the items in the list. I'm going to lose my hair from programming.
 
Member
✔️ HL Verified
🌟 Senior Member
Joined
Oct 16, 2006
Messages
379
Best answers
0
Location
the Netherlands
So every tile has

std::list<Unit*> UnitsOnTile

It's a collection of pointers to units. That are on the tile. It's not even doing anything at the moment other than being used to retrieve a unit list occasionally. As far as I can tell units are only added at the proper times. Yet when I step through the code, it sometimes gives me some absurdly high number of objects in containers that aren't empty with garbage values. When I go to remove items from the container, it also seems to not actually remove, as far as I can tell from stepping through the code. The values will be correct before the line that removes them, and after the line that removes them, the container is still the same size, except that the element removed is just replaced with garbage values. I've been trying to figure this out all day, it's been driving me mad. I'm just calling UnitsOnTile.remove(UnitToRemove), unit to remove being a pointer that points to the same object as one of the items in the list. I'm going to lose my hair from programming.
Are you sure you don't add them multiple times? Have you tried debugging the delete line, then break and see what's actually in that collection?
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Yeah, I think that's what happened.

I have an object which handles and keeps track of which unit is currently selected. When a user clicks on a tile, the game finds which tile was clicked, and then if there's a unit on the tile, the selection object gets handed that pointer. When a unit moves / gets destroyed, the pointer on the tile is removed. I'm thinking maybe I messed up and somehow tied that pointer to the tile pointer, and when it gets destroyed the game gets messed up? It's the only explanation I can come up with.

I replaced the pointer on the tile with three ints, one which contains the objects position in an array, one which contains the player index that holds the unit, and the last contains a unique id that's given to each unit. This strikes me as a rather terrible idea as I think about it, though, since I'll have to refind every units position in the array whenever a unit gets destroyed.
 
Last edited:

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
All right, everything now works and I'm happy. Can now build units and have multiple units on a tile.



I wrote a function which updated the unit index when a unit is destroyed. I don't really care about performance too much since it's a turn based game, and looping over a few units when a unit is destroyed hardly seems like a huge sin. City screen still needs some work, but I'm going to work on something else next... maybe a tile improvement system?
 
Just a nice person :)
✔️ HL Verified
Joined
Jul 17, 2009
Messages
262
Best answers
0
Location
The Netherlands / HOLLAND!
Great that you got it working :D

It's an interesting read.
I think what you said is basically what I am about the say, but I throw it in anyway: quadtree.

Ignore below if you did this..lawl

If you have click events, try to box your controls into containers (like panels for example) to keep your check loops shorter.
In this case, whenever a user clicks somewhere this happens:

  1. Check if mouse position is in between the bounds of a panel that is currently active.
  2. Check if the mouse position is in the bounds of a control in that panel.
  3. Is control clickable? Execute click.

Illustrated:
http://en.wikipedia.org/wiki/Quadtree
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Ah, yeah, thanks for the comment. I was talking about something different I think, but quadtrees are awesome, I do need to look into how to implement them one day.

I don't think I'm going to be using them with this though. I'm not too worried about performance since nothing is in real time. But right now what it does every frame is this --

- Checks the game state. So for example, if the game state is city screen it goes to a loop that checks input for any widgets that appear in the city screen and won't check anything for a state that we're not in.
- For any gui object that's clickable which is on the screen and belongs to a panel, it'll only check for input on them when the mouse is in the bounds of that panel. So if there's two panels and the mouse is inside panel 1, it'll only check for input on panel 1.

That seems more than good enough. I know the fps from the screen isn't that high for what it is, but it goes to 1000ish when I compile it in release mode.
 
Just a nice person :)
✔️ HL Verified
Joined
Jul 17, 2009
Messages
262
Best answers
0
Location
The Netherlands / HOLLAND!
Alright well, you did a pretty amazing job so far anyway.
The current click handler is indeed more than you'll need for your game and
what you have now is very close to the functionality of a quadtree.

Looks like it will turn out just fine. GG Sub!
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Thanks. Working on tile improvement system right now, wondering if this would work...

Cities can be set to produce either buildings or workforce points (needs a better name, but bear with me).


  • Buildings are the typical civilization thing, they provide nice upgrades for the city such as improving the food rate and cost maintenance every turn
  • Workforce points can be used to build three things
    • Tile improvements such as farms.
    • World wonders would no longer function as a building, but rather as a form tile improvement
    • All units would be produced out of this pool, you'd be able to construct them anywhere within your borders, kind of like warping in protoss units in Starcraft 2
  • Workforce wouldn't be a limitless resource, it'd draw from the population of cities
  • Workforce generated on landmass a can only be used on landmass a and not another continent otherwise everything would be overpowered

This would solve a lot of micromanagement, I think. Wouldn't even need rally points if you can just construct units on the map where you want. The main problem that I see with this is that cities have 3 resources, really -- Food, Production, and Gold/Science (serves as 1 resource). This might make food overvalued and diminish the importance of production, which isn't what I want. Although maybe not, the amount of workforce you can accrue per turn would still be tied to production.
 
Last edited:

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Tile improvements necessitate that I know how I'm going to eventually add shadows to the tiles... so I have to get that working first. Found out how to shade tiles with color, and it's basically free performance wise, so that's pretty nice.



Now, to write something that will determine what needs shadows.

edit: So it's free performance wise if I'm coloring the whole tile, but if I just need to do half the tile, I'm not sure how to do that without drawing the tile twice. It'd probably cut off like 400 FPS from 1000, which I guess is fine, but that's on my desktop, which is pretty good.

edit 2: All right, so I think I've figured out how to do it without the performance hit, or at least without a noticeable one. I've also doubled the FPS, it now gets 2000 on my machine... Which is kind of sad, honestly.

I read an article about a programmer who managed to get like a 2000% increase in speed for a product his company was working on. When his manager found out, he said that they'd have to artificially slow the program down and roll the performance increase out over the course of months. A huge increase in performance like that would make them look incompetent, people would ask how they released a product that could be improved so much.
 
Last edited:

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Shadows done! I'm not sure if this makes the game look better, but I'm very happy with the code I wrote to make this happen.

So this is supposed to be the logic to the light





There might be some rare cases where it ****s up, but I'll fix them as I find them.

Maybe it'd look better if I only had one direction for the light source and I went much more subtle?

edit: This one is a bit more subtle, looks better I think
http://i.imgur.com/EPDnG8C.jpg
 
Last edited:
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
I gotta say this have has come quite far from what it was once. Keep up the good work. If you need someone to test anything, hit me up.
 
Cunning as Zeus
Banned
✔️ HL Verified
💻 Oldtimer
Joined
Nov 23, 2003
Messages
6,079
Best answers
0
Last one looks better. The first one with super dark tiles looks like the sun is constantly rising or setting (as it'd have to be relatively low in the sky not to cast light onto the other side), unless each of the hills is actually a ginormous mountain.

Side note: Why the hell is this shit also double posting?
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Going to mess around with it some more today, I think it's very jarring to look at. I'm going to try to use more shades (right now there are only 3) and limit the direction of light. I think it'd also be more logical if flat areas were more lit.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Unfortunately I'm not sure how to make the edges less sharp.

Changed the shadows a bit and made the grid lines semi-transparent so they're not as harsh. It's still pretty similar to how it was before though.



I think I should probably move on, although I'm still not really happy with it.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Why didn't I know that resizing a vector changes the address for all the values in the vector? I just realized this, that's what was causing my code to **** up a few days ago. I should probably reread the learn c++ programming book that I have.

I'm guessing that elements in a vector need to be located next to each other in memory like a regular array?
 
Last edited:
Just a nice person :)
✔️ HL Verified
Joined
Jul 17, 2009
Messages
262
Best answers
0
Location
The Netherlands / HOLLAND!
Yea, I guess vectors have this because they are value-types. Meaning that whenever you "change" a value, you actually get a copy of the vectory with the changed value.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
This didn't strike me as that hard when I first set out to do it, but I'm struggling to come up with a way to draw borders around a nations territory. I'm going for something like this



What I came up with doesn't quite work. I don't think I can accurately explain what the code tries to do, but it basically scans the tiles on the screen, and then when it finds a tile that is owned by someone, it starts a new line segment. it then looks at all the neighbors, and goes to neighboring tiles in a clockwise motion drawing the segment depending on if those neighboring tiles have neighbors owned by a different person. To ensure that we're not drawing on tiles that we've already done, there's a list of tiles that were accounted for. There are a number of problems with this approach, and I think I might need to go back to the drawing board.

This is the result

 

Users who are viewing this thread

Top Bottom