사용자:Jinhoftyu/Undungeon/플러그인: 두 판 사이의 차이

리버티게임, 모두가 만들어가는 자유로운 게임
편집 요약 없음
편집 요약 없음
12번째 줄: 12번째 줄:


{{#vardefine:code|
{{#vardefine:code|
// 이부분에 코드 입력 //
// 이부분에 코드 입력 //
class Random {
const readline = require('readline');
  nextInt(max) {
 
    return Math.floor(Math.random() * max);
const rl = readline.createInterface({
   }
  input: process.stdin,
}
   output: process.stdout
});


class Item {
class Item {
23번째 줄: 24번째 줄:
     this.type = type;
     this.type = type;
     this.name = name;
     this.name = name;
    this.value = value;
  }
  getType() {
    return this.type;
  }
  getName() {
    return this.name;
  }
  getValue() {
    return this.value;
  }
  setName(name) {
    this.name = name;
  }
  setValue(value) {
     this.value = value;
     this.value = value;
   }
   }
54번째 줄: 35번째 줄:
     this.y = y;
     this.y = y;
     this.health = health;
     this.health = health;
  }
  getType() {
    return this.type;
  }
  getName() {
    return this.name;
  }
  getHealth() {
    return this.health;
   }
   }


   getAttack() {
   getAttack() {
     return Main.getAttack(this.getName());
     return getAttack(this.name);
  }
 
  setHealth(health) {
    this.health = health;
  }
 
  getX() {
    return this.x;
  }
 
  setX(x) {
    this.x = x;
  }
 
  getY() {
    return this.y;
  }
 
  setY(y) {
    this.y = y;
   }
   }
}
}


class StringBuilder {
const rand = new (function() {
  constructor() {
  const seed = new Date().getTime();
    this.data = [];
   this.nextInt = (n) => Math.floor(Math.random() * n);
  }
   this.seed = (s) => {};
 
})();
  append(text) {
    this.data.push(text);
   }
 
  delete(start, end) {
    this.data.splice(start, end - start);
   }
 
  length() {
    return this.data.length;
  }
 
  charAt(index) {
    return this.data[index];
  }


  toString() {
    return this.data.join('');
  }
}
const rand = new Random();
let game = false;
let game = false;
const mapY = 20;
let mapY = 20;
const mapX = 80;
let mapX = 80;
const base = new Array(mapY);
const base = new Array(mapY).fill(null).map(() => new Array(mapX).fill(null));
const entity = new Array(mapY);
const entity = new Array(mapY).fill(null).map(() => new Array(mapX).fill(null));
const entities = [];
const entities = [];
const item = [];
const item = [];


let dl;
let dl = 0;
let xl;
let xl = 0;
let hp;
let hp = 0;
let maxhp;
let maxhp = 0;
let pw;
let pw = 0;
let maxpw;
let maxpw = 0;
let time;
let time = 0;


let role;
let role;
140번째 줄: 68번째 줄:
let pl;
let pl;
const inv = [];
const inv = [];
const log = new StringBuilder();
 
let showInv = false;
const log = [];


function refLog() {
function refLog() {
   let size = 0;
   while (log.length > 3) {
   for (let i = 0; i < log.length(); i++) {
    log.shift();
     if (log.charAt(i) === '\n') {
  }
       size++;
}
 
async function main() {
   for (let i = 0; i < mapY; i++) {
     const row = [];
    for (let j = 0; j < mapX; j++) {
       row.push([]);
     }
     }
    item.push(row);
   }
   }


   if (size > 3) {
   while (name === null) {
     let index = log.toString().indexOf("\n") + 1;
     name = await askQuestion('Your name?');
    log.delete(0, index);
   }
   }
}


function main() {
   while (!game) {
   for (let i = 0; i < mapY; i++) {
     const roleInput = await askQuestion(`
     const row = new Array(mapX);
                    Your role?
    base[i] = row;
                    a) Monk
 
                    b) Ranger
    const entityRow = new Array(mapX);
                    c) Valkryie
     entity[i] = entityRow;
                    d) Wizard`);
     preset(roleInput);
   }
   }


   const readline = require('readline');
   mapGen();
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });


   const askName = () => {
   while (game) {
     rl.question("Your name? ", (answer) => {
     if (showInv) {
       name = answer;
      inventory();
       while (!game) {
      await askQuestion('');
         console.log(`
    } else {
         Your role?
       display();
         a) Monk
      const cmd = (await askQuestion('')).split(' ');
         b) Ranger
       switch (cmd[0]) {
         c) Valkryie
         case '.':
         d) Wizard`);
          turn();
         rl.question("Choose your role: ", (roleChoice) => {
          break;
           preset(roleChoice);
         case 'h':
           mapGen();
          move(pl, -1, 0);
           gameLoop();
          break;
         });
         case 'l':
          move(pl, 1, 0);
          break;
         case 'j':
          move(pl, 0, -1);
          break;
         case 'k':
          move(pl, 0, 1);
          break;
         case 'y':
          move(pl, -1, -1);
          break;
        case 'u':
          move(pl, 1, -1);
          break;
         case 'b':
          move(pl, -1, 1);
          break;
        case 'n':
           move(pl, 1, 1);
           break;
        case '<':
          ascend();
           break;
        case ',':
          pickup();
          break;
        case 'i':
          showInv = true;
          break;
         case 'Q':
          process.exit(0);
          break;
       }
       }
     });
     }
   };
   }
 
  askName();
}
}


function updateStack(x, y, items) {
function updateStack(x, y, items) {
   const row = item[y];
   item[y][x] = items;
  row[x] = items;
  item[y] = row;
}
}


function pickup() {
async function pickup() {
   const plx = pl.getX();
   const plx = pl.x;
   const ply = pl.getY();
   const ply = pl.y;
   const items = item[ply][plx];
   const items = item[ply][plx];
   if (items.length > 0) {
   if (items.length > 0) {
     const item = items[0];
     const i = items[0];
     inv.push(item);
     inv.push(i);
     items.shift();
     items.shift();
     updateStack(plx, ply, items);
     updateStack(plx, ply, items);
     log.append("You picked up " + itemName(item) + ".\n");
     log.push(`You picked up ${itemName(i)}.`);
     turn();
     turn();
   } else {
   } else {
     log.append("There is nothing here.\n");
     log.push('There is nothing here.');
   }
   }
}
}


function ascend() {
function ascend() {
   const plx = pl.getX();
   const plx = pl.x;
   const ply = pl.getY();
   const ply = pl.y;
   if (base[ply][plx] === '<') {
   if (base[ply][plx] === '<') {
     console.log("You escaped from the dungeon.");
     console.log('You escaped from the dungeon.');
     process.exit(0);
     process.exit(0);
   } else {
   } else {
     log.append("You can't go up here.\n");
     log.push("You can't go up here.");
   }
   }
}
}


function itemName(item) {
function itemName(i) {
   return item.getValue() + " " + item.getName() + (item.getValue() > 1 ? "s" : "");
   return `${i.value} ${i.name}${i.value > 1 ? 's' : ''}`;
}
}


function move(en, dx, dy) {
async function move(en, dx, dy) {
   const x = en.getX();
   const x = en.x;
   const y = en.getY();
   const y = en.y;
   if (base[y + dy][x + dx] !== '#' && entity[y + dy][x + dx] === null) {
   if (base[y + dy][x + dx] !== '#' && entity[y + dy][x + dx] === null) {
     entity[y][x] = null;
     entity[y][x] = null;
     x += dx;
     en.x += dx;
     y += dy;
     en.y += dy;
    en.setX(x);
     const floor = base[en.y][en.x];
    en.setY(y);
     entity[en.y][en.x] = en;
     const floor = base[y][x];
     const items = item[en.y][en.x];
     entity[y][x] = en;
     const items = item[y][x];


     // log output
     // log output
248번째 줄: 204번째 줄:
       turn();
       turn();
       if (floor !== '.' && floor !== '\0') {
       if (floor !== '.' && floor !== '\0') {
         log.append("You are standing in " + floorDes(floor) + ".\n");
         log.push(`You are standing in ${floorDes(floor)}.`);
       }
       }
       if (items.length > 1) {
       if (items.length > 1) {
         log.append("You see here several items.\n");
         log.push('You see here several items.');
       } else if (items.length > 0) {
       } else if (items.length > 0) {
         const item = items[0];
         const i = items[0];
         log.append("You see here " + itemName(item) + ".\n");
         log.push(`You see here ${itemName(i)}.`);
       }
       }
     }
     }
260번째 줄: 216번째 줄:
}
}


function turn() {
async function turn() {
   for (let i = 0; i < entities.length; i++) {
   for (const e of entities) {
    const e = entities[i];
     if (e !== pl) {
     if (e !== pl) {
       if (adjacent(e, pl)) {
       if (adjacent(e, pl)) {
         attack(e, pl);
         attack(e, pl);
       } else {
       } else {
         const randChoice = rand.nextInt(8);
         switch (rand.nextInt(8)) {
        switch (randChoice) {
           case 0:
           case 0:
             move(e, -1, 0);
             move(e, -1, 0);
303번째 줄: 257번째 줄:
   for (let y = 0; y < mapY; y++) {
   for (let y = 0; y < mapY; y++) {
     for (let x = 0; x < mapX; x++) {
     for (let x = 0; x < mapX; x++) {
       // old mapgen
       // old mapgen
       const xborder = (y === 1 || y === mapY - 2) && (x !== 0 && x !== mapX - 1);
       const xborder = (y === 1 || y === mapY - 2) && (x !== 0 && x !== mapX - 1);
323번째 줄: 276번째 줄:
       if (base[y][x] === '.' && rand.nextInt(80) === 0) {
       if (base[y][x] === '.' && rand.nextInt(80) === 0) {
         const items = [];
         const items = [];
         items.push((() => {
         items.push(
          switch (rand.nextInt(6)) {
          (() => {
            case 0:
            switch (rand.nextInt(6)) {
              return new Item('%', "food ration", 1);
              case 0:
            case 1:
                return new Item('%', 'food ration', 1);
              return new Item('$', "gold piece", 25);
              case 1:
            case 2:
                return new Item('$', 'gold piece', 25);
              return new Item(')', "dagger", 1);
              case 2:
            case 3:
                return new Item(')', 'dagger', 1);
              return new Item('[', "leather armor", 1);
              case 3:
            case 4:
                return new Item('[', 'leather armor', 1);
              return new Item('?', "scroll of upgrade", 1);
              case 4:
            case 5:
                return new Item('?', 'scroll of upgrade', 1);
              return new Item('!', "potion of healing", 1);
              case 5:
          }
                return new Item('!', 'potion of healing', 1);
        })());
              default:
                return null;
            }
          })()
        );
         item[y][x] = items;
         item[y][x] = items;
       }
       }
347번째 줄: 304번째 줄:
           switch (rand.nextInt(5)) {
           switch (rand.nextInt(5)) {
             case 0:
             case 0:
               return new Entity('B', "bat", x, y, getHP("bat"));
               return new Entity('B', 'bat', x, y, getHP('bat'));
             case 1:
             case 1:
               return new Entity('r', "sewer rat", x, y, getHP("sewer rat"));
               return new Entity('r', 'sewer rat', x, y, getHP('sewer rat'));
             case 2:
             case 2:
               return new Entity('k', "kobold", x, y, getHP("kobold"));
               return new Entity('k', 'kobold', x, y, getHP('kobold'));
             case 3:
             case 3:
               return new Entity('o', "goblin", x, y, getHP("goblin"));
               return new Entity('o', 'goblin', x, y, getHP('goblin'));
             case 4:
             case 4:
               return new Entity('&', "Demogorgon", x, y, getHP("Demogorgon"));
               return new Entity('&', 'Demogorgon', x, y, getHP('Demogorgon'));
            default:
              return null;
           }
           }
         })();
         })();
369번째 줄: 328번째 줄:
function getHP(name) {
function getHP(name) {
   switch (name) {
   switch (name) {
     case "bat":
     case 'bat':
       return 2;
       return 2;
     case "sewer rat":
     case 'sewer rat':
       return 4;
       return 4;
     case "kobold":
     case 'kobold':
       return 5;
       return 5;
     case "goblin":
     case 'goblin':
       return 8;
       return 8;
     case "Demogorgon":
     case 'Demogorgon':
       return 456;
       return 456;
     default:
     default:
386번째 줄: 345번째 줄:
function getAttack(s) {
function getAttack(s) {
   switch (s) {
   switch (s) {
     case "bat":
     case 'bat':
     case "goblin":
     case 'goblin':
     case "kobold":
     case 'kobold':
       return 4;
       return 4;
     case "sewer rat":
     case 'sewer rat':
       return 3;
       return 3;
     case "Demogorgon":
     case 'Demogorgon':
       return 48;
       return 48;
     default:
     default:
400번째 줄: 359번째 줄:


function adjacent(a, b) {
function adjacent(a, b) {
   return Math.abs(a.getX() - b.getX()) <= 1 && Math.abs(a.getY() - b.getY()) <= 1;
   return Math.abs(a.x - b.x) <= 1 && Math.abs(a.y - b.y) <= 1;
}
}


function attack(damager, damagee) {
function attack(damager, damagee) {
   const dam = damager.getAttack();
   const dam = damager.getAttack();
   const health = damagee.getHealth();
   const health = damagee.health;
   if (damagee === pl) {
   if (damagee === pl) {
     log.append("The " + damager.getName() + " hits!\n");
     log.push(`The ${damager.name} hits!`);
     hp -= dam;
     hp -= dam;
   } else if (damager === pl) {
   } else if (damager === pl) {
     log.append("You hit the " + damagee.getName() + "\n");
     log.push(`You hit the ${damagee.name}`);
   }
   }
   if (dam >= health) {
   if (dam >= health) {
     death(damagee);
     death(damagee);
   } else {
   } else {
     damagee.setHealth(damagee.getHealth() - dam);
     damagee.health -= dam;
   }
   }
}
}
421번째 줄: 380번째 줄:
function death(e) {
function death(e) {
   if (e === pl) {
   if (e === pl) {
     log.append("You were slain...");
     log.push('You were slain...');
     refLog();
     refLog();
     display();
     display();
427번째 줄: 386번째 줄:
   } else {
   } else {
     entities.splice(entities.indexOf(e), 1);
     entities.splice(entities.indexOf(e), 1);
     entity[e.getY()][e.getX()] = null;
     entity[e.y][e.x] = null;
     log.append(e.getName() + " died!\n");
     log.push(`${e.name} died!`);
   }
   }
}
}


function display() {
async function display() {
   const display = new StringBuilder();
   await clearConsole();
  let display = '';
   for (let y = 0; y < mapY; y++) {
   for (let y = 0; y < mapY; y++) {
     for (let x = 0; x < mapX; x++) {
     for (let x = 0; x < mapX; x++) {
439번째 줄: 399번째 줄:
       const it = item[y][x];
       const it = item[y][x];
       const en = entity[y][x];
       const en = entity[y][x];
       let s = " ";
       let s = ' ';
       if (floor !== '\0') {
       if (floor !== '\0') {
         s = floor;
         s = floor;
       }
       }
       if (it.length > 0) {
       if (it.length > 0) {
         s = it[0].getType();
         s = it[0].type;
       }
       }
       if (en !== null) {
       if (en !== null) {
         s = en.getType();
         s = en.type;
       }
       }
       display.append(s);
       display += s;
     }
     }
     display.append("\n");
     display += '\n';
   }
   }
   refLog();
   refLog();
   console.log(log.toString());
   log.forEach((line) => {
   display.append(name + " the " + role + " t:" + time + "\nDlvl:" + dl + " Lv:" + xl + " HP:" + hp + "/" + maxhp + " Pw:" + pw + "/" + maxpw + "\n\n");
    display += line + '\n';
   console.log(display.toString());
  });
   display += `${name} the ${role} t:${time}\nDlvl:${dl} Lv:${xl} HP:${hp}/${maxhp} Pw:${pw}/${maxpw}\n\n`;
   console.log(display);
}
}


function inventory() {
async function inventory() {
   const display = new StringBuilder();
   await clearConsole();
  let display = '';
   let c = 97;
   let c = 97;
   display.append("Your inventory:\n");
   display += 'Your inventory:\n';
   if (inv.length === 0) {
   if (inv.length === 0) {
     display.append("It's empty.\n");
     display += "It's empty.\n";
   } else {
   } else {
     for (let i = 0; i < inv.length; i++) {
     for (const i of inv) {
       display.append(String.fromCharCode(c) + ") " + itemName(inv[i]) + "\n");
       display += `${String.fromCharCode(c)}) ${itemName(i)}\n`;
       c++;
       c++;
     }
     }
   }
   }
   display.append("\nType any command...\n");
   display += '\nType any command...\n';
   console.log(display.toString());
   console.log(display);
   showInv = false;
   showInv = false;
}
}
478번째 줄: 441번째 줄:
function preset(s) {
function preset(s) {
   switch (s) {
   switch (s) {
     case "a":
     case 'a':
       role = "Monk";
       role = 'Monk';
       maxhp = 20;
       maxhp = 20;
       maxpw = 10;
       maxpw = 10;
       break;
       break;
     case "b":
     case 'b':
       role = "Ranger";
       role = 'Ranger';
       maxhp = 15;
       maxhp = 15;
       maxpw = 5;
       maxpw = 5;
       break;
       break;
     case "c":
     case 'c':
       role = "Valkriye";
       role = 'Valkriye';
       maxhp = 20;
       maxhp = 20;
       maxpw = 5;
       maxpw = 5;
       break;
       break;
     case "d":
     case 'd':
       role = "Wizard";
       role = 'Wizard';
       maxhp = 15;
       maxhp = 15;
       maxpw = 15;
       maxpw = 15;
510번째 줄: 473번째 줄:
   switch (c) {
   switch (c) {
     case '<':
     case '<':
       return "staircase leading upward";
       return 'staircase leading upward';
     case '>':
     case '>':
       return "staircase leading downward";
       return 'staircase leading downward';
     default:
     default:
       return "nothing";
       return 'nothing';
   }
   }
}
function askQuestion(question) {
  return new Promise((resolve) => {
    rl.question(question, (answer) => {
      resolve(answer);
    });
  });
}
function clearConsole() {
  return new Promise((resolve) => {
    console.clear();
    resolve();
  });
}
}



2023년 10월 18일 (수) 15:03 판

요령 => 아래 변수들의 첫번째 인자들을 내용에 맞게 자르고 붙여넣어주세요

!!!!!!!!!!!!!!!! 플러그인 정보 입력 구간 윗부분은 만지지 마시오 절대로!!!!!!!!!!!!!!!!!






!!!!!!!!!!!!!!!! 플러그인 정보 입력 구간 끝 아래는 만지지 마시오!!!!!!!!!!!!!!!!!

이 플러그인에 대한 설명문서는 사용자:Jinhoftyu/Undungeon/플러그인/설명문서에서 만들 수 있습니다.

		 // 이부분에 코드 입력 //
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

class Item {
  constructor(type, name, value) {
    this.type = type;
    this.name = name;
    this.value = value;
  }
}

class Entity {
  constructor(type, name, x, y, health) {
    this.type = type;
    this.name = name;
    this.x = x;
    this.y = y;
    this.health = health;
  }

  getAttack() {
    return getAttack(this.name);
  }
}

const rand = new (function() {
  const seed = new Date().getTime();
  this.nextInt = (n) => Math.floor(Math.random() * n);
  this.seed = (s) => {};
})();

let game = false;
let mapY = 20;
let mapX = 80;
const base = new Array(mapY).fill(null).map(() => new Array(mapX).fill(null));
const entity = new Array(mapY).fill(null).map(() => 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();
  }
}

async function main() {
  for (let i = 0; i < mapY; i++) {
    const row = [];
    for (let j = 0; j < mapX; j++) {
      row.push([]);
    }
    item.push(row);
  }

  while (name === null) {
    name = await askQuestion('Your name?');
  }

  while (!game) {
    const roleInput = await askQuestion(`
                    Your role?
                    a) Monk
                    b) Ranger
                    c) Valkryie
                    d) Wizard`);
    preset(roleInput);
  }

  mapGen();

  while (game) {
    if (showInv) {
      inventory();
      await askQuestion();
    } else {
      display();
      const cmd = (await askQuestion()).split(' ');
      switch (cmd[0]) {
        case '.':
          turn();
          break;
        case 'h':
          move(pl, -1, 0);
          break;
        case 'l':
          move(pl, 1, 0);
          break;
        case 'j':
          move(pl, 0, -1);
          break;
        case 'k':
          move(pl, 0, 1);
          break;
        case 'y':
          move(pl, -1, -1);
          break;
        case 'u':
          move(pl, 1, -1);
          break;
        case 'b':
          move(pl, -1, 1);
          break;
        case 'n':
          move(pl, 1, 1);
          break;
        case '<':
          ascend();
          break;
        case ',':
          pickup();
          break;
        case 'i':
          showInv = true;
          break;
        case 'Q':
          process.exit(0);
          break;
      }
    }
  }
}

function updateStack(x, y, items) {
  item[y][x] = items;
}

async function pickup() {
  const plx = pl.x;
  const ply = pl.y;
  const items = item[ply][plx];
  if (items.length > 0) {
    const 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() {
  const plx = pl.x;
  const 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' : }`;
}

async function move(en, dx, dy) {
  const x = en.x;
  const 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;
    const floor = base[en.y][en.x];
    entity[en.y][en.x] = en;
    const 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) {
        const i = items[0];
        log.push(`You see here ${itemName(i)}.`);
      }
    }
  }
}

async function turn() {
  for (const e of entities) {
    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 mapGen() {
  for (let y = 0; y < mapY; y++) {
    for (let x = 0; x < mapX; x++) {
      // old mapgen
      const xborder = (y === 1