Announcement

Collapse
No announcement yet.

A quick Python question

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

  • A quick Python question

    Let's say you have a plot: pPlot. How do you find the nearest city to that plot?

    Roger Bacon

  • #2
    If you want to calculate the nearest city in terms of a units movements costs you may use the function CyPlot.movementCost(CyUnit pUnit, CyPlot pFromPlot). This one should consider that shortest way is not always the fastest.

    If you looking fore the shortest way, you may have to calculate the x,y-distance to the all your cities by using pythagoras and select the shortest one.
    Have a look on my mods :
    Modified Special Domestic Advisor 1.8b !!! UPDATE !!!
    Plot List Enhancement
    Restart interceptor animation after game load

    Comment


    • #3
      You can use:

      Code:
      # find the nearest city to the plot whose co-ordinates are < iX, iY >
      
      CyCity findCity(INT iX,                       # X co-ordinate
      		INT iY, 		      # Y co-ordinate
      		PlayerType eOwner, 	      # owner of the desired city
      		TeamType eTeam, 	      # team that city owner belongs to 
      		BOOL bSameArea, 	      # set false if you want to look outside of the area that
      					      # < iX, iY > is in
      		BOOL bCoastalOnly, 	      # set true if you want to look only for coastal cities
      		TeamType eTeamAtWarWith,      # if defined will only look for cities whose owner is at
      					      # war with the specified team
      		DirectionType eDirection,     # if specified, will look for cities in that direction
      		CyCity pSkipCity	      # if specified, will skip this city
      	       )
      
      Defaults:
      
      CyCity* (int iX,
      	int iY, 
      	int (PlayerTypes) eOwner = NO_PLAYER,
      	int (TeamTypes) eTeam = NO_TEAM,
      	bool bSameArea = true, 
      	bool bCoastalOnly = false, 
      	int (TeamTypes) eTeamAtWarWith = NO_TEAM, 
      	int (DirectionTypes) 
      	eDirection = NO_DIRECTION, 
      	CvCity* pSkipCity = NULL)

      Comment


      • #4
        Wow, thanks Peter. That looks like just what I need. I can't wait to get home and test it out tonight. Darn, and I thought I might actually have a night to PLAY the game instead of programming.

        By the way, where did you find that code?

        Roger Bacon

        Comment


        • #5
          Originally posted by 12monkeys
          If you want to calculate the nearest city in terms of a units movements costs you may use the function CyPlot.movementCost(CyUnit pUnit, CyPlot pFromPlot). This one should consider that shortest way is not always the fastest.

          If you looking fore the shortest way, you may have to calculate the x,y-distance to the all your cities by using pythagoras and select the shortest one.
          Sorry 12Monkeys. I should have been clearer. I wanted to find the nearest city so that I could crete a message saying ".... near [nearest city]". I think the code Peter posted should do the trick.

          Roger Bacon

          Comment


          • #6
            I got the basic info from

            Gabung bersama DEPE4D situs Judi Online terpercaya di indonesia menyediakan sarana deposit pulsa dan e-money cukup 1 user id untuk semua permainan judi online.


            but filled in the comments myself. You can pretty well work out what the parameters are for by looking at what they default to.

            Comment


            • #7
              OK, what am I doing wrong?


              pMap = CyMap()
              ...
              nearestCity = pMap.findCity(iX, iY)


              I get the error message:

              TypeError: this constructor takes no arguments

              Very frustrating tonight.

              Roger Bacon

              Comment


              • #8
                just a guess :

                did you call "pMap = CyMap()" inside of an obejct or outside of an object? Will say :

                Code:
                pMap = CyMap()
                
                class MyObject:
                
                   def AnyFunc():
                      ...
                      nearestCity = pMap.findCity(iX, iY)
                      ...
                or

                Code:
                class MyObject:
                
                   def AnyFunc():
                      ...
                      pMap = CyMap()
                      ...
                      nearestCity = pMap.findCity(iX, iY)
                      ...
                In the second case, the "real" call is "pMap = CyMay(self)", because inside of an object always the self parameter is passed. But this self-reference is not defined for the CyMap() constructor.
                First case should work!

                As I said, just a guess!!!
                Have a look on my mods :
                Modified Special Domestic Advisor 1.8b !!! UPDATE !!!
                Plot List Enhancement
                Restart interceptor animation after game load

                Comment


                • #9
                  Originally posted by 12monkeys
                  just a guess :

                  did you call "pMap = CyMap()" inside of an obejct or outside of an object?
                  I had called it inside an object so, after reading your post, I moved it outside of the object but it didn't make a difference. It still says "TypeError: this constructor takes no arguments"

                  I also tried
                  Code:
                  nearestCity = CyMap().findCity(iX, iY, pPlot.getOwner(), NO_TEAM, True, False, NO_TEAM, NO_DIRECTION, NULL)
                  but then it says "NameError: global name 'NO_TEAM' is not defined"

                  Am I maybe missing a file to include that has things like NO_TEAM and NO_DIRECTION defined?

                  Roger Bacon

                  Comment


                  • #10
                    I did a search for "findCity" in the *.py files and found that Locutus and Dale used:

                    Code:
                    cNew = gc.getMap().findCity(city.getX(), city.getY(), -1, iAxis, False, False, -1, -1, city)
                    in their Desert War mod. Try

                    Code:
                    nearestCity = gc.getMap().findCity(iX, iY)
                    and see what happens.

                    Comment


                    • #11
                      [SIZE=1] Try

                      Code:
                      nearestCity = gc.getMap().findCity(iX, iY)
                      and see what happens.
                      TypeError: this constructor takes no arguments

                      Well, thanks all for trying. I'm just going to display the X,Y coordinates and release the mod as is. Maybe someone will figure out how to make it work once it's out there.

                      Roger Bacon

                      Comment


                      • #12
                        Well, I've released the mod. Actually, it's an update (improvement?) of a great mod twiggins did (Terraform mod). You can see it over at:


                        If anyone wants to tinker with it and find out why the findCity() function wouldn't work, please feel free to.

                        Roger Bacon

                        Comment

                        Working...
                        X