/
load_map.py
149 lines (121 loc) · 4.57 KB
/
load_map.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import math
from lxml import etree
from copy import copy
from pprint import pprint
from dijkstra import Node, add_edge, solve, get_route
STD_UNIT_COST = 29 / 100.
CHANGE_MAP_COST = 2
class LocatableObject():
def __init__(self, xml):
self.xml = xml
def __repr__(self):
m = self.xml.getparent()
return "LocatableObject(%s, %s [%s])" %\
(m.get('name'),
self.xml.get('name'),
self.xml.get('loc'))
#~ return etree.tostring(self.xml)
def loc_touple(self):
retval = self.xml.get('loc').split(',')
return (int(retval[0]), int(retval[1]))
def target(self):
return self.xml.get('target')
def map_name(self):
return self.xml.getparent().get('name')
def distance(from_loc, to_loc):
return math.sqrt( (from_loc[0] - to_loc[0])**2 +\
(from_loc[1] - to_loc[1])**2)
doc = etree.ElementTree(file='map.xml')
dist_doc = etree.ElementTree(file='distances.xml')
all_nodes = []
for map_xml in doc.xpath('//map'):
#~ for item in doc.xpath('//map[@name="Desert Pines"]/door'):
nodes = []
for item in map_xml.xpath('door'):
node = Node(LocatableObject(item))
nodes.append(node)
#connected locations on same map
unconnted = copy(nodes)
while len(unconnted) > 0:
current = unconnted.pop()
for item in unconnted:
from_loc = current.payload.xml.get('loc')
to_loc = item.payload.xml.get('loc')
#~ print "from_loc: %s, to_loc: %s" % (from_loc, to_loc)
dist_xml = dist_doc.xpath('//distance[@from="%s"][@to="%s"]'\
% (from_loc, to_loc))
if not dist_xml:
dist_xml = dist_doc.xpath('//distance[@from="%s"][@to="%s"]'\
% (to_loc, from_loc))
if dist_xml:
#~ print "dist_xml: %s" % etree.tostring(dist_xml[0])
cost = int(dist_xml[0].get('seconds'))
else:
from_loc = current.payload.loc_touple()
to_loc = item.payload.loc_touple()
#~ cost = math.sqrt( (from_loc[0] - to_loc[0])**2 +\
#~ (from_loc[1] - to_loc[1])**2)
cost = distance(from_loc, to_loc)
cost = int(cost * STD_UNIT_COST)
#~ cost = 1
add_edge(cost, current, item)
all_nodes.extend(nodes)
#connect doors
unconnted = copy(all_nodes)
while len(unconnted) > 0:
current = unconnted.pop()
for item in unconnted:
if current.payload.target() == item.payload.target():
unconnted.remove(item)
add_edge(CHANGE_MAP_COST, current, item)
#add beam point
beam_location = Node(LocatableObject(doc.xpath('//beam')[0]))
for item in all_nodes:
#~ add_edge(CHANGE_MAP_COST, beam_location, item)
item.edges.append((CHANGE_MAP_COST, beam_location))
all_nodes.append(beam_location)
#~ for item in all_nodes:
#~ print item
#~ for subitem in item.edges:
#~ print "-->" + str(subitem)
def find_location_obj(map_name, loc):
for item in all_nodes:
p = item.payload
if p.map_name() == map_name and p.loc_touple() == loc:
return item
raise Exception("not found")
import os
from get_location import Extractor
ex = Extractor()
with open(os.path.expanduser('~/.elc/main/chat_log.txt')) as f:
for line in f:
ex.feed(line.strip())
print "Your are in '%s' at %s" % (ex.map_name, ex.loc)
near_locations = []
for item in all_nodes:
if item.payload.map_name() == ex.map_name:
#~ print item
d = distance(item.payload.loc_touple(), ex.loc)
near_locations.append((d, item))
near_locations.sort()
print "\nnear_locations"
pprint(near_locations)
where_am_i = near_locations[0][1]
print "\nnear_location: %s" % where_am_i
#~ where_am_i = find_location_obj(ex.map_name, ex.loc)
#~ where_am_i = find_location_obj('White Stone', (707,162))
#~ where_am_i = find_location_obj('Desert Pines', (166,100)) #sto
#~ where_am_i = find_location_obj('Nordcarn', (51,184))
#~ where_am_i = find_location_obj('Desert Pines', (44,302))
#~ where_am_i = find_location_obj('Desert Pines', (172,12)) #South Exit to Portland
where_am_i.cost = 0
solve(all_nodes)
#~ for item in all_nodes:
#~ print item
#~ destination_location = find_location_obj('Desert Pines', (44,302))
#~ destination_location = find_location_obj('Nordcarn', (51,184))
destination_location = find_location_obj('Desert Pines', (166,100)) #sto
print "\nRoute"
for item in get_route(all_nodes, destination_location):
print item
#~ print beam_location_xml