사용자:Jinhoftyu/common.js: 두 판 사이의 차이
보이기
편집 요약 없음 태그: 되돌려진 기여 |
편집 요약 없음 태그: 되돌려진 기여 |
||
17번째 줄: | 17번째 줄: | ||
}); | }); | ||
function Item(type, name, value) { | |||
this.type = type; | |||
this.name = name; | |||
this.value = value; | |||
} | } | ||
function Entity(type, name, x, y, health) { | |||
this.type = type; | |||
this.name = name; | |||
this.x = x; | |||
this.y = y; | |||
this.health = health; | |||
} | } | ||
const rand = new (function() { | const rand = new (function () { | ||
var seed = new Date().getTime(); | |||
this.nextInt = (n) | this.nextInt = function (n) { | ||
this.seed = (s) | return Math.floor(Math.random() * n); | ||
}; | |||
this.seed = function (s) {}; | |||
})(); | })(); | ||
48번째 줄: | 42번째 줄: | ||
let mapY = 20; | let mapY = 20; | ||
let mapX = 80; | let mapX = 80; | ||
const base = new Array(mapY).fill(null).map(() | const base = new Array(mapY).fill(null).map(function () { | ||
const entity = new Array(mapY).fill(null).map(() | return new Array(mapX).fill(null); | ||
}); | |||
const entity = new Array(mapY).fill(null).map(function () { | |||
return new Array(mapX).fill(null); | |||
}); | |||
const entities = []; | const entities = []; | ||
const item = []; | const item = []; | ||
74번째 줄: | 72번째 줄: | ||
} | } | ||
function main() { | |||
for ( | for (var i = 0; i < mapY; i++) { | ||
var row = []; | |||
for ( | for (var j = 0; j < mapX; j++) { | ||
row.push([]); | row.push([]); | ||
} | } | ||
83번째 줄: | 81번째 줄: | ||
} | } | ||
askQuestion('Your name?').then(function (nameInput) { | |||
name = | name = nameInput; | ||
} | chooseRole(); | ||
}); | |||
} | |||
function chooseRole() { | |||
if (name === null) { | |||
askQuestion(` | |||
Your role? | Your role? | ||
a) Monk | a) Monk | ||
b) Ranger | b) Ranger | ||
c) Valkryie | c) Valkryie | ||
d) Wizard` | d) Wizard`).then(function (roleInput) { | ||
preset(roleInput); | |||
}); | |||
} else { | } else { | ||
preset(null); | |||
} | } | ||
} | } | ||
function | function preset(s) { | ||
switch (s) { | |||
case 'a': | |||
role = 'Monk'; | |||
maxhp = 20; | |||
maxpw = 10; | |||
break; | |||
case 'b': | |||
role = 'Ranger'; | |||
maxhp = 15; | |||
maxpw = 5; | |||
break; | |||
case 'c': | |||
role = 'Valkriye'; | |||
maxhp = 20; | |||
maxpw = 5; | |||
break; | |||
case 'd': | |||
role = 'Wizard'; | |||
maxhp = 15; | |||
maxpw = 15; | |||
break; | |||
default: | |||
return; | |||
} | } | ||
xl = 1; | |||
hp = maxhp; | |||
pw = maxpw; | |||
game = true; | |||
mapGen(); | |||
} | } | ||
function mapGen() { | function mapGen() { | ||
for ( | for (var y = 0; y < mapY; y++) { | ||
for ( | for (var x = 0; x < mapX; x++) { | ||
// old mapgen | // old mapgen | ||
var xborder = (y === 1 || y === mapY - 2) && (x !== 0 && x !== mapX - 1); | |||
var yborder = (x === 1 || x === mapX - 2) && (y !== 0 && y !== mapY - 1); | |||
var floor = y > 1 && y < mapY - 2 && x > 1 && x < mapX - 2; | |||
var up = x === 2 && y === 2; | |||
var down = x === mapX - 3 && y === mapY - 3; | |||
if (xborder || yborder) { | if (xborder || yborder) { | ||
base[y][x] = '#'; | base[y][x] = '#'; | ||
272번째 줄: | 155번째 줄: | ||
// item | // item | ||
if (base[y][x] === '.' && rand.nextInt(80) === 0) { | if (base[y][x] === '.' && rand.nextInt(80) === 0) { | ||
var items = []; | |||
items.push( | items.push( | ||
(() | (function () { | ||
switch (rand.nextInt(6)) { | switch (rand.nextInt(6)) { | ||
case 0: | case 0: | ||
298번째 줄: | 181번째 줄: | ||
// entity | // entity | ||
if (base[y][x] === '.' && rand.nextInt(100) === 0) { | if (base[y][x] === '.' && rand.nextInt(100) === 0) { | ||
var e = (function () { | |||
switch (rand.nextInt(5)) { | switch (rand.nextInt(5)) { | ||
case 0: | case 0: | ||
321번째 줄: | 204번째 줄: | ||
pl = new Entity('@', name, 2, 2, hp); | pl = new Entity('@', name, 2, 2, hp); | ||
entity[2][2] = pl; | entity[2][2] = pl; | ||
startGame(); | |||
} | } | ||
340번째 줄: | 224번째 줄: | ||
} | } | ||
function | function startGame() { | ||
var showInv = false; | |||
function updateStack(x, y, items) { | |||
item[y][x] = items; | |||
} | } | ||
function | function pickup() { | ||
var plx = pl.x; | |||
} | var ply = pl.y; | ||
var items = item[ply][plx]; | |||
if (items.length > 0) { | |||
var i = items[0]; | |||
inv.push(i); | |||
items.shift(); | |||
updateStack(plx, ply, items); | |||
log.push('You picked up ' + itemName(i) + '.'); | |||
turn(); | |||
} else { | |||
log.push('There is nothing here.'); | |||
} | |||
} | |||
function | function ascend() { | ||
var plx = pl.x; | |||
var ply = pl.y; | |||
if (base[ply][plx] === '<') { | |||
console.log('You escaped from the dungeon.'); | |||
process.exit(0); | |||
} else { | |||
log.push("You can't go up here."); | |||
} | |||
} | } | ||
function itemName(i) { | |||
return i.value + ' ' + i.name + (i.value > 1 ? 's' : ''); | |||
} | } | ||
function | function move(en, dx, dy) { | ||
var x = en.x; | |||
var y = en.y; | |||
if (base[y + dy][x + dx] !== '#' && entity[y + dy][x + dx] === null) { | |||
entity[y][x] = null; | |||
en.x += dx; | |||
en.y += dy; | |||
var floor = base[en.y][en.x]; | |||
entity[en.y][en.x] = en; | |||
var items = item[en.y][en.x]; | |||
// log output | |||
if (en === pl) { | |||
turn(); | |||
if (floor !== '.' && floor !== '\0') { | |||
log.push('You are standing in ' + floorDes(floor) + '.'); | |||
} | |||
if (items.length > 1) { | |||
log.push('You see here several items.'); | |||
} else if (items.length > 0) { | |||
var i = items[0]; | |||
log.push('You see here ' + itemName(i) + '.'); | |||
} | |||
} | |||
} | |||
} | } | ||
function turn() { | |||
for (var i = 0; i < entities.length; i++) { | |||
var e = entities[i]; | |||
if (e !== pl) { | |||
for ( | if (adjacent(e, pl)) { | ||
attack(e, pl); | |||
} else { | |||
switch (rand.nextInt(8)) { | |||
case 0: | |||
move(e, -1, 0); | |||
break; | |||
case 1: | |||
move(e, 1, 0); | |||
break; | |||
case 2: | |||
move(e, 0, -1); | |||
break; | |||
case 3: | |||
move(e, 0, 1); | |||
break; | |||
case 4: | |||
move(e, -1, -1); | |||
break; | |||
case 5: | |||
move(e, 1, -1); | |||
break; | |||
case 6: | |||
move(e, -1, 1); | |||
break; | |||
case 7: | |||
move(e, 1, 1); | |||
break; | |||
} | |||
} | |||
} | } | ||
} | } | ||
time++; | |||
} | |||
function floorDes(c) { | |||
switch (c) { | |||
case '<': | |||
return 'staircase leading upward'; | |||
case '>': | |||
return 'staircase leading downward'; | |||
default: | |||
return 'nothing'; | |||
} | |||
} | |||
function getAttack(s) { | |||
switch (s) { | |||
case 'bat': | |||
case 'goblin': | |||
case 'kobold': | |||
return 4; | |||
case 'sewer rat': | |||
return 3; | |||
case 'Demogorgon': | |||
return 48; | |||
default: | |||
return 1; | |||
} | |||
} | |||
function adjacent(a, b) { | |||
return Math.abs(a.x - b.x) <= 1 && Math.abs(a.y - b.y) <= 1; | |||
} | |||
function attack(damager, damagee) { | |||
var dam = damager.getAttack(); | |||
var health = damagee.health; | |||
if (damagee === pl) { | |||
log.push('The ' + damager.name + ' hits!'); | |||
hp -= dam; | |||
} else if (damager === pl) { | |||
log.push('You hit the ' + damagee.name); | |||
} | |||
if (dam >= health) { | |||
death(damagee); | |||
} else { | |||
damagee.health -= dam; | |||
} | |||
} | } | ||
function death(e) { | |||
if (e === pl) { | |||
log.push('You were slain...'); | |||
refLog(); | |||
display(); | |||
process.exit(0); | |||
} else { | |||
entities.splice(entities.indexOf(e), 1); | |||
entity[e.y][e.x] = null; | |||
log.push(e.name + ' died!'); | |||
} | } | ||
} | } | ||
function | function display() { | ||
clearConsole().then(function () { | |||
var display = ''; | |||
for (var y = 0; y < mapY; y++) { | |||
for (var x = 0; x < mapX; x++) { | |||
var floor = base[y][x]; | |||
var it = item[y][x]; | |||
var en = entity[y][x]; | |||
var s = ' '; | |||
if (floor !== '\0') { | |||
maxpw | s = floor; | ||
} | |||
if (it.length > 0) { | |||
s = it[0].type; | |||
} | |||
if (en !== null) { | |||
s = en.type; | |||
} | |||
display += s; | |||
} | |||
display += '\n'; | |||
} | |||
refLog(); | |||
log.forEach(function (line) { | |||
display += line + '\n'; | |||
}); | |||
display += | |||
name + | |||
' the ' + | |||
role + | |||
' t:' + | |||
time + | |||
'\nDlvl:' + | |||
dl + | |||
' Lv:' + | |||
xl + | |||
' HP:' + | |||
hp + | |||
'/' + | |||
maxhp + | |||
' Pw:' + | |||
pw + | |||
'/' + | |||
maxpw + | |||
'\n\n'; | |||
console.log(display); | |||
}); | |||
} | |||
function inventory() { | |||
clearConsole().then(function () { | |||
var display = ''; | |||
var c = 97; | |||
display += 'Your inventory:\n'; | |||
if (inv.length === 0) { | |||
display += "It's empty.\n"; | |||
} else { | |||
for (var i = 0; i < inv.length; i++) { | |||
var item = inv[i]; | |||
display += String.fromCharCode(c) + ') ' + itemName(item) + '\n'; | |||
c++; | |||
} | |||
} | |||
display += '\nType any command...\n'; | |||
console.log(display); | |||
showInv = false; | |||
}); | |||
} | } | ||
function | function askQuestion(question) { | ||
return new Promise(function (resolve) { | |||
rl.question(question, function (answer) { | |||
resolve(answer); | |||
}); | |||
}); | |||
} | } | ||
function | function clearConsole() { | ||
return new Promise(function (resolve) { | |||
console.clear(); | |||
resolve( | resolve(); | ||
}); | }); | ||
}); | } | ||
} | |||
function updateStack(x, y, items) { | |||
item[y][x] = items; | |||
} | |||
main(); | |||
} | } | ||
} | } |
2023년 10월 18일 (수) 15:12 판
/** 플러그인 uncydungeon***************************
* uncydungeon 실행
* 버전 => 0.1
* 작성자 : [[사용자:Jinhoftyu|Jinhoftyu]]
* JSON => uncydungeon = {"name":"uncydungeon","descript":"uncydungeon 실행","version":"0.1","local":true,"creat":"Jinhoftyu","state":"사용자:Jinhoftyu/Undungeon/플러그인","executable":true};
*/
function plugin_uncydungeon(){
if($("[data-name='uncydungeon']").length >= 1){
// 이부분에 코드 입력 //
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function Item(type, name, value) {
this.type = type;
this.name = name;
this.value = value;
}
function Entity(type, name, x, y, health) {
this.type = type;
this.name = name;
this.x = x;
this.y = y;
this.health = health;
}
const rand = new (function () {
var seed = new Date().getTime();
this.nextInt = function (n) {
return Math.floor(Math.random() * n);
};
this.seed = function (s) {};
})();
let game = false;
let mapY = 20;
let mapX = 80;
const base = new Array(mapY).fill(null).map(function () {
return new Array(mapX).fill(null);
});
const entity = new Array(mapY).fill(null).map(function () {
return new Array(mapX).fill(null);
});
const entities = [];
const item = [];
let dl = 0;
let xl = 0;
let hp = 0;
let maxhp = 0;
let pw = 0;
let maxpw = 0;
let time = 0;
let role;
let name = null;
let pl;
const inv = [];
const log = [];
function refLog() {
while (log.length > 3) {
log.shift();
}
}
function main() {
for (var i = 0; i < mapY; i++) {
var row = [];
for (var j = 0; j < mapX; j++) {
row.push([]);
}
item.push(row);
}
askQuestion('Your name?').then(function (nameInput) {
name = nameInput;
chooseRole();
});
}
function chooseRole() {
if (name === null) {
askQuestion(`
Your role?
a) Monk
b) Ranger
c) Valkryie
d) Wizard`).then(function (roleInput) {
preset(roleInput);
});
} else {
preset(null);
}
}
function preset(s) {
switch (s) {
case 'a':
role = 'Monk';
maxhp = 20;
maxpw = 10;
break;
case 'b':
role = 'Ranger';
maxhp = 15;
maxpw = 5;
break;
case 'c':
role = 'Valkriye';
maxhp = 20;
maxpw = 5;
break;
case 'd':
role = 'Wizard';
maxhp = 15;
maxpw = 15;
break;
default:
return;
}
xl = 1;
hp = maxhp;
pw = maxpw;
game = true;
mapGen();
}
function mapGen() {
for (var y = 0; y < mapY; y++) {
for (var x = 0; x < mapX; x++) {
// old mapgen
var xborder = (y === 1 || y === mapY - 2) && (x !== 0 && x !== mapX - 1);
var yborder = (x === 1 || x === mapX - 2) && (y !== 0 && y !== mapY - 1);
var floor = y > 1 && y < mapY - 2 && x > 1 && x < mapX - 2;
var up = x === 2 && y === 2;
var down = x === mapX - 3 && y === mapY - 3;
if (xborder || yborder) {
base[y][x] = '#';
} else if (up) {
base[y][x] = '<';
} else if (down) {
base[y][x] = '>';
} else if (floor) {
base[y][x] = '.';
}
// item
if (base[y][x] === '.' && rand.nextInt(80) === 0) {
var items = [];
items.push(
(function () {
switch (rand.nextInt(6)) {
case 0:
return new Item('%', 'food ration', 1);
case 1:
return new Item('$', 'gold piece', 25);
case 2:
return new Item(')', 'dagger', 1);
case 3:
return new Item('[', 'leather armor', 1);
case 4:
return new Item('?', 'scroll of upgrade', 1);
case 5:
return new Item('!', 'potion of healing', 1);
default:
return null;
}
})()
);
item[y][x] = items;
}
// entity
if (base[y][x] === '.' && rand.nextInt(100) === 0) {
var e = (function () {
switch (rand.nextInt(5)) {
case 0:
return new Entity('B', 'bat', x, y, getHP('bat'));
case 1:
return new Entity('r', 'sewer rat', x, y, getHP('sewer rat'));
case 2:
return new Entity('k', 'kobold', x, y, getHP('kobold'));
case 3:
return new Entity('o', 'goblin', x, y, getHP('goblin'));
case 4:
return new Entity('&', 'Demogorgon', x, y, getHP('Demogorgon'));
default:
return null;
}
})();
entities.push(e);
entity[y][x] = e;
}
}
}
pl = new Entity('@', name, 2, 2, hp);
entity[2][2] = pl;
startGame();
}
function getHP(name) {
switch (name) {
case 'bat':
return 2;
case 'sewer rat':
return 4;
case 'kobold':
return 5;
case 'goblin':
return 8;
case 'Demogorgon':
return 456;
default:
return 1;
}
}
function startGame() {
var showInv = false;
function updateStack(x, y, items) {
item[y][x] = items;
}
function pickup() {
var plx = pl.x;
var ply = pl.y;
var items = item[ply][plx];
if (items.length > 0) {
var i = items[0];
inv.push(i);
items.shift();
updateStack(plx, ply, items);
log.push('You picked up ' + itemName(i) + '.');
turn();
} else {
log.push('There is nothing here.');
}
}
function ascend() {
var plx = pl.x;
var ply = pl.y;
if (base[ply][plx] === '<') {
console.log('You escaped from the dungeon.');
process.exit(0);
} else {
log.push("You can't go up here.");
}
}
function itemName(i) {
return i.value + ' ' + i.name + (i.value > 1 ? 's' : '');
}
function move(en, dx, dy) {
var x = en.x;
var y = en.y;
if (base[y + dy][x + dx] !== '#' && entity[y + dy][x + dx] === null) {
entity[y][x] = null;
en.x += dx;
en.y += dy;
var floor = base[en.y][en.x];
entity[en.y][en.x] = en;
var items = item[en.y][en.x];
// log output
if (en === pl) {
turn();
if (floor !== '.' && floor !== '\0') {
log.push('You are standing in ' + floorDes(floor) + '.');
}
if (items.length > 1) {
log.push('You see here several items.');
} else if (items.length > 0) {
var i = items[0];
log.push('You see here ' + itemName(i) + '.');
}
}
}
}
function turn() {
for (var i = 0; i < entities.length; i++) {
var e = entities[i];
if (e !== pl) {
if (adjacent(e, pl)) {
attack(e, pl);
} else {
switch (rand.nextInt(8)) {
case 0:
move(e, -1, 0);
break;
case 1:
move(e, 1, 0);
break;
case 2:
move(e, 0, -1);
break;
case 3:
move(e, 0, 1);
break;
case 4:
move(e, -1, -1);
break;
case 5:
move(e, 1, -1);
break;
case 6:
move(e, -1, 1);
break;
case 7:
move(e, 1, 1);
break;
}
}
}
}
time++;
}
function floorDes(c) {
switch (c) {
case '<':
return 'staircase leading upward';
case '>':
return 'staircase leading downward';
default:
return 'nothing';
}
}
function getAttack(s) {
switch (s) {
case 'bat':
case 'goblin':
case 'kobold':
return 4;
case 'sewer rat':
return 3;
case 'Demogorgon':
return 48;
default:
return 1;
}
}
function adjacent(a, b) {
return Math.abs(a.x - b.x) <= 1 && Math.abs(a.y - b.y) <= 1;
}
function attack(damager, damagee) {
var dam = damager.getAttack();
var health = damagee.health;
if (damagee === pl) {
log.push('The ' + damager.name + ' hits!');
hp -= dam;
} else if (damager === pl) {
log.push('You hit the ' + damagee.name);
}
if (dam >= health) {
death(damagee);
} else {
damagee.health -= dam;
}
}
function death(e) {
if (e === pl) {
log.push('You were slain...');
refLog();
display();
process.exit(0);
} else {
entities.splice(entities.indexOf(e), 1);
entity[e.y][e.x] = null;
log.push(e.name + ' died!');
}
}
function display() {
clearConsole().then(function () {
var display = '';
for (var y = 0; y < mapY; y++) {
for (var x = 0; x < mapX; x++) {
var floor = base[y][x];
var it = item[y][x];
var en = entity[y][x];
var s = ' ';
if (floor !== '\0') {
s = floor;
}
if (it.length > 0) {
s = it[0].type;
}
if (en !== null) {
s = en.type;
}
display += s;
}
display += '\n';
}
refLog();
log.forEach(function (line) {
display += line + '\n';
});
display +=
name +
' the ' +
role +
' t:' +
time +
'\nDlvl:' +
dl +
' Lv:' +
xl +
' HP:' +
hp +
'/' +
maxhp +
' Pw:' +
pw +
'/' +
maxpw +
'\n\n';
console.log(display);
});
}
function inventory() {
clearConsole().then(function () {
var display = '';
var c = 97;
display += 'Your inventory:\n';
if (inv.length === 0) {
display += "It's empty.\n";
} else {
for (var i = 0; i < inv.length; i++) {
var item = inv[i];
display += String.fromCharCode(c) + ') ' + itemName(item) + '\n';
c++;
}
}
display += '\nType any command...\n';
console.log(display);
showInv = false;
});
}
function askQuestion(question) {
return new Promise(function (resolve) {
rl.question(question, function (answer) {
resolve(answer);
});
});
}
function clearConsole() {
return new Promise(function (resolve) {
console.clear();
resolve();
});
}
function updateStack(x, y, items) {
item[y][x] = items;
}
main();
}
}
}
$( plugin_uncydungeon );
/* uncydungeon 끝 */