-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand.lua
More file actions
245 lines (203 loc) · 7.62 KB
/
hand.lua
File metadata and controls
245 lines (203 loc) · 7.62 KB
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
--[[
function to control the cards in hand
inserting, removing, cleaning, sorting etc.
]]
local CARD = require("card")
Hand = {
cards = {},
returnSpeed = 5,
desperateMeasuresFound = false --probably should be elsewhere and isnt very modular but fine for now
}
function Hand:createStartingHand()
self:clear()
-- this is the initial card amounts for a game
self:addCards({ money = 2, cultist = 2, food = 2, prisoner = 2 })
end
function Hand:addCards(resourceTable)
-- recieves a table as in the function above
-- Insert cards for each resource type and count
for resource, count in pairs(resourceTable) do
for _ = 1, count do
local newcard = CARD.create(resource:sub(1, 1))
table.insert(self.cards, newcard)
end
end
end
function Hand:checkIfCanRemove(resourceTable, treatPrisonerCultistAsSame)
-- Count available selected cards by resource name
local available = {}
for idx, _ in pairs(resourceTable) do
available[idx] = 0
end
for _, card in ipairs(self.cards) do
if card.selected then
local key = card.resource.name
available[key] = (available[key] or 0) + 1
end
end
-- Calculate total required cards and total selected cards
local totalRequired = 0
local totalSelected = 0
for _, count in pairs(resourceTable) do
totalRequired = totalRequired + (count or 0)
end
for _, card in ipairs(self.cards) do
if card.selected then
totalSelected = totalSelected + 1
end
end
-- If total selected doesn't match total required, return false immediately
if totalSelected ~= totalRequired then
return false
end
-- First check if exact requirements are met
local cultistPreReq = (available["cultist"] or 0) == (resourceTable["cultist"] or 0)
local prisonerPreReq = (available["prisoner"] or 0) == (resourceTable["prisoner"] or 0)
local otherPreReqs = (available["money"] or 0) == (resourceTable["money"] or 0)
and (available["food"] or 0) == (resourceTable["food"] or 0)
and (available["suspicion"] or 0) == (resourceTable["suspicion"] or 0)
and (available["relic"] or 0) == (resourceTable["relic"] or 0)
-- Check if requirements are met exactly
if otherPreReqs and prisonerPreReq and cultistPreReq then
return true
end
-- Check prisoner/cultist interchangeability
if treatPrisonerCultistAsSame then
if (available["cultist"] or 0) + (available["prisoner"] or 0) == (resourceTable["cultist"] or 0) and otherPreReqs then
return true
end
end
-- Relic wildcard logic - relics can substitute for missing resources
local availableRelics = available["relic"] or 0
local requiredRelics = resourceTable["relic"] or 0
local extraRelics = availableRelics - requiredRelics
if extraRelics > 0 then
-- Calculate shortfall for each resource type
local shortfall = 0
-- Check money shortfall
if (available["money"] or 0) < (resourceTable["money"] or 0) then
shortfall = shortfall + ((resourceTable["money"] or 0) - (available["money"] or 0))
end
-- Check food shortfall
if (available["food"] or 0) < (resourceTable["food"] or 0) then
shortfall = shortfall + ((resourceTable["food"] or 0) - (available["food"] or 0))
end
-- Check suspicion shortfall
if (available["suspicion"] or 0) < (resourceTable["suspicion"] or 0) then
shortfall = shortfall + ((resourceTable["suspicion"] or 0) - (available["suspicion"] or 0))
end
-- Check cultist/prisoner shortfall
if treatPrisonerCultistAsSame then
local totalCultistPrisoner = (available["cultist"] or 0) + (available["prisoner"] or 0)
local requiredCultist = resourceTable["cultist"] or 0
if totalCultistPrisoner < requiredCultist then
shortfall = shortfall + (requiredCultist - totalCultistPrisoner)
end
else
if (available["cultist"] or 0) < (resourceTable["cultist"] or 0) then
shortfall = shortfall + ((resourceTable["cultist"] or 0) - (available["cultist"] or 0))
end
if (available["prisoner"] or 0) < (resourceTable["prisoner"] or 0) then
shortfall = shortfall + ((resourceTable["prisoner"] or 0) - (available["prisoner"] or 0))
end
end
-- If we have enough extra relics to cover the shortfall, return true
if extraRelics >= shortfall then
return true
end
end
return false
end
function Hand:removeCards()
idxToRemove = {}
for idx, card in ipairs(self.cards) do
if card.selected then
table.insert(idxToRemove, idx)
end
end
USEFUL.invertTable(idxToRemove)
for _, rem in ipairs(idxToRemove) do
table.remove(self.cards, rem)
end
end
function Hand:checkPunishments()
if tutorialMode then
return
end
local punishments = {}
if self:Count() > 15 then
-- greed, if you have more than 15 cards
table.insert(punishments, "greed")
end
if self:getNumResource("food") < 1 then -- less than one to be super safe
-- desperateMeasures, if you have 0 food
table.insert(punishments, "desperateMeasures")
end
if self:getNumResource("suspicion") > 4 then
-- police raid, if you have more than 4 suspicion
table.insert(punishments, "policeRaid")
end
if punishments[1] then
local randNum = math.random(100)
if randNum <= 50 then -- randomly deciding whether to place the punishment as the next card
-- this has been set as 50%, it seems to not be known what is is in the original game
-- shuffle the table and choose the first, if you are in danger of multiple punishments this decides which one you get first randomly
-- punishments do have weight in the events. community says its unused so i wont use it here
punishments = USEFUL.fisherYates(punishments)
table.insert(decks.deck, 1, EVENTS.punishments[punishments[1]])
end
end
end
function Hand:Count()
return #self.cards
end
function Hand:clear()
-- emptys the hand
self.cards = {}
end
function Hand:sort()
-- sorts the hand based off the value in the base resources table
-- order should be relic, money, cultist, food, prisoner, suspicion
table.sort(self.cards, function(x, y)
return x.resource.order < y.resource.order
end)
end
function Hand:isSorted()
-- checks if the hand is already sorted so cards arent flapping around constantly
for i = 2, #self.cards do
if self.cards[i - 1].resource.order > self.cards[i].resource.order then
return false
end
end
return true
end
function Hand:getNumResource(resource)
-- checks how many of the specified resource is in the hand
-- used for variableRequirements checks
if not self.cards then
return 0
end
local count = 0
for _, card in ipairs(self.cards) do
if card.resource.name == resource then
count = count + 1
end
end
return count
end
function Hand:returnCards()
-- returns all cards to the hand area from the playarea
for _, card in ipairs(self.cards) do
if card.selected then
card.selected = false
end
end
end
function Hand:cardsInHand()
local resourceCountTable = {}
for _, card in ipairs(self.cards) do
table.insert(resourceCountTable, card.resource.name)
end
return resourceCountTable
end
return Hand