Announcement

Collapse
No announcement yet.

E's Source Code attempts

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #76
    Originally posted by E
    Ok, I think I figured what you are saying and it compiles and it works EXCEPT I removed the
    Code:
    ((m_buyingResources[g] + m_collectingResources[g]) > 0)
    Not quite actual I told you that this is the condition to use instead of the condition you use in the CityStyle code. Of course this line has to be modified.

    Originally posted by E
    Not sure how to fit that back in... but here is the code and i did anothre compiled revision


    Code:
    	if(rec->GetNumNeedsCityGood() > 0) {
    		sint32 g;
    		bool found = false;
    		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
    			if((HasResource(rec->GetNeedsCityGoodIndex(g))) || m_buyingResources[rec->GetNeedsCityGoodIndex(g)])
    				found = true;
    					break;
    		}
    				if(!found)
    				return FALSE;
    	}
    Actual you have removed two lines instead of one line. (The closing brace had its own line.)

    And the code does not do what it is supposed to do. If in the array is just one good than it is ok, but if there are more goods in the list that are supposed to be checked these aren't checked. The break breaks the loop in its first iteration. So what did I tell you about brace-blocks?

    And one thing I should have done at first is to insist on the indention, the if(!found) is on the same level as the for loop.

    -Martin
    Civ2 military advisor: "No complaints, Sir!"

    Comment


    • #77
      so should I move the brace ahead of the break? does that keep it looping? or do I add another brace at the before the found=true and another closing brace. not sure about how that affects the looping. I have to relook brace-blocks...

      for ">" I need to add that line back in, but what is the value of that check regarding the code, not sure what its accomplishing.

      Oh and an english note "actually" is used not "actual" just caught that a few times, but no problem.
      Formerly known as "E" on Apolyton

      See me at Civfanatics.com

      Comment


      • #78
        Originally posted by E
        so should I move the brace ahead of the break? does that keep it looping? or do I add another brace at the before the found=true and another closing brace. not sure about how that affects the looping. I have to relook brace-blocks...
        I don't quite understand this. Here is a try:

        whatever;

        In c++ the above is a statement, look at the semicolon at the end of the line. If ypi put multipile statements into a block of braces you combine them to one statement.

        {whatever}

        So the stuff can be counted as statement as well.

        An "if" just executes the next statement if its condition is true.

        Originally posted by E
        for ">" I need to add that line back in, but what is the value of that check regarding the code, not sure what its accomplishing.
        There is a difference to your code, its cleaner, your code asumes that this m_buyingResources[rec->GetNeedsCityGoodIndex(g)] returns a BOOL, but it returns an integer. In that case it doesn't matter, because non-zero values are casted to TRUE and otherwise to FALSE. In my opinion you shouldn't mix up such things, even if there is like here no difference. If you start to mix up such things and your data represents indices then you get a problem, zero is a valid indix. In fact such a problem was in the game, the AI in the default game couldn't use entertainers, and you gotn't a warning for the first wonder in the database when someone else had nealy doen it.

        Its cleaner and in the case of doubt you save some bugs.

        Originally posted by E
        Oh and an english note "actually" is used not "actual" just caught that a few times, but no problem.
        Well, thanks for pointing it out. In fact I wasn't sure about it, but you are right it has to be an adverb.

        -Martin
        Civ2 military advisor: "No complaints, Sir!"

        Comment


        • #79
          Ok I did this:

          Code:
          	if(rec->GetNumNeedsCityGood() > 0) {
          		sint32 g;
          		bool found = false;
          		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
          			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0)
          			if((HasResource(rec->GetNeedsCityGoodIndex(g))) || m_buyingResources[rec->GetNeedsCityGoodIndex(g)])
          				found = true;
          		}	
          		if(!found)
          			return FALSE;
          	}

          and now its either good one OR good two. so I think I have CASE2 working...
          Formerly known as "E" on Apolyton

          See me at Civfanatics.com

          Comment


          • #80
            Originally posted by E
            Ok I did this:

            Code:
            	if(rec->GetNumNeedsCityGood() > 0) {
            		sint32 g;
            		bool found = false;
            		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
            			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0)
            			[b]if((HasResource(rec->GetNeedsCityGoodIndex(g))) || m_buyingResources[rec->GetNeedsCityGoodIndex(g)])[/b]
            				found = true;
            		}	
            		if(!found)
            			return FALSE;
            	}

            and now its either good one OR good two. so I think I have CASE2 working...
            Yes, indeed it does work. The indention looks fine. However it is still suboptimal. First the line in bold the quote was to be supposed to be removed. It just does the what the line does before, but with more steps.

            And this version of the loop cycles through all the entries of the NeedsCityGood array even if the first good is in the city. The equivalent section of the CityStyleOnly code breaks out the loop by a break; after seeting found to true. And these two statements are in a block of braces unified to one statement.

            -Martin
            Civ2 military advisor: "No complaints, Sir!"

            Comment


            • #81
              I did this but it doesnt work:

              Code:
              	if(rec->GetNumNeedsCityGood() > 0) {
              		sint32 g;
              		bool found = false;
              		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
              			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0)
              				found = true;
              				{break;}	
              		}
              
              		if(!found)
              			return FALSE;
              	}
              with the break it makes the first resource required but it also make that first resource the only one necessary. Not like how it worked in my code before where it was either one.

              However if I remove the break:
              Code:
              	if(rec->GetNumNeedsCityGood() > 0) {
              		sint32 g;
              		bool found = false;
              		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
              			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0)
              				found = true;
              		}
              
              		if(!found)
              			return FALSE;
              	}
              The code works. with my test i have the warrior requiring either cotton OR tobacco. one or the other not both. I know you said to add the break but it didnt let my code work any way I tried it. Is there a reason its necessary? because it works as intended
              Last edited by Ekmek; September 23, 2005, 22:37.
              Formerly known as "E" on Apolyton

              See me at Civfanatics.com

              Comment


              • #82
                Originally posted by E
                I did this but it doesnt work:

                Code:
                	if(rec->GetNumNeedsCityGood() > 0) {
                		sint32 g;
                		bool found = false;
                		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
                			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0)
                				found = true;
                				{break;}	
                		}
                
                		if(!found)
                			return FALSE;
                	}
                with the break it makes the first resource required but it also make that first resource the only one necessary. Not like how it worked in my code before where it was either one.
                Of course it does not work if you don't put the found to true set and the break into the same block, like it is done in your other code.

                -Martin
                Civ2 military advisor: "No complaints, Sir!"

                Comment


                • #83
                  Ok so I assume its to be like this:
                  Code:
                  	if(rec->GetNumNeedsCityGood() > 0) {
                  		sint32 g;
                  		bool found = false;
                  		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
                  			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0){
                  			found = true;
                  			break;
                  			}
                  		}
                  		if(!found)
                  			return FALSE;
                  	}

                  You said it would be easy to do NeedsCityGoodAll after this, where should I start? and as a question in case3 (&&) If I put cotton twice will it look at the same cotton twice or do you have to have two cottons. I want the latter, where you need two cottons because seventually I think this could be a good alternative to the outpost method used in AOM where you need increasinglevels of goods to research different techs.




                  And also I was trying to play with another part of the code, surprising it compiled each time. Actually that was unfortunate because my code never worked so I couldnt find why. But I was messing with this piece:

                  Code:
                  double CityData::ProcessFood(sint32 food) const
                  {
                  	///////////////////////////////////////////////
                  	// Apply building boni
                  	double foodBonus;
                  	buildingutil_GetFoodPercent(GetEffectiveBuildings(), foodBonus);
                  	double grossFood = static_cast(food) * foodBonus;
                  
                  	///////////////////////////////////////////////
                  	// Apply feat boni
                  	// No feat boni. Something to add here
                  	
                  	///////////////////////////////////////////////
                  	// Apply wonder boni
                  	grossFood += grossFood * (wonderutil_GetIncreaseFoodAllCities(
                  	                   g_player[m_owner]->m_builtWonders) / 100.0);
                  
                  	///////////////////////////////////////////////
                  	// Add food from citizen
                  	// No food from citizen. Maybe something to add.
                  [b]
                  	///////////////////////////////////////////////
                  	//Add If City has a good checks if it has a Food Percent bonus and give to city
                  	//
                  //	const ResourceRecord *rec = g_theResourceDB->GetFoodPercent();
                  //				rec->GetFoodPercent(p) > 0
                  //						found = true;
                  //			}
                  //		}
                  //	}
                  		grossFood += grossFood * (HasResource()->g_theResourceDB->GetFoodPercent(p)) ; [/b]
                  	
                  	
                  	return grossFood;
                  }
                  that was my last attempt but essentially I put the FoodPercent flag from the buildings into the goods.cdb and I wanted to mmake something where if a city has a resource and that resource has the GetFood attribute than that would be added to the grossfood. Basically like a granery but you get if itsin the radius or if you are buying it (I took out the m_buying previously to just try the HasResource). I think this would be great code to add so its like trading food, but really we can have wheat made in one city and sent to another so the recipient gets the food bonus (like how the US with the midwest is not populous but sends food to NYC and LA)
                  Formerly known as "E" on Apolyton

                  See me at Civfanatics.com

                  Comment


                  • #84
                    Originally posted by E
                    Ok so I assume its to be like this:
                    Code:
                    	if(rec->GetNumNeedsCityGood() > 0) {
                    		sint32 g;
                    		bool found = false;
                    		for(g = 0; g < rec->GetNumNeedsCityGood(); g++) {
                    			if((m_buyingResources[rec->GetNeedsCityGoodIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodIndex(g)]) > 0){
                    			found = true;
                    			break;
                    			}
                    		}
                    		if(!found)
                    			return FALSE;
                    	}
                    You are right, this is what I had in mind, you only need to get the indention right and you crossed the finish line.

                    Originally posted by E
                    You said it would be easy to do NeedsCityGoodAll after this, where should I start?
                    It is simple, use this code above as a template, the only ignificant difference is that you look whether the city doesn't collect or doesn't receive any goods. So again count the collected and incoming goods, if the result is zero you have a reason to return false.

                    Originally posted by E
                    and as a question in case3 (&&) If I put cotton twice will it look at the same cotton twice or do you have to have two cottons. I want the latter, where you need two cottons because seventually I think this could be a good alternative to the outpost method used in AOM where you need increasinglevels of goods to research different techs.
                    It doesn't count the number of cotton goods the city has, if a good is twice in the list then you have two successfull tests and the method may return TRUE. Of course you have enough pieces of information to do it.

                    Originally posted by E
                    And also I was trying to play with another part of the code, surprising it compiled each time. Actually that was unfortunate because my code never worked so I couldnt find why. But I was messing with this piece:

                    Code:
                    double CityData::ProcessFood(sint32 food) const
                    {
                    	///////////////////////////////////////////////
                    	// Apply building boni
                    	double foodBonus;
                    	buildingutil_GetFoodPercent(GetEffectiveBuildings(), foodBonus);
                    	double grossFood = static_cast(food) * foodBonus;
                    
                    	///////////////////////////////////////////////
                    	// Apply feat boni
                    	// No feat boni. Something to add here
                    	
                    	///////////////////////////////////////////////
                    	// Apply wonder boni
                    	grossFood += grossFood * (wonderutil_GetIncreaseFoodAllCities(
                    	                   g_player[m_owner]->m_builtWonders) / 100.0);
                    
                    	///////////////////////////////////////////////
                    	// Add food from citizen
                    	// No food from citizen. Maybe something to add.
                    [b]
                    	///////////////////////////////////////////////
                    	//Add If City has a good checks if it has a Food Percent bonus and give to city
                    	//
                    //	const ResourceRecord *rec = g_theResourceDB->GetFoodPercent();
                    //				rec->GetFoodPercent(p) > 0
                    //						found = true;
                    //			}
                    //		}
                    //	}
                    		grossFood += grossFood * (HasResource()->g_theResourceDB->GetFoodPercent(p)) ; [/b]
                    	
                    	
                    	return grossFood;
                    }
                    that was my last attempt but essentially I put the FoodPercent flag from the buildings into the goods.cdb and I wanted to mmake something where if a city has a resource and that resource has the GetFood attribute than that would be added to the grossfood. Basically like a granery but you get if itsin the radius or if you are buying it (I took out the m_buying previously to just try the HasResource). I think this would be great code to add so its like trading food, but really we can have wheat made in one city and sent to another so the recipient gets the food bonus (like how the US with the midwest is not populous but sends food to NYC and LA)
                    In the first place I have no idea what this food percentage flag is supposed to do since we have already a good food terrain bonus.

                    Now to the other thing, what did I say about the preprosessor and about such preprocessor directives:

                    Code:
                    #if defined(BLOCK_A)
                        Some Code A
                    #else
                        Some Code B
                    #endif
                    So again what does the preprocessor if BLOCK_A is defined. And what does it if BLOCK_B is not defined?

                    And of course the answer to this question has to be posted and can be found in some of the threads here.

                    -Martin
                    Civ2 military advisor: "No complaints, Sir!"

                    Comment


                    • #85
                      Originally posted by Martin Gühmann

                      You are right, this is what I had in mind, you only need to get the indention right and you crossed the finish line.
                      I'll clean it up and post it along with cdb changes then go on to the next stuff.



                      It is simple, use this code above as a template, the only significant difference is that you look whether the city doesn't collect or doesn't receive any goods. So again count the collected and incoming goods, if the result is zero you have a reason to return false.
                      A little unclear here, do I change the NumNeedsCityGood part or the line with ">". For the > I thought being zero already returned false? How does returning zero get the code to look for more than one instance of the same attribute and require both?



                      It doesn't count the number of cotton goods the city has, if a good is twice in the list then you have two successfull tests and the method may return TRUE. Of course you have enough pieces of information to do it.
                      Just to clarify that my current code is enough to check for multiple cottons to require 1 or more of the same good to build. Would I have to add a line? Do I need to make NeedCityGood1 a flag NeedCityGood2 a flag? I think that would be slow but thats all I can think of right now.



                      Now to the other thing, what did I say about the preprosessor and about such preprocessor directives:

                      Code:
                      #if defined(BLOCK_A)
                          Some Code A
                      #else
                          Some Code B
                      #endif
                      So again what does the preprocessor if BLOCK_A is defined. And what does it if BLOCK_B is not defined?

                      And of course the answer to this question has to be posted and can be found in some of the threads here.

                      -Martin
                      Are you talking about adding a preprocessor #if defined etc or should I use this format for the Case3(&&) code? As for preprocessor my code comes after the CultureOnly so it should fit in the the previous preprocessor stuff (then again I thought we removed most of those)


                      In the first place I have no idea what this food percentage flag is supposed to do since we have already a good food terrain bonus.
                      the good food terrain bonus on gives it to the city that hasthis food in the radius. If we sell a good to another city they don't get this bonus, but if I add this flag than a city that buys wheat for example gets a 10 percent food increase. then if the city is getting cattle, fish, etc it would represent a diverse and advance food importation system and the city could grow very big, it would simultate new york city or ancient rome better instead of just having the city with most farms around it be the biggest which the American midwest, and most farm communities, are not.
                      Formerly known as "E" on Apolyton

                      See me at Civfanatics.com

                      Comment


                      • #86
                        Originally posted by E
                        A little unclear here, do I change the NumNeedsCityGood part or the line with ">". For the > I thought being zero already returned false? How does returning zero get the code to look for more than one instance of the same attribute and require both?
                        Of course you have to modify the NumNeedsCityGood part, because your flag isn't called NeedsCityGood, but this isn't a significant change just some stupid copy & paste & modify work. And not really worth to mention. And should be obviously.

                        Originally posted by E
                        Just to clarify that my current code is enough to check for multiple cottons to require 1 or more of the same good to build. Would I have to add a line? Do I need to make NeedCityGood1 a flag NeedCityGood2 a flag? I think that would be slow but thats all I can think of right now.
                        Your code checks whether there is at least one good of the certain type in the city, and not at least two or at least three.

                        If you want something like for that you need at least two or at least three you have to do other code. But I don't think this is desirable, you hardly have more than one good of a certain in a city.

                        Originally posted by E
                        Are you talking about adding a preprocessor #if defined etc or should I use this format for the Case3(&&) code? As for preprocessor my code comes after the CultureOnly so it should fit in the the previous preprocessor stuff (then again I thought we removed most of those)
                        We only removed the ACTIVISION_ORIGINAL stuff and nothing else. And for the first questions in the quote: I was not talking about case three, I did it before. This has to do with your code that did compile even if it wouldn't compile, and I brought up the preprocessor. The rest is left to your brain to fill in.

                        Originally posted by E
                        the good food terrain bonus on gives it to the city that hasthis food in the radius. If we sell a good to another city they don't get this bonus, but if I add this flag than a city that buys wheat for example gets a 10 percent food increase. then if the city is getting cattle, fish, etc it would represent a diverse and advance food importation system and the city could grow very big, it would simultate new york city or ancient rome better instead of just having the city with most farms around it be the biggest which the American midwest, and most farm communities, are not.
                        From a logical point of view it should then reduce food in the source city.

                        -Martin
                        Civ2 military advisor: "No complaints, Sir!"

                        Comment


                        • #87
                          Okay for case3 It compiled and worked:

                          Code:
                          	if(rec->GetNumNeedsCityGoodAll() > 0) {
                          		sint32 g;
                          		bool found = false;
                          		for(g = 0; g < rec->GetNumNeedsCityGoodAll(); g++) {
                          			if((m_buyingResources[rec->GetNeedsCityGoodAllIndex(g)] + m_collectingResources[rec->GetNeedsCityGoodAllIndex(g)]) == 0){
                          				found = true;
                          				break;
                          			}
                          		}
                          		if(found)
                          			return FALSE;
                          	}
                          any optimization needed?

                          I mess with the other code soon.
                          Formerly known as "E" on Apolyton

                          See me at Civfanatics.com

                          Comment


                          • #88
                            Originally posted by E
                            Okay for case3 It compiled and worked:
                            Yes, indeed it does work.

                            Originally posted by E
                            any optimization needed?
                            Yes, there is no need to break out the loop, instead of the break in the loop you can return FALSE; immediately. And therefore no need for the found variable.

                            -Martin
                            Civ2 military advisor: "No complaints, Sir!"

                            Comment


                            • #89
                              ok I'll correct it soon. but another question, can I use this template for a NeedsCityGoodAnywhere?

                              basically a code where if any city the player has is collecting or buying the good then the unit become available in any city. I might have missed it but I didn't see anything in the code that looks at all the cites or the enitre civ in citydata. can i use player.cpp, because i found this:

                              Code:
                              Player::Player(CivArchive &archive)
                              
                              	m_all_cities = new UnitDynamicArray;
                              Last edited by Ekmek; September 29, 2005, 00:04.
                              Formerly known as "E" on Apolyton

                              See me at Civfanatics.com

                              Comment


                              • #90
                                Originally posted by E
                                ok I'll correct it soon.
                                Looks fine. And you the best is you got Case1 for free.

                                Originally posted by E
                                but another question, can I use this template for a NeedsCityGoodAnywhere?
                                Not quite, as there is no elegant way to figure out how many goods you have available empirewide. Therefore you have to put it into a loop that cycles through all your cities. Probably something like HappyInc is easier to implement. By the way I would see something like this for CityStyles as well.

                                Originally posted by E
                                basically a code where if any city the player has is collecting or buying the good then the unit become available in any city. I might have missed it but I didn't see anything in the code that looks at all the cites or the enitre civ in citydata. can i use player.cpp, because i found this:

                                Code:
                                Player::Player(CivArchive &archive)
                                
                                	m_all_cities = new UnitDynamicArray;
                                Well you are right that you need this m_all_cities array for cycling through all the cities. I think below the Player::CanBuildUnit you find an instance of use, basicly you have to put the good code into this loop, and you have to modify the condition, as the two Resource objects of the CityData object are private, therefore you have to encapsulate the condition into a boolean method. Something that you already wanted to do but with just one instance of the code superflous was. The problem is that we should avoid code duplication. However we should come to this point of finetuning later.

                                -Martin
                                Civ2 military advisor: "No complaints, Sir!"

                                Comment

                                Working...
                                X