unitname teamID experience frame_died
special character is the "Game ended!" line after which the widget dumps all units into the file and quits the game.
Known bug: destroying non fully built units also gets logged.
The widget:
Code: Select all
function widget:GetInfo()
return {
name = "stats-unitkilled",
desc = "saves unit xp",
author = "beherith",
date = "2011 may",
license = "PD",
layer = 0,
enabled = true -- loaded by default?
}
end
function widget:Initialize()
gameid=os.time()
Spring.Echo(gameid)
mapname=Game.mapName
modname=Game.modName
fname=modname .. "$"..mapname.."$"..gameid .."$stats.txt"
fd= io.open("stats/"..fname,"w")
if fd then
fileopened=true
else
Spring.Echo("Error, opening stats/"..fname.." failed!")
widgetHandler:RemoveWidget()
end
Spring.SendCommands("setminspeed 20")
end
function widget:UnitDestroyed(unitID, unitDefID, unitTeamID)
xp=Spring.GetUnitExperience(unitID)
unitDefID = Spring.GetUnitDefID(unitID)
unitDef = UnitDefs[unitDefID or -1]
gf=Spring.GetGameFrame()
fd:write(unitDef.name .. " " .. unitTeamID .. " " .. xp .. " " .. gf .. "\n")
--Spring.Echo("Unit "..unitID.." " .. unitDef.name .. " from team "..unitTeamID.." just got destroyed by enemy unit " .. xp)
end
function widget:GameOver()
allunits=Spring.GetAllUnits()
fd:write("Game Ended!\n")
for i=1, #allunits do
unitID=allunits[i]
xp=Spring.GetUnitExperience(unitID)
unitDefID = Spring.GetUnitDefID(unitID)
unitDef = UnitDefs[unitDefID or -1]
unitTeamID=Spring.GetUnitTeam(unitID)
fd:write(unitDef.name .. " " .. unitTeamID .. " " .. xp .. "\n")
end
fd:close()
widgetHandler:RemoveWidget()
Spring.SendCommands("quitforce")
end
The spring batch launcher: (python)
Code: Select all
import os
import time
time.clock()
i=0
for filename in os.listdir(os.getcwd()+'\\tehdemos\\'):
cmd='copy .\\tehdemos\\'+filename+' '+filename
print cmd
os.system(cmd)
cmd='spring.exe '+filename
print cmd
os.system(cmd)
cmd='del '+filename
print cmd
os.system(cmd)
i+=1
print 'Number, total secs', i, int(time.clock())
The plot generator: (python)
Code: Select all
import os
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
names={} #ADD NAMES OF UNITS HERE
d={}
i=0
for filename in os.listdir(os.getcwd()):
if 'stats' in filename:
i+=1
f=open(filename,'r')
ln=f.readlines()
f.close()
ended=0
for l in ln:
if 'ended!' in l:
ended=1
continue
l=l.split(' ')
if len(l)<4:
continue
if ended==0 or (ended==1 and float(l[2])>0):
if l[0] in d:
d[l[0]].append((float(l[2]),float(l[3])))
else:
d[l[0]]=[]
d[l[0]].append((float(l[2]),float(l[3])))
print 'Processed',i,'files, found',len(d),'unique units'
for k,v in d.iteritems():
exp=[]
time=[]
nonattack=1
avgexp=0
for e in v:
exp.append(e[0])
avgexp+=e[0]
time.append(e[1]/(30*60))
if e[0]>0:
nonattack=0
avgexp/=len(exp)
if nonattack==1 or avgexp<0.0001:
print names[k] ,'is not an attack unit'
else:
print names[k],'is an attack unit'
fig=plt.figure(1)
plt.clf()
plt.plot(time,exp,marker='o',color='b',linestyle='None')
plt.title(names[k]+' avg exp='+str(avgexp)+' samples='+str(len(time)))
fig.savefig((names[k]+'.png'),dpi=144)