Repository navigation
Expand file tree
/
Copy pathinit.lua
More file actions
549 lines (498 loc) · 16.6 KB
/
Copy pathinit.lua
File metadata and controls
549 lines (498 loc) · 16.6 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
local require=simpRequire('ZTC.')
Brik=require'briks'
require'rotsys_brik'
---@type Techmino.MechLib
mechLib={}
local modeLib={} ---@type Map<string | Techmino.Mode>
local modeMeta={
__index={
initialize=NULL,
settings={},
layout='default',
checkFinish=function() return true end,
result=NULL,
resultPage=NULL,
},
__metatable=true,
}
local layoutFuncs={}
do -- function layoutFuncs.default():
local defaultPosList={
alive={
[1]={main={0,0}},
[3]={main={0,0},
{580,100,.5},
{-580,100,.5},
},
[5]={main={0,0},
{-580,-240,.5},
{580,-240,.5},
{-580,240,.5},
{580,240,.5},
},
[7]={main={0,0},
{-580,-300,.34},{-580,0,.34},{-580,300,.34},
{580,-300,.34},{580,0,.34},{580,300,.34},
},
[17]={main={0,0},
{-680,-360,.26},{-680,-120,.26},{-680,120,.26},{-680,360,.26},
{-480,-360,.26},{-480,-120,.26},{-480,120,.26},{-480,360,.26},
{480,-360,.26},{480,-120,.26},{480,120,.26},{480,360,.26},
{680,-360,.26},{680,-120,.26},{680,120,.26},{680,360,.26},
},
[37]=(function()
local l={main={0,0}}
for y=-2.5,2.5 do
for x=0,2 do
table.insert(l,{-460-130*x,160*y,.17})
table.insert(l,{460+130*x, 160*y,.17})
end
end
return l
end)(),
[73]=(function()
local l={main={0,0}}
for y=-4,4 do
for x=0,3 do
table.insert(l,{-440-100*x,110*y,.13})
table.insert(l,{440+100*x, 110*y,.13})
end
end
return l
end)(),
[MATH.inf]={main={0,0}},
},
dead={
[1]={{0,0}},
[2]={
{-380,0,.9},{380,0,.9},
},
[3]={
{-520,0,.66},{-800,0,.66},{520,0,.66},
},
[4]={
{-590,0,.5},{-200,0,.5},{200,0,.5},{590,0,.5},
},
[15]=(function()
local l={}
for y=-1,1 do
for x=-2,2 do
table.insert(l,{315*x,320*y,.36})
end
end
return l
end)(),
[32]=(function()
local l={}
for y=-1.5,1.5 do
for x=-3.5,3.5 do
table.insert(l,{200*x,240*y,.25})
end
end
return l
end)(),
[72]=(function()
local l={}
for y=-2.5,2.5 do
for x=-5.5,5.5 do
table.insert(l,{130*x,160*y,.17})
end
end
return l
end)(),
[MATH.inf]={},
},
}
function layoutFuncs.default()
local mode=GAME.mainPlayer and defaultPosList.alive or defaultPosList.dead
local minCap=MATH.inf
for count in next,mode do
if count<=minCap and count>=#GAME.playerList then
minCap=count
end
end
local layoutData=mode[minCap]
local pos=1
for _,P in next,GAME.playerList do
if P.isMain then
P:setPosition(unpack(layoutData.main))
else
P:setPosition(unpack(layoutData[pos]))
pos=pos+1
end
end
end
end
---@class Techmino.Game
---@field playing boolean
---@field playerList Techmino.Player[] | false
---@field playerMap Map<Techmino.Player> | false
---@field camera Zenitha.Camera
---@field hitWaves table
---@field seed number | false
---@field mode Techmino.Mode | false
---@field mainID number | false
---@field mainPlayer Techmino.Player | false
local GAME={
playing=false,
playerList=false,
playerMap=false,
camera=GC.newCamera(),
hitWaves={},
seed=false,
mode=false,
mainID=false,
mainPlayer=false,
}
GAME.camera.moveSpeed=12
-- Basic sound events, this should be modified according to specific game designs
GAME.sndFunc={__metatable=true}
GAME.sndFunc.__index=GAME.sndFunc
for _,v in next,{
'move','move_down','move_failed',
'touch','lock','tuck',
'rotate','rotate_init','rotate_locked','rotate_corners','rotate_failed','rotate_special',
'hold','hold_init',
'drop','tech','clear_all',
'suffocate','desuffocate',
'finish_win','finish_suffocate','finish_lockout','finish_topout',
'finish_timeout','finish_rule','finish_exhaust','finish_taskfail','finish_other',
'beep_rise','beep_drop','beep_notice',
} do GAME.sndFunc[v]=function(vol) SFX.play(v,vol) end end
function GAME.sndFunc.countDown(num) SFX.play('countdown_'..MATH.clamp(num,1,3)) end
function GAME.sndFunc.clear(lines) SFX.play('clear_'..MATH.clamp(lines,1,5)) end
function GAME.sndFunc.spin(lines) SFX.play('spin_'..MATH.clamp(lines,0,3)) end
function GAME.sndFunc.combo(num) SFX.play('combo_'..MATH.clamp(num,1,10)) end
GAME.regFtoS,GAME.regStoF={},{}
---Flatten a table of functions into string-to-function and function-to-string maps
---@param obj table | function
---@param path string
function GAME.regFunc(obj,path)
if type(obj)=='function' or type(obj)=='table' and rawget(obj,'__register') then
GAME.regFtoS[obj]=path
GAME.regStoF[path]=obj
elseif type(obj)=='table' then
for k,v in next,obj do
if k~='__index' then
GAME.regFunc(v,path.."."..k)
end
end
end
end
GAME.regFunc(GAME.sndFunc,'GAME.sndFunc')
function GAME._refresh()
TABLE.clear(modeLib)
TABLE.update(mechLib,TABLE.linkSource({},require'mechanicLib',function(path) return FILE.load(path,'-lua') end,true))
GAME.regFunc(mechLib,'mechLib')
end
GAME._refresh()
--- Register mode to the modeLib
---@param name string
---@param path string
function GAME.addMode(name,path)
assert(type(name)=='string',"name must be string")
assert(not modeLib[name],"mode "..tostring(name).." already exists")
assert(type(path)=='string',"path must be string")
if not (love.filesystem.getInfo(path) and FILE.isSafe(path)) then LOG('warn',"No mode file named "..tostring(name)) end
modeLib[name]=path
end
function GAME.getMode(name)
local M=modeLib[name]
if type(M)=='table' then return M end
---@cast M string
-- Load & Cache the mode file
assert(love.filesystem.getInfo(M) and FILE.isSafe(M),"No mode file: "..tostring(name))
M=FILE.load(M,'-lua')
assert(type(M)=='table',"Mode file must return a table")
setmetatable(M,modeMeta)
assert(type(M.initialize)=='function',"[mode].initialize must be function")
assert(type(M.settings)=='table', "[mode].settings must be table")
assert(type(layoutFuncs[M.layout])=='function',"[mode].layout type wrong")
assert(type(M.checkFinish)=='function',"[mode].checkFinish must be function")
assert(type(M.result)=='function',"[mode].result must be function")
assert(type(M.resultPage)=='function',"[mode].resultPage must be function")
for _,plyType in next,{'brik','gela','acry'} do
if M.settings[plyType] then
GAME.regFunc(M.settings[plyType].event,name)
end
end
M.name=name
modeLib[name]=M
return M
end
function GAME.load(name,seed)
-- print("Game Loaded: "..mode.."-"..seed)
if GAME.mode then
MSG.log('warn',"Game is running")
return
end
GAME.playing=true
GAME.playerList={}
GAME.playerMap={}
GAME.mainPlayer=false
GAME.seed=seed or math.random(2^16,2^26)
GAME.mode=GAME.getMode(name)
if GAME.mode.initialize then GAME.mode.initialize() end
if #GAME.playerList==0 then
MSG.log('warn',"No players created in this mode")
else
if GAME.mainPlayer then
local conf=CONF['game_'..GAME.mainPlayer.gameMode]
if conf then GAME.mainPlayer:loadSettings(conf) end
end
if GAME.mode.settings then
for i=1,#GAME.playerList do
local conf=GAME.mode.settings[GAME.playerList[i].gameMode]
if conf then
GAME.playerList[i]:loadSettings(conf)
end
end
end
for i=1,#GAME.playerList do
GAME.playerList[i]:initialize()
GAME.playerList[i]:triggerEvent('playerInit')
end
layoutFuncs[GAME.mode['layout']]()
end
GAME.hitWaves={}
end
function GAME.unload()
-- print("Game Unloaded")
GAME.playing=false
GAME.playerList=false
GAME.playerMap=false
GAME.hitWaves={}
GAME.seed=false
GAME.mode=false
GAME.mainID=false
GAME.mainPlayer=false
end
---@param id integer
---@param pType 'brik' | 'gela' | 'acry'
---@param remote? boolean
function GAME.newPlayer(id,pType,remote)
if not (type(id)=='number' and math.floor(id)==id and id>=1 and id<=1000) then
MSG.log('error',"player id must be 1~1000 integer")
return
end
local P
if pType=='brik' then
P=require'brikPlayer'.new(remote)
elseif pType=='gela' then
P=require'gelaPlayer'.new(remote)
elseif pType=='acry' then
P=require'acryPlayer'.new(remote)
else
MSG.log('error',"invalid player type :'"..tostring(pType).."'")
return
end
P.gameMode=pType
P.id=id
P.team=0
GAME.playerMap[id]=P
table.insert(GAME.playerList,P)
end
function GAME.setMain(id)
if GAME.mainPlayer then
GAME.playerMap[GAME.mainPlayer].isMain=false
GAME.mainID=false
GAME.mainPlayer=false
end
if GAME.playerMap[id] then
GAME.mainID=id
GAME.mainPlayer=GAME.playerMap[id]
GAME.mainPlayer.isMain=true
GAME.mainPlayer.sound=true
end
end
function GAME.setTeam(id,teamID)
assert(type(teamID)=='number' and teamID>=0 and teamID%1==teamID,"Invalid team id")
if GAME.playerMap[id] then
GAME.playerMap[id].team=teamID
end
end
function GAME.press(action,id)
local p=id and GAME.playerMap[id] or GAME.mainPlayer
p:pressKey(action)
end
function GAME.release(action,id)
local p=id and GAME.playerMap[id] or GAME.mainPlayer
p:releaseKey(action)
end
function GAME.cursorDown(x,y,tid,id)
local p=id and GAME.playerMap[id] or GAME.mainPlayer
if p.gameMode=='acry' then
x,y=GAME.camera.transform:inverseTransformPoint(SCR.xOy_m:inverseTransformPoint(SCR.xOy:transformPoint(x,y)))
p:mouseDown(x,y,tid)
elseif CONF.system.touchControl then
VCTRL.press(x,y,tid)
end
end
function GAME.cursorMove(x,y,dx,dy,tid,id)
local p=id and GAME.playerMap[id] or GAME.mainPlayer
if p.gameMode=='acry' then
x,y=GAME.camera.transform:inverseTransformPoint(SCR.xOy_m:inverseTransformPoint(SCR.xOy:transformPoint(x,y)))
p:mouseMove(x,y,dx,dy,
tid or
IsMouseDown(1) and 1 or
IsMouseDown(2) and 2 or
3
)
elseif CONF.system.touchControl then
VCTRL.move(x,y,tid)
end
end
function GAME.cursorUp(x,y,tid,id)
local p=id and GAME.playerMap[id] or GAME.mainPlayer
if p.gameMode=='acry' then
x,y=GAME.camera.transform:inverseTransformPoint(SCR.xOy_m:inverseTransformPoint(SCR.xOy:transformPoint(x,y)))
p:mouseUp(x,y,tid)
elseif CONF.system.touchControl then
VCTRL.release(tid)
end
end
---@class Techmino.Game.Attack
---@field srcMode Techmino.Player.Type
---@field power number -∞~∞, no default
---@field sharpness number 0~∞, default to 1, how strong the attack can cancel the others
---@field hardness number 0~∞, default to 1, how strong the attack can defend the others
---@field mode number 0 | 1, default to 0, 0: trigger by time, 1:trigger by step
---@field time number 0~∞, default to 0, ms / step
---@field fatal number 0~100, default to 30, percentage
---@field speed number 0~100, default to 30, percentage
---@field target number | Techmino.Player | nil default to nil
local atkTemplate={
sharpness=1,
hardness=1,
mode=0,
time=0,
fatal=30,
speed=30,
}
---@param atk Techmino.Game.Attack
function GAME.initAtk(atk) -- Normalize the attack object
assert(type(atk)=='table',"GAME.initAtk: need table")
TABLE.updateMissing(atk,atkTemplate)
assert(type(atk.power)=='number',"GAME.initAtk: .power need number")
assert(type(atk.sharpness)=='number' and atk.sharpness>=0,"GAME.initAtk: .sharpness need non-negative number")
assert(type(atk.hardness)=='number' and atk.hardness>=0,"GAME.initAtk: .hardness need non-negative number")
assert(atk.mode==0 or atk.mode==1,"mode not 0 or 1")
assert(type(atk.time)=='number' and atk.time>=0,"GAME.initAtk: .time need non-negative number")
assert(type(atk.fatal)=='number',"GAME.initAtk: .fatal need number")
assert(type(atk.speed)=='number',"GAME.initAtk: .speed need number")
if atk.mode==1 then atk.time=math.floor(atk.time+.5) end
atk.fatal=MATH.clamp(math.floor(atk.fatal+.5),0,100)
atk.speed=MATH.clamp(math.floor(atk.speed+.5),0,100)
return atk
end
---@param sender Techmino.Player | number | false
---@param atk Techmino.Game.Attack?
function GAME.send(sender,atk)
if not atk then return end
---@type Techmino.Player
local target
-- Find target
if atk.target==nil then
---@type Techmino.Player[]
local l=GAME.playerList
local sourceTeam=sender and sender.team or 0 -- 0 means no team, not Team 0
if #l>1 then
local count=0
for i=1,#l do
if sourceTeam==0 and l[i]~=sender or sourceTeam~=l[i].team then
count=count+1
end
end
if count>0 then
count=math.random(count)
for i=1,#l do
if sourceTeam==0 and l[i]~=sender or sourceTeam~=l[i].team then
count=count-1
if count==0 then
target=l[i]
break
end
end
end
end
end
elseif type(atk.target)=='number' then
target=GAME.playerMap[atk.target]
else
error("GAME.send: invalid target type")
end
-- Sending airmail
if target then
target:receive(atk)
end
end
function GAME.checkFinish()
if GAME.playing and GAME.mode.checkFinish() then
GAME.playing=false
GAME.mode.result()
end
end
function GAME.update(dt)
if not GAME.playerList then return end
for _,P in next,GAME.playerList do P:update(dt) end
GAME.camera:update(dt)
for i=#GAME.hitWaves,1,-1 do
local wave=GAME.hitWaves[i]
wave.time=wave.time+dt
end
end
local _canvasSetting={stencil=true}
local gc_setCanvas,gc_setShader=GC.setCanvas,GC.setShader
local gc_replaceTransform,gc_applyTransform=GC.replaceTransform,GC.applyTransform
local gc_clear,gc_draw,gc_setColor=GC.clear,GC.draw,GC.setColor
function GAME.render()
if not GAME.playerList then return end
_canvasSetting[1]=ZENITHA.bigCanvas.player
gc_setCanvas(_canvasSetting)
gc_replaceTransform(SCR.xOy_m)
gc_applyTransform(GAME.camera.transform)
gc_clear(0,0,0,0)
for _,P in next,GAME.playerList do P:render() end
gc_setCanvas()
gc_replaceTransform(SCR.origin)
if GAME.hitWaves[1] then
local L=GAME.hitWaves
for i=1,#L do
local timeK=1/(400*L[i].time+50)-.0026
if timeK<=0 then
L[i][3]=0
else
L[i][3]=6.26-2.6*L[i].time
L[i][4]=math.cos(L[i].time*26)*L[i].power*timeK
end
end
SHADER.warp:send('hitWaves',unpack(L))
gc_setShader(SHADER.warp)
else
gc_setShader(SHADER.none) -- Directly draw the content, don't consider color, for better performance(?)
end
gc_setColor(1,1,1)
gc_draw(ZENITHA.bigCanvas.player)
gc_setShader()
end
function GAME._addHitWave(x,y,power)
if CONF.system.hitWavePower<=0 then return end
if #GAME.hitWaves>=8 then
local maxI=1
for i=2,#GAME.hitWaves do
if GAME.hitWaves[i].time>GAME.hitWaves[maxI].time then
maxI=i
end
end
table.remove(GAME.hitWaves,maxI)
end
table.insert(GAME.hitWaves,{
x,
y,
nil,
nil, -- power1 & power2, calculated before sending uniform
time=0,
power=power*CONF.system.hitWavePower,
})
end
return GAME