forked from prophile/libdiana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·129 lines (106 loc) · 4.45 KB
/
example.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
#!/usr/bin/python3
import diana
import diana.tracking
import pprint
pp = pprint.PrettyPrinter(indent=2)
# 172.16.104.171
tx, rx = diana.connect('127.0.0.1') # or whatever IP
tx(diana.packet.SetShipPacket(1)) # Select Ship by #, Artemis = 1, etc..
for console in diana.packet.Console: # select a bunch of consoles
if console.value not in (3,4,5): continue
# 3,4,5 = eng, sci, comms
tx(diana.packet.SetConsolePacket(console,True))
# 1-5: helm,weapons,engineering,science,comms
tx(diana.packet.ReadyPacket())
tracker = diana.tracking.Tracker()
class ScienceAI:
def __init__(self,tx,rx,tracker):
self._tx = tx
self._rx = rx
self._tracker = tracker
self.scannable = [5,8,15] # other_ship, anomaly, creature
self.known_objects = {}
def try_scan(self,obj_id):
self._tx(
diana.packet.SciScanPacket(obj_id)
)
def is_scanning(self):
try: return (self._tracker.player_ship['scanning-id'] not in (0,None))
except KeyError:
return False
def tracker_callback(self,oid):
try:
if self._tracker.player_ship['object'] == oid:
self.update() # update if player_ship updated, else give up
except KeyError: # if we cant find player_ship yet, just ignore
pass
def update(self):
# update list of known objects
for _,obj in self._tracker.objects.items():
try:
assert type(obj['type']) is diana.enumerations.ObjectType
except KeyError:
raise Exception("Bad obj['type'] on object \""+repr(obj)+"\"")
if obj['type'].value in self.scannable:
obj_id = obj['object']
if obj_id not in self.known_objects.keys():
self.known_objects[obj_id] = {
"scan-level":0, # not seen =?=> not scanned?
#"distance":None # calculate on demand
}
else:
try:
self.known_objects[obj_id]['scan-level'] = self._tracker.objects[obj_id]['scan-level?']
except:
pass
# print list of known objects
if len(self.known_objects.keys()) > 0:
#print("ScienceAI update:")
#print("\t%d objects known"%len(self.known_objects.keys()))
#print("\t ObjID\t Info")
#for k,v in self.known_objects.items():
# print("\t",k,"\t",v)
print("\tSCANNING_STATE:\t",self.is_scanning())
if(self.is_scanning()):
print("\tSCAN_PROGRESS:\t",
"%d"%(100*(self._tracker.player_ship['scanning-progress']),) + "%"
)
else:
return # nothing to do anyway
# if not scanning, scan!
if not self.is_scanning():
unscanned = [oid for oid in self.known_objects.keys()
if self.known_objects[oid]['scan-level'] < 1]
if not len(unscanned): # no more to scan left = just stop
return
self.try_scan(unscanned[0])
if __name__ == "__main__":
import tkinter as tk
tk_root = tk.Tk()
tk_w = tk.Canvas(tk_root, width=800, height=600)
tk_w.pack()
SciAI = ScienceAI(tx,rx,tracker) # instantiate science AI instance
tracker.bind_to_updates(SciAI.tracker_callback) # call this func on obj updates
while True:
for packet in rx: # stream packets from the server
tracker.rx(packet) # Update the tracker with new information
tk_w.delete(tk.ALL) # clear canvas
for oid,odata in tracker.objects.items():
try:
x = odata['x']/150
z = odata['z']/150# - tracker.player_ship['z']
#print("(%d,%d)"%(x,z))
tk_w.create_rectangle(x,z,x+2,z+2,fill='blue')
except: pass
tk_root.update()
#pp.pprint(packet)
#pp.pprint(tracker.objects)
#print("")
#print("name\t enemy?\t side?")
for oid,odata in tracker.objects.items():
try:
print(odata["name"],"\t",odata["enemy?"],"\t",odata["side?"])
except: pass
#for k,v in tracker.player_ship.items():
# if k in ["science-target","scanning-id","scanning-progress"]:
# print(k,v)