#!/usr/bin/python import sdb import time from threading import Thread class world(Thread): def __init__(self, worldset, map): self.ticks = 0 self.rooms = [] self.monsters = set() self.players = set() self.__stop = False self.ents = set() from stuff import monster self.monsters = set() self.players = set() self.worldset = worldset self.map = map Thread.__init__(self) # load monsters cursor = sdb.db.cursor() cursor.execute("""SELECT id FROM monsters WHERE map=%s ORDER BY id""", (self.map)) for i in range(cursor.rowcount): row = cursor.fetchone() m = monster() m.dbID = row[0] m.load() m.world = self # load rooms cursor = sdb.db.cursor() cursor.execute("""SELECT id FROM plot WHERE map=%s ORDER BY id""", (self.map)) for i in range(cursor.rowcount): row = cursor.fetchone() x = room(self, row[0]) # make it so this thread wont cause program to not exit self.setDaemon(True) # start this thing a ticking self.start() def findEntByName(self, searcher, name, range=False): name = name.strip().lower() match = False match_distance = 0 for ent in self.ents: lname = ent.name.strip().lower() instring = lname.find(name) if instring != -1: if match is False: match = ent match_distance = searcher.distance(ent.o) else: new_distance = searcher.distance(ent.o) if new_distance < match_distance: match = ent match_distance = new_distance return match def findByName(self, searcher, name, range=False): # at some point this should search rooms too return self.findEntByName(self, name, searcher, range) def stop(self): self.__stop = True def run(self): ents = self.ents monsters = self.monsters players = self.players while not self.__stop: # ents think for x in self.ents.copy(): x.think() # regen players if self.ticks % 100 == 0: for p in players: if p.hp < p.maxhp: n = p.hp + p.level if n <= p.maxhp: p.hp = n else: p.hp = p.maxhp p.hearStats() if self.ticks % 1000 == 0: for p in players: p.gold += 1 p.save() # regen monsters if self.ticks % 10 == 0: for m in monsters: if m.hp < m.maxhp: n = m.hp + m.level if n <= m.maxhp: m.hp = n else: m.hp = m.maxhp time.sleep(.2) self.ticks += 1