C++ help, please!

Thanks for helping out. :) I'm just an amateur programmer, so you'll have to forgive if these are half-assed questions! :D

Just above the part where I set those two ints, I have two ifs that must be true for the code to proceed:
Code:
if(!GC.getBuildingInfo((BuildingTypes)iI).isRequiredInputBonus((BonusTypes)iJ))
	{
		continue;
	}
	
if(!GC.getBuildingInfo((BuildingTypes)iI).isAndRequiredInputBonus((BonusTypes)iJ))
	{
		continue;
	}

Is this what you meant? I guess I don't fully understand how looping works... but it seems to me you're saying that iJ is only one loop. Would iK be a second loop (over the same resources, basically)?
 
Thanks for helping out. :) I'm just an amateur programmer, so you'll have to forgive if these are half-assed questions! :D

Just above the part where I set those two ints, I have two ifs that must be true for the code to proceed:
Code:
if(!GC.getBuildingInfo((BuildingTypes)iI).isRequiredInputBonus((BonusTypes)iJ))
	{
		continue;
	}
	
if(!GC.getBuildingInfo((BuildingTypes)iI).isAndRequiredInputBonus((BonusTypes)iJ))
	{
		continue;
	}

Is this what you meant? I guess I don't fully understand how looping works... but it seems to me you're saying that iJ is only one loop. Would iK be a second loop (over the same resources, basically)?

I'm just a professional programmer, so I sometimes forget that what is obvious to me may not be obvious to others. I also sometimes gloss over things that are important because they are so obvious to me. I also tend use the term "resources" when in the code they are called "bonuses".

The problem is that it is checking the same bonus in both statements. You will only get to run the rest of the statements after this in the loop if neither of the two continue statements happens. That is, it will only run the rest if one specific bonus causes both isRequiredInputBonus() and isAndRequiredInputBonus() to return true. If that doesn't happen it will hit a continue and go back to the start of the loop, increment iJ to the next value, and try again.

How often do you list the same bonus for both of those in the XML for one building? Hopefully never.

You need to check each of the two (the regular required bonus and the "and" required bonus) individually to find out what they are. This is done most easily via two separate loops over the bonus types, although you can do it in one. Then proceed with the checks to see if the city has access to the two required bonus types. Alternatively you can also merge the check to see if the city has the required bonus with the check to see if the bonus is required, then you can store a true/false value for each of the two (which is what I was previously suggesting). Then, in either case, if the building has everything it needs you do what the rest of the loop was doing (I didn't look at the rest - it may require some tweaks there too).

This may not be incredibly clear either...

As an aside, this could be made to use half as much memory if the "and" required bonus was stored in the list that already exists for the reqular one - two bonuses would be set to true in the one list, instead of two lists that have one set to true in each. It isn't any more difficult to program the checks, although you can't program it to bail out of the loop once you've found the requirement(s) unless you also store a count. This is the advantage to using an XML tag that can takes multiple sub-tags instead of two separate top-level (inside the building definition) tags, like the "BuildingClassNeededs" or "ProductionTraits" type tags. Doing this would probably break the existing XML file entries, though.
 
It would really help to have an English description of what you want to have happen. I get your intention of the first part: skip buildings that don't have the needed resources available. GE is correct that your logic is flawed. Let's start with some psuedo-code:

Code:
for each building
    ...
    for each bonus
        if building requires bonus
            if city doesn't have resource, skip building
        if building requires and bonus
            if city doesn't have resource, skip building
    process building if all bonuses it requires are found

Is this what you're looking to have happen? I only read the first part of the code since there are no comments, and it can be tough to figure out what you want to have happen especially if it's coded incorrectly. That's not a dis, just a prodding to put comments in your code so we can help you do what you want to do instead of what we think you want to do. ;)

I noticed one thing. You use ! in a way that I would never use it. It's okay as long as you know what you're doing, but I honestly do not know which binds higher: the ! operator or the boolean operators (< in this case). I suspect the boolean one and you will get what you wanted, but if the ! binds higher you will definitely not get what you want. I would change

if(!getNumActiveBuilding((BuildingTypes)iI) > 0)​

to

if(getNumActiveBuilding((BuildingTypes)iI) <= 0)​

or even

if(getNumActiveBuilding((BuildingTypes)iI) != 0)​

since getNumActiveBuilding() cannot won't a negative value.
 
My apologies for getting back to this so late. I have been swamped with work and travel in the past weeks. Thanks for your replies, they are much appreciated! :)

Code:
Code:
for each building
    ...
    for each bonus
        if building requires bonus
            if city doesn't have resource, skip building
        if building requires and bonus
            if city doesn't have resource, skip building
    process building if all bonuses it requires are found

Yes. That is exactly what I want to happen. The code base I work off did this, but only taking a single bonus into account. I am basically trying to use an 'AND'-type method (similar to the AND requirement for bonuses in unit building).

I noticed one thing. You use ! in a way that I would never use it. It's okay as long as you know what you're doing, but I honestly do not know which binds higher: the ! operator or the boolean operators (< in this case). I suspect the boolean one and you will get what you wanted, but if the ! binds higher you will definitely not get what you want.

Don't blame me! :p I'm just trying to copy the existing code, made by TheLopez. I have no idea what you just stated, but if you advise me to make corrections, I will certainly do so.

As an aside, this could be made to use half as much memory if the "and" required bonus was stored in the list that already exists for the reqular one - two bonuses would be set to true in the one list, instead of two lists that have one set to true in each. It isn't any more difficult to program the checks, although you can't program it to bail out of the loop once you've found the requirement(s) unless you also store a count. This is the advantage to using an XML tag that can takes multiple sub-tags instead of two separate top-level (inside the building definition) tags, like the "BuildingClassNeededs" or "ProductionTraits" type tags. Doing this would probably break the existing XML file entries, though.

This was actually my intent right from the beginning. However, a few mistypes in the C code actually made it easier to implement a new top-level tag. I had no idea this would cost extra memory, though, so I'm probably going to redo the xml from scratch if that makes the C even easier?

I'll definitely need more help to get this going, though.
 
Back
Top Bottom