-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraria.py
287 lines (245 loc) · 6.91 KB
/
terraria.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from z3 import *
npcs = []
biomes = []
class npc(object):
def __init__(self, name):
self.name = name
self.sells = True
self.guide = False
self._loves = []
self._likes = []
self._dislikes = []
self._hates = []
self.near = {}
npcs.append(self)
def loves(self, *loves):
self._loves = loves
def likes(self, *likes):
self._likes = likes
def dislikes(self, *dislikes):
self._dislikes = dislikes
def hates(self, *hates):
self._hates = hates
guide = npc("Guide")
merchant = npc("Merchant")
zoologist = npc("Zoologist")
golfer = npc("Golfer")
nurse = npc("Nurse")
tavernkeep = npc("Tavernkeep")
party_girl = npc("Party girl")
wizard = npc("Wizard")
demolitionist = npc("Demolitionist")
goblin_tinkerer = npc("Goblin tinkerer")
clothier = npc("Clothier")
dye_trader = npc("Dye trader")
arms_dealer = npc("Arms dealer")
steampunker = npc("Steampunker")
dryad = npc("Dryad")
painter = npc("Painter")
witch_doctor = npc("Witch doctor")
stylist = npc("Stylist")
angler = npc("Angler")
pirate = npc("Pirate")
mechanic = npc("Mechanic")
tax_collector = npc("Tax collector")
cyborg = npc("Cyborg")
#santa = npc("Santa claus")
truffle = npc("Truffle")
class biome(object):
def __init__(self, name):
self.name = name
biomes.append(self)
forest = biome("Forest")
hallow = biome("Hallow")
underground = biome("Underground")
desert = biome("Desert")
jungle = biome("Jungle")
ocean = biome("Ocean")
snow = biome("Snow")
mushroom = biome("Mushroom")
guide.likes(forest, clothier, zoologist)
guide.dislikes(ocean, steampunker)
guide.hates(painter)
guide.sells = False
guide.guide = True
merchant.likes(forest, golfer, nurse)
merchant.dislikes(desert, tax_collector)
merchant.hates(angler)
zoologist.loves(witch_doctor)
zoologist.likes(forest, golfer)
zoologist.dislikes(desert, angler)
zoologist.hates(arms_dealer)
golfer.loves(angler)
golfer.likes(forest, painter, zoologist)
golfer.dislikes(underground, pirate)
golfer.hates(merchant)
nurse.loves(arms_dealer)
nurse.likes(hallow, wizard)
nurse.dislikes(snow, dryad, party_girl)
nurse.hates(zoologist)
nurse.sells = False
tavernkeep.loves(demolitionist)
tavernkeep.likes(hallow, goblin_tinkerer)
tavernkeep.dislikes(snow, guide)
tavernkeep.hates(dye_trader)
party_girl.loves(wizard, zoologist)
party_girl.likes(hallow, stylist)
party_girl.dislikes(underground, merchant)
party_girl.hates(tax_collector)
wizard.loves(golfer)
wizard.likes(hallow, merchant)
wizard.dislikes(ocean, witch_doctor)
wizard.hates(cyborg)
demolitionist.loves(tavernkeep)
demolitionist.likes(underground, mechanic)
demolitionist.dislikes(ocean, arms_dealer, goblin_tinkerer)
goblin_tinkerer.loves(mechanic)
goblin_tinkerer.likes(underground, dye_trader)
goblin_tinkerer.dislikes(jungle, clothier)
goblin_tinkerer.hates(stylist)
clothier.loves(truffle)
clothier.likes(underground, tax_collector)
clothier.dislikes(hallow, nurse)
clothier.hates(mechanic)
dye_trader.likes(desert, arms_dealer, painter)
dye_trader.dislikes(forest, steampunker)
dye_trader.hates(pirate)
arms_dealer.loves(nurse)
arms_dealer.likes(desert, steampunker)
arms_dealer.dislikes(snow, golfer)
arms_dealer.hates(demolitionist)
steampunker.loves(cyborg)
steampunker.likes(desert, painter)
steampunker.dislikes(jungle, dryad, wizard, party_girl)
dryad.likes(jungle, witch_doctor, truffle)
dryad.dislikes(desert, angler)
dryad.hates(golfer)
painter.loves(dryad)
painter.likes(jungle, party_girl)
painter.dislikes(forest, truffle, cyborg)
witch_doctor.likes(jungle, dryad, guide)
witch_doctor.dislikes(hallow, nurse)
witch_doctor.hates(truffle)
stylist.loves(dye_trader)
stylist.likes(ocean, pirate)
stylist.dislikes(snow, tavernkeep)
stylist.hates(goblin_tinkerer)
angler.likes(ocean, demolitionist, party_girl, tax_collector)
angler.hates(tavernkeep)
angler.sells = False
pirate.loves(angler)
pirate.likes(ocean, tavernkeep)
pirate.dislikes(underground, stylist)
pirate.hates(guide)
mechanic.loves(goblin_tinkerer)
mechanic.likes(snow, cyborg)
mechanic.dislikes(underground, arms_dealer)
mechanic.hates(clothier)
tax_collector.loves(merchant)
tax_collector.likes(snow, party_girl)
tax_collector.dislikes(hallow, demolitionist, mechanic)
#tax_collector.hates(santa)
tax_collector.sells = False
cyborg.likes(snow, steampunker, pirate, stylist)
cyborg.dislikes(jungle, zoologist)
cyborg.hates(wizard)
#santa.loves(snow)
#santa.hates(desert, tax_collector)
truffle.loves(guide)
truffle.likes(dryad)
truffle.dislikes(clothier)
truffle.hates(witch_doctor)
NPC = Datatype("NPC")
for n in npcs:
NPC.declare(n.name)
NPC = NPC.create()
for n in npcs:
n.ctr = getattr(NPC, n.name)
Biome = Datatype("Biome")
for b in biomes:
Biome.declare(b.name)
Biome = Biome.create()
for b in biomes:
b.ctr = getattr(Biome, b.name)
for n in npcs:
n.biome = Const(n.name + "_biome", Biome)
n.near = {}
for i in range(len(npcs)):
for j in range(i+1, len(npcs)):
near = Bool("near_" + npcs[i].name + "_" + npcs[j].name)
npcs[i].near[npcs[j].name] = near
npcs[j].near[npcs[i].name] = near
r = RealVal
def modifier(l, n, mod, result):
if isinstance(l, biome):
return result + If(l.ctr == n.biome, mod, 0)
elif isinstance(l, npc):
return result + If(n.near[l.name], mod, 0)
else:
raise
def happiness(npc):
result = 95
for l in npc._loves:
result = modifier(l, npc, -12, result)
for l in npc._likes:
result = modifier(l, npc, -6, result)
for l in npc._dislikes:
result = modifier(l, npc, 6, result)
for l in npc._hates:
result = modifier(l, npc, 12, result)
return If(result >= 150, 75,
If(result <= 75, 0,
result - 75))
def sells_pylon(npc):
if not npc.sells:
return False
return happiness(npc) <= 10
def biome_sells_pylon(biome):
accum = False
for n in npcs:
accum = Or(accum, And(sells_pylon(n), n.biome == biome.ctr))
return accum
total = 0
for n in npcs:
if not n.guide:
n.happiness = happiness(n)
total += n.happiness
o = Optimize()
o.add(truffle.biome == mushroom.ctr)
o.add(goblin_tinkerer.happiness <= 0)
o.add(tax_collector.happiness <= 2)
o.add(angler.happiness <= 2)
for b in biomes:
#o.add(biome_sells_pylon(b))
nbiome = 0
for n in npcs:
nbiome += If(n.biome == b.ctr, 1, 0)
o.add(nbiome <= 4)
for n in npcs:
nnear = 0
for n2 in npcs:
if n.name != n2.name:
o.add(Implies(n.near[n2.name], n.biome == n2.biome))
for n3 in npcs:
if n3.name != n2.name and n3.name != n.name:
o.add(Implies(And(n.near[n2.name], n2.near[n3.name]), n.near[n3.name]))
nnear += If(n.near[n2.name], 1, 0)
o.add(nnear < 3)
o.minimize(total)
print(o.check())
m = o.model()
print(m.eval(total))
for n in npcs:
print(n.name, ":", m[n.biome], "=", str(m.eval(happiness(n)).as_long() + 75) + "%", end=' ')
near=False
for n2 in npcs:
if n.name != n2.name:
if m[n.near[n2.name]]:
near=True
if near:
print("near", end=' ')
for n2 in npcs:
if n.name != n2.name:
if m[n.near[n2.name]]:
print(n2.name, end=' ')
print()