Skip to content

Instantly share code, notes, and snippets.

@larsfu
Created August 5, 2010 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsfu/510087 to your computer and use it in GitHub Desktop.
Save larsfu/510087 to your computer and use it in GitHub Desktop.
# visualize3.py
#
# Copyright 2010 Lars Funke <larsfunke1996@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import sys, string, os, pygame.surface, pygame.image
def visualize(map):
if lastset != 1:
shrinkfactor = input('PNG shrink factor (1/x) [6] > ')
if shrinkfactor == '':
shrinkfactor = 6
else:
shrinkfactor = int(shrinkfactor)
rawradius = input('Influence radius [150] > ')
if rawradius == '':
rawradius = 150
else:
rawradius = int(rawradius)
else:
settingsfile = open('output3/{}.settings.meta'.format(map))
settings = settingsfile.read()
settings = settings.split(',')
shrinkfactor = int(settings[0])
rawradius = int(settings[1])
try:
logfile = open('logs/{}.log'.format(map))
except IOError:
print('The logfile could not be found. Exiting.')
quit()
print('Parsing logfile...')
log = logfile.read()
log = log.split(',')
showkey = False
color_start = [0, 255, 0, 0]
color_end = [255, 0, 0, 255]
deaths = []
#Find the maximum values
xmax = 0
ymax = 0
xlist = []
ylist = []
for coord in log:
coordspl = coord.split(' ')
try:
xlist.append(int(coordspl[0]))
ylist.append(int(coordspl[1]))
except ValueError:
pass
xmax = round((max(xlist)+rawradius)/shrinkfactor)
ymax = round((max(ylist)+rawradius)/shrinkfactor)
print(' done, {} coordinates found, horizontal size: {}px, vertical size: {}px.'.format(str(len(log)-1), str(round(xmax)), str(round(ymax))))
def colorFromGradient(percentage):
if percentage < 1:
percentage = 1
diff_r = color_end[0]- color_start[0]
diff_g = color_end[1]- color_start[1]
diff_b = color_end[2]- color_start[2]
diff_a = color_end[3]- color_start[3]
factor = round(percentage) / 1000;
r = round(color_start[0] + diff_r * factor)
g = round(color_start[1] + diff_g * factor)
b = round(color_start[2] + diff_b * factor)
a = round(color_start[3] + diff_a * factor)
return [r, g, b, a]
print('Computing distances...')
radius = round(rawradius/shrinkfactor)
blood = {}
percent = 1
countingVariable = 1
for coord in log:
if round(countingVariable/(len(log)-1)*100) > percent:
sys.stdout.write('\r')
sys.stdout.write('{}%'.format(str(percent)))
percent = round(countingVariable/(len(log)-1)*100)
coordspl = coord.split(' ')
try:
x = round(int(coordspl[0])/shrinkfactor)
y = round(int(coordspl[1])/shrinkfactor)
except ValueError:
pass
topleft = [0 if x - radius < 1 else x - radius, 0 if y - radius < 1 else y - radius]
bottomright = [xmax if x + radius > xmax else x + radius, ymax if y + radius > ymax else y + radius]
i=[0,0]
i[0] = topleft[0]
i[1] = topleft[1]
while(i[0] < bottomright[0]):
if i[0] not in blood:
blood[i[0]] = {}
i[1] = topleft[1]
while(i[1] < bottomright[1]):
if i[1] not in blood[i[0]]:
blood[i[0]][i[1]] = 0
diffx = i[0] - x
diffy = i[1] - y
space = (diffx**2 + diffy**2)**.5
space = -space+radius
if space < 0:
space = 0
blood[i[0]][i[1]] += space
i[1] += 1
i[0] += 1
countingVariable += 1
sys.stdout.write('\r')
sys.stdout.write(' done. \n')
print('Rendering... ')
surface = pygame.Surface((xmax, ymax), pygame.SRCALPHA)
surface.fill(pygame.Color(0x00000000))
numblood = 0
bloodlist = []
for x in blood:
numblood += 1
for y in blood[x]:
bloodlist.append(blood[x][y])
maxblood = max(bloodlist)
minblood = min(bloodlist)
countingVariable = 1
percent = 1
for x in blood:
if round(countingVariable/numblood*100) > percent:
sys.stdout.write('\r')
sys.stdout.write(str(percent)+'%')
percent = round(countingVariable/numblood*100)
for y in blood[x]:
percentage = round((blood[x][y]-minblood)/(maxblood-minblood)*1000)
col = colorFromGradient(percentage)
try:
color = pygame.Color(col[0], col[1], col[2], col[3])
except ValueError:
print('Erroneous color value: {} {} {} {}'.format(str(col[0]), str(col[1]), str(col[2]), str(col[3])))
surface.set_at((round(x), round(y)), color)
countingVariable += 1
pygame.image.save(surface, 'output3/{}.png'.format(map))
sys.stdout.write('\r')
sys.stdout.write(' done. \n')
print('Writing metadata...')
meta = '[{}]'.format(str(len(log)-1))
metafile = open('output3/{}.map.meta'.format(map), 'w')
metafile.write(meta);
metafile.close();
try:
mapsfile = open('output3/allmaps.meta', 'r')
maps = mapsfile.read()
maps = maps.replace('[', '')
maps = maps.replace(']', '')
maps = maps.replace('"', '')
maps = maps.split(', ')
mapsfile.close()
except IOError:
maps = []
print('All map file was not found, creating')
if not map in maps:
mapsfile = open('output3/allmaps.meta', 'w')
mapsnew = '['
for _map in maps:
if _map != "":
mapsnew += '"'+_map+'", '
mapsnew += '"'+map+'"'
mapsnew += ']'
mapsfile.write(mapsnew)
mapsfile.close()
colorfile = open('output3/colors.meta', 'w')
colors = '["rgb({}, {}, {})", "rgb({}, {}, {})"]'.format(str(color_start[0]), str(color_start[1]), str(color_start[2]), str(color_end[0]), str(color_end[1]), str(color_end[2]))
colorfile.write(colors)
colorfile.close()
settingsfile = open('output3/{}.settings.meta'.format(map), 'w')
settings = '{},{}'.format(str(shrinkfactor), str(rawradius))
settingsfile.write(settings)
settingsfile.close()
print (' done.')
print('This script will visualize the logged deaths.')
map = input('Map to parse > ')
if map == "ALL":
logs = os.listdir("logs");
lastset = int(input("Use settings from last run? (1/0) > "))
for log in logs:
log = log.split(".log")
print('Parsing map: {}'.format(log[0]))
visualize(log[0])
else:
lastset = 0;
visualize(map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment