-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy paththreads.py
60 lines (42 loc) · 1.67 KB
/
threads.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from globals import MAP_SIZE, MAP_WINDOW_SIZE, WORLD_INFO, SETTINGS, LIFE
import graphics as gfx
import bad_numbers
import maps
import threading
import logging
import time
class ChunkHandler(threading.Thread):
def __init__(self, check_every=5):
threading.Thread.__init__(self)
self.last_checked = -check_every
self.check_every = check_every
self.load_clusters = []
def check_chunks(self, force=False):
if not force and WORLD_INFO['ticks']-self.last_checked<self.check_every:
return False
self.last_checked = WORLD_INFO['ticks']
for life in [l for l in LIFE.values() if l['online']]:
_x_min = bad_numbers.clip(life['pos'][0]-MAP_WINDOW_SIZE[0], 0, MAP_SIZE[0]-1-MAP_WINDOW_SIZE[0])
_y_min = bad_numbers.clip(life['pos'][1]-MAP_WINDOW_SIZE[1], 0, MAP_SIZE[1]-1-MAP_WINDOW_SIZE[1])
_x_max = bad_numbers.clip(life['pos'][0]+MAP_WINDOW_SIZE[0], 0, MAP_SIZE[0]-1)
_y_max = bad_numbers.clip(life['pos'][1]+MAP_WINDOW_SIZE[1], 0, MAP_SIZE[1]-1)
_refresh = False
for y in range(_y_min, _y_max, WORLD_INFO['chunk_size']):
for x in range(_x_min, _x_max, WORLD_INFO['chunk_size']):
maps.load_cluster_at_position_if_needed((x, y))
SETTINGS['loading'] = True
if 'player' in life:
_refresh = True
if _refresh:
gfx.refresh_view('map')
SETTINGS['loading'] = False
def load_cluster(self, cluster_key):
self.load_clusters.append(cluster_key)
def run(self):
while SETTINGS['running'] and not SETTINGS['kill threads']:
self.check_chunks()
time.sleep(1)
logging.debug('ChunkHandler thread shutting down.')
def init():
SETTINGS['chunk_handler'] = ChunkHandler()
SETTINGS['chunk_handler'].start()