Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
dj_lib.py
Go to the documentation of this file.
1 import VS
2 import vsrandom
3 import debug
4 BATTLELIST=0
5 THREATLIST=BATTLELIST
6 PEACELIST=1
7 PANICLIST=2
8 VICTORYLIST=3
9 LOSSLIST=4
10 
11 BATTLESITUATION=0
12 THREATSITUATION=1
13 PEACESITUATION=2
14 PANICSITUATION=3
15 VICTORYSITUATION=4
16 LOSSSITUATION=5
17 
18 situation=PEACELIST
19 
20 factionmap={
21  "confed":"terran",
22  "aera":"aera",
23  "rlaan":"rlaan",
24  "merchant":"terran",
25  "luddites":"iso",
26  "pirates":"iso",
27  "hunter":"iso",
28  "homeland-security":"terran",
29  "ISO":"iso",
30  "unknown":"unknown",
31  "andolian":"terran",
32  "highborn":"terran",
33  "shaper":"terran",
34  "unadorned":"terran",
35  "purist":"terran",
36  "forsaken":"terran",
37  "LIHW":"iso",
38  "uln":"rlaan",
39  "dgn":"terran",
40  "klkk":"terran",
41  "mechanist":"terran",
42  "shmrn":"terran",
43  "rlaan_briin":"rlaan"
44  }
45 
46 HOSTILE_AUTODIST=10000
47 HOSTILE_NEWLAUNCH_DISTANCE=20000
48 SIGNIFICANT_DISTANCE_OFFSET=10000
49 SIGNIFICANT_DISTANCE_MULTIPLY=2
50 peaceawayfriendlylist={
51  "aera":VS.musicAddList('playlists/peace/away/aera_friend.m3u'),
52  "rlaan":VS.musicAddList('playlists/peace/away/rlaan_friend.m3u'),
53  "terran":VS.musicAddList('playlists/peace/away/terran_friend.m3u'),
54  "iso":VS.musicAddList('playlists/peace/away/iso_friend.m3u'),
55  None:PEACELIST
56  }
57 peaceawayhostilelist={
58  "aera":VS.musicAddList('playlists/peace/away/aera_hostile.m3u'),
59  "rlaan":VS.musicAddList('playlists/peace/away/rlaan_hostile.m3u'),
60  "terran":VS.musicAddList('playlists/peace/away/terran_hostile.m3u'),
61  "iso":VS.musicAddList('playlists/peace/away/iso_hostile.m3u'),
62  None:PEACELIST
63  }
64 peacearoundsiglist={
65  "aera":VS.musicAddList('playlists/peace/around_sig/aera.m3u'),
66  "rlaan":VS.musicAddList('playlists/peace/around_sig/rlaan.m3u'),
67  "terran":VS.musicAddList('playlists/peace/around_sig/terran.m3u'),
68  "iso":VS.musicAddList('playlists/peace/around_sig/iso.m3u'),
69  None:PEACELIST
70  }
71 threatlist={
72  "aera":VS.musicAddList('playlists/threat/aera.m3u'),
73  "rlaan":VS.musicAddList('playlists/threat/rlaan.m3u'),
74  "terran":VS.musicAddList('playlists/threat/terran.m3u'),
75  "iso":VS.musicAddList('playlists/threat/iso.m3u'),
76  None:THREATLIST
77  }
78 battlelist={
79  "aera":VS.musicAddList('playlists/battle/aera.m3u'),
80  "rlaan":VS.musicAddList('playlists/battle/rlaan.m3u'),
81  "terran":VS.musicAddList('playlists/battle/terran.m3u'),
82  "iso":VS.musicAddList('playlists/battle/iso.m3u'),
83  None:BATTLELIST
84  }
85 paniclist={
86  "aera":VS.musicAddList('playlists/battle/aera.m3u'),
87  "rlaan":VS.musicAddList('playlists/battle/rlaan.m3u'),
88  "terran":VS.musicAddList('playlists/battle/terran.m3u'),
89  "iso":VS.musicAddList('playlists/battle/iso.m3u'),
90  None:PANICLIST
91  }
92 asteroidlist=VS.musicAddList('asteroids.m3u')
93 
94 def LookupTable(list,faction):
95  if faction in factionmap:
96  faction_name = factionmap[faction]
97  else:
98  faction_name = faction
99  if faction_name in list:
100  if (list[faction_name]!=-1):
101  return list[faction_name]
102  else:
103  return list[None]
104  else:
105  return list[None]
106 
107 __enabled = True
108 
109 def enable():
110  global __enabled
111  if not __enabled:
112  debug.debug('DJ enabled.')
113  __enabled = True
114 
115 def disable():
116  global __enabled
117  if __enabled:
118  debug.debug('DJ disabled.')
119  __enabled = False
120 
121 def mpl (list,newsituation,forcechange):
122  global situation
123  debug.debug("SITUATION IS "+str( situation)+" force change "+str(forcechange) + " bool "+ str(forcechange or newsituation!=situation))
124  if (forcechange or newsituation!=situation):
125  debug.debug("SITUATION IS RESET TO "+str( newsituation))
126  situation=newsituation
127  VS.musicPlayList(list)
128 
129 def PlayMusik(forcechange=1,hostile_dist=0):
130  un = VS.getPlayer()
131  if not __enabled:
132  return
133  elif not un:
134  mpl (PEACELIST,PEACELIST,forcechange)
135  debug.debug("Ppeace")
136  elif un.DockedOrDocking() not in [1,2]:
137  perfect=1
138  threat=0
139  around_sig=0
140  iter = VS.getUnitList()
141  target = iter.current()
142  unfcount={}
143  siglist=[]
144  asteroid=0
145  while (iter.notDone()):
146  if (target):
147  ftmp = 2*target.getRelation(un)
148  nam=target.getName().lower()
149  fact=target.getFactionName()
150  if un.getSignificantDistance(target)<=2*target.rSize() and ('afield'==nam[:6] or 'asteroid'==nam[:8]):
151  asteroid=1
152  hdis = HOSTILE_AUTODIST
153  if (hostile_dist!=0):
154  hdis = hostile_dist
155  if (target.GetTarget()==un or (ftmp<0 and un.getDistance(target)<hdis)):
156  unfcount[fact] = unfcount.get(fact,0)+1
157  perfect=0
158  if ((target.GetTarget()==un) and (ftmp<0 and un.getDistance(target)<hdis)):
159  threat=1
160  if (target.isSignificant() and (un.getSignificantDistance(target)<(SIGNIFICANT_DISTANCE_OFFSET+SIGNIFICANT_DISTANCE_MULTIPLY*target.rSize()))):
161  around_sig=1
162  iter.advance()
163  target=iter.current()
164  if (perfect):
165  if asteroid and asteroidlist!=-1 and vsrandom.random()<.7:
166  mpl(asteroidlist,PEACELIST,forcechange)
167  return
168  sys=VS.getSystemFile()
169  fact=VS.GetGalaxyFaction(sys)
170  ufact=un.getFactionName()
171  if (around_sig):
172  mpl(LookupTable(peacearoundsiglist,fact),PEACESITUATION,forcechange)
173  debug.debug("peace-around-significant " + fact)
174  else:
175  if (VS.GetRelation(ufact,fact)>=0):
176  mpl(LookupTable(peaceawayfriendlylist,fact),PEACESITUATION,forcechange)
177  debug.debug("peace-away-frienly " + fact)
178  else:
179  mpl(LookupTable(peaceawayhostilelist,fact),PEACESITUATION,forcechange)
180  debug.debug("peace-away-hostile " + fact)
181  else:
182  ftmp = un.FShieldData()+2*un.GetHullPercent()+un.RShieldData()-2+0.5
183  mfname=None
184  mfcount=0
185  for (fname,fcount) in unfcount.iteritems():
186  if fcount>mfcount:
187  mfname=fname
188  mfcount=fcount
189  if (threat):
190  if (ftmp<0):
191  mpl(LookupTable(paniclist,mfname),PANICSITUATION,forcechange)
192  debug.debug("panic " + mfname)
193  else:
194  mpl(LookupTable(battlelist,mfname),BATTLESITUATION,forcechange)
195  debug.debug("battle " + mfname)
196  else:
197  mpl(LookupTable(threatlist,mfname),THREATSITUATION,forcechange)
198  debug.debug("threat " + mfname)
199 
200 class DJ:
201  def __init__(self, check_frequency, hostile_dist=0):
202  self.hostile_dist = hostile_dist
203  self.check_frequency = check_frequency
204  self.last_check_time = VS.GetGameTime()
205  def Execute(self):
206  if ((VS.GetGameTime() - self.last_check_time) >= self.check_frequency):
207  PlayMusik(0,self.hostile_dist)
208  self.last_check_time = VS.GetGameTime()