Skip to content

Commit 0990ddf

Browse files
committed
feat: update koshien blocks
1 parent ba43f4f commit 0990ddf

File tree

6 files changed

+187
-158
lines changed

6 files changed

+187
-158
lines changed

src/lib/ruby-generator/koshien.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,87 @@ export default function (Generator) {
1010
};
1111

1212
Generator.koshien_getMapArea = function (block) {
13-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
14-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
15-
return `koshien.get_map_area(${x}, ${y})\n`;
13+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
14+
return `koshien.get_map_area(${position})\n`;
1615
};
1716

1817
Generator.koshien_map = function (block) {
19-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
20-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
21-
return [`koshien.map(${x}, ${y})`];
18+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
19+
return [`koshien.map(${position})`];
2220
};
2321

2422
Generator.koshien_moveTo = function (block) {
25-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
26-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
27-
return `koshien.move_to(${x}, ${y})\n`;
23+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
24+
return `koshien.move_to(${position})\n`;
2825
};
2926

3027
Generator.koshien_calcRoute = function (block) {
31-
const srcX = Generator.valueToCode(block, 'SRC_X', Generator.ORDER_NONE) || 0;
32-
const srcY = Generator.valueToCode(block, 'SRC_Y', Generator.ORDER_NONE) || 0;
33-
const dstX = Generator.valueToCode(block, 'DST_X', Generator.ORDER_NONE) || 0;
34-
const dstY = Generator.valueToCode(block, 'DST_Y', Generator.ORDER_NONE) || 0;
35-
const exceptCells = Generator.quote_(
36-
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE) || ' '
28+
const src = Generator.valueToCode(block, 'SRC', Generator.ORDER_NONE) || Generator.quote_('0:0');
29+
const dst = Generator.valueToCode(block, 'DST', Generator.ORDER_NONE) || Generator.quote_('0:0');
30+
const exceptCellsListName = Generator.listNameByName(
31+
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE)
3732
);
38-
const result = Generator.quote_(
39-
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
33+
const exceptCells = exceptCellsListName ? `list(${Generator.quote_(exceptCellsListName)})` : 'nil';
34+
const resultListName = Generator.listNameByName(
35+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
4036
);
37+
const result = resultListName ? `list(${Generator.quote_(resultListName)})` : 'nil';
4138

42-
// eslint-disable-next-line max-len
43-
return `koshien.calc_route(src: [${srcX}, ${srcY}], dst: [${dstX}, ${dstY}], except_cells: ${exceptCells}, result: ${result})\n`;
39+
return `koshien.calc_route(result: ${result}, src: ${src}, dst: ${dst}, except_cells: ${exceptCells})\n`;
4440
};
4541

4642
Generator.koshien_setItem = function (block) {
4743
const item = Generator.getFieldValue(block, 'ITEM') || null;
48-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
49-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
50-
return `koshien.set_${item}(${x}, ${y})\n`;
44+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
45+
return `koshien.set_${item}(${position})\n`;
5146
};
5247

53-
Generator.koshien_loadMap = function (block) {
54-
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
55-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
56-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
57-
return [`koshien.load_map(${location}, ${x}, ${y})`];
48+
Generator.koshien_mapFrom = function (block) {
49+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
50+
const mapVariableName = Generator.variableNameByName(
51+
Generator.getFieldValue(block, 'MAP', Generator.ORDER_NONE)
52+
);
53+
const map = mapVariableName ? mapVariableName : 'nil';
54+
return [`koshien.map_from(${position}, ${map})`];
5855
};
5956

60-
Generator.koshien_saveMapAll = function (block) {
61-
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
62-
return `koshien.save_map_all(${location})\n`;
57+
Generator.koshien_mapAll = function () {
58+
return ['koshien.map_all'];
6359
};
6460

6561
Generator.koshien_locateObjects = function (block) {
66-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
67-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
62+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
6863
const sqSize = Generator.valueToCode(block, 'SQ_SIZE', Generator.ORDER_NONE) || 5;
6964
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('A B C D');
70-
const result = Generator.quote_(
71-
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
65+
const resultListName = Generator.listNameByName(
66+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
7267
);
68+
const result = resultListName ? `list(${Generator.quote_(resultListName)})` : 'nil';
7369

7470
// eslint-disable-next-line max-len
75-
return `koshien.locate_objects(sq_size: ${sqSize}, cent: [${x}, ${y}], objects: ${objects}, result: ${result})\n`;
71+
return `koshien.locate_objects(result: ${result}, sq_size: ${sqSize}, cent: ${position}, objects: ${objects})\n`;
7672
};
73+
7774
Generator.koshien_targetCoordinate = function (block) {
7875
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
7976
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
8077
return [`koshien.${target}_${coordinate}`];
8178
};
79+
8280
Generator.koshien_turnOver = function () {
8381
return `koshien.turn_over\n`;
8482
};
85-
Generator.koshien_coordinateOf = function (block) {
86-
const where = Generator.valueToCode(block, 'WHERE', Generator.ORDER_NONE) || Generator.quote_('0:0');
83+
84+
Generator.koshien_position = function (block) {
85+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
86+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
87+
return [`koshien.position(${x}, ${y})`];
88+
};
89+
90+
Generator.koshien_positionOf = function (block) {
91+
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
8792
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
88-
return [`koshien.coordinate_of_${coordinate}(${where})`];
93+
return [`koshien.position_of_${coordinate}(${position})`];
8994
};
9095

9196
return Generator;

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,17 +522,17 @@ class RubyToBlocksConverter {
522522
}
523523

524524
isBlock (block) {
525-
return this._isBlock(block);
526-
}
527-
528-
_isBlock (block) {
529525
try {
530526
return Object.prototype.hasOwnProperty.call(block, 'opcode');
531527
} catch (e) {
532528
return false;
533529
}
534530
}
535531

532+
_isBlock (block) {
533+
return this.isBlock(block);
534+
}
535+
536536
_isStatementBlock (block) {
537537
const blockType = this._getBlockType(block);
538538
return blockType === 'statement' || blockType === 'terminate';
@@ -590,10 +590,18 @@ class RubyToBlocksConverter {
590590
return false;
591591
}
592592

593-
_isVariableBlock (block) {
593+
isVariableBlockType (block) {
594594
return /_variable$/.test(this._getBlockType(block));
595595
}
596596

597+
isVariableBlock (block) {
598+
return this.isVariableBlockType(block) && block.opcode === 'data_variable';
599+
}
600+
601+
isListBlock (block) {
602+
return this.getBlockType(block) === 'value_variable' && block.opcode === 'data_listcontents';
603+
}
604+
597605
_isRubyExpression (block) {
598606
return this._isBlock(block) && block.opcode === 'ruby_expression';
599607
}
@@ -843,6 +851,19 @@ class RubyToBlocksConverter {
843851
return this._lookupOrCreateVariableOrList(name, Variable.LIST_TYPE);
844852
}
845853

854+
lookupVariableFromVariableBlock (block) {
855+
if (!this.isVariableBlock(block)) return null;
856+
857+
return this._context.variables[block.fields.VARIABLE.value] ||
858+
this._context.localVariables[block.fields.VARIABLE.value];
859+
}
860+
861+
lookupListFromListBlock (block) {
862+
if (!this.isListBlock(block)) return null;
863+
864+
return this._context.lists[block.fields.LIST.value];
865+
}
866+
846867
lookupOrCreateBroadcastMsg (name) {
847868
return this._lookupOrCreateBroadcastMsg(name);
848869
}
@@ -922,6 +943,20 @@ class RubyToBlocksConverter {
922943
return b;
923944
}
924945

946+
removeListBlock (block) {
947+
if (!this.isListBlock(block)) {
948+
return;
949+
}
950+
951+
const previousBlockId = block.parent;
952+
if (previousBlockId) {
953+
const previousBlock = this._context.blocks[previousBlockId];
954+
previousBlock.next = block.next;
955+
}
956+
957+
delete this._context.blocks[block.id];
958+
}
959+
925960
_removeWaitBlocks (block) {
926961
if (!block || block === Opal.nil) {
927962
return null;
@@ -958,17 +993,25 @@ class RubyToBlocksConverter {
958993
return firstBlock;
959994
}
960995

961-
_getBlockType (block) {
962-
if (this._isBlock(block)) {
996+
getBlockType (block) {
997+
if (this.isBlock(block)) {
963998
return this._context.blockTypes[block.id];
964999
}
9651000
return 'primitive';
9661001
}
9671002

1003+
_getBlockType (block) {
1004+
return this.getBlockType(block);
1005+
}
1006+
9681007
_setBlockType (block, type) {
9691008
this._context.blockTypes[block.id] = type;
9701009
}
9711010

1011+
changeBlock (block, opcode, blockType) {
1012+
return this._changeBlock(block, opcode, blockType);
1013+
}
1014+
9721015
_changeBlock (block, opcode, blockType) {
9731016
block.opcode = opcode;
9741017
this._setBlockType(block, blockType);

0 commit comments

Comments
 (0)