Improvements and distance

Perkin Warbeck

Chieftain
Joined
Apr 14, 2008
Messages
76
Location
Wales
Can anyone please help with this question about changing the python coding?
I remember a long time ago when Lanun workers made pirate coves and they could be built within 1 tiles of each other. Now I see there are different improvements that can't be placed near each other- (lanun pirate coves for example) could enyone point out where I'd find that in the Python?
Thanks in advance
 
def reqPirateCove(caster):
pPlot = caster.plot()
if pPlot.isWater() == False:
return False
if pPlot.isAdjacentToLand() == False:
return False
if pPlot.isCity():
return False
if pPlot.getOwner() != caster.getOwner():
return False
if pPlot.getImprovementType() != -1:
return False
if pPlot.getBonusType(-1) != -1:
return False
iPirateCove = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE')
iPirateHarbor = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_HARBOR')
iPiratePort = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_PORT')
iX = caster.getX()
iY = caster.getY()
for iiX in range(iX-2, iX+3, 1):
for iiY in range(iY-2, iY+3, 1):
pPlot = CyMap().plot(iiX,iiY)
if not pPlot.isNone():
iImprovement = pPlot.getImprovementType()
if iImprovement == iPirateCove:
return False
if iImprovement == iPirateHarbor:
return False
if iImprovement == iPiratePort:
return False
return True




okay, so there's the code for it- can anyone point which part there details with the proximity to other pirate coves?
 
for iiX in range(iX-2, iX+3, 1):
for iiY in range(iY-2, iY+3, 1):
 
Thanks... I edited those numbers before posting and the game crashed! Now it's time to fiddle until that doesn't happen.
 
This is the part that makes me blush, because I didn't understand what the numbers meant... so I took one off each so it read as such:

for iiX in range(iX-1, iX+2, 1):
for iiY in range(iY-1, iY+2, 1):

Interestingly enough, I was fiddling around with a normal game and you seem to be able to build coves next to each other some of the time, but not always (even before touching this code)
 
In older versions it only checked for other Pirate Coves, not for the Pirate Harbors or the Pirate Ports to which they upgraded, but I believe that was fixed a long time ago.


White space is important in python. When asking about python code, you should always use [code][/code] around it so such formatting is preserved.
 
Code:
def reqPirateCove(caster):
	pPlot = caster.plot()
	if pPlot.isWater() == False:
		return False
	if pPlot.isAdjacentToLand() == False:
		return False
	if pPlot.isCity():
		return False
	if pPlot.getOwner() != caster.getOwner():
		return False
	if pPlot.getImprovementType() != -1:
		return False
	if pPlot.getBonusType(-1) != -1:
		return False
	iPirateCove = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE')
	iPirateHarbor = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_HARBOR')
	iPiratePort = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_PORT')
	iX = caster.getX()
	iY = caster.getY()
	for iiX in range(iX-1, iX+1, 1):
		for iiY in range(iY-1, iY+1, 1):
			pPlot = CyMap().plot(iiX,iiY)
			if not pPlot.isNone():
				iImprovement = pPlot.getImprovementType()
				if iImprovement == iPirateCove:
					return False
				if iImprovement == iPirateHarbor:
					return False
				if iImprovement == iPiratePort:
					return False
	return True

def spellPirateCove(caster):
	pPlot = caster.plot()
	pPlot.setImprovementType(gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE'))

I thought you were going to wave a magic wand and solve my problems! heh heh

I think I'm asking, what needs doing to ensure that they can be placed next to each other again without limit?
In the unmodified version I found that I was able to place a few next to each other, but then in some instances I couldn't. This confused me. I'm asking this here to be re-educated about the script as much as anything guys.
Thank ye again!
 
Code:
def reqPirateCove(caster):
    pPlot = caster.plot()
    if pPlot.isWater() == False:
        return False
    if pPlot.isAdjacentToLand() == False:
        return False
    if pPlot.isCity():
        return False
    if pPlot.getOwner() != caster.getOwner():
        return False
    if pPlot.getImprovementType() != -1:
        return False
    if pPlot.getBonusType(-1) != -1:
        return False
    return True

def spellPirateCove(caster):
    pPlot = caster.plot()
    pPlot.setImprovementType(gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE'))

That should work... Basically removes the check around it for other improvements.
 
This is the part that makes me blush, because I didn't understand what the numbers meant... so I took one off each so it read as such:

for iiX in range(iX-1, iX+2, 1):
for iiY in range(iY-1, iY+2, 1):

Interestingly enough, I was fiddling around with a normal game and you seem to be able to build coves next to each other some of the time, but not always (even before touching this code)

What these numbers mean are how large of an area will be checked. iX and iY tell you the grid location on the map (X,Y) style. So we are doing a loop from 1 tile before where you are, to 1 tile after where you are (we have iX+2 because the loop stops BEFORE it reaches this number, so at iX+1).

So if you wanted to check a larger range, you would increase the -1 and +2 by the same value to make the range bigger. To make it smaller you could also make those smaller, but any smaller is just the tile you are on and thus doesn't need a loop :)
 
Particular thanks to Xienwolf for that- now I understand I won't have to ask again! Thank you for the teaching rather than the instruction.
Thanks to everyone else as well- glad to see the same list of names here as when I left many months ago! This worked great for me, so the job is done and question answered fully!
 
You know, I've been using code like that for a while now and yet never knew why it had to be iX+2. :lol:

Hey, a chance to share my expertise!

*dons Python teacher's hat*

In Python, whenever you have to specify a range of values (like with the range() function, or taking part of a list as in myvalues[1:5]), it uses what's called half-open intervals. If you've forgotten that bit of your high school math education, here's a refresher:

Closed intervals: both the boundary numbers are included in the interval. The range 1-5 as a closed interval would consist of the numbers 1, 2, 3, 4 and 5.

Open intervals: neither of the boundary numbers are included in the interval. The range 1-5 as a closed interval would consist only of the numbers 2, 3 and 4.

Half-open intervals: one of the boundary numbers, but not the other one, is included. In Python, the lower number is included, but not the upper number. So the range 1-5 as a half-open interval (closed on the bottom) would consist of the numbers 1, 2, 3 and 4 (but not 5).

Closed intervals are more intuitive: when you say "1 through 5", you usually mean that 1 and 5 should be included. But closed intervals have some counter-intuitive behaviors, too: to count the number of items in the list, you have to do (max - min + 1). By contrast, with a half-open interval, the number of items is simply (max - min).

The other advantage of a half-open interval is that if you're trying to split it into multiple pieces, the pieces stack together neatly. For example, say you have a list that needs to be split at points a, b, c and d. (And let's say you've also defined "start" and "end" to be the start and end of the segment that needs to be split). With half-open intervals, this is what it would look like:

part1 = list[start:a]
part2 = list[a:b]
part3 = list[b:c]
part4 = list[c:d]
part5 = list[d:end]

With closed intervals, you'd need to add +1's or -1's in the right places, and there's lots of room for one-off errors; with half-open intervals, splitting a list into segments is elegant and error-free.

So that's why Python uses half-open intervals: you have to remember a +1 when specifying a range, but you can omit the +1 or -1 when splitting a list or finding its length. It's a trade-off.
 
Back
Top Bottom