Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
trading.py
Go to the documentation of this file.
1 import vsrandom
2 import VS
3 import debug
4 
5 production={}
6 def getImports(name,faction):
7  try:
8  prodlist=[]
9  s = VS.LookupUnitStat(name,faction,"Cargo_Import")
10  while (len(s)):
11  where=s.find("{")
12  if (where==-1):
13  s=""
14  break;
15  else:
16  s=s[where+1:]
17  #debug.debug("beg: "+s)
18  where = s.find("}")
19  if (where==-1):
20  s=""
21  break;
22  else:
23  tmp=s[:where]
24  prodlist.append(tmp.split(";"))
25  s=s[where+1:]
26  if (len(prodlist[-1])>4):
27  try:
28  prodlist[-1][1]=float(prodlist[-1][1])
29  except:
30  prodlist[-1][1]=0.0
31  try:
32  prodlist[-1][2]=float(prodlist[-1][2])
33  except:
34  prodlist[-1][2]=0.0
35  try:
36  prodlist[-1][3]=float(prodlist[-1][3])
37  except:
38  prodlist[-1][3]=0.0
39  try:
40  prodlist[-1][4]=float(prodlist[-1][4])
41  except:
42  prodlist[-1][4]=0.0
43  #debug.debug("rest "+s)
44  #debug.debug("whole list: " +str(prodlist))
45  return prodlist
46  except:
47  import sys
48  debug.error("GetImportFailure\n"+str(sys.exc_info()[0])+str(sys.exc_info()[1]))
49  return []
50 def getExports(name,faction, twice=1000000):
51  prodlist=getImports(name,faction)
52  for i in range(len(prodlist)-1,-1,-1):
53  if prodlist[i][3]==0 and prodlist[i][4]<=3:
54  del prodlist[i]
55  elif prodlist[i][3]>twice:
56  prodlist.append(prodlist[i])
57  debug.debug("trading.getExports(%s,%s,%s)" %(name,faction,twice))
58  debug.debug("prodlist ="+str(prodlist))
59 
60  return prodlist
61 def getNoStarshipExports(name,faction,twice=10000):
62  prodlist=getExports(name,faction,twice)
63  for i in range(len(prodlist)-1,-1,-1):
64  if prodlist[i][0].find('upgrades')==0:
65  del prodlist[i]
66  elif prodlist[i][0].find('starships')==0:
67  del prodlist[i]
68  debug.debug("trading.getNoStarshipExports(%s,%s,%s)" %(name,faction,twice))
69  debug.debug("prodlist ="+str(prodlist))
70 
71  return prodlist
72 class trading:
73  def __init__(self):
74  self.last_ship=0
75  self.quantity=4
77  self.count=0
78  def SetPriceInstability(self, inst):
79  self.price_instability=inst
80 
81  def SetMaxQuantity (self,quant):
82  self.quantity=quant
83 
84  def Execute(self):
85  self.count+=1
86  if (self.count<3):
87  return
88  self.count=0
89  quant = (vsrandom.random()*(self.quantity-1))+1
90  un = VS.getUnit (self.last_ship)
91  if (un.isNull()):
92  self.last_ship=0
93  else:
94  if (un.isSignificant()):
95  if (un.isPlayerStarship()==-1):
96  global production
97  name = un.getName()
98  faction= un.getFactionName()
99  if un.isPlanet():
100  name = un.getFullname();
101  faction="planets"
102  prad=production.get((name,faction))
103  if None==prad:
104  prad= getImports(name,faction)
105  production[(name,faction)]=prad
106  if len(prad):
107  prod=prad[vsrandom.randrange(0,len(prad))]
108  cargo=VS.getRandCargo(int(prod[3]+prod[4]),prod[0])
109  if (cargo.GetCategory()==prod[0]):
110  removeCargo=False
111  if (prod[3] or prod[4]):
112  ownedcargo=un.GetCargo(cargo.GetContent())
113  quant=ownedcargo.GetQuantity()
114  #if un.getName()=="mining_base" and (cargo.GetContent()=="Tungsten" or cargo.GetContent()=="Space_Salvage"):
115  # debug.debug("Mining "+str(quant)+" from "+str(prod[3])+" to "+str(prod[4]))
116  if (quant<prod[3]-prod[4] or quant==0):
117  quant=int(prod[3]+vsrandom.uniform(-1,1)*prod[4])
118  #if un.getName()=="mining_base" and (cargo.GetContent()=="Tungsten" or cargo.GetContent()=="Space_Salvage"):
119  # debug.debug("Will add quant "+str(quant))
120  if (quant>0):
121  cargo.SetQuantity(quant)
122  price = prod[1]+vsrandom.uniform(-1,1)*prod[2]
123  cargo.SetPrice(cargo.GetPrice()*price)
124  debug.debug("Adding "+str(quant)+" of "+cargo.GetContent()+" cargo for "+str(price))
125  un.addCargo(cargo)
126  else:
127  removeCargo=True
128  elif quant>prod[3]+prod[4]:
129  removeCargo=True
130  else:
131  removeCargo=True
132  if removeCargo:
133  ownedcargo=un.GetCargo(cargo.GetContent())
134  if (ownedcargo.GetQuantity()):
135  debug.debug("Removing one "+ownedcargo.GetContent())
136 
137  un.removeCargo(ownedcargo.GetContent(),ownedcargo.GetQuantity()/3+1,0)
138  self.last_ship+=1
139