From 3faea430d52611b486fd11d3afb9ed9b38082a83 Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 10 Jul 2024 20:14:35 +0300 Subject: [PATCH] Added minimap module (bad) and miss counters --- swarm/manager.py | 24 ++++++++++++++++++++++++ swarm/worker.py | 14 +++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/swarm/manager.py b/swarm/manager.py index c3a3026..e0f994f 100644 --- a/swarm/manager.py +++ b/swarm/manager.py @@ -211,6 +211,30 @@ class Manager: self.put_image(shrek_x, shrek_y, im) self.animation_functions.append(update_shrek) + elif elem["type"] == "minimap": + minimap_x, minimap_y = elem["x"], elem["y"] + minimap_w, minimap_h = elem["w"], elem["h"] + + minimap_state = { + "last_update": time.time() + } + def update_minimap(): + if self.shmem is None: + return + if (time.time() - minimap_state["last_update"]) < 5: + return + for y in range(minimap_h): + for x in range(minimap_w): + world_x = int(x * 1000 / minimap_w) + world_y = int(y * 1000 / minimap_h) + world_i = world_x + world_y * 1000 + byte, mask = world_i >> 3, 0x80 >> (world_i & 7) + state = bool(self.shmem.buf[byte] & mask) + px, py = x + minimap_x, y + minimap_y + self.set_index(px + py * 1000, state) + minimap_state["last_update"] = time.time() + + self.animation_functions.append(update_minimap) else: raise TypeError(f"invalid element: {elem}") print("ADD", elem) diff --git a/swarm/worker.py b/swarm/worker.py index 7fc1e4a..8bb94b1 100644 --- a/swarm/worker.py +++ b/swarm/worker.py @@ -31,9 +31,12 @@ class WorkerManager: self.n_toggles = 0 self.workers: set[tuple[int, int]] = set() self._restarts: dict[int, int] = {} + self.miss_avoid = 0 + self.miss_mask = 0 + self.miss_state = 0 async def queue_manager(self): - offset = random.randint(0, 999999) + offset = random.randint(0, 1000000) while True: for oy in [0, 2, 1, 3]: for y in range(oy, 1000, 4): @@ -73,9 +76,11 @@ class WorkerManager: mask = 0x80 >> bit if self.shmem.buf[OFFSET_AVOID + byte] & mask: + self.miss_avoid += 1 continue if (self.shmem.buf[OFFSET_MASK + byte] & mask) == 0: + self.miss_mask += 1 continue if (self.shmem.buf[OFFSET_CANVAS + byte] & mask) != ( @@ -85,7 +90,10 @@ class WorkerManager: self.n_toggles += 1 self.shmem.buf[OFFSET_STATE + byte] ^= 0x80 >> bit await sio.emit("toggle_bit", {"index": index}) + self.queue.task_done() await asyncio.sleep(self.delay) + else: + self.miss_state += 1 finally: self.workers.remove(cookie) @@ -104,6 +112,9 @@ class WorkerManager: print(f"Workers: {len(self.workers)} {self.workers}") print(f"Queue size: {self.queue.qsize()}/{self.queue.maxsize}") print(f"Toggles: {self.n_toggles / diff:.2f}/s") + print(f"Misses: A:{self.miss_avoid} M:{self.miss_mask} S:{self.miss_state}") + + print(f"Q: {self.queue}") self.n_toggles = 0 last_printout = time.time() @@ -113,6 +124,7 @@ class WorkerManager: return self async def __aexit__(self, a, b, c): + print("Closing shmem") self.shmem.close()