-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathactions.cpp
More file actions
592 lines (573 loc) · 13 KB
/
Copy pathactions.cpp
File metadata and controls
592 lines (573 loc) · 13 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include "tools.h"
using namespace std;
struct Var{
string name;
string value;
};
struct VarList{
Var var[100];
int varNum;
void setValue(string aName, string aValue){
// 寻找变量是否存在,大小写不敏感
int find= -1;
for (int i= 0; i< varNum; i++){
if (upCase(aName)== upCase(var[i].name)){
find= i;
break;
}
}
string str1;
if (aValue[0]=='%'){
str1= getValue(aValue.substr(1));
} else {
str1= aValue;
}
if (find>=0){
// 如果找到,直接修改值
var[find].value= str1;
} else {
// 如果是新变量,则添加
var[varNum].name= aName;
var[varNum].value= str1;
varNum++;
}
}
string getValue(string aName){
// 寻找变量是否存在,大小写不敏感
int find= -1;
for (int i= 0; i< varNum; i++){
if (upCase(aName)== upCase(var[i].name)){
find= i;
break;
}
}
if (find>=0){
// 如果找到
return var[find].value;
} else {
// 如果是不存在
return "";
}
}
// 计算条件表达式(目前只支持最简单的)
bool calcRelaExp(string aExp){
for (int i= 0; i< varNum; i++){
//cout << var[i].name << " : " << var[i].value << endl;
}
//cout << aExp << endl;
string str1, str2;
split2(aExp, '=', str1, str2);
string vst1, vst2;
if (str1!=""){
if (str1[0]=='%'){
vst1= getValue(str1.substr(1));
} else {
vst1= str1;
}
//cout << "a1: " << str1 << endl;
//cout << "a2: " << str2 << endl;
//cout << "b1: " << vst1 << endl;
if (str2==""){
return str2int(vst1);
} else {
if (str2[0]=='%'){
vst2= getValue(str2.substr(1));
} else {
vst2= str2;
}
//cout << "b2: " << vst2 << endl;
return (vst2==vst1);
}
} else {
return false;
}
}
};
struct Pos{
int x, y;
void setValue(string aStr){
string str1, str2;
split2(aStr, ',', str1, str2);
x= str2int(str1);
y= str2int(str2);
}
};
struct ActionList;
struct Event;
struct World{
VarList varList;
int CurrentMap;
Pos hero;
bool menuOn;
virtual void changeMap(string aName, int x, int y){}
virtual void showMap(){}
virtual void showMapPart(int x, int y, int w, int h){}
virtual void showMenu(string aName, int x, int y, int aLockMode){}
virtual void showSpecMenu(string aName, int x, int y, ActionList *aCode){}
virtual void talk(string Who, string Words, int aWait){}
virtual void save(){}
virtual void load(string fName){}
virtual int fightOn(string aTeam, ActionList *win, ActionList *lose){}
virtual void callBackFunc(){}
virtual void Info(){}
virtual void InitVar(){}
virtual void updateNPC(){}
virtual void hideNPC(){}
virtual void hideEvent(Event *evt){}
};
struct Action{
string cmd;
Event *event;
World *world;
virtual void execute(){
if (upCase(cmd)=="EXIT"){
exit(0);
} else if (upCase(cmd)=="SAVE"){
world->save();
} else if (upCase(cmd)=="INIT"){
world->InitVar();
} else if (upCase(cmd)=="HIDESELF"){
world->hideEvent(event);
} else if (upCase(cmd)=="INFO"){
world->Info();
} else if (upCase(cmd)=="CALLBACK"){
world->callBackFunc();
} else if (upCase(cmd)=="UPDATENPC"){
world->updateNPC();
} else if (upCase(cmd)=="HIDENPC"){
world->hideNPC();
}
}
virtual void show(){
cout << "cmd: " << cmd << endl;
}
};
// 由于在两个地方用到类似的结构,且希望用一个函数统一处理
// 故建立结构
struct ActionList{
// 这里必须用指针,可以指向其继承对象
Action *cmd[50];
int cmdNum;
void show(){
cout << "指令数: " << cmdNum << " {"<<endl;
for (int i=0; i< cmdNum; i++){
cmd[i]->show();
}
cout << "}" << endl;
}
void execute(){
for (int i=0; i< cmdNum; i++){
gotoxy(0, 31);
cmd[i]->execute();
}
}
};
struct CLoad:Action{
string Name;
void show(){
cout << "load from file " << Name <<endl;
}
void execute(){
//cout << Name << endl;
world->load(world->varList.getValue(Name));
}
};
struct CJump:Action{
string mapName;
Pos newp;
void show(){
cout << "jump to " << mapName << ":" << newp.x << "-" << newp.y <<endl;
}
void execute(){
world->changeMap(mapName, newp.x, newp.y);
}
};
struct CTalk:Action{
string Name[20];
string Speach[20];
int sentNum;
void execute(){
for (int i=0; i< sentNum; i++){
world->talk(Name[i], Speach[i], 0);
}
world->showMap();
}
void show(){
cout << "Talk of " << sentNum << endl;
for (int i=0; i< sentNum; i++){
cout << " sent " << Name[i] << ":" << Speach[i] << endl;
}
}
};
struct CVar:Action{
string Name;
string Value;
void show(){
cout << "变量赋值 " << Name << "," << Value << endl;
}
void execute(){
world->varList.setValue(Name, Value);
}
};
struct CMenu:Action{
string Name;
Pos mpos;
int Mode; // 锁定模式
void show(){
cout << "打开菜单 " << Name << endl;
}
void execute(){
world->showMenu(Name, mpos.x, mpos.y, Mode);
}
};
// 特殊菜单
struct CSpecMenu:Action{
string Name; // 类型
Pos mpos;
ActionList onClick;
void show(){
cout << "打开特殊菜单 " << Name << endl;
}
void execute(){
//onClick.show();
world->showSpecMenu(Name, mpos.x, mpos.y, &onClick);
}
};
struct CIf:Action{
string exp;
ActionList block;
ActionList elseblock;
void show(){
cout << "if (" << exp << ")"<<endl;
block.show();
cout << "else" << endl;
elseblock.show();
}
void execute(){
//cout << exp;
if (world->varList.calcRelaExp(exp)) {
block.execute();
} else {
elseblock.execute();
}
}
};
struct CFight:Action{
string team;
ActionList win;
ActionList lose;
void show(){
cout << "fight win with (" << team << ")"<<endl;
win.show();
cout << "lose" << endl;
lose.show();
}
void execute(){
//cout << exp;
world->fightOn(team, &win, &lose);
}
};
struct Event{
int trigMode; // 1=走到触发
// 2=探索(撞击)触发
// 3=随机走动的探索触发
// 4=自动触发
// 5=随机行走的接触触发
Pos pos[20]; // 触发位置,支持多个位置
int posNum; // 位置数量
ActionList block;
string chart; // NPC图像
int color; // 图像的颜色
bool enabled; // 默认是打开的,可以关闭,即隐藏
// 通过一个字符串,增加一个位置点
void addPos(string aStr){
pos[posNum].setValue(aStr);
posNum++;
}
void addPosList(string aStr){
string str1, str2;
while(true) {
split2(aStr, ';', str1, str2);
addPos(str1);
if (aStr != str1) {
aStr= str2;
} else {
break;
}
}
}
//
bool check(int aMode, int x, int y){
if (trigMode== aMode){
for(int i= 0; i< posNum; i++){
if (pos[i].x==x && pos[i].y==y){
return true;
}
}
}
return false;
}
//
bool isNear(int aMode, int x, int y){
if (trigMode== aMode){
for(int i= 0; i< posNum; i++){
if ((pos[i].x==x+2 && pos[i].y==y) ||
(pos[i].x==x-2 && pos[i].y==y) ||
(pos[i].x==x && pos[i].y==y+1) ||
(pos[i].x==x && pos[i].y==y-1) ){
//
//gotoxy(70, 8);
//cout << x << "," << y << " ";
return true;
}
}
}
return false;
}
//
void show(){
block.show();
}
};
// 这是一个通用函数,返回值,是为了帮助if...else...endif这种判断
int readActions(ifstream &ff, ActionList &block, string aStop, World *wd, Event *evt){
block.cmdNum= 0;
while (!ff.eof()){
string str1, str2;
getline(ff, str1);
if (str1==""){
continue;
} else if (str1[0]==';'){
// 分号开头代表注释
continue;
} else if (upCase(str1)==upCase(aStop)){
break;
} else if (str1[0]=='@'){
// 一个新的指令的开始
str1= str1.substr(1);
if (upCase(str1)=="JUMP"){
//cout << "jump" << endl;
CJump *jump= new CJump();
block.cmd[block.cmdNum]= jump;
jump->world= wd;
getline(ff, str2);
jump->mapName= str2;
getline(ff, str2);
jump->newp.setValue(str2);
} else if (upCase(str1)=="EXIT" || upCase(str1)=="SAVE" ||
upCase(str1)=="INIT" || upCase(str1)=="HIDESELF" ||
upCase(str1)=="INFO" || upCase(str1)=="UPDATENPC" ||
upCase(str1)=="HIDENPC" ){
Action *act= new Action();
block.cmd[block.cmdNum]= act;
act->cmd= upCase(str1);
act->world= wd;
} else if (upCase(str1)=="LOAD"){
CLoad *cld= new CLoad();
cld->world= wd;
block.cmd[block.cmdNum]= cld;
getline(ff, str2);
cld->Name= str2;
} else if (upCase(str1)=="MENU" || upCase(str1)=="LOCKMENU"){
CMenu *cmn= new CMenu();
cmn->world= wd;
block.cmd[block.cmdNum]= cmn;
getline(ff, str2);
cmn->Name= str2;
getline(ff, str2);
cmn->mpos.setValue(str2);
if (upCase(str1)=="LOCKMENU"){
cmn->Mode= 1; // 锁定模式的菜单不可关闭,如封面菜单
} else {
cmn->Mode= 0; // 普通模式,不锁定
}
} else if (upCase(str1)=="SPECMENU"){
CSpecMenu *cmn= new CSpecMenu();
cmn->world= wd;
block.cmd[block.cmdNum]= cmn;
getline(ff, str2);
cmn->Name= str2;
getline(ff, str2);
cmn->mpos.setValue(str2);
readActions(ff, cmn->onClick, "@endspec", wd, evt);
} else if (upCase(str1)=="TALK"){
//cout << "talk" << endl;
CTalk *talk= new CTalk();
talk->world= wd;
block.cmd[block.cmdNum]= talk;
talk->sentNum= 0;
do{
getline(ff, str2);
if (str2[0]!='@'){
split2(str2, ',', talk->Name[talk->sentNum], talk->Speach[talk->sentNum]);
talk->sentNum++;
} else {
break;
}
} while (1);
} else if (upCase(str1)=="VAR"){
//cout << "var" << endl;
CVar *cvar= new CVar();
cvar->world= wd;
block.cmd[block.cmdNum]= cvar;
getline(ff, str2);
split2(str2, '=', cvar->Name, cvar->Value);
} else if (upCase(str1)=="IF"){
CIf *cif= new CIf();
cif->world= wd;
block.cmd[block.cmdNum]= cif;
getline(ff, str2);
cif->exp= str2;
int a= readActions(ff, cif->block, "@endif", wd, evt);
if (a==1) {
// 说明的读到@ELSE结束的,继续读
readActions(ff, cif->elseblock, "@endif", wd, evt);
}
} else if (upCase(str1)=="FIGHT"){
CFight *cfit= new CFight();
cfit->world= wd;
block.cmd[block.cmdNum]= cfit;
getline(ff, str2);
cfit->team= str2;
int a= readActions(ff, cfit->win, "@endfight", wd, evt);
if (a==2) {
// 说明的读到@ELSE结束的,继续读
readActions(ff, cfit->lose, "@endfight", wd, evt);
}
} else if (upCase(str1)=="ELSE"){
// 出现else,如果不是语法错误
//cout << "else"<< endl;
if (upCase(aStop)=="@ENDIF") {
// 这是正常的语法,前一个block完成
// cout << "else2"<< endl;
return 1;
} else if (upCase(aStop)=="@ENDFIGHT") {
return 2;
} else {
cout << "else 错误:" << str1;
throw 1;
}
} else {
cout << "未知指令:"+ str1 << endl;
throw 2;
}
block.cmd[block.cmdNum]->world= wd;
block.cmd[block.cmdNum]->event= evt;
block.cmdNum++;
} else {
// 这里也是报错
cout << "指令错误:" << str1 << endl;
throw 3;
}
}
return 0;
}
struct EventList{
Event event[50];
int evtNum;
void readEvent(string aName, World *wd){
//cout << "Event file: " << aName << endl;
//system("pause");
ifstream fin(aName.c_str());
if (fin){
while (!fin.eof()){
string str1, str2;
getline(fin, str1);
if (str1==""){
continue;
} else if (str1[0]=='#'){
// 一个新的事件开始
event[evtNum].trigMode= (str1[1]- 'A')+ 1;
event[evtNum].enabled= true;
//
if (str1.size()>6){
int a= str1.find(":");
if (a>0){
string str2= getBetween(str1, "(", ")");
string str3, str4;
split2(str2, ',', str3, str4);
event[evtNum].chart= str3;
event[evtNum].color= str2int(str4);
} else {
event[evtNum].chart= "女";
event[evtNum].color= 10;
}
} else {
event[evtNum].chart= "男";
event[evtNum].color= 12;
}
// 后面必然是位置定义
getline(fin, str2);
event[evtNum].addPosList(str2);
readActions(fin, event[evtNum].block, "#END", wd, &event[evtNum]);
//event[evtNum].show();
evtNum++;
}
}
}
fin.close();
//system("pause");
}
// 调试的必备函数
void show(){
for (int i=0; i< evtNum; i++){
cout << "Event " << i << endl;
event[i].show();
}
}
// 根据坐标,定位event
Event* findEvent(int aMode, int x, int y){
for (int i=0; i< evtNum; i++){
if (event[i].check(aMode, x, y)){
return &event[i];
}
}
return 0;
}
Event* nearEvent(int aMode, int x, int y){
for (int i=0; i< evtNum; i++){
if (event[i].isNear(aMode, x, y)){
return &event[i];
}
}
return 0;
}
// 直接运行
void runEvent(int aMode, int x, int y){
Event *evt;
evt= findEvent(aMode, x, y);
if (evt) {
if (evt->enabled){
evt->block.execute();
}
} else if (aMode==5) {
evt= nearEvent(aMode, x, y);
if (evt) {
if (evt->enabled){
evt->block.execute();
}
}
}
}
// 特殊操作,NPC刷新
void updateEvent(int aMode){
for (int i=0; i< evtNum; i++){
if (event[i].trigMode==aMode){
event[i].enabled= true;
}
}
}
// 更加特殊的操作,隐藏所有NPC
void hideNPC(int aMode){
for (int i=0; i< evtNum; i++){
if (event[i].trigMode==aMode){
event[i].enabled= false;
}
}
}
};