Need this snipet of code fixed for mapscript

Astax

Prince
Joined
Oct 27, 2005
Messages
556
There are bound to be small mistakes here. I'm trying to make some code to add to isValid, to determine if the starting plot has food. That way i can overwrite normalizeAddFoodBonuses() with no detriment to the map.

arrayX and arrayY are meant to store the x/y values of all the plots in the starting city fat-cross. arrayBonus is meant to hold all the bonus indexes that represent acceptable food. Now all I want is for loops to go through each array and check the plot for food. If they get to last plot and they have not continued yet, I want it to return false for isValid.

Now don't laugh about the mistakes I made here, I have no clue :)

Code:
arrayX = [x-1, x,   x+1, x-2, x-1, x,   x+1, x+2, x-2, x-1, x, x+1, x+2, x-2, x-1, x,   x+1, x+2, x-1, x,   x+1]
arrayY = [y-2, y-2, y-2, y-1, y-1, y-1, y-1, y-1, y,   y,   y, y,   y,   y+1, y+1, y+1, y+1, y+1, y+2, y+2, y+2]
arrayBonus = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
				
	for iY in range(arrayX):
		for iX in range(arrayY):
			pPlot = map.plot(iX,iY)
			pBonus = pPlot.getBonusType(-1): 	
			if (pBonus in arrayBonus)
				continue
						
			if (iX = x+1 and iY = y+2)
				return false
 
In your original arrays, x and y do not appear to be known, and it seems you are treating them as constant. Have you done programming in other languages before? One good way to do this is to write a function (off the top of my head, not tested for syntax)

Code:
def doesBFCHaveFood(x, y):
   arrayBonus = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
   for i in range (x-2, x+3):
      for j in range (y-2, y+3):
         if (abs (i-x) == 2) and (abs(j-y) == 2)): continue # exclude the corners
         iBonus = map.plot(iX,iY).getBonusType(-1)
         if (iBonus in arrayBonus): return true
   return false

I'm sure there is a better way to create the arrayBonus so it is not hard-coded, but I can't think of one right away. What you have is a list, so "in" must check each element of the list; it would be more efficient to create a dictionary and use has_key instead.
 
x and y are passed to isValid, so they are defined when the function is called. the whole block of code is inside isValid function.

I hope ur function excludes the corners properly. I'll give that a go thnx! :)

I could write it as a separate function I guess, I just hope I put it in the rite spot. Thnx for the help again

And yes I know a bit of VB, C, C++, C#
 
ok i figured it out,

Code:
def doesBFCHaveFood(x, y):
	map = CyMap()
	arrayBonus = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
	for i in range (x-2, x+3):
		for j in range (y-2, y+3):
			if abs(i-x) == 2 and abs(j-y) == 2: continue 
			# exclude the corners
			iBonus = map.plot(i,j).getBonusType(-1)
			if (iBonus in arrayBonus): return true
			
	return false
 
I also decided to write a second function which I will use to balance different resources. This function will see if there is a resource in range of the starting settler. The 4 range options I have chose are 0, for directly under the settler, 1, for within 0% culture, 2 for 20% culture, and 3 for 40% culture. Take a look:

Code:
def isResourceInRange(x, y, iBonus, iDistance):
	if iBonus > 34: return false
	if iBonus < -1: return false
	if iDistance > 3: return false
	if iDistance < 0: return false

	map = CyMap()
		
	if (iDistance == 0):
		return (map.plot(x,y).getBonusType(-1) == iBonus)
	
	if (iDistance == 1):
		for i in range (x-1, x+2):
			for j in range (y-1, y+2):
				if (iBonus == map.plot(i,j).getBonusType(-1)): return true
	
	if (iDistance == 2):
		for i in range (x-2, x+3):
			for j in range (y-2, y+3):
				if abs(i-x) == 2 and abs(j-y) == 2: continue 
				if (iBonus == map.plot(i,j).getBonusType(-1)): return true
			
	if (iDistance == 3):
		for i in range (x-3, x+4):
			for j in range (y-3, y+4):
				if abs(i-x) == 3 and abs(j-y) == 3: continue 
				if abs(i-x) == 2 and abs(j-y) == 3: continue 
				if abs(i-x) == 3 and abs(j-y) == 2: continue 
				if (iBonus == map.plot(i,j).getBonusType(-1)): return true
	
	return false
 
Top Bottom