-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resources.go
338 lines (291 loc) · 10.1 KB
/
resources.go
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package game
import (
"fmt"
"github.com/BigJk/end_of_eden/lua/ludoc"
luhelp "github.com/BigJk/end_of_eden/lua/luhelp"
"github.com/samber/lo"
lua "github.com/yuin/gopher-lua"
"io/fs"
"log"
"path/filepath"
"strings"
)
// ResourcesManager can load Artifacts, Cards, Events, Enemy and StoryTeller data from lua.
// The manager will walk the ./scripts directory and evaluate all found .lua files.
type ResourcesManager struct {
Artifacts map[string]*Artifact
Cards map[string]*Card
Events map[string]*Event
Enemies map[string]*Enemy
StatusEffects map[string]*StatusEffect
StoryTeller map[string]*StoryTeller
luaState *lua.LState
luaDocs *ludoc.Docs
log *log.Logger
registered *lua.LTable
mapper *luhelp.Mapper
}
func NewResourcesManager(state *lua.LState, docs *ludoc.Docs, logger *log.Logger) *ResourcesManager {
man := &ResourcesManager{
log: logger,
luaState: state,
Artifacts: map[string]*Artifact{},
Cards: map[string]*Card{},
Events: map[string]*Event{},
Enemies: map[string]*Enemy{},
StatusEffects: map[string]*StatusEffect{},
StoryTeller: map[string]*StoryTeller{},
registered: state.NewTable(),
mapper: luhelp.NewMapper(state),
}
// Create global variable to access registered values in lua
lo.ForEach([]string{"artifact", "card", "enemy", "event", "status_effect", "story_teller"}, func(t string, _ int) {
man.registered.RawSetString(t, state.NewTable())
})
man.luaState.SetGlobal("registered", man.registered)
// Attach all register methods
man.luaState.SetGlobal("register_artifact", man.luaState.NewFunction(man.luaRegisterArtifact))
man.luaState.SetGlobal("register_card", man.luaState.NewFunction(man.luaRegisterCard))
man.luaState.SetGlobal("register_enemy", man.luaState.NewFunction(man.luaRegisterEnemy))
man.luaState.SetGlobal("register_event", man.luaState.NewFunction(man.luaRegisterEvent))
man.luaState.SetGlobal("register_status_effect", man.luaState.NewFunction(man.luaRegisterStatusEffect))
man.luaState.SetGlobal("register_story_teller", man.luaState.NewFunction(man.luaRegisterStoryTeller))
man.luaState.SetGlobal("delete_event", man.luaState.NewFunction(man.luaDeleteEvent))
man.defineDocs(docs)
// Load all local scripts
_ = filepath.Walk("./assets/scripts", func(path string, info fs.FileInfo, err error) error {
// Don't load libs
if strings.Contains(path, "scripts/libs") {
return nil
}
if err != nil {
return nil
}
if !info.IsDir() && strings.HasSuffix(path, ".lua") {
if err := man.luaState.DoFile(path); err != nil {
// TODO: error handling
panic(err)
}
}
return nil
})
return man
}
func (man *ResourcesManager) luaRegisterArtifact(l *lua.LState) int {
def := Artifact{
Callbacks: map[string]luhelp.OwnedCallback{},
}
if err := man.mapper.Map(l.ToTable(2), &def); err != nil {
man.log.Println("Error while luaRegisterArtifact:", err)
return 0
}
// Set id after evaluating the table to avoid ID overwrite
def.ID = l.ToString(1)
man.log.Println("Registered artifact:", def.ID, def.Name)
man.Artifacts[def.ID] = &def
man.registered.RawGetString("artifact").(*lua.LTable).RawSetString(def.ID, l.ToTable(2))
return 0
}
func (man *ResourcesManager) luaRegisterCard(l *lua.LState) int {
def := Card{
Callbacks: map[string]luhelp.OwnedCallback{},
}
if err := man.mapper.Map(l.ToTable(2), &def); err != nil {
man.log.Println("Error while luaRegisterCard:", err)
return 0
}
// Set id after evaluating the table to avoid ID overwrite
def.ID = l.ToString(1)
man.log.Println("Registered card:", def.ID, def.Name)
man.Cards[def.ID] = &def
man.registered.RawGetString("card").(*lua.LTable).RawSetString(def.ID, l.ToTable(2))
return 0
}
func (man *ResourcesManager) luaRegisterEnemy(l *lua.LState) int {
def := Enemy{
Callbacks: map[string]luhelp.OwnedCallback{},
}
if err := man.mapper.Map(l.ToTable(2), &def); err != nil {
man.log.Println("Error while luaRegisterEnemy:", err)
return 0
}
// Set id after evaluating the table to avoid ID overwrite
def.ID = l.ToString(1)
man.log.Println("Registered enemy:", def.ID, def.Name)
man.Enemies[def.ID] = &def
man.registered.RawGetString("enemy").(*lua.LTable).RawSetString(def.ID, l.ToTable(2))
return 0
}
func (man *ResourcesManager) luaRegisterEvent(l *lua.LState) int {
def := Event{}
if err := man.mapper.Map(l.ToTable(2), &def); err != nil {
man.log.Println("Error while luaRegisterEvent:", err)
return 0
}
// Set id after evaluating the table to avoid ID overwrite
def.ID = l.ToString(1)
man.log.Println("Registered event:", def.ID, def.Name)
man.Events[def.ID] = &def
man.registered.RawGetString("event").(*lua.LTable).RawSetString(def.ID, l.ToTable(2))
return 0
}
func (man *ResourcesManager) luaRegisterStatusEffect(l *lua.LState) int {
def := StatusEffect{
Callbacks: map[string]luhelp.OwnedCallback{},
}
if err := man.mapper.Map(l.ToTable(2), &def); err != nil {
man.log.Println("Error while luaRegisterStatusEffect:", err)
return 0
}
// Set id after evaluating the table to avoid ID overwrite
def.ID = l.ToString(1)
man.log.Println("Registered status_effect:", def.ID, def.Name)
man.StatusEffects[def.ID] = &def
man.registered.RawGetString("status_effect").(*lua.LTable).RawSetString(def.ID, l.ToTable(2))
return 0
}
func (man *ResourcesManager) luaRegisterStoryTeller(l *lua.LState) int {
def := StoryTeller{}
if err := man.mapper.Map(l.ToTable(2), &def); err != nil {
man.log.Println("Error while luaRegisterStoryTeller:", err)
return 0
}
// Set id after evaluating the table to avoid ID overwrite
def.ID = l.ToString(1)
man.log.Println("Registered story_teller:", def.ID)
man.StoryTeller[def.ID] = &def
man.registered.RawGetString("story_teller").(*lua.LTable).RawSetString(def.ID, l.ToTable(2))
return 0
}
func (man *ResourcesManager) luaDeleteEvent(l *lua.LState) int {
delete(man.Events, l.ToString(1))
man.registered.RawGetString("event").(*lua.LTable).RawSetString(l.ToString(1), lua.LNil)
return 0
}
func (man *ResourcesManager) defineDocs(docs *ludoc.Docs) {
if docs == nil {
return
}
docs.Category("Content Registry", "These functions are used to define new content in the base game and in mods.", 100)
docs.Function("register_artifact", fmt.Sprintf("Registers a new artifact.\n\n```lua\n%s\n```", `register_artifact("REPULSION_STONE",
{
name = "Repulsion Stone",
description = "For each damage taken heal for 2",
price = 100,
order = 0,
callbacks = {
on_damage = function(ctx)
if ctx.target == ctx.owner then
heal(ctx.owner, 2)
end
return nil
end,
}
}
)`), "", "id : String", "definition : Table")
docs.Function("register_card", fmt.Sprintf("Registers a new artifact.\n\n```lua\n%s\n```", `register_card("MELEE_HIT",
{
name = "Melee Hit",
description = "Use your bare hands to deal 5 (+3 for each upgrade) damage.",
state = function(ctx)
return "Use your bare hands to deal " .. highlight(5 + ctx.level * 3) .. " damage."
end,
max_level = 1,
color = "#2f3e46",
need_target = true,
point_cost = 1,
price = 30,
callbacks = {
on_cast = function(ctx)
deal_damage(ctx.caster, ctx.target, 5 + ctx.level * 3)
return nil
end,
}
}
)`), "", "id : String", "definition : Table")
docs.Function("register_enemy", fmt.Sprintf("Registers a new artifact.\n\n```lua\n%s\n```", `register_enemy("RUST_MITE",
{
name = "Rust Mite",
description = "Loves to eat metal.",
look = "/v\\",
color = "#e6e65a",
initial_hp = 22,
max_hp = 22,
gold = 10,
callbacks = {
on_turn = function(ctx)
if ctx.round % 4 == 0 then
give_status_effect("RITUAL", ctx.guid)
else
deal_damage(ctx.guid, PLAYER_ID, 6)
end
return nil
end
}
}
)`), "", "id : String", "definition : Table")
docs.Function("register_event", fmt.Sprintf("Registers a new artifact.\n\n```lua\n%s\n```", `register_event("SOME_EVENT",
{
name = "Event Name",
description = [[Flavor Text... Can include **Markdown** Syntax!]],
choices = {
{
description = "Go...",
callback = function()
-- If you return nil on_end will decide the next game state
return nil
end
},
{
description = "Other Option",
callback = function() return GAME_STATE_FIGHT end
}
},
on_enter = function()
play_music("energetic_orthogonal_expansions")
give_card("MELEE_HIT", PLAYER_ID)
give_card("MELEE_HIT", PLAYER_ID)
give_card("MELEE_HIT", PLAYER_ID)
give_card("RUPTURE", PLAYER_ID)
give_card("BLOCK", PLAYER_ID)
give_artifact(get_random_artifact_type(150), PLAYER_ID)
end,
on_end = function(choice)
-- Choice will be nil or the index of the choice taken
return GAME_STATE_RANDOM
end,
}
)`), "", "id : String", "definition : Table")
docs.Function("register_status_effect", fmt.Sprintf("Registers a new artifact.\n\n```lua\n%s\n```", `register_artifact("REPULSION_STONE",
{
name = "Repulsion Stone",
description = "For each damage taken heal for 2",
price = 100,
order = 0,
callbacks = {
on_damage = function(ctx)
if ctx.target == ctx.owner then
heal(ctx.owner, 2)
end
return nil
end,
}
}
)`), "", "id : String", "definition : Table")
docs.Function("register_story_teller", fmt.Sprintf("Registers a new artifact.\n\n```lua\n%s\n```", `register_artifact("REPULSION_STONE",
{
name = "Repulsion Stone",
description = "For each damage taken heal for 2",
price = 100,
order = 0,
callbacks = {
on_damage = function(ctx)
if ctx.target == ctx.owner then
heal(ctx.owner, 2)
end
return nil
end,
}
}
)`), "", "id : String", "definition : Table")
}