Skip to content

Commit

Permalink
make part of a rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpobject committed Nov 28, 2015
1 parent 766a1a8 commit 60c6527
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 26 deletions.
70 changes: 62 additions & 8 deletions cards.lua
@@ -1,5 +1,6 @@
json = require("dkjson")
require("util")
require("stridx")

-- Most card data from disguised_monkey can be reused here.
-- So this is just the name and internal abilities.
Expand Down Expand Up @@ -176,19 +177,72 @@ for _,name in pairs(filenames) do
if not name_to_card[card.name] then
name_to_card[card.name] = card
end
if card.spec then
if spec_to_color[card.spec] then
assert(spec_to_color[card.spec] == card.color)
end
spec_to_color[card.spec] = card.color
end
end
end

local accept_fields = arr_to_set{
"ATK",
"ATK_1",
"ATK_2",
"ATK_3",
"HP",
"HP_1",
"HP_2",
"HP_3",
"color",
"cost",
"max_level",
"mid_level",
"spec",
"starting_zone",
"subtype",
"tech_level",
"type",
}
for id,card in pairs(id_to_card) do
local read_card = name_to_card[card.name]
for k,v in pairs(read_card) do
assert(k ~= "abilities")
card[k] = v
if accept_fields[k] then
if type(v) == "string" then
v = v:lower()
end
card[k] = v
end
end
end

for id,card in pairs(id_to_card) do
if card.spec then
if spec_to_color[card.spec] then
assert(spec_to_color[card.spec] == card.color)
end
spec_to_color[card.spec] = card.color
end

if card.type == "hero" then
card.subtype = nil
end

local types = card.type:split(" ")
for k,v in ipairs(types) do
if v == "legendary" then
card.legendary = true
elseif v == "ongoing" then
card.ongoing = true
elseif v == "ultimate" then
card.ultimate = true
elseif v == "minor" then
else
card.type = v
end
end

if card.type == "unit" or card.type == "spell" then
local subtypes = {}
if card.subtype then
subtypes = card.subtype:split(" ")
end
card.subtypes = subtypes
end
card.subtype = nil
end
112 changes: 94 additions & 18 deletions engine.lua
Expand Up @@ -2,7 +2,7 @@ require("cards")
require("class")
require("queue")

Player = class(function(self, specs, idx, game)
Player = class(function(self, specs, idx)
local main_spec = specs[1]
assert(main_spec, "No specs provided")
self.idx = idx
Expand All @@ -11,7 +11,6 @@ Player = class(function(self, specs, idx, game)
self.deck = {}
self.hand = {}
self.command = {}
self.field = {}
self.patrol = {}
self.future = {}
self.workers = 4
Expand All @@ -25,22 +24,24 @@ Player = class(function(self, specs, idx, game)
for id, card in pairs(id_to_card) do
if card.spec == spec then
if card.starting_zone == "codex" then
self.codex[#self.codex+1] = self:print_card(id)
self.codex[#self.codex+1] = self:print_card(id)
self.codex[#self.codex+1] = self:make_card(id)
self.codex[#self.codex+1] = self:make_card(id)
elseif card.starting_zone == "command" then
self.command[#self.command+1] = {uid=self:print_card(id), cooldown=0}
self.command[#self.command+1] = {uid=self:make_card(id), cooldown=0}
end
end
end
end
local main_color = spec_to_color[main_spec]
for id, card in pairs(id_to_card) do
if card.color == main_color and card.starting_zone == "deck" then
self.discard[#self.discard+1] = self:print_card(id)
self.discard[#self.discard+1] = self:make_card(id)
end
end
for k,v in pairs(self.id_to_next_uid) do
self.id_to_next_uid[k] = nil
end
self:draw(5)
self:clear_nontoken_uid_state()
end)

function Player:draw(n)
Expand All @@ -67,22 +68,15 @@ end
Game = class(function(self, specs1, specs2)
self.next_timestamp = 1
self.players = {Player(specs1, 1, self), Player(specs2, 2, self)}
self.field = {}
self.extra_turns = 0
self.next_actions = Queue()
self.next_steps = Queue()
self.active_player = 0
self:start_turn(1)
end)

function Player:clear_nontoken_uid_state()
for k,v in pairs(self.id_to_next_uid) do
if k[2] ~= "T" then
self.id_to_next_uid[k] = nil
end
end
end

function Player:print_card(card_id)
function Player:make_card(card_id)
local this_uid = self.id_to_next_uid[card_id] or 1
local this_uid_str = this_uid .. ""
if card_id[2] == "T" then
Expand All @@ -108,15 +102,97 @@ function Game:start_turn(idx)
self.next_steps:push("STEP_UPKEEP")
end

-- Just takes the internal state of a card and adds
-- The stuff you would get by reading the card.
-- Doesn't apply ongoing effects or w/e.
local function get_basic_card_state(card)
local ret = {}
if type(card) == "string" then
ret = {uid = card}
else
for k,v in pairs(card) do
ret[k] = v
end
end
local card_id = ret.uid:sub(1, 6)
local orig_card = id_to_card[card_id]

ret.type = orig_card.type
ret.ATK = orig_card.ATK
ret.HP = orig_card.HP
ret.color = orig_card.color
ret.cost = orig_card.cost
ret.name = orig_card.name
ret.spec = orig_card.spec
ret.tech_level = orig_card.tech_level

if orig_card.subtypes then
ret.subtypes = {}
for k,v in ipairs(orig_card.subtypes) do
ret.subtypes[k] = v
end
end

ret.abilities = deepcpy(orig_card.abilities or {})

if orig_card.type == "hero" then
ret.mid_level = orig_card.mid_level
ret.max_level = orig_card.max_level
ret.ATK_1 = orig_card.ATK_1
ret.ATK_2 = orig_card.ATK_2
ret.ATK_3 = orig_card.ATK_3
ret.HP_1 = orig_card.HP_1
ret.HP_2 = orig_card.HP_2
ret.HP_3 = orig_card.HP_3

ret.level = ret.level or 1
ret.ATK = ret.ATK_1
ret.HP = ret.HP_1

if ret.level >= ret.mid_level then
ret.ATK = ret.ATK_2
ret.HP = ret.HP_2
for _,ability in ipairs(orig_card.mid_abilities) do
ret.abilities[#ret.abilities+1] = deepcpy(ability)
end
end

if ret.level >= ret.max_level then
ret.ATK = ret.ATK_3
ret.HP = ret.HP_3
for _,ability in ipairs(orig_card.max_abilities) do
ret.abilities[#ret.abilities+1] = deepcpy(ability)
end
end
end

return ret
end

function Game:get_derived_state()
-- Get the derived state of the game
-- That's the state after applying ongoing effects and stuff

local ret = {}
ret.players = {}
for i=1,#self.players do
ret.players[i] = {}
for _,zone in ipairs({"codex", "discard", "deck", "hand", "command", "future"}) do
ret.players[i][zone] = {}
for j=1,#self.players[i][zone] do
ret.players[i][zone][j] = get_basic_card_state(self.players[i][zone][j])
end
end
end
ret.field = {}
for i=1,#self.field do
ret.field[i] = get_basic_card_state(self.field[i])
end
return ret
end

function Game:update()

--
end

print(json.encode(Game({"Bashing"},{"Finesse"})))
print(json.encode(Game({"bashing"},{"finesse"}):get_derived_state()))
22 changes: 22 additions & 0 deletions stridx.lua
@@ -0,0 +1,22 @@
local stringmt = getmetatable("")
local stringmt_idx = stringmt.__index
local type = type
assert(type(stringmt_idx) == "table")
stringmt.__index = function(t,k)
if type(k) == "number" then
local ret = t:sub(k,k)
if #ret == 0 then
return nil
end
return ret
end
return stringmt_idx[k]
end


function string:split(sep)
local sep, fields = sep or " ", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end

0 comments on commit 60c6527

Please sign in to comment.