Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwp committed Jun 17, 2020
1 parent 3634276 commit df730c1
Show file tree
Hide file tree
Showing 38 changed files with 664 additions and 475 deletions.
2 changes: 1 addition & 1 deletion app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int main() {

auto alert_label = make_shared<Label>(font);
alert_label->setText("alert label");
alert_label->position = glm::vec3(200.f, 280.f, 0.f);
alert_label->position = glm::vec3(80.f, 280.f, 0.f);
gui_scene->addChild(alert_label);

auto gui_card_zoomed_view = make_shared<ZoomCardView>(card_model);
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/Side.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function Side:actionPayEvent(card, from)
return false
end

if not cardspec:canPlay(card.meta) then
if not card.meta:canPlay() then
return false
end

Expand All @@ -78,6 +78,6 @@ end
--- @param from string
--- @return boolean
function Side:actionPlayEvent(card, from)
cardspec:onPlay(card.meta)
card.meta:onPlay()
board:cardPop(from, card)
end
188 changes: 188 additions & 0 deletions app/scripts/cardspec/CardMeta.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
--- @class CardMeta
--- @field info CardInfo
--- @field until_forever table
--- @field until_turn_end table
--- @field until_run_end table
CardMeta = class()

--- @param info CardInfo
--- @return CardMeta
function CardMeta:New(info)
return construct(self, {
info = info,

rezzed = false,
until_turn_end = {},
until_run_end = {},
until_forever = {},
until_use = {},
})
end

--- @return string
function CardMeta:interactionFromHand()
local t = self.info.type_code
if t == "agenda" or t == "asset" or t == "ice" or t == "resource" or t == "hardware" or t == "program" then
return "install"
elseif t == "operation" or t == "event" then
return "play"
end
end

--- @return string
function CardMeta:interactionFromBoard()
local t = self.info.type_code
if t == "agenda" then
return "score"
elseif t == "asset" then
return "rez"
elseif t == "ice" then
return "rez"
end
end

-- predicates

--- @return boolean
function CardMeta:canAdvance()
if self.info.type_code == "agenda" then
return true
else
return false
end
end
--- @return boolean
function CardMeta:canAction()
if self.info.canAction then
return self.info.canAction(self)
else
return self.info.onAction ~= nil
end
end

--- @return boolean
function CardMeta:canPlay()
return self.info.canPlay == nil or self.info.canPlay(meta)
end

--- @return boolean
function CardMeta:canInstall()
return self:isCardRemote() or self:isCardIce()
end

--- @param meta CardMeta
--- @return boolean
function CardMeta:canBreakIce(meta)
return self.info.canBreakIce(meta)
end

--- @param slot string
--- @return boolean
function CardMeta:canInstallTo(slot)
if not self:canInstall() then
return false
end

if self.info.type_code == "ice" then
return isSlotIce(slot)
else
return isSlotRemote(slot)
end
end

--- @return boolean
function CardMeta:isCardRemote()
return self.info.type_code == "asset" or self.info.type_code == "agenda"
end

--- @return boolean
function CardMeta:isCardIce()
return self.info.type_code == "ice"
end

--- @return boolean
function CardMeta:isCardIcebreaker()
return self.info.type_code == "icebreaker"
end

--- @return boolean
function CardMeta:isCardProgram()
return self.info.type_code == "program"
end

--- @return boolean
function CardMeta:isCardHardware()
return self.info.type_code == "hardware"
end

--- @return boolean
function CardMeta:isCardResource()
return self.info.type_code == "resource"
end

--- @return boolean
function CardMeta:isCardConsole()
return self.info.type_code == "hardware" and self.info.keywords == "Console"
end

--- @param kws table<number, string>
--- @return boolean
function CardMeta:keywordsInclude(kws)
local str = self.info.keywords
for _, kw in pairs(kws) do
if string.find(str, kw) then
return true
end

local result = false
self:iterModifications(function (t)
if string.find(t.additional_keywords, kw) then
result = true
end
end)

if result then
return true
end
end

return false
end

--- @param func fun(description: string, effect: function)
function CardMeta:iterSubroutines(func)
for i = 1, #self.info.subroutines do
func(self.info.subroutine_descriptions[i], self.info.subroutines[i])
end
end

--- @param func fun(t: table)
function CardMeta:iterModifications(func)
func(self.until_forever)
func(self.until_turn_end)
func(self.until_run_end)
end

-- events

function CardMeta:onRez() if self.info.onRez then return self.info.onRez(self) end end
function CardMeta:onPlay() if self.info.onPlay then return self.info.onPlay(self) end end
function CardMeta:onAction() if self.info.onAction then return self.info.onAction(self) end end
function CardMeta:onInstall() if self.info.onInstall then return self.info.onInstall(self) end end
function CardMeta:onRemoval() if self.info.onRemoval then return self.info.onRemoval(meta) end end
function CardMeta:onScore() if self.info.onScore then return self.info.onScore(self) end end

--- @param meta CardMeta
function CardMeta:onNewTurn(meta)
self.until_turn_end = {}

if self.info.onNewTurn then return self.info.onNewTurn(self) end
end

function CardMeta:onUse()
self.until_use = {}
end

function CardMeta:onRunEnd(meta)
self.until_run_end = {}
end
56 changes: 56 additions & 0 deletions app/scripts/cardspec/Db.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--- @class Db
--- @field cards table<number, CardInfo>
--- @field card_titles table<string, number>
Db = {
cards = {},
card_titles = {},
}

--- @class CardInfo
--- @field code string
--- @field flavor string
--- @field keywords string
--- @field side_code string
--- @field title string
--- @field text string
--- @field type_code string

--- @param text string
--- @return table<number, Card>
function Db:_buildCardInfoArray(text)
local deck = {}
for line in text:gmatch("[^\n]+") do
local count = tonumber(line:sub(0, 1))
local title = line:sub(3)
for _ = 0, count do
table.insert(deck, self.cards[self.card_titles[title]])
end
end

return deck
end

--- @param uid number
--- @return Card
function Db:card(uid)
local info = self.cards[uid]
if not info then
error("Failed to find card %d", uid)
end

return Card(uid, CardMeta:New(info))
end

--- @param descr string
--- @return Deck
function Db:deck(descr)
local deck = Deck()
for _, info in pairs(self:_buildCardInfoArray(descr)) do
local card = self:card(tonumber(info.code))
card.faceup = false
deck:append(card)
end

return deck
end

Loading

0 comments on commit df730c1

Please sign in to comment.