Sorry for the big code dump but I'm having a really strange issue with this code, where an instance method doesn't seem to be working.
In the "basic mutator", the process of adding a further item to the list via bareAddMutator works, but the similar one using c.addMutator does not work and does not even display the "In AddMutator.." message, as if the method is being missed. Yet the test earlier in the code indicates that the method does exist. I've tried looking at both the livescript and the compiled javascript and neither makes it clear why this might not work. Can anyone give any suggestion?
{take, take-while, drop, any, concat} = require 'prelude-ls'
[STR, DEX, INT, CON, WIS, CHA] = [0,1,2,3,4,5]
class ModifiedValue
(x) ->
@baseValue = x
@finalValue = x
@modifiers = []
addModifier: ((m,v) ->
@modifiers ++= [v, m]
@finalValue += v
)
class CharData
level: 0
statsBase: [0,0,0,0,0,0]
profMod: 0
race: null
subrace: null
speed: 0
profs: []
size: "Medium"
abilities: []
subRaceOptions: []
addMutator: ((m) !->
console.log "In AddMutator.."
@mutatorChain = bareAddMutator(@mutatorChain,m))
addMutator: ((ms) !-> bareAddMutators(@mutatorChain,ms))
runMutators: ( !->
console.log "Here"
@mutatorChain = [bm_basic]
while @mutatorChain[0]?
console.log "Here" + @mutatorChain
current = @mutatorChain[0]
@mutatorChain = drop 1, @mutatorChain
console.log "Running " + current.text + " .."
current.mutate(this)
)
class Mutator
(t, r, w, f) ->
@mutate = f
@reads = r
@writes = w
@text = t
mutate: (c) -> c
text: ""
bm_basic = new Mutator "Basic mutator", [], [], ((c) ->
console.log "Actually running it.."
c.addMutator bm_loadbasestats
c.mutatorChain = bareAddMutator(c.mutatorChain, bm_statstomods)
console.log c.mutatorChain
c.stats = [new ModifiedValue 0 for til 6]
if c.race? then c.addMutators c.race.mutators
if c.subrace? then c.addMutators c.subrace.mutators
console.log c.mutatorChain
)
bm_loadbasestats = new Mutator "Load basic stats", [], ["Stats"], ((c) ->
)
bm_statstomods = new Mutator "Calculate modifiers from basic stats", ["Stats"], ["Statmods"], ((c) ->
c.statsMod = [Math.trunc(stat.finalValue - 10 / 2) for stat in c.stats]
)
bm_leveltoprof = new Mutator "Calculate Proficiency mod from level", ["Level"], ["Profmod"], ((c) ->
c.profMod = (Math.trunc(c.level / 4)) + 2
)
class Race
(name, mutators) ->
@name = name
@mutators = mutators
class SubRace
(name, mutators) ->
@name = name
@mutators = mutators
bareAddMutator = ((mutatorChain, newMutator) ->
console.log "In bareAddMutator"
if mutatorChain.length == 0
[newMutator]
else
maxDepend = 0
for read in newMutator.reads
depends = take-while ((mut) -> read in mut.writes), mutatorChain
if depends.length > maxDepend then maxDepend = depends.length
for dep in depends
if any ((read) -> read in newMutator.writes), dep.reads
console.log "Can't add mutator " + newMutator + ", recursive dependency"
concat [(take maxDepend, mutatorChain), [newMutator], (drop maxDepend, mutatorChain)]
)
bareAddMutators = ((mutatorChain, mutatorList) ->
for mut in mutatorList
bareAddMutator(mutatorChain, mut)
)
srhDwarf = new SubRace "Hill Dwarf" [
new Mutator "Hill Dwarf Wisdom bonus", [], ["Stats"], ((c) ->
c.stats[WIS].addModifier("Hill Dwarf",1)
),
new Mutator "Hill Dwarf bonus HP", ["Level"], ["HPMax"], ((c) ->
c.hpMax += c.level
)]
srmDwarf = new SubRace "Mountain Dwarf" [
new Mutator "Mountain Dwarf Strength bonus", [], ["Stats"], ((c) ->
c.stats[STR].addModifier("Mountain Dwarf",1)
),
new Mutator "Mountain Dwarf armor proficiency", [], ["Profset"], ((c) ->
c.profs += ["Light", "Medium"]
)]
rDwarf = new Race "Dwarf" [
new Mutator "Dwarf base traits", [], ["Stats", "Size", "Speed"], ((c) ->
c.stats[CON].addModifier("Dwarf",2)
c.size = "Medium"
c.speed = 25
c.abilities += "Darkvision 60'"
c.abilities += "Advantage on saves vs. poison"
c.abilities += "Resistance against poison"
c.subRaceOptions = [srhDwarf, srmDwarf]
),
new Mutator "Dwarf ignores heavy armor penalty", [], [], ((c) ->
c = c
)]
srhElf = new SubRace "High Elf" [
new Mutator "High Elf traits", [], ["Stats","Profset"], ((c) ->
c.stats[INT].addModifier("High Elf",1)
c.profs += ["Longsword","Shortsword","Shortbow","Longbow"]
)]
srvElf = new SubRace "Wood Elf" [
new Mutator "Wood Elf traits", [], ["Stats","Profset","Speed","Abilities"], ((c) ->
c.stats[WIS].addModifier("Wood Elf",1)
c.profs ++= ["Longsword","Shortsword","Shortbow","Longbow"]
c.speed += 5
c.abilites += "Mask of the Wild"
)]
rElf = new Race "Elf" [
new Mutator "Elf base traits", [], ["Stats", "Size", "Speed"], ((c) ->
c.stats[DEX].addModifier("Elf",2)
c.size = "Medium"
c.speed = 30
c.abilities += "Darkvision 60'"
c.abilities += "Advantages on saves vs. charm"
c.abilities += "Immune to magical sleep"
c.abilities += "Trance"
c.subRaceOptions = [srhElf, srwElf]
)]
class DndClass
(name, basemutator, levelmutators) ->
@name = name
@basemutator = basemutator
@levelmutators = levelmutators
/*
mutatorChain = []
console.log [m.text for m in mutatorChain]
mutatorChain = addMutator mutatorChain, bm_loadbasestats
mutatorChain = addMutator mutatorChain, bm_statstomods
mutatorChain = addMutator mutatorChain, rDwarf.mutators[0]
console.log [m.text for m in mutatorChain]
*/
c = new CharData
if (c.addMutator?)
console.log "Method seems to be there.."
c.runMutators!