From 6f6c61da00b020d51ffe092177a82489dbc2b2ad Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 24 Aug 2015 03:36:20 +0200 Subject: debugging +/- --- main.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'main.py') diff --git a/main.py b/main.py index d87eb3c..c8cc341 100644 --- a/main.py +++ b/main.py @@ -25,8 +25,10 @@ for i in range(1,10): # 10 connection attempts try: token = sys.argv[1] addr, *_ = utils.get_party_address(token) + print("using party token") except: addr, token, *_ = utils.find_server() + print("joining random game") # connect c.connect(addr,token) -- cgit v1.2.3 From 65a4820a90799dfd52f64e95e1ac226953e2cbbc Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 24 Aug 2015 03:36:45 +0200 Subject: autorejoin --- main.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'main.py') diff --git a/main.py b/main.py index c8cc341..d13c4ea 100644 --- a/main.py +++ b/main.py @@ -48,6 +48,8 @@ gui.set_client(c) # initialize strategy strategy = Strategy(c) +autorespawn_counter = 60 + # main loop while True: c.on_message() @@ -63,3 +65,10 @@ while True: stats.log_pos(c.player.center) stats.log_mass(c.player.total_mass) gui.update() + + if not c.player.is_alive: + if autorespawn_counter == 0: + c.send_respawn() + autorespawn_counter = 60 + else: + autorespawn_counter-=1 -- cgit v1.2.3 From c2e13a67d969f1208a8b8c127c94a9841c7048d9 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 24 Aug 2015 18:06:59 +0200 Subject: cell motion history --- gui.py | 7 +++++++ main.py | 5 +++-- subscriber.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) (limited to 'main.py') diff --git a/gui.py b/gui.py index 19fffbb..440e0ef 100644 --- a/gui.py +++ b/gui.py @@ -142,6 +142,11 @@ def generate_virus(spikes, spike_length, radius, global_coords): def draw_cell(cell): cx,cy = world_to_win_pt(cell.pos,c.player.center) + try: + cx2,cy2 = world_to_win_pt(cell.poslog[-2],c.player.center) + except: + print("wtf, no poslog available?!") + cx2,cy2=cx,cy radius = world_to_win_length(cell.size) if cell.is_virus: @@ -158,6 +163,8 @@ def draw_cell(cell): gfxdraw.aapolygon(screen, polygon2, color) else: color=(int(cell.color[0]*255), int(cell.color[1]*255), int(cell.color[2]*255)) + + gfxdraw.filled_circle(screen, cx2, cy2, radius, (127,127,127)) if not (cell.is_ejected_mass or cell.is_food): gfxdraw.filled_circle(screen, cx, cy, radius, color) diff --git a/main.py b/main.py index d13c4ea..fd75b42 100644 --- a/main.py +++ b/main.py @@ -9,13 +9,14 @@ import time import random import gui import stats -from subscriber import DummySubscriber +from subscriber import EnhancingSubscriber from interval_utils import * from strategy import * # global vars -sub = DummySubscriber() +sub = EnhancingSubscriber() c = client.Client(sub) +sub.set_client(c) stats = stats.Stats() for i in range(1,10): # 10 connection attempts diff --git a/subscriber.py b/subscriber.py index a637e67..1dcccc6 100644 --- a/subscriber.py +++ b/subscriber.py @@ -1,4 +1,5 @@ from log import log +from collections import deque import sys class DummySubscriber: @@ -65,3 +66,33 @@ class DummySubscriber: def on_debug_line(self,x,y): log("debug line") +class CellHistory: + def __init__(self): + self.poslog = deque(maxlen=10) + self.stale = False + +class EnhancingSubscriber(DummySubscriber): + def __init__(self): + self.c = None + self.history = {} + + def set_client(self,c): + self.c = c + + def on_world_update_post(self): + for cid in self.history: + self.history[cid].stale = True + + for cid in self.c.world.cells: + if cid not in self.history: + self.history[cid] = CellHistory() + print("unknown cell") + + self.history[cid].poslog.append(self.c.world.cells[cid].pos.copy()) + self.c.world.cells[cid].poslog = self.history[cid].poslog + print("poslog of size="+str(len(self.history[cid].poslog))) + + self.history[cid].stale = False + + self.history = {k: v for k, v in self.history.items() if v.stale == False} + -- cgit v1.2.3 From 066b391e0e3b644353e6bafe10656ea902e06780 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 25 Aug 2015 00:49:21 +0200 Subject: allow specification of nickname --- main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index fd75b42..146087a 100644 --- a/main.py +++ b/main.py @@ -27,6 +27,11 @@ for i in range(1,10): # 10 connection attempts token = sys.argv[1] addr, *_ = utils.get_party_address(token) print("using party token") + + try: + nick = sys.argv[2] + except: + nick = "test cell pls ignore" except: addr, token, *_ = utils.find_server() print("joining random game") @@ -40,7 +45,7 @@ for i in range(1,10): # 10 connection attempts c.disconnect() -c.player.nick="test cell pls ignore" +c.player.nick=nick # initialize GUI -- cgit v1.2.3 From 96ab1a8f44caf4f90148842fd826a8698ce144c5 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 25 Aug 2015 15:12:23 +0200 Subject: fix --- main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 146087a..69a9499 100644 --- a/main.py +++ b/main.py @@ -18,6 +18,11 @@ sub = EnhancingSubscriber() c = client.Client(sub) sub.set_client(c) stats = stats.Stats() + +try: + nick = sys.argv[2] +except: + nick = "test cell pls ignore" for i in range(1,10): # 10 connection attempts print("trying to connect, attempt "+str(i)) @@ -28,10 +33,6 @@ for i in range(1,10): # 10 connection attempts addr, *_ = utils.get_party_address(token) print("using party token") - try: - nick = sys.argv[2] - except: - nick = "test cell pls ignore" except: addr, token, *_ = utils.find_server() print("joining random game") -- cgit v1.2.3 From 8386ae0e63313ffc5d0fefebda8451d982679e01 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 25 Aug 2015 17:30:58 +0200 Subject: process.frame() in stats.py, no functional changes --- main.py | 6 +++--- stats.py | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 69a9499..08793d3 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ from strategy import * sub = EnhancingSubscriber() c = client.Client(sub) sub.set_client(c) -stats = stats.Stats() +stats = stats.Stats(c) try: nick = sys.argv[2] @@ -69,8 +69,8 @@ while True: if gui.bot_input: c.send_target(target[0], target[1]) - stats.log_pos(c.player.center) - stats.log_mass(c.player.total_mass) + stats.process_frame() + gui.update() if not c.player.is_alive: diff --git a/stats.py b/stats.py index f850339..9a745ab 100644 --- a/stats.py +++ b/stats.py @@ -1,7 +1,8 @@ import time class Stats: - def __init__(self): + def __init__(self,c): + self.c = c self.min_mass = 0 self.max_mass = 0 self.current_mass = 0 @@ -33,4 +34,8 @@ class Stats: self.cell_defensiveness[cell] = value def get_last_steps(self, list, steps = 10): - return list[-steps:] \ No newline at end of file + return list[-steps:] + + def process_frame(self): + self.log_pos(self.c.player.center) + self.log_mass(self.c.player.total_mass) -- cgit v1.2.3 From 0df1c5f7799fdd63c3cd3fdc92bc9ccc60d1da8f Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 25 Aug 2015 19:49:46 +0200 Subject: clean quit --- gui.py | 6 ++++-- main.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'main.py') diff --git a/gui.py b/gui.py index 57b08c5..895b1ac 100644 --- a/gui.py +++ b/gui.py @@ -8,6 +8,8 @@ import math import time from agarnet.agarnet.vec import Vec +running = True + font_fallback = False try: from pygame import freetype @@ -272,7 +274,7 @@ def draw_markers(): draw_marker(marker[i], colors[i], marker_updated[i]) def draw_frame(): - global screen, movement, zoom, screensize, input, bot_input, marker, marker_updated + global screen, movement, zoom, screensize, input, bot_input, marker, marker_updated, running pygame.event.pump() clock.tick() @@ -328,7 +330,7 @@ def draw_frame(): input = False bot_input = True if event.key == K_ESCAPE: - pygame.quit() + running = False if event.key == K_r: c.send_respawn() if event.type == MOUSEBUTTONDOWN: diff --git a/main.py b/main.py index 08793d3..0cd9b30 100644 --- a/main.py +++ b/main.py @@ -58,7 +58,7 @@ strategy = Strategy(c) autorespawn_counter = 60 # main loop -while True: +while gui.running: c.on_message() gui.draw_frame() @@ -79,3 +79,5 @@ while True: autorespawn_counter = 60 else: autorespawn_counter-=1 + +print("bye") -- cgit v1.2.3 From 5b91b2732ed339a1c2559e06833cc4f1daec01ae Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 25 Aug 2015 19:53:35 +0200 Subject: statistics --- main.py | 2 ++ stats.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'main.py') diff --git a/main.py b/main.py index 0cd9b30..c0e9a21 100644 --- a/main.py +++ b/main.py @@ -80,4 +80,6 @@ while gui.running: else: autorespawn_counter-=1 +stats.save("stats.pickle") + print("bye") diff --git a/stats.py b/stats.py index 9a745ab..aed7b42 100644 --- a/stats.py +++ b/stats.py @@ -1,4 +1,6 @@ import time +from collections import defaultdict +import pickle class Stats: def __init__(self,c): @@ -12,6 +14,10 @@ class Stats: self.cell_aggressivity = {} self.cell_split_frequency = {} self.cell_defensiveness = {} + + self.size_vs_speed = defaultdict(lambda : defaultdict(lambda : 0)) + self.size_vs_visible_window = defaultdict(lambda : []) + self.mass_vs_visible_window = defaultdict(lambda : []) def log_mass(self, mass): self.mass_history.append((time.time(), mass)) @@ -39,3 +45,31 @@ class Stats: def process_frame(self): self.log_pos(self.c.player.center) self.log_mass(self.c.player.total_mass) + + cells = self.c.world.cells.values() + own_cells = self.c.player.own_cells + + own_total_size = sum( map(lambda cell : cell.size, own_cells) ) + own_total_mass = sum( map(lambda cell : cell.mass, own_cells) ) + + n = 3 + for cell in filter(lambda cell : not cell.is_food and not cell.is_virus and not cell.is_ejected_mass, cells): + if hasattr(cell,'poslog') and len(cell.poslog) > n+1: + cellspeed = 0 + for i in range(1,n+1): + cellspeed += (cell.poslog[-i] - cell.poslog[-i-1]).len() / n + + cellspeed = int(cellspeed) + self.size_vs_speed[cell.size][cellspeed] += 1 + + visible_width = max( map(lambda cell : cell.pos.x - cell.size, cells) ) - min( map(lambda cell : cell.pos.x + cell.size, cells) ) + visible_height = max( map(lambda cell : cell.pos.y - cell.size, cells) ) - min( map(lambda cell : cell.pos.y + cell.size, cells) ) + + self.size_vs_visible_window[own_total_size].append((visible_width,visible_height)) + self.mass_vs_visible_window[own_total_mass].append((visible_width,visible_height)) + + def save(self,filename): + pickle.dump(self, open(filename,"wb")) + + def load(filename): + return pickle.load(open(filename,"rb")) -- cgit v1.2.3 From ada18916c2ad68f7f2ac2d69ca51ac2e9a8b2df4 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Wed, 26 Aug 2015 22:51:28 +0200 Subject: draw cell.parent --- gui.py | 13 +++++++++++++ main.py | 1 + 2 files changed, 14 insertions(+) (limited to 'main.py') diff --git a/gui.py b/gui.py index 895b1ac..ed5c9d3 100644 --- a/gui.py +++ b/gui.py @@ -273,6 +273,19 @@ def draw_markers(): for i in [0, 1, 2]: draw_marker(marker[i], colors[i], marker_updated[i]) +def draw_debug(): + for cell in c.world.cells.values(): + parent = None + try: + parent = cell.parent + except AttributeError: + pass + + if parent != None: + draw_line(cell.pos, parent.pos,(255,0,0)) + draw_circle(parent.pos,3,(255,0,0),True) + + def draw_frame(): global screen, movement, zoom, screensize, input, bot_input, marker, marker_updated, running diff --git a/main.py b/main.py index c0e9a21..d07b6b8 100644 --- a/main.py +++ b/main.py @@ -71,6 +71,7 @@ while gui.running: stats.process_frame() + gui.draw_debug() gui.update() if not c.player.is_alive: -- cgit v1.2.3 From c13c6ca7f3d2ed069d8b0d9af4a09ea2c952d845 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 31 Aug 2015 22:42:56 +0200 Subject: --nogui flag. closes #14 --- main.py | 13 +++++++++++-- strategy.py | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index d07b6b8..b9fc832 100644 --- a/main.py +++ b/main.py @@ -7,12 +7,21 @@ import sys import math import time import random -import gui +import nogui as gui # might be overridden later. import stats from subscriber import EnhancingSubscriber from interval_utils import * from strategy import * +if "--nogui" in sys.argv: + sys.argv.remove("--nogui") +else: + try: + import gui + except: + print("ERROR: could not import gui... running without gui.") + + # global vars sub = EnhancingSubscriber() c = client.Client(sub) @@ -53,7 +62,7 @@ c.player.nick=nick gui.set_client(c) # initialize strategy -strategy = Strategy(c) +strategy = Strategy(c, gui) autorespawn_counter = 60 diff --git a/strategy.py b/strategy.py index 67b3da9..900cf8f 100644 --- a/strategy.py +++ b/strategy.py @@ -1,20 +1,24 @@ import math from interval_utils import * -import gui import random +import nogui friendly_players=["Windfisch","windfisch","Cyanide","cyanide"] +\ ["Midna","Nayru","Farore","Din","Ezelo","Navi","Zelda","Tetra","Link","Ciela","Linebeck","Salia","Epona","Shiek"] +\ ["Vaati","Ganon","Ganondorf","Ghirahim","Agahnim"] class Strategy: - def __init__(self, c): + def __init__(self, c, gui=None): self.target = (0,0) self.has_target = False self.target_cell = None self.color = (0,0,0) self.c = c self.do_approach_friends = True + if gui != None: + self.gui = gui + else: + self.gui = nogui def get_my_smallest(self): return sorted(self.c.player.own_cells, key = lambda x: x.mass)[0] @@ -133,14 +137,14 @@ class Strategy: print("friend too small") friend_to_feed = None if friend_to_feed: - gui.hilight_cell(friend_to_feed, (255,255,255),(255,127,127),30) + self.gui.hilight_cell(friend_to_feed, (255,255,255),(255,127,127),30) self.target_cell = friend_to_feed self.has_target = True if self.do_approach_friends: for c in self.c.player.own_cells: - gui.hilight_cell(c, (255,255,255), (255,127,127), 20) + self.gui.hilight_cell(c, (255,255,255), (255,127,127), 20) # can this cell feed that cell? # "False" means "No, definitely not" @@ -170,7 +174,7 @@ class Strategy: good_intervals = [] for feedable in possibly_feedable_cells: - gui.hilight_cell(feedable, (255,192,127), (127,127,255)) + self.gui.hilight_cell(feedable, (255,192,127), (127,127,255)) if feedable not in friendly_cells: break @@ -181,7 +185,7 @@ class Strategy: success_rate += area / (2*10*math.pi/180) / len(list(self.c.player.own_cells)) - gui.draw_bar(((100,40),(500,24)), success_rate, thresh=.98, color=(0,0,127)) + self.gui.draw_bar(((100,40),(500,24)), success_rate, thresh=.98, color=(0,0,127)) if success_rate >= 0.98: self.c.send_shoot() @@ -237,7 +241,7 @@ class Strategy: # a bit of debugging information for i in forbidden_intervals: - gui.draw_arc(self.c.player.center, self.c.player.total_size+10, i, (255,0,255)) + self.gui.draw_arc(self.c.player.center, self.c.player.total_size+10, i, (255,0,255)) # if however there's no enemy to avoid, try to feed a friend. or chase food or jizz randomly around else: @@ -270,6 +274,6 @@ class Strategy: # more debugging - gui.draw_line(self.c.player.center, self.target, self.color) + self.gui.draw_line(self.c.player.center, self.target, self.color) return self.target -- cgit v1.2.3 From aee2f231e702c5b7fa025a297c107e086838b204 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 1 Sep 2015 00:01:43 +0200 Subject: FPS, problem manager --- main.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index b9fc832..79b0e54 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,54 @@ import stats from subscriber import EnhancingSubscriber from interval_utils import * from strategy import * +import time + +class Clock: + def __init__(self): + self.t = time.time() + self.fps_t = time.time() + self.fps = 27 + self.cnt = 0 + self.newfps = False + + def tic(self): + t = time.time() + result = t-self.t + self.t=t + return result + + def getfps(self): + self.cnt+=1 + if time.time() > self.fps_t + 1: + self.fps_t += 1 + self.fps = self.cnt + self.cnt = 0 + self.newfps = True + else: + self.newfps = False + return self.fps + +class ProblemException(BaseException): + pass + +class ProblemManager: + def __init__(self, setup): + self.setup = setup + self.problems = {} + for t in setup: + self.problems[t] = [] + + def report(self, prob): + self.problems[prob] += [time.time()] + self.problems[prob] = list(filter(lambda t : time.time() < t + self.setup[prob][1], self.problems[prob])) + + if len(self.problems[prob]) > self.setup[prob][0]: + print("PROBLEM: "+prob) + if self.setup[prob][2]: + raise ProblemException + +probs = ProblemManager({"network lag":(100,5,True), "strategy lag":(5,2,False), "gui lag":(5,2,False), "high fps":(300,6,True), "low fps":(5,6,True)}) + if "--nogui" in sys.argv: sys.argv.remove("--nogui") @@ -31,7 +79,7 @@ stats = stats.Stats(c) try: nick = sys.argv[2] except: - nick = "test cell pls ignore" + nick = "" for i in range(1,10): # 10 connection attempts print("trying to connect, attempt "+str(i)) @@ -66,11 +114,19 @@ strategy = Strategy(c, gui) autorespawn_counter = 60 +clock = Clock() + # main loop while gui.running: c.on_message() + if clock.tic() > 1./20: + print("NETWORK LAG") + probs.report("network lag") gui.draw_frame() + if clock.tic() > 1./40: + print("GUI SLOW") + probs.report("gui lag") if len(list(c.player.own_cells)) > 0: target = strategy.process_frame() @@ -80,6 +136,10 @@ while gui.running: stats.process_frame() + if clock.tic() > 1./25.: + print("STRATEGY LAG") + probs.report("strategy lag") + gui.draw_debug() gui.update() @@ -90,6 +150,14 @@ while gui.running: else: autorespawn_counter-=1 + fps = clock.getfps() + if clock.newfps: + print("FPS: %3d" % fps) + if fps < 24: + probs.report("low fps") + if fps > 50: + probs.report("high fps") + stats.save("stats.pickle") print("bye") -- cgit v1.2.3 From cf9460c7926be62dac8b786a586da01b8c637ad5 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 1 Sep 2015 00:12:56 +0200 Subject: improvement --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 79b0e54..6a50ce6 100644 --- a/main.py +++ b/main.py @@ -58,7 +58,7 @@ class ProblemManager: if self.setup[prob][2]: raise ProblemException -probs = ProblemManager({"network lag":(100,5,True), "strategy lag":(5,2,False), "gui lag":(5,2,False), "high fps":(300,6,True), "low fps":(5,6,True)}) +probs = ProblemManager({"network lag":(100,5,True), "strategy lag":(5,2,False), "gui lag":(5,2,False), "high fps":(5,6,True), "low fps":(5,6,True)}) if "--nogui" in sys.argv: @@ -155,8 +155,8 @@ while gui.running: print("FPS: %3d" % fps) if fps < 24: probs.report("low fps") - if fps > 50: - probs.report("high fps") + if fps > 50: + probs.report("high fps") stats.save("stats.pickle") -- cgit v1.2.3 From 8391000d2a03baf02cd4b3f5c46d9471e3be1127 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Tue, 1 Sep 2015 00:17:34 +0200 Subject: exit if persistent problems occur. closes #21 --- main.py | 85 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 41 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 6a50ce6..ab92ce3 100644 --- a/main.py +++ b/main.py @@ -116,47 +116,50 @@ autorespawn_counter = 60 clock = Clock() -# main loop -while gui.running: - c.on_message() - if clock.tic() > 1./20: - print("NETWORK LAG") - probs.report("network lag") - - gui.draw_frame() - if clock.tic() > 1./40: - print("GUI SLOW") - probs.report("gui lag") - - if len(list(c.player.own_cells)) > 0: - target = strategy.process_frame() - - if gui.bot_input: - c.send_target(target[0], target[1]) - - stats.process_frame() - - if clock.tic() > 1./25.: - print("STRATEGY LAG") - probs.report("strategy lag") - - gui.draw_debug() - gui.update() - - if not c.player.is_alive: - if autorespawn_counter == 0: - c.send_respawn() - autorespawn_counter = 60 - else: - autorespawn_counter-=1 - - fps = clock.getfps() - if clock.newfps: - print("FPS: %3d" % fps) - if fps < 24: - probs.report("low fps") - if fps > 50: - probs.report("high fps") +try: + # main loop + while gui.running: + c.on_message() + if clock.tic() > 1./20: + print("NETWORK LAG") + probs.report("network lag") + + gui.draw_frame() + if clock.tic() > 1./40: + print("GUI SLOW") + probs.report("gui lag") + + if len(list(c.player.own_cells)) > 0: + target = strategy.process_frame() + + if gui.bot_input: + c.send_target(target[0], target[1]) + + stats.process_frame() + + if clock.tic() > 1./25.: + print("STRATEGY LAG") + probs.report("strategy lag") + + gui.draw_debug() + gui.update() + + if not c.player.is_alive: + if autorespawn_counter == 0: + c.send_respawn() + autorespawn_counter = 60 + else: + autorespawn_counter-=1 + + fps = clock.getfps() + if clock.newfps: + print("FPS: %3d" % fps) + if fps < 24: + probs.report("low fps") + if fps > 50: + probs.report("high fps") +except ProblemException: + print("Exiting due to a problem such as low/high fps, network lags etc") stats.save("stats.pickle") -- cgit v1.2.3