forked from coolsee/OpenMugen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdManager.cpp
408 lines (368 loc) · 16.5 KB
/
CmdManager.cpp
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
#include "global.h"
CCmdManager::CCmdManager( int keyBufferSize )
{
m_Commands = NULL;
m_CommandCount = 0;
m_CurrCommandName = NULL;
m_KeyBuffer = new PLCOMMANDFRAMEINPUT[ keyBufferSize ];
m_KeyBufferSize = keyBufferSize;
m_KeyIndex = 0;
memset( m_KeyBuffer, 0, keyBufferSize );
}
CCmdManager::~CCmdManager()
{
delete []m_Commands;
delete m_KeyBuffer;
}
bool CCmdManager::LoadCMDFile( const char* file )
{
int defaultCommandTime = 15;
int defaultBufferTime = 1;
m_CommandCount = 0;
CTokenizer tok;
//changed this to throw a exception
if( !tok.OpenFile( file ) )
{
throw(CError("CCmdManager::LoadCMDFile : Can't open %s",file));
return false;
}
// get count first to set up memory
while( !tok.AtEndOfFile() )
{
bool foundSomething = false;
if( tok.CheckToken( "command.time" ) )
{
foundSomething = true;
if( !tok.CheckToken( "=" ) )
{
}
if( tok.CheckTokenIsNumber() )
{
defaultCommandTime = tok.GetInt();
}
}
if( tok.CheckToken( "command.buffer.time" ) )
{
foundSomething = true;
if( !tok.CheckToken( "=" ) )
{
}
if( tok.CheckTokenIsNumber() )
{
defaultBufferTime = tok.GetInt();
}
}
if( tok.CheckToken( "[" ) )
{
foundSomething = true;
if( tok.CheckToken( "Command" ) )
{
if( tok.CheckToken( "]" ) )
{
m_CommandCount++;
}
}
}
if( !foundSomething )
{
tok.GetToken(); // skip it
}
}
tok.CloseFile();
if( !tok.OpenFile( file ) )
return false;
m_Commands = new PLCOMMAND[ m_CommandCount ];
PLCOMMAND* command = m_Commands;
while( !tok.AtEndOfFile() )
{
bool foundCommand = false;
if( tok.CheckToken( "[" ) )
{
if( tok.CheckToken( "Command" ) )
{
if( !tok.CheckToken( "]" ) )
{
}
foundCommand = true;
command->nCommandTime = defaultCommandTime;
command->nBufferTime = defaultBufferTime;
command->strCommand[0] = 0;
command->nHowManyCommand = 0;
while( command->nHowManyCommand < MAXCOMMAND && !tok.CheckToken( "[", false ) && !tok.AtEndOfFile() )
{
if( tok.CheckToken( "name" ) )
{
if( !tok.CheckToken( "=" ) )
{
}
strcpy( command->strCommand, tok.GetToken() );
}
else if( tok.CheckToken( "command" ) )
{
if( !tok.CheckToken( "=" ) )
{
}
while( !tok.AtEndOfLine() )
{
const char* token = tok.GetToken();
if( !strcmp( token, "~" ) )
{
command->nCommand[ command->nHowManyCommand ].keyModifier += PLC_KEYMOD_ON_RELEASE;
if( tok.CheckTokenIsNumber() )
{
command->nCommand[ command->nHowManyCommand ].gameTicksForHold = tok.GetInt();
}
else
{
command->nCommand[ command->nHowManyCommand ].gameTicksForHold = 1;
}
}
else if( !strcmp( token, "+" ) )
{
}
else if( !strcmp( token, "/" ) )
{
command->nCommand[ command->nHowManyCommand ].keyModifier += PLC_KEYMOD_MUST_BE_HELD;
}
else if( !strcmp( token, "$" ) )
{
command->nCommand[ command->nHowManyCommand ].keyModifier += PLC_KEYMOD_DETECT_AS_4WAY;
}
else if( !strcmp( token, ">" ) )
{
command->nCommand[ command->nHowManyCommand ].keyModifier += PLC_KEYMOD_BAN_OTHER_INPUT;
}
else if( !strcmp( token, "," ) )
{
command->nHowManyCommand++;
}
else if( !strcmp( token, "D" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_DOWN );
}
else if( !strcmp( token, "U" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_UP );
}
else if( !strcmp( token, "B" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_LEFT );
}
else if( !strcmp( token, "F" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_RIGHT );
}
else if( !strcmp( token, "DB" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_DOWN ) + PLC_KEYCODE( KEY_LEFT );
}
else if( !strcmp( token, "DF" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_DOWN ) + PLC_KEYCODE( KEY_RIGHT );
}
else if( !strcmp( token, "UF" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_UP ) + PLC_KEYCODE( KEY_LEFT );
}
else if( !strcmp( token, "UB" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_UP ) + PLC_KEYCODE( KEY_LEFT );
}
else if( !strcmp( token, "a" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_A );
}
else if( !strcmp( token, "b" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_B );
}
else if( !strcmp( token, "c" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_C );
}
else if( !strcmp( token, "x" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_X );
}
else if( !strcmp( token, "y" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_Y );
}
else if( !strcmp( token, "z" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_Z );
}
else if( !strcmp( token, "s" ) )
{
command->nCommand[ command->nHowManyCommand ].keyCode += PLC_KEYCODE( KEY_BUTTON_START );
}
}
command->nHowManyCommand++;
}
else if( tok.CheckToken( "time" ) )
{
if( !tok.CheckToken( "=" ) )
{
}
if( !tok.CheckTokenIsNumber() )
{
}
command->nCommandTime = tok.GetInt();
}
else if( tok.CheckToken( "buffer.time" ) )
{
if( !tok.CheckToken( "=" ) )
{
}
if( !tok.CheckTokenIsNumber() )
{
}
command->nBufferTime = tok.GetInt();
}
}
command++;
}
}
if( !foundCommand )
tok.GetToken(); // skip it
}
tok.CloseFile();
return true;
}
void CCmdManager::Update( KEYBOARDDATA* keys, bool facingRight )
{
m_CurrCommandName = NULL;
m_KeyBuffer[ m_KeyIndex ].keyBitfield = 0; // buffer是用来存储上次的键值
m_KeyBuffer[ m_KeyIndex ].gameTicks = m_pTimer->GetGameTime();
// add current keystrokes to input buffer
for( int k = 0; k < KEY_COUNT; k++ )
{
if( keys->keyInfo[ k ].isPressed )
{
if( ( k == KEY_LEFT ) && !facingRight )
{
m_KeyBuffer[ m_KeyIndex ].keyBitfield += PLC_KEYCODE( KEY_RIGHT );
}
else if( ( k == KEY_RIGHT ) && !facingRight )
{
m_KeyBuffer[ m_KeyIndex ].keyBitfield += PLC_KEYCODE( KEY_LEFT );
}
else
{
m_KeyBuffer[ m_KeyIndex ].keyBitfield += PLC_KEYCODE( k );
}
}
}
PLCOMMAND* currCommand = m_Commands;
for( int a = 0; a < m_CommandCount; a++ )// 遍历所有命令
{
int nTime = -1, nLastTime = -1;
int currKeyIndex = 0;
// 使用例子command = ~D, DF, F, x讲解
// 最初从x判定,按下,且与要求一致,将记录最后的时间,然后currKeyIndex++,将buffer判定向前推进
// F,DF,一致的时候向前走,直到~D一致,记录第一时间,判定是否超出。
for( int b = currCommand->nHowManyCommand - 1; b >= 0; b-- )// 一个命令,判断是否匹配所有按键
{
bool bCommand = false;
bool onRelease = (( currCommand->nCommand[ b ].keyModifier & PLC_KEYMOD_ON_RELEASE ) != 0 );// 按键是否需要放开
bool onHold = (( currCommand->nCommand[ b ].keyModifier & PLC_KEYMOD_MUST_BE_HELD ) != 0 );// 是否要求按住
bool use4Way = (( currCommand->nCommand[ b ].keyModifier & PLC_KEYMOD_DETECT_AS_4WAY ) != 0 );// 是否是四个方向
bool banOtherInput = (( currCommand->nCommand[ b ].keyModifier & PLC_KEYMOD_BAN_OTHER_INPUT ) != 0 );// 是否禁止有其他按键按下
int gameTicksToHold = currCommand->nCommand[ b ].gameTicksForHold; // 按键按下的时间
int keyCode = currCommand->nCommand[ b ].keyCode;// 按键转码
for( ; currKeyIndex < m_KeyBufferSize; currKeyIndex++ )// 每个键对所有buff处理!!!!
{
PLCOMMANDFRAMEINPUT* frameInput = &m_KeyBuffer[ AdjustKeyIndex( m_KeyIndex, -currKeyIndex ) ];// 从当前的输入键值,向前遍历
bool keyDown = (( frameInput->keyBitfield & keyCode ) == keyCode );
if( keyDown && !use4Way )// 需要按键按下,不是四个方向
{
int keyCodeDirs = ( keyCode & PLC_ALL_DIRECTIONS_BITFIELD );
int frameInputDirs = ( frameInput->keyBitfield & PLC_ALL_DIRECTIONS_BITFIELD );
keyDown = !keyCodeDirs || ( keyCodeDirs == frameInputDirs );//按下的按钮是否是方向键,是则要与需求一致,不是,则正确,继续判定
}
bool buttonConditionsMet = false;
// see how long it's been held
if( onRelease != keyDown )// 按键松开,并保持一定时间时间(keyDown&&!onRelease)
{
int gameTicksHeld = 0;
for( int k = currKeyIndex + 1; k < m_KeyBufferSize; k++ )
{
PLCOMMANDFRAMEINPUT* frameInput2 = &m_KeyBuffer[ AdjustKeyIndex( m_KeyIndex, -k ) ];// 从当前的输入键值,向前遍历
bool keyDown2 = (( frameInput2->keyBitfield & keyCode ) == keyCode );
if( keyDown2 && !use4Way )// 按键按下,不是四个方向
{
int keyCodeDirs = ( keyCode & PLC_ALL_DIRECTIONS_BITFIELD );
int frameInputDirs = ( frameInput2->keyBitfield & PLC_ALL_DIRECTIONS_BITFIELD );
keyDown2 = !keyCodeDirs || ( keyCodeDirs == frameInputDirs );
}
if( keyDown2 )
{
gameTicksHeld++;
if( onHold )
{
buttonConditionsMet = keyDown;
break;
}
else if( onRelease )
{
if( gameTicksHeld >= gameTicksToHold )
{
buttonConditionsMet = true;
break;
}
}
else
{
buttonConditionsMet = ( b < currCommand->nHowManyCommand - 1 );
break;
}
}
else
{
buttonConditionsMet = !( onHold || onRelease );
break;
}
}
}
// 如果单键过关了,就记录开始结束时间,继续下一个按键,
if( buttonConditionsMet )
{
//if its the first element store the time of it
if( b == 0 )
{
nTime = frameInput->gameTicks;
}
if( b == ( currCommand->nHowManyCommand - 1 ))
{
nLastTime = frameInput->gameTicks;
}
bCommand = true;
currKeyIndex++;
break;
}
}
if( !bCommand )
break;
}
if( ( nTime >= 0 ) && ( nLastTime > 0 ) )
{
// the last button of the sequenz must be pressed int the Current game tick to
// be valid and then it must be check for how long it has taken to do the input
// int gameTicks = GetGameTicks();
// bufer时间,即时命令有效的tick时间
if( ( nLastTime > ( m_pTimer->GetGameTime()- currCommand->nBufferTime )) && ( nLastTime - nTime ) <= currCommand->nCommandTime )
{
m_CurrCommandName = currCommand->strCommand;
PrintMessage("time:%5d, %s",m_pTimer->GetGameTime(), m_CurrCommandName);
break;
}
}
currCommand++;
}
if( ++m_KeyIndex >= m_KeyBufferSize )
m_KeyIndex = 0;
}
const char* CCmdManager::GetCurrentCommandName()
{
return m_CurrCommandName;
}