-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworldmodel.py
More file actions
121 lines (106 loc) · 3.7 KB
/
worldmodel.py
File metadata and controls
121 lines (106 loc) · 3.7 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import entities
import pygame
import ordered_list
import actions
import occ_grid
import point
import save_load
import image_store
PROPERTY_KEY = 0
BGND_KEY = 'background'
BGND_NUM_PROPERTIES = 4
BGND_NAME = 1
BGND_COL = 2
BGND_ROW = 3
class WorldModel:
def __init__(self, num_rows, num_cols, background):
self.background = occ_grid.Grid(num_cols, num_rows, background)
self.num_rows = num_rows
self.num_cols = num_cols
self.occupancy = occ_grid.Grid(num_cols, num_rows, None)
self.entities = []
self.action_queue = ordered_list.OrderedList()
def within_bounds(self, pt):
return (pt.x >= 0 and pt.x < self.num_cols and
pt.y >= 0 and pt.y < self.num_rows)
def is_occupied(self, pt):
return (self.within_bounds(pt) and
self.occupancy.get_cell(pt) != None)
def find_nearest(self, pt, type):
oftype = [(e, distance_sq(pt, e.get_position()))
for e in self.entities if isinstance(e, type)]
return nearest_entity(oftype)
def add_entity(self, entity):
pt = entity.get_position()
if self.within_bounds(pt):
old_entity = self.occupancy.get_cell(pt)
if old_entity != None:
old_entity.clear_pending_actions()
self.occupancy.set_cell(pt, entity)
self.entities.append(entity)
def move_entity(self, entity, pt):
tiles = []
if self.within_bounds(pt):
old_pt = entity.get_position()
self.occupancy.set_cell(old_pt, None)
tiles.append(old_pt)
self.occupancy.set_cell(pt, entity)
tiles.append(pt)
entity.set_position(pt)
return tiles
def remove_entity(self, entity):
self.remove_entity_at(entity.get_position())
def remove_entity_at(self, pt):
if (self.within_bounds(pt) and
self.occupancy.get_cell(pt) != None):
entity = self.occupancy.get_cell(pt)
entity.set_position(point.Point(-1, -1))
self.entities.remove(entity)
self.occupancy.set_cell(pt, None)
def schedule_action(self, action, time):
self.action_queue.insert(action, time)
def unschedule_action(self, action):
self.action_queue.remove(action)
def update_on_time(self, ticks):
tiles = []
next = self.action_queue.head()
while next and next.ord < ticks:
self.action_queue.pop()
tiles.extend(next.item(ticks)) # invoke action function
next = self.action_queue.head()
return tiles
def get_background_image(self, pt):
if self.within_bounds(pt):
entity = self.background.get_cell(pt)
return entity.get_image()
def get_background(self, pt):
if self.within_bounds(pt):
return self.background.get_cell(pt)
def set_background(self, pt, bgnd):
if self.within_bounds(pt):
self.background.set_cell(pt, bgnd)
def get_tile_occupant(self, pt):
if self.within_bounds(pt):
return self.occupancy.get_cell(pt)
def get_entities(self):
return self.entities
def schedule_entity(self, entity, i_store):
if isinstance(entity, entities.MinerNotFull):
actions.schedule_miner(self, entity, 0, i_store)
elif isinstance(entity, entities.Vein):
actions.schedule_vein(self, entity, 0, i_store)
elif isinstance(entity, entities.Ore):
actions.schedule_ore(self, entity, 0, i_store)
#these functions help above methods
def nearest_entity(entity_dists):
if len(entity_dists) > 0:
pair = entity_dists[0]
for other in entity_dists:
if other[1] < pair[1]:
pair = other
nearest = pair[0]
else:
nearest = None
return nearest
def distance_sq(p1, p2):
return (p1.x - p2.x)**2 + (p1.y - p2.y)**2