summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/gurps/gurps.lua
blob: 22273aeb30e12929037ded6cc5375f36aeeffc29 (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
require "gurps_tables"

function thrust_or_swing(typ, st)
  if st < 1 then
    return "0"
  end

  if _GTHRUSTSWINGTABLE[typ][st] then
    return _GTHRUSTSWINGTABLE[typ][st]
  else
    return thrust_or_swing(typ, st - 1)
  end
end

function thrust(st)
  return thrust_or_swing("thrust", st)
end

function swing(st)
  return thrust_or_swing("swing", st)
end

function print_dice(dice_no, modifier)
  tex.sprint([[\mbox{]])
  tex.sprint(dice_no .. "d")

  -- If `modifier` is a valid number

  local modifier_number = tonumber(modifier)
  if modifier_number then
    if modifier_number > 0 then
      tex.sprint("+" .. modifier_number)
    elseif modifier_number < 0 then
      tex.sprint("-" .. modifier_number)
    -- else, don't print anything
    end
  -- elseif `modifier` is non-nil
  elseif modifier then
    tex.sprint("+" .. modifier)
  end
  tex.sprint("}")
end

function valued_trait(value, points)
  return {
    value=value,
    points=points or "?"
  }
end

function value(value)
  return {
    value=value
  }
end

function trait(points)
  return {
    points=points or "?"
  }
end

function base_stat(stat, multiplier, default)
  default = default or 10
  stat = stat or default
  multiplier = multiplier or 10
  return valued_trait(stat, (stat - default)*multiplier)
end

-- Creates an attack table.
function attack(stat, damage, based_on)
  rettable = {
    stat=stat or "?",
    damage=damage or "?",
    based_on=based_on or "?"
  }

  -- i.e. if it's based on a real stat and that stat is a skill
  if based_on ~= "?" and character.skills[based_on] then
    rettable.adjustment = character.skills[based_on] - stat
  elseif based_on ~= "?" and character.spells[based_on] then
    rettable.adjustment = character.spells[based_on] - stat
  end

  return rettable
end

function create_character(args)
  -- Creates a character
  local args = args or {}
  local c = {}

  c.pointless_stats = {}
  c.pointless_stats.DR = value(args.DR or 0)
  c.pointless_stats.SM = value(args.SM or 0)

  c.base_stats = {
    ST=base_stat(args.ST, 10 - c.pointless_stats.SM.value),
    DX=base_stat(args.DX, 20),
    IQ=base_stat(args.IQ, 20),
    HT=base_stat(args.HT),
  }

  -- Gets the value for a base stat
  function gv(c, key)
    return c.base_stats[key].value
  end

  c.pointless_stats.thr = value(thrust(gv(c, "ST")))
  c.pointless_stats.sw = value(swing(gv(c, "ST")))

  c.base_stats.HP = base_stat(args.HP or gv(c, "ST"), 5, gv(c, "ST"))
  c.base_stats.Per = base_stat(args.Per or gv(c, "IQ"), 5, gv(c, "IQ"))
  c.base_stats.Will = base_stat(args.Will or gv(c, "IQ"), 5, gv(c, "IQ"))
  c.base_stats.FP = base_stat(args.FP or gv(c, "HT"), 5, gv(c, "HT"))

  -- c.advantages = {}
  -- c.disadvantages = {}
  c.advantages = args.advantages
  c.disadvantages = args.disadvantages
  -- TODO abstract the logic for spells into general skills
  c.skills = {}
  for name,obj in pairs(args.skills) do
    if obj.difficulty then
      x = split(obj.difficulty)
      -- TODO fix this to be spell points vv
      points = calculate_skill_points(c, x[1], x[2], obj.value)
      c.skills[name] = valued_trait(obj.value, points)
    else
      c.skills[name] = valued_trait(obj.value)
    end
  end
  c.spells = {}
  for name,obj in pairs(args.spells) do
    if obj.difficulty then
      x = split(obj.difficulty)
      -- TODO fix this to be spell points vv
      points = calculate_skill_points(c, x[1], x[2], obj.value)
      c.spells[name] = valued_trait(obj.value, points)
    else
      c.spells[name] = valued_trait(obj.value)
    end
  end
  c.attacks = {}

  return c
end

-- character = create_character()


function count_points()
  running_total = 0
  for i,traits in pairs({"base_stats",
                         "advantages",
                         "disadvantages",
                         "skills",
                         "spells"}) do
    if character[traits] then
      for j,v in pairs(character[traits]) do
        if v.points ~= "?" then
          running_total = running_total + v.points
        end
      end
    end
  end
  return running_total
end


function print_little_section(title, tbl)
  tex.print([[\paragraph{]] .. title .. [[}]])

  local x = {}
  for k,v in pairs(tbl) do
    if v.value == nil then
      table.insert(x, k .. "[" .. v.points .. "]")
    elseif v.points == nil then
        table.insert(x, k .. "~" .. v.value)
    else
      table.insert(x, k .. "~" .. v.value .. "[" .. v.points .. "]")
    end
  end

  if next(tbl) == nil then
    table.insert(x, [[\ldots{}]])
  end
  tex.print(table.concat(x, ", ") .. ".")
end


base_stats = {
  "ST", "DX", "IQ", "HT", "HP", "Per", "Will", "FP"
}


function print_character()
  tex.print([[\subsubsection{Stats (]] .. count_points() .. [[~pt)}]])
  tex.print([[\paragraph{Base stats}]])
  local x = {}
  for i, base_stat in ipairs(base_stats) do
    local obj = character.base_stats[base_stat]
    table.insert(
      x,
      base_stat .. [[~]] .. obj.value
        .. "[" .. obj.points .. "]")
  end
  tex.print(table.concat(x, ", ") .. ".")

  print_little_section("Other", character.pointless_stats)

  print_little_section("Advantages", character.advantages)
  print_little_section("Disadvantages", character.disadvantages)
  print_little_section("Skills", character.skills)
  if next(character.spells) then
    print_little_section("Spells", character.spells)
  end
end

function print_character_as_lens()
  tex.print([[\subsubsection{Lens (+]] .. count_points() .. [[~pt)}]])
  tex.print([[\paragraph{Base stats}]])
  local x = {}
  for i, base_stat in ipairs(base_stats) do
    local obj = character.base_stats[base_stat]
    if obj.value ~= 10 then
      table.insert(
        x,
        base_stat .. [[~+]] .. obj.value - 10
          .. "[" .. obj.points .. "]")
    end
  end
  tex.print(table.concat(x, ", ") .. ".")

  if next(character.advantages) ~= nil then
    print_little_section("Advantages", character.advantages)
  end
  if next(character.disadvantages) ~= nil then
    print_little_section("Disadvantages", character.disadvantages)
  end
  if next(character.skills) ~= nil then
    print_little_section("Skills", character.skills)
  end
  if next(character.spells) ~= nil then
    print_little_section("Spells", character.spells)
  end
end


-- Creates a new LaTeX command
-- 
-- TODO move this to the dtx file because that's where documentation for LaTeX
-- files lives
function print_set_cmd(name, extra_args)
  extra_args = extra_args or {"nil"}
  tex.print(
    string.format(
      [[\NewDocumentCommand\set%s{m}]]
        .. [[{\luadirect{character.%s = base_stat(#1, %s)}}]],
      name,
      name,
      table.concat(extra_args, ", ")
    )
  )
end

print_set_cmd("ST")
print_set_cmd("DX", {"20"})
print_set_cmd("IQ", {"20"})
print_set_cmd("HT")
print_set_cmd("HP", {"5", "character.base_stats.ST.value"})
print_set_cmd("Per", {"5", "character.base_stats.IQ.value"})
print_set_cmd("Will", {"5", "character.base_stats.IQ.value"})
print_set_cmd("FP", {"5", "character.base_stats.HT.value"})

--- Splits string (on / by default)
function split(s, split_on)
  local split_on = split_on or [[/]]
  local rettable = {}
  for i in string.gmatch(s, "[^" .. split_on .. "]+") do
    table.insert(rettable, i)
  end
  return rettable
end


function calculate_skill_points(character, based_on, difficulty, skill_level)
  -- TODO rename this variable
  if difficulty == "Easy" then
    difficulty_modifier = 0
  elseif difficulty == "Average" then
    difficulty_modifier = 1
  elseif difficulty == "Hard" then
    difficulty_modifier = 2
  elseif difficulty == "Very Hard" then
    difficulty_modifier = 3
  elseif difficulty == "Wildcard" then
    return 3*calculate_skill_points(based_on, "Very Hard", skill_level)
  end

  -- TODO make support for non-base stat based_on values
  local relative_level = skill_level - character.base_stats[based_on].value
  if relative_level == (0 - difficulty_modifier) then
    return 1
  elseif relative_level == (1 - difficulty_modifier) then
    return 2
  elseif relative_level == (2 - difficulty_modifier) then
    return 4
  elseif relative_level > (2 - difficulty_modifier) then
    return (relative_level - 1 + difficulty_modifier) * 4
  end
end