def naive_factor(n): f = dict() if n < 0: n = -n f[-1] = 1 root_n = int(sqrt(n)) for p in take_while(lambda p: n > 1 and p <= root_n, primes()): n, i = div_while(n, p) if i > 0: f = dict_sum(f, {p: i}) return f if n == 1 else dict_sum(f, {n: 1})
def resolv_constructions(self): self.poc = 0 for p in self.planets.itervalues(): self.poc += 1 + dict_sum(p.constructions, I('YOT')) if self.policy == I('construct'): self.poc = floor(self.poc * 1.5) poc = self.poc quit = 0 while poc > 0 and not quit: quit = 1 for c in self.constructions.itervalues(): if poc <= 0: break if c.poc > 0: c.poc -= 1 poc -= 1 quit = 0 const_ok = [] for c in self.constructions.itervalues(): if c.poc == 0: try: self.build_construction(c) const_ok.append(c.id) except Exception as e: tb = exc_info()[2] print str(e) print str(print_tb(tb)) self.owner.add_msg('Impossible de construire %d %s sur %s: %s'% (c.count, c.what.name, self.name, str(e))) for id in const_ok: del self.constructions[id]
def finalize(self): # civ or mil? self.define(I('military'), dict_max) if I('military') not in self.caracs: self.caracs[I('civilian')] = 1 tmp = max(1, dict_sum(self.compos, I('cases'))) self.caracs[I('size')] = floor(log(tmp-1, 2))+1 self.caracs[I('poc')] = floor(tmp/2) tmp = 17 - self.caracs[I('size')] + dict_max(self.compos, I('propeller')) if I('military') in self.caracs: tmp = floor(tmp*2/3) self.caracs[I('velocity')] = tmp self.caracs[I('cost_prod')] = dict_sum(self.compos, I('cost_prod')) # wait, wat? self.caracs[I('cost_ore')] = dict_sum(self.compos, I('cost_ore')) self.define(I('cargo'), dict_sum) self.define(I('syst_rad'), dict_max) self.define(I('fleet_rad'), dict_max) self.define(I('SCM'), dict_max) self.define(I('colo'), dict_max) sa, pa = 0, 0 for t in self.compos: if I('weapon') in t.caracs: w = t.caracs[I('weapon')][0] n = t.caracs[I('weapon')][1] sa += (w.ha+w.sa) * n * self.compos[t] pa += (w.pa) * n * self.compos[t] if sa > 0: self.caracs[I('SA')] = sa if pa > 0: self.caracs[I('PA')] = pa for t in self.compos: if I('cost_merch') in t.caracs: merch = t.caracs[I('cost_merch')] if merch[0] not in self.merchs: self.merchs[merch[0]] = 0 self.merchs[merch[0]] += merch[1] * self.compos[t]
def prod_min(self): mine = dict_sum(self.constructions, I('extract')) extra = dict_max(self.constructions, I('advance_ex')) + self.ore[0] prod = (2 * extra - mine + 1) * (mine / 2.0) self.ore[1] += prod return prod