-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.py
105 lines (88 loc) · 3.19 KB
/
action.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
#!/usr/bin/env python
# encoding: utf-8
"""
action.py
Created by Morgan Mcclure on 2010-10-03.
Copyright (c) 2010 __MyCompanyName__. All rights reserved.
"""
import sys
import os
import unittest
import json
from unit import *
from structure import *
"""An action has
a cost
a time to perform it
a structure type needed to perform it
"""
units = json.load(open("./units.json", "r"))
structdata = json.load(open("./structures.json", "r"))
def makeAction(string):
if string.startswith('Build'):
string = string[6:]
print string
try:
act = BuildUnitAction(string)
except:
act = BuildStructAction(string)
#raise ValueError("Improperly formatted action string %s" %(string) )
return act
else:
raise ValueError("Invalid action string")
class Action(object):
"""This is a class that represents any user interaction possible"""
def __init__(self):
self.mineralcost = 0
self.vespenecost = 0
self.delay = 0
self.returns = None
def execute(self,simulation):
pass
class BuildUnitAction(Action):
def __init__(self, unittype):
super(BuildUnitAction, self).__init__()
self.unittype = unittype
self.MineralCost = units[unittype]["MineralCost"]
self.VespeneCost = units[unittype]["VespeneCost"]
self.delay = units[unittype]["BuildTime"]
def __str__(self):
return "Build %s" % self.unittype
def assign(self, simulation):
print "assign"
self.simulation = simulation
if simulation.wehave(units[self.unittype]['Requires']):
self.parentstruct = simulation.availableStruct(units[self.unittype]['BuiltBy'])
simulation.spend(self.MineralCost, self.VespeneCost)
self.parentstruct.queueAction(self)
def execute(self):
"""Raises an InProgressError, or appends the unit"""
sim = self.simulation
if self.returns is None:
#intentionally uncaught exception here
#If not enough supply, constructor throws exception
if self.unittype == WORKERTYPE:
self.returns = Worker(sim)
else:
self.returns = Unit(sim, self.unittype)
if self.returns.currentstat() is "Building":
raise InProgressError("Building Unit")
else:
sim.units.append(self.returns)
class BuildStructAction(Action):
def __init__(self, structtype):
super(BuildStructAction, self).__init__()
self.structtype = structtype
self.MineralCost = structdata[structtype]['MineralCost']
self.VespeneCost = structdata[structtype]["VespeneCost"]
self.delay = structdata[structtype]["BuildTime"]
def __str__(self):
return "Build %s" % self.structtype
def assign(self, simulation):
self.simulation = simulation
if simulation.wehave(structdata[self.structtype]['Requires']):
simulation.spend(self.MineralCost, self.VespeneCost)
thestruct = Structure(simulation, self.structtype)
simulation._lamestworker().build(thestruct)
if __name__ == '__main__':
foo = BuildUnitFactory("Zealot")