Announcement

Collapse
No announcement yet.

Ushhan's Source Code Attempt

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

  • Ushhan's Source Code Attempt

    I was trying to move a human unit in the game using code.
    I can now move a unit in the game, but the game crashes the next turn. I have the following code in the ctpai.cpp file in the BeginTurn method:


    Code:
        if (player_ptr != NULL && player_ptr->m_playerType == PLAYER_TYPE_HUMAN) 
        {
        	if (round >= 3)
        	{
        		a = player_ptr->m_all_armies->Access(0);
    
        		for (int dir = 0; !found && (dir < NOWHERE); ++dir)
        		{
        			MapPoint dest;
    
        			if (pos.GetNeighborPosition (static_cast(dir), dest) 
        				&& a.CanEnter(dest))
        			{
        				Path * tmpPath = new Path;
        				tmpPath->SetStart(pos);
        				tmpPath->AddDir(static_cast(dir));
        				tmpPath->Start(pos);
    
        				g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_MoveOrder,
        					   GEA_Army,		a,
        					   GEA_Path,		tmpPath,
        					   GEA_MapPoint,	dest,
        					   GEA_Int,			FALSE,
        					   GEA_End
        					  );
    
        				found = true;
        			}
        		}
        	}
        }
    Once the first settler moves, I can move the second settler, it then allows me to move the first settler again (I am guessing it does not recognise the move which happened due to code). In the next turn, it gives me an Assertion failure

    Program: CivCTP_dgb.exe
    File:gameobj/objpool.h
    line:93

    If I ignore this error, it gives me another failure, then gives me a Gameobj.cpp Error, then gives me another assertion failure and then the game exits.

    What I think is that I am not updating some game state information and hence the is some synchronization problem the next turn. Can someone please let me know what am I doing wrong/what else I need to do to move a unit?

    Thanks

    Cheers,
    Ushhan

    For backgroud information about my work, please have a look at the CTPII GameState Information thread in this section

  • #2
    thanks, I think I know where your problem is and I'll post it in your new thread


    well I dont see your new thread yet but I think it might have to do with:


    code:--------------------------------------------------------------------------------
    if (round >= 3)
    {
    --------------------------------------------------------------------------------


    the next turn might cause the begin turn sequence to run and then it gets confused if its round 3 or something. thats my best guess.


    __________________


    ushhan

    Thanks a lot. The error was because of that line, but due to a completely different reason. It was because of how I was reassigning the value of the army. Simple programming mistake

    On another note, I could not find the crash.txt file anywhere. Where does it save that?

    The other thread is up now. Was typing it when you replied.


    Call to Power II\ctp2_program\ctp\crash.txt it might have to be enabled in your profile.txt
    Formerly known as "E" on Apolyton

    See me at Civfanatics.com

    Comment


    • #3
      Does it work now? are you looking to add this to the game?
      Formerly known as "E" on Apolyton

      See me at Civfanatics.com

      Comment


      • #4
        The movement code itself looks quite OK, except that you don't test the validity of the army a. What happens when a player does not have any units at all? This will happen after having used the starting settler(s) to found cities, while the cities have not produced any units yet.

        Another problem (from the other thread) is the creation of an Army object. You are not supposed to use new directly, as in
        Code:
         Army *a = new Army(1);
        For efficiency, most game objects are allocated using pools, and you have to use something like Player::GetNewArmy to create a new army. This function will take care of the game state bookkeeping for you. Have a look at CityWindow:isbandQuery for an example.

        Comment


        • #5
          Thanks for the help. The code I posted in this thread is working. I realize that I need to check the validity of this army, which I have to add. The new Army stuff is a little out of date and I don't use that any more, but thanks anyway for the guidance about creating one.

          Regarding it additions to the game, I have no objections to it, but I am not sure how useful it will be. I am automating the human player, using an independent decision system to make the decisions for the human, as part of my Masters Theses. Let me know if it sounds interesting and I will gladly give more details.

          Cheers,
          Ushhan
          Last edited by ushhan; November 7, 2005, 14:29.

          Comment


          • #6
            it sound interesting because if i understand it right we could use it as a well to have population migrations across a civ. it would be a human pplayer unit but you could see it leaving the city and joining other cities.

            the other option is also to use it for naval trade by having ship units travel to destinations each turn.

            just some possibilities.
            Formerly known as "E" on Apolyton

            See me at Civfanatics.com

            Comment


            • #7
              Regarding the possibilities that you mention, they certainly seem feasible. I will be happy to share whatever I learn while working on my stuff. Once I have something substantial, I will post the progress on the forum. I was having some trouble adding units to a city build queue that has just been settled. I am using the following code:

              Code:
              UnitDynamicArray *city_list = player_ptr->m_all_cities;
              
              for (sint32 i = 0; i < city_list->Num(); i++)
              {
              
              	if (!g_theUnitPool->IsValid(city_list->Access(i)))
              		continue;
              
              	CityData *city = city_list->Access(i)->GetCityData();
              
              
              	if (city->GetBuildQueue()->GetLen() <= 0)
              	{
              		//Nothing in the build queue
              		BOOL insert_ok = false;
              
              		//Took the value 4 from "BOOL AiMain::BeginTurn(uint32 end_frame_by, BOOL &my_turn_is_over )"
              		sint32 settler_type = 4;
              
              		insert_ok = city->BuildUnit(settler_type);
              
              		if (!insert_ok)
              			printf("Error!!\n");
              	}
              }
              I am guessing that the type I am passing is incorrect. Any comments would be helpful. I did look at the Slic_AddUnitToBuildList::Call(SlicArgList *args) implementation, and it uses the same command internally.

              Thanks

              Cheers,
              Ushhan

              Comment


              • #8
                You are right. The unit type value 4 (BattleShip) will be a problem when the city is not adjacent to water or you don't have the technology yet. The stuff in AiMain::BeginTurn (and the other files in robotcom) is a leftover from CTP1.

                Comment


                • #9
                  Thanks. Could you please provide me with the value for the settler unit. It will be great if someone can guide me to a file that has the unit types for all the units.

                  Thanks

                  Cheers,
                  Ushhan

                  Comment


                  • #10
                    units.txt in the default\gamedata folder. its not stored in the code.

                    but maybe you could add a check to see if the unit is a settler. See unitdata::CanSettle (I think)
                    Formerly known as "E" on Apolyton

                    See me at Civfanatics.com

                    Comment


                    • #11
                      Happy New Year to everyone. The current status of my efforts is the following:

                      - I can move units

                      - I can settle cities

                      - I can build units from these cities

                      Now I was trying to add city improvements like building a granary.

                      Code:
                      CityData *city = city_list->Access(i)->GetCityData();
                      
                      if (city->GetBuildQueue()->GetLen() <= 0)
                      {
                                 if (city->CanBuildBuilding(building_type))
                                 {
                                           city->BuildImprovement(building_type);
                                 }
                      }
                      Unfortunately, I cant find the building types. I have had a look at the buildings.txt in the ctp2_data\default\gamedata folder, the same place where I founf the unit ids in the units.txt file. Unfortunately, there is no id in the buildings file. Could some please guide me to a file where I could find those ids?

                      Thanks

                      Cheers,
                      Ushhan

                      Comment


                      • #12
                        If nothing changed, ids to records are assigned dynamically by the occurence of their definition in the file (e.g. buildings.txt), starting from zero. This would then be the ids for buildings:

                        Code:
                          0  IMPROVE_ACADEMY
                          1  IMPROVE_AIRPORT
                          2  IMPROVE_ANTI_BALLISTIC_MISSILES
                          3  IMPROVE_AQUA_FILTER
                          4  IMPROVE_AQUEDUCT
                          5  IMPROVE_ARCOLOGIES
                          6  IMPROVE_ARENA
                          7  IMPROVE_BALLISTA_TOWERS
                          8  IMPROVE_BANK
                          9  IMPROVE_BASCILICA
                         10  IMPROVE_BATTLEMENTS
                         11  IMPROVE_BAZAAR
                         12  IMPROVE_BEHAVIORAL_MOD_CENTER
                         13  IMPROVE_BODY_EXCHANGE
                         14  IMPROVE_BROKERAGE
                         15  IMPROVE_CAPITOL
                         16  IMPROVE_CITY_WALLS
                         17  IMPROVE_COMPUTER_CENTER
                         18  IMPROVE_CORNUCOPIC_VAT
                         19  IMPROVE_CORRECTIONAL_FACILITY
                         20  IMPROVE_COURTHOUSE
                         21  IMPROVE_DRUG_STORE
                         22  IMPROVE_E_BANK
                         23  IMPROVE_ECO_TRANSIT
                         24  IMPROVE_FACTORY
                         25  IMPROVE_FLAK_TOWERS
                         26  IMPROVE_FOOD_SILO
                         27  IMPROVE_FORCEFIELD
                         28  IMPROVE_FUSION_PLANT
                         29  IMPROVE_GAIA_COMPUTER
                         30  IMPROVE_GRANARY
                         31  IMPROVE_HOSPITAL
                         32  IMPROVE_INCUBATION_CENTER
                         33  IMPROVE_MATTER_DECOMPILER
                         34  IMPROVE_MICRO_DEFENSE
                         35  IMPROVE_MILL
                         36  IMPROVE_MOVIE_PALACE
                         37  IMPROVE_NANITE_FACTORY
                         38  IMPROVE_NUCLEAR_PLANT
                         39  IMPROVE_OIL_REFINERY
                         40  IMPROVE_ORBITAL_LABORATORY
                         41  IMPROVE_POWER_SATELLITE
                         42  IMPROVE_PUBLIC_TRANSPORTATION
                         43  IMPROVE_PUBLISHING_HOUSE
                         44  IMPROVE_RECYCLING_PLANT
                         45  IMPROVE_ROBOTIC_PLANT
                         46  IMPROVE_SECURITY_MONITOR
                         47  IMPROVE_SHRINE
                         48  IMPROVE_TELEVISION
                         49  IMPROVE_THEATER
                         50  IMPROVE_UNIVERSITY
                         51  IMPROVE_VR_AMUSEMENT_PARK

                        Comment


                        • #13
                          Hi,

                          I currently have a function that allows me to move armies to a neighboring block. Now I wanted to move units to some destination point. I was trying to use the following code:

                          Code:
                          bool CTP_API::MoveArmyTo(const Player *player_ptr, const sint32 armyId, const MapPoint newPos)
                          {
                          	MapPoint pos;
                          	Path path;
                          
                          	Army army = player_ptr->m_all_armies->Access(armyId);
                          	Assert(army);
                          
                          	army.GetPos(pos);
                          
                          	bool found = CTPAgent::FindPath(army, newPos, true, path); 
                          
                          	fprintf( stderr, "Trying to find a path\n");
                          
                          	if (found)
                          	{
                          		Path *tmpPath = new Path(path);
                          		MapPoint target_pos = tmpPath->GetEnd();
                          
                          		fprintf( stderr, "Path found for army %d\n", armyId);
                          
                          		g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_MoveOrder,
                          			   GEA_Army,		army,
                          			   GEA_Path,		tmpPath,
                          			   GEA_MapPoint,	target_pos,
                          			   GEA_Int,			FALSE,
                          			   GEA_End
                          			  );
                          	}
                          	return found;
                          }
                          When I test it with only one block away, the unit moves but anything more than that, and the pathing function does not return a path. e.g it does not work with the destination 2 blocks away.

                          Code:
                          MapPoint newPos;
                          
                          newPos.x = pos.x;
                          newPos.y = pos.y + 2;
                          I have also used the follwoing code which I found in the CtpAi::RefuelAirplane function and this also does not work.

                          Code:
                          double trans_max_r = 0.8;
                          	Path new_path;
                          	float total_cost;
                          	if (! RobotAstar2::s_aiPathing.FindPath(RobotAstar2::PATH_TYPE_DEFAULT,
                          		army,
                          		army->GetMovementType(),
                          		pos,
                          		newPos,
                          		true, 
                          		g_theWorld->GetContinent(newPos),
                          		trans_max_r,
                          		path,
                          		total_cost))
                          	{
                          		
                          		return false;
                          	}
                          	
                          	
                          	Path *tmpPath = new Path(path);
                          	MapPoint target_pos = tmpPath->GetEnd();
                          	
                          	
                          	g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_MoveOrder, 
                          		GEA_Army, army,			
                          		GEA_Path, tmpPath,		
                          		GEA_MapPoint, target_pos,
                          		GEA_Int, FALSE, 
                          		GEA_End);
                          Could some help me out with this?

                          Thanks
                          Cheers,
                          Ushhan

                          Comment


                          • #14
                            Hi,

                            As I have mentioned before, I was trying to move an army to a given (x,y) position on the map. Apart from the two methods I have tried above, which havent work, is there any other way that we can do this? Is there a similar function that I can have a look at?

                            Thanks

                            Cheers,
                            Ushhan

                            Comment


                            • #15
                              the events might be the problem, you might have to find executeMove or teleport unit and use that instead of calling the event. atleast thats what i did when i had problems with sneakattack and adding a settler to a city
                              Formerly known as "E" on Apolyton

                              See me at Civfanatics.com

                              Comment

                              Working...
                              X