0.0.6-alpha3
・中置記法を逆ポーランド記法に変換する処理を修正
・逆ポーランド記法の数式を計算する処理を追加
・CSSの追加処理を修正
@@ -656,137 +656,14 @@ | ||
656 | 656 | return this.action(); |
657 | 657 | }; |
658 | 658 | |
659 | + | |
660 | +// TODO | |
659 | 661 | THIEF.command.wield = function () { |
660 | 662 | |
661 | - var val = '2d100 * (2 + 15) + Math.max(3, 4) + (Math.round(11.2) * Math.PI + 6)'; | |
662 | - //var val = '2d100*(2+15)+Math.max(3,4)+(Math.round(11.2)*Math.PI+6)'; | |
663 | - //var val = '(Math.round(11.2)*Math.PI+6)'; | |
664 | - //var val = '(Math.round(11.2) * Math.PI)'; | |
665 | - //var val = 'Math.max(3, 4) + Math.round(11.2) + Math.PI'; | |
666 | - //var val = '8 + Math.max(3, 4) + Math.PI'; | |
667 | - var stack = []; | |
668 | - var revP = []; | |
669 | - var math; | |
670 | - var subIndex; | |
671 | - | |
672 | - THIEF.html.addDebugMessage('数式を逆ポーランドに変換開始...'); | |
673 | - THIEF.html.addDebugMessage('変換前:' + val); | |
674 | - | |
675 | - for(var i = 0; i < val.length; i++){ | |
676 | - var ope = val[i]; | |
677 | - switch (ope) { | |
678 | - case '+' : | |
679 | - case '-' : | |
680 | - while(/^[\*\/\^%dM]$/.test(stack.getLast())){ | |
681 | - revP.push(stack.pop()); | |
682 | - } | |
683 | - stack.push(ope); | |
684 | - break; | |
685 | - | |
686 | - case '*' : | |
687 | - case '/' : | |
688 | - while(/^[\^%dM]$/.test(stack.getLast())){ | |
689 | - revP.push(stack.pop()); | |
690 | - } | |
691 | - stack.push(ope); | |
692 | - break; | |
693 | - | |
694 | - case '^' : | |
695 | - case '%' : | |
696 | - while(/^[dM]$/.test(stack.getLast())){ | |
697 | - revP.push(stack.pop()); | |
698 | - } | |
699 | - stack.push(ope); | |
700 | - break; | |
701 | - | |
702 | - case 'd' : | |
703 | - while(/^[M]$/.test(stack.getLast())){ | |
704 | - revP.push(stack.pop()); | |
705 | - } | |
706 | - stack.push(ope); | |
707 | - break; | |
708 | - | |
709 | - case 'M' : | |
710 | - | |
711 | - // M から演算子 or 空白 までを取り出す | |
712 | - // Math. を必ず含むこと | |
713 | - // Math.PId6 とかは使用上無理 | |
714 | - // Math.round () というように、()の前にスペースが入るのも無理 | |
715 | - for(subIndex=i+5; subIndex<val.length; subIndex++){ | |
716 | - if(/[^a-zA-Z]$/.test(val[subIndex])){ | |
717 | - math = val.substring(i+5, subIndex); | |
718 | - break; | |
719 | - } else if(subIndex === val.length-1){ | |
720 | - math = val.substring(i+5, val.length); | |
721 | - break; | |
722 | - } | |
723 | - } | |
724 | - | |
725 | - if(typeof Math[math] === 'function'){ | |
726 | - stack.push('('); // 先に括弧を処理するため、インデックスはそのまま | |
727 | - stack.push(Math[math]); | |
728 | - } else { | |
729 | - revP.push(Math[math]); | |
730 | - subIndex--; // 定数の場合は、インデックスを一つ戻す | |
731 | - } | |
732 | - | |
733 | - i = subIndex; | |
734 | - | |
735 | - break; | |
736 | - | |
737 | - case '(' : | |
738 | - stack.push(ope); | |
739 | - break; | |
740 | - | |
741 | - case ')' : | |
742 | - // Math 関数の引数の数が不定なので、判別用のダミーの引数を加えておく | |
743 | - if(typeof stack.getLast() === 'function'){ | |
744 | - revP.push('dummy'); | |
745 | - } | |
746 | - | |
747 | - while(stack.getLast() !== '('){ | |
748 | - revP.push(stack.pop()); | |
749 | - } | |
750 | - stack.pop(); // Delete ( | |
751 | - break; | |
752 | - | |
753 | - case '0' : | |
754 | - case '1' : | |
755 | - case '2' : | |
756 | - case '3' : | |
757 | - case '4' : | |
758 | - case '5' : | |
759 | - case '6' : | |
760 | - case '7' : | |
761 | - case '8' : | |
762 | - case '9' : | |
763 | - case '.' : | |
764 | - for(subIndex = i+1; subIndex<val.length; subIndex++){ | |
765 | - if(/[^0-9.]/.test(val[subIndex])){ | |
766 | - revP.push(val.substring(i, subIndex)); | |
767 | - subIndex--; // 行き過ぎたインデックスを元に戻す | |
768 | - break; | |
769 | - } else if(subIndex === val.length-1){ | |
770 | - revP.push(val.substring(i, val.length)); | |
771 | - break; | |
772 | - } | |
773 | - } | |
774 | - i = subIndex; | |
775 | - break; | |
776 | - | |
777 | - default : | |
778 | - break; | |
779 | - | |
780 | - } | |
781 | - } | |
782 | - | |
783 | - while(stack.getLast()){ | |
784 | - revP.push(stack.pop()); | |
785 | - } | |
786 | - | |
787 | - THIEF.html.addDebugMessage('変換後:' + revP); | |
788 | - THIEF.html.addDebugMessage('...変換完了'); | |
789 | - | |
663 | + //var val = '2d100 * (2 + 15) + Math.max(3, 4) + (Math.round(11.2) * Math.PI + 6)'; | |
664 | + var val = '5 / (4 + 5) * 10 / (6 - 3)'; | |
665 | + var revP = THIEF.util.infixToRevP(val); | |
666 | + THIEF.util.execRevP(revP); | |
790 | 667 | return true; |
791 | 668 | }; |
792 | 669 |
@@ -1524,6 +1401,216 @@ | ||
1524 | 1401 | |
1525 | 1402 | }; |
1526 | 1403 | |
1404 | +THIEF.util.infixToRevP = function (val) { | |
1405 | + //var val = '2d100 * (2 + 15) + Math.max(3, 4) + (Math.round(11.2) * Math.PI + 6)'; | |
1406 | + //var val = '2d100*(2+15)+Math.max(3,4)+(Math.round(11.2)*Math.PI+6)'; | |
1407 | + //var val = '(Math.round(11.2)*Math.PI+6)'; | |
1408 | + //var val = '(Math.round(11.2) * Math.PI)'; | |
1409 | + //var val = 'Math.max(3, 4) + Math.round(11.2) + Math.PI'; | |
1410 | + //var val = '8 + Math.max(3, 4) + Math.PI'; | |
1411 | + var stack = []; | |
1412 | + var revP = []; | |
1413 | + var math; | |
1414 | + var subIndex; | |
1415 | + | |
1416 | + THIEF.html.addDebugMessage('数式を逆ポーランドに変換開始...'); | |
1417 | + THIEF.html.addDebugMessage('変換前:' + val); | |
1418 | + | |
1419 | + for(var i = 0; i < val.length; i++){ | |
1420 | + var ope = val.charAt(i); | |
1421 | + switch (ope) { | |
1422 | + case '+' : | |
1423 | + case '-' : | |
1424 | + while(/^[\*\/\^%dM]$/.test(stack.getLast())){ | |
1425 | + revP.push(stack.pop()); | |
1426 | + } | |
1427 | + stack.push(ope); | |
1428 | + break; | |
1429 | + | |
1430 | + case '*' : | |
1431 | + while(/^[\/\^%dM]$/.test(stack.getLast())){ | |
1432 | + revP.push(stack.pop()); | |
1433 | + } | |
1434 | + stack.push(ope); | |
1435 | + break; | |
1436 | + | |
1437 | + case '/' : | |
1438 | + while(/^[\^%dM]$/.test(stack.getLast())){ | |
1439 | + revP.push(stack.pop()); | |
1440 | + } | |
1441 | + stack.push(ope); | |
1442 | + break; | |
1443 | + | |
1444 | + | |
1445 | + case '^' : | |
1446 | + case '%' : | |
1447 | + while(/^[dM]$/.test(stack.getLast())){ | |
1448 | + revP.push(stack.pop()); | |
1449 | + } | |
1450 | + stack.push(ope); | |
1451 | + break; | |
1452 | + | |
1453 | + case 'd' : | |
1454 | + while(/^[M]$/.test(stack.getLast())){ | |
1455 | + revP.push(stack.pop()); | |
1456 | + } | |
1457 | + stack.push(ope); | |
1458 | + break; | |
1459 | + | |
1460 | + case 'M' : | |
1461 | + | |
1462 | + // M から演算子 or 空白 までを取り出す | |
1463 | + // Math. を必ず含むこと | |
1464 | + // Math.PId6 とかは使用上無理 | |
1465 | + // Math.round () というように、()の前にスペースが入るのも無理 | |
1466 | + for(subIndex=i+5; subIndex<val.length; subIndex++){ | |
1467 | + if(/[^a-zA-Z]$/.test(val.charAt(subIndex))){ | |
1468 | + math = val.substring(i+5, subIndex); | |
1469 | + break; | |
1470 | + } else if(subIndex === val.length-1){ | |
1471 | + math = val.substring(i+5, val.length); | |
1472 | + break; | |
1473 | + } | |
1474 | + } | |
1475 | + | |
1476 | + if(typeof Math[math] === 'function'){ | |
1477 | + revP.push('dummy'); | |
1478 | + stack.push('('); // 先に括弧を処理するため、インデックスはそのまま | |
1479 | + stack.push(Math[math]); | |
1480 | + } else { | |
1481 | + revP.push(Math[math]); | |
1482 | + subIndex--; // 定数の場合は、インデックスを一つ戻す | |
1483 | + } | |
1484 | + | |
1485 | + i = subIndex; | |
1486 | + | |
1487 | + break; | |
1488 | + | |
1489 | + case '(' : | |
1490 | + stack.push(ope); | |
1491 | + break; | |
1492 | + | |
1493 | + case ')' : | |
1494 | + // Math 関数の引数の数が不定なので、判別用のダミーの引数を加えておく | |
1495 | + if(typeof stack.getLast() === 'function'){ | |
1496 | + //revP.push('dummy'); | |
1497 | + } | |
1498 | + | |
1499 | + while(stack.getLast() !== '('){ | |
1500 | + revP.push(stack.pop()); | |
1501 | + } | |
1502 | + stack.pop(); // Delete ( | |
1503 | + break; | |
1504 | + | |
1505 | + case '0' : | |
1506 | + case '1' : | |
1507 | + case '2' : | |
1508 | + case '3' : | |
1509 | + case '4' : | |
1510 | + case '5' : | |
1511 | + case '6' : | |
1512 | + case '7' : | |
1513 | + case '8' : | |
1514 | + case '9' : | |
1515 | + case '.' : | |
1516 | + for(subIndex = i; subIndex<val.length; subIndex++){ | |
1517 | + if(/[^0-9\.]/.test(val.charAt(subIndex))){ | |
1518 | + revP.push(parseFloat(val.substring(i, subIndex))); | |
1519 | + subIndex--; // 行き過ぎたインデックスを元に戻す | |
1520 | + break; | |
1521 | + } else if(subIndex === val.length-1){ | |
1522 | + revP.push(parseFloat(val.substring(i, val.length))); | |
1523 | + break; | |
1524 | + } | |
1525 | + } | |
1526 | + i = subIndex; | |
1527 | + break; | |
1528 | + | |
1529 | + default : | |
1530 | + break; | |
1531 | + | |
1532 | + } | |
1533 | + } | |
1534 | + | |
1535 | + while(stack.getLast()){ | |
1536 | + revP.push(stack.pop()); | |
1537 | + } | |
1538 | + | |
1539 | + THIEF.html.addDebugMessage('変換後:' + revP); | |
1540 | + THIEF.html.addDebugMessage('...変換完了'); | |
1541 | + | |
1542 | + return revP; | |
1543 | +}; | |
1544 | + | |
1545 | +THIEF.util.execRevP = function (array) { | |
1546 | + THIEF.html.addDebugMessage('逆ポーランド記法の数式を計算開始...'); | |
1547 | + var revP = array.slice(0); | |
1548 | + var index=0; | |
1549 | + THIEF.html.addDebugMessage('X = ' + revP); | |
1550 | + for( ; revP.length > 1; index++){ | |
1551 | + var ope = revP[index]; | |
1552 | + | |
1553 | + switch (typeof ope) { | |
1554 | + case 'number' : | |
1555 | + break; | |
1556 | + | |
1557 | + case 'string' : | |
1558 | + switch (ope) { | |
1559 | + case '+' : | |
1560 | + revP[index-2] = revP[index-2] + revP[index-1]; | |
1561 | + break; | |
1562 | + case '-' : | |
1563 | + revP[index-2] = revP[index-2] - revP[index-1]; | |
1564 | + break; | |
1565 | + case '/' : | |
1566 | + revP[index-2] = revP[index-2] / revP[index-1]; | |
1567 | + break; | |
1568 | + case '*' : | |
1569 | + revP[index-2] = revP[index-2] * revP[index-1]; | |
1570 | + break; | |
1571 | + case '%' : | |
1572 | + revP[index-2] = revP[index-2] % revP[index-1]; | |
1573 | + break; | |
1574 | + case '^' : | |
1575 | + revP[index-2] = revP[index-2] ^ revP[index-1]; | |
1576 | + break; | |
1577 | + case 'd' : | |
1578 | + var tmp = 0; | |
1579 | + for(var i=0; i<revP[index-2]; i++){ | |
1580 | + tmp += Math.round(Math.random() * revP[index-1] + 0.5); | |
1581 | + } | |
1582 | + revP[index-2] = tmp; | |
1583 | + break; | |
1584 | + case 'dummy' : | |
1585 | + continue; | |
1586 | + default : break; | |
1587 | + } | |
1588 | + revP.splice(index-1, 2); | |
1589 | + index -= 2; | |
1590 | + break; | |
1591 | + | |
1592 | + case 'function' : | |
1593 | + if(revP[index-2] === 'dummy'){ | |
1594 | + revP[index-2] = revP[index](revP[index-1]); | |
1595 | + revP.splice(index-1, 2); | |
1596 | + index -= 2; | |
1597 | + } else if (revP[index-3] === 'dummy') { | |
1598 | + revP[index-3] = revP[index](revP[index-2], revP[index-1]); | |
1599 | + revP.splice(index-2, 3); | |
1600 | + index -= 3; | |
1601 | + } | |
1602 | + break; | |
1603 | + | |
1604 | + default : break; | |
1605 | + } | |
1606 | + } | |
1607 | + | |
1608 | + THIEF.html.addDebugMessage(' = ' + revP[0]); | |
1609 | + THIEF.html.addDebugMessage('...計算完了。'); | |
1610 | + return revP[0]; | |
1611 | +}; | |
1612 | + | |
1613 | + | |
1527 | 1614 | THIEF.html = {}; |
1528 | 1615 | |
1529 | 1616 | THIEF.html.setXy = function(x, y, text) { |
@@ -1708,12 +1795,12 @@ | ||
1708 | 1795 | document.getElementsByTagName('head')[0].appendChild(style); |
1709 | 1796 | var sheet = document.styleSheets[0]; |
1710 | 1797 | if (sheet.insertRule) { |
1711 | - sheet.addCSS = function(selector, property, value) { | |
1798 | + sheet.addStyle = function(selector, property, value) { | |
1712 | 1799 | sheet.insertRule(selector + '{' + property + ':' + value + ';}', sheet.cssRules.length); |
1713 | 1800 | }; |
1714 | 1801 | } else { |
1715 | - sheet.addCSS = function(selector, property, value) { | |
1716 | - sheet.addRule(selector, '{' + property + ':' + value + ';}'); | |
1802 | + sheet.addStyle = function(selector, property, value) { | |
1803 | + sheet.addRule(selector, property + ':' + value); | |
1717 | 1804 | }; |
1718 | 1805 | |
1719 | 1806 | } |
@@ -1728,32 +1815,32 @@ | ||
1728 | 1815 | var info = THIEF.conf.id + 'Infomation'; // 情報出力(インベントリの内容など) |
1729 | 1816 | var foot = THIEF.conf.id + 'Footer'; // |
1730 | 1817 | var dbug = THIEF.conf.id + 'Debug'; // デバッグ情報 |
1731 | - sheet.addCSS('#' + root, 'color', THIEF.conf.textColor); | |
1732 | - sheet.addCSS('#' + root, 'background-color', THIEF.conf.bgColor); | |
1733 | - sheet.addCSS('#' + root, 'font-family', THIEF.conf.fontFace); | |
1734 | - sheet.addCSS('#' + root, 'letter-spacing', '0px'); | |
1735 | - sheet.addCSS('#' + root, 'line-height', '1'); | |
1736 | - sheet.addCSS('#' + root + ' p', 'margin', '0em'); | |
1737 | - sheet.addCSS('#' + root + ' p', 'padding', '0em'); | |
1738 | - sheet.addCSS('#' + root + ' em', 'color', THIEF.conf.emTextColor); | |
1739 | - sheet.addCSS('#' + main, 'margin', '1em'); | |
1740 | - sheet.addCSS('#' + main, 'float', THIEF.conf.mainWindowPosition); | |
1741 | - sheet.addCSS('#' + maps, 'text-align', 'center'); | |
1742 | - sheet.addCSS('#' + maps, 'vertical-align', 'middle'); | |
1743 | - sheet.addCSS('#' + maps + ' table', 'border', '1px solid ' + THIEF.conf.textColor); | |
1744 | - sheet.addCSS('#' + maps + ' table', 'border-collapse', 'collapse'); | |
1745 | - sheet.addCSS('#' + maps + ' table', 'border-spacing', '0px'); | |
1746 | - sheet.addCSS('#' + maps + ' td', 'border', THIEF.conf.gridStyle); | |
1747 | - sheet.addCSS('#' + maps + ' td', 'margin', '0px'); | |
1748 | - sheet.addCSS('#' + maps + ' td', 'padding', '0px'); | |
1749 | - sheet.addCSS('#' + maps + ' td', 'color', THIEF.conf.textColor); // テーブル内文字色には必須? | |
1750 | - sheet.addCSS('#' + info, 'border', THIEF.conf.gridStyle); | |
1751 | - //sheet.addCSS('#' + info, 'height', THIEF.conf.mapLength + 'em'); | |
1752 | - sheet.addCSS('#' + info, 'overflow', THIEF.conf.msgOverflow); | |
1753 | - sheet.addCSS('#' + info, 'padding', '1em'); | |
1754 | - sheet.addCSS('#' + dbug, 'border', THIEF.conf.gridStyle); | |
1755 | - //sheet.addCSS('#' + dbug, 'height', THIEF.conf.debugMessageHeight); | |
1756 | - //sheet.addCSS('#' + dbug, 'overflow', 'scroll'); | |
1818 | + sheet.addStyle('#' + root, 'color', THIEF.conf.textColor); | |
1819 | + sheet.addStyle('#' + root, 'background-color', THIEF.conf.bgColor); | |
1820 | + sheet.addStyle('#' + root, 'font-family', THIEF.conf.fontFace); | |
1821 | + sheet.addStyle('#' + root, 'letter-spacing', '0px'); | |
1822 | + sheet.addStyle('#' + root, 'line-height', '1'); | |
1823 | + sheet.addStyle('#' + root + ' p', 'margin', '0em'); | |
1824 | + sheet.addStyle('#' + root + ' p', 'padding', '0em'); | |
1825 | + sheet.addStyle('#' + root + ' em', 'color', THIEF.conf.emTextColor); | |
1826 | + sheet.addStyle('#' + main, 'margin', '1em'); | |
1827 | + sheet.addStyle('#' + main, 'float', THIEF.conf.mainWindowPosition); | |
1828 | + sheet.addStyle('#' + maps, 'text-align', 'center'); | |
1829 | + sheet.addStyle('#' + maps, 'vertical-align', 'middle'); | |
1830 | + sheet.addStyle('#' + maps + ' table', 'border', '1px solid ' + THIEF.conf.textColor); | |
1831 | + sheet.addStyle('#' + maps + ' table', 'border-collapse', 'collapse'); | |
1832 | + sheet.addStyle('#' + maps + ' table', 'border-spacing', '0px'); | |
1833 | + sheet.addStyle('#' + maps + ' td', 'border', THIEF.conf.gridStyle); | |
1834 | + sheet.addStyle('#' + maps + ' td', 'margin', '0px'); | |
1835 | + sheet.addStyle('#' + maps + ' td', 'padding', '0px'); | |
1836 | + sheet.addStyle('#' + maps + ' td', 'color', THIEF.conf.textColor); // テーブル内文字色には必須? | |
1837 | + sheet.addStyle('#' + info, 'border', THIEF.conf.gridStyle); | |
1838 | + //sheet.addStyle('#' + info, 'height', THIEF.conf.mapLength + 'em'); | |
1839 | + sheet.addStyle('#' + info, 'overflow', THIEF.conf.msgOverflow); | |
1840 | + sheet.addStyle('#' + info, 'padding', '1em'); | |
1841 | + sheet.addStyle('#' + dbug, 'border', THIEF.conf.gridStyle); | |
1842 | + //sheet.addStyle('#' + dbug, 'height', THIEF.conf.debugMessageHeight); | |
1843 | + //sheet.addStyle('#' + dbug, 'overflow', 'scroll'); | |
1757 | 1844 | |
1758 | 1845 | // メイン画面の位置決め |
1759 | 1846 | line += '<div id="' + main + '">\n'; |