Skip to content

Instantly share code, notes, and snippets.

/a_RTS_Core.py Secret

Created April 20, 2013 10:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6954bfd8d0f4ceefa944 to your computer and use it in GitHub Desktop.
Save anonymous/6954bfd8d0f4ceefa944 to your computer and use it in GitHub Desktop.
a_RTS_Core
from os import path, mkdir
from PIL import Image
from scene import *
import itertools
import operator
### Contains all core functions that are rarely edited.
### Imported to keep main file cleaner
##Constants to make things easier to understand later on
FOOD, STONE, WOOD = 0, 1, 2 #Resources. Not used yet.
LAND = [Point(0,0),Point(1,0),Point(2,0),Point(3,0),Point(4,0),Point(5,0),Point(6,0)] #For image loading
CLIFF, SAND, MTN, GRASS, MTN_TO_WATER, WATER, SAND_TO_WATER = 0, 1, 2, 3, 4, 5, 6 #For accessing LAND
## IMAGES MODULE
folderPath = 'Images'
imgPath = 'a_RTS-Sprites'
urlPath = "http://i766.photobucket.com/albums/xx303/TurtleSoupguild/iPad_RTS.png"
def loadImage(folderPath,imgPath,urlPath):
if not path.exists(folderPath): mkdir(folderPath)
if not path.exists(folderPath+"/"+imgPath):
url = urlopen(urlPath)
with open(folderPath+"/"+imgPath, "wb") as output:
output.write(url.read())
return Image.open(folderPath+"/"+imgPath).convert('RGBA')
#A simple function to return the ratio of two integers or floats
def ratios(item1,item2):
if item1 > item2:
return float(item1)/item2
return float(item2)/item1
#Hit test function. Tests a point to see if a Point() is hitting a square.
def hit(loc1, loc2, size2):
if loc1.x>loc2.x and loc1.x<loc2.x+size2.x:
if loc1.y>loc2.y and loc1.y<loc2.y+size2.y:
return True
def aStar(graph, current, end):
openList = set()
closedList = set()
def retracePath(c):
def parentgen(c):
while c:
yield c
c = c.parent
result = [element for element in parentgen(c)]
result.reverse()
return result
openList.add(current)
while openList:
current = sorted(openList, key=lambda inst:inst.H)[0]
if current == end:
return retracePath(current)
openList.remove(current)
closedList.add(current)
for tile in current.neighbors:
if tile not in closedList and tile is not None and tile.population <= 5:
tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))
tile.H += current.H
openList.add(tile)
tile.parent = current
return []
def cropImage(img, start, ssize=Size(60, 60)):
strt = Point(start.x * ssize.w, start.y * ssize.h)
img = img.crop((strt.x,strt.y,strt.x+ssize.w,strt.y+ssize.h))
d = img.load()
#keycolor = d[0,0] #1st pixel is used as keycolor.
#for x in range(img.size[0]):
# for y in range(img.size[1]):
# p = d[x, y]
# if p == keycolor: #if keycolor set alpha to 0.
# d[x, y] = (p[0], p[1], p[2], 0)
return img
## END IMAGES MODULE
#Cleans all pathfinding data from nodes
def cleanNodes(node_array):
for nodes in node_array:
nodes.H = 0
nodes.parent = None
return node_array
#Returns the most common element of a list.
#Used to find out what is the most common unit being selected
#For display.
def most_common(L):
# get an iterable of (item, iterable) pairs
SL = sorted((x, i) for i, x in enumerate(L))
# print 'SL:', SL
groups = itertools.groupby(SL, key=operator.itemgetter(0))
# auxiliary function to get "quality" for an item
def _auxfun(g):
item, iterable = g
count = 0
min_index = len(L)
for _, where in iterable:
count += 1
min_index = min(min_index, where)
# print 'item %r, count %r, minind %r' % (item, count, min_index)
return count, -min_index
# pick the highest-count/earliest item
return max(groups, key=_auxfun)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment