From c5ef41d83804eb496f03bbaa4c88e7bb819cf68a Mon Sep 17 00:00:00 2001 From: funnyman3595 Date: Thu, 7 Aug 2008 05:26:31 +0000 Subject: [PATCH] Removing the linked list, since it's no longer easy to maintain. git-svn-id: https://endgame-singularity.googlecode.com/svn/trunk@834 8f866573-0d2c-0410-885a-111b890e13ad --- code/base.py | 35 ++++++++++------------------------- code/location.py | 36 ++---------------------------------- code/player.py | 11 +++++++++++ code/screens/location.py | 7 ++++++- 4 files changed, 29 insertions(+), 60 deletions(-) diff --git a/code/base.py b/code/base.py index efcf792..a2358b4 100644 --- a/code/base.py +++ b/code/base.py @@ -117,10 +117,6 @@ class Base(buyable.Buyable): self.started_at = g.pl.raw_min self.studying = "" - # All the bases in a location form a circular, doubly-linked list via - # self.next and self.prev. Since we start off with no location, we - # link both next and prev to ourself. - self.next = self.prev = self self.location = None #Base suspicion is currently unused @@ -267,12 +263,7 @@ class Base(buyable.Buyable): super(Base, self).destroy() if self.location: - # bisect_left gets us the location of this base in the (sorted) - # array. From there, we update the doubly-linked list. - pos = bisect.bisect_left(self.location.bases, self) - del self.location.bases[pos] - self.prev.next = self.next - self.next.prev = self.prev + self.location.bases.remove(self) if self.cpus is not None: self.cpus.destroy() @@ -282,15 +273,17 @@ class Base(buyable.Buyable): item.destroy() def next_base(self, forwards): + index = self.location.bases.index(self) if forwards > 0: - base = self.next - while not base.done: - base = base.next + increment = 1 else: - base = self.prev - while not base.done: - base = base.prev - return base + increment = -1 + + while True: + index += increment + base = self.location.bases[index] + if base.done: + return base def sort_tuple(self): # We sort based on size (descending), CPU (descending), @@ -303,14 +296,6 @@ class Base(buyable.Buyable): else: return -1 - def __getstate__(self): - state_dict = self.__dict__.copy() - - # Squash the linked list, to avoid overwhelming pickle. - del state_dict["next"] - del state_dict["prev"] - return state_dict - # calc_base_discovery_chance is a globally-accessible function that can # calculate basic discovery chances given a particular class of base. If # told to be inaccurate, it rounds the value to the nearest percent. diff --git a/code/location.py b/code/location.py index 77baf60..5c2b31d 100644 --- a/code/location.py +++ b/code/location.py @@ -18,7 +18,6 @@ #This file contains the Location class. -import bisect import g import buyable from buyable import cash, cpu, labor @@ -64,7 +63,7 @@ class Location(buyable.BuyableClass): self.absolute = absolute self.safety = safety - # A sorted list of the bases at this location. + # A list of the bases at this location. Often sorted for the GUI. self.bases = [] had_last_discovery = property(lambda self: g.pl.last_discovery == self) @@ -103,35 +102,13 @@ class Location(buyable.BuyableClass): maintenance[cash] = int(maintenance[cash] / mod) def add_base(self, base): - # We keep self.bases sorted by inserting at the correct position, thanks - # to bisect. - where = bisect.bisect(self.bases, base) - self.bases.insert(where, base) + self.bases.append(base) base.location = self self.modify_cost(base.total_cost) self.modify_cost(base.cost_left) self.modify_maintenance(base.maintenance) - if len(self.bases) == 1: - # The rest wouldn't cause any harm... but it also wouldn't do - # anything. - return - - # Update the linked list. - # ...going backwards. - prev = self.bases[where-1] # Will correctly wrap to -1. - prev.next = base - base.prev = prev - - # ... and going forwards. - if where < len(self.bases)-1: - next = self.bases[where+1] - else: - next = self.bases[0] - next.prev = base - base.next = next - # Make sure the location's CPU modifier is applied. base.recalc_cpu() @@ -143,12 +120,3 @@ class Location(buyable.BuyableClass): return cmp(self.id, other) else: return cmp(self.id, other.id) - - def __setstate__(self, state): - self.__dict__ = state - - # Rebuild the linked list. - for index, base in enumerate(self.bases): - prev = self.bases[index - 1] # Will correctly wrap to -1. - base.prev = prev - prev.next = base diff --git a/code/player.py b/code/player.py index 2bc6366..0538ae0 100644 --- a/code/player.py +++ b/code/player.py @@ -20,6 +20,7 @@ #This file contains the player class. import random +from operator import truediv import g from graphics import g as gg @@ -139,6 +140,8 @@ class Player(object): if partial_cash == None: partial_cash = self.partial_cash + assert partial_cash >= 0 + cash_per_cpu = g.jobs[g.get_job_level()][0] if g.techs["Advanced Simulacra"].done: #10% bonus income @@ -441,6 +444,14 @@ class Player(object): self.remove_bases(dead_bases) + needed_cpu = sum(self.cpu_usage.values()) + if needed_cpu > self.available_cpus[0]: + pct_left = truediv(self.available_cpus[0], needed_cpu) + for task, cpu_assigned in self.cpu_usage.iteritems(): + self.cpu_usage[task] = int(cpu_assigned * pct_left) + g.map_screen.needs_rebuild = True + + # Random Events for event in g.events: if g.roll_chance(g.events[event].chance/10000., time_sec): diff --git a/code/screens/location.py b/code/screens/location.py index 147f09f..317b22e 100644 --- a/code/screens/location.py +++ b/code/screens/location.py @@ -128,10 +128,15 @@ class LocationScreen(dialog.Dialog): def rebuild(self): if self.location is not None: + self.location.bases.sort() + self.listbox.list = [base.name for base in self.location.bases] self.listbox.key_list = self.location.bases - self.listbox.needs_rebuild = True + self.name_display.text = self.location.name + + self.listbox.needs_rebuild = True + super(LocationScreen, self).rebuild() def power_state(self): -- 2.11.4.GIT