rm original dotd.js

This commit is contained in:
lee2sman 2021-04-08 03:07:13 -04:00
parent 633413dad8
commit 9cec601b64
1 changed files with 0 additions and 749 deletions

749
dotd.js
View File

@ -1,749 +0,0 @@
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
const Charlatan = require('charlatan');
const blessed = require('blessed');
const fs = require("fs");
const generator = require("project-name-generator");
const dungeonFile = ".dungeonfile.txt";
const gameFile = ".gamefile.txt";
const dateFile = ".datefile.txt";
const itemsFile = ".itemsfile.txt";
const descriptionsFile = ".nonKittenItems.txt";
const verbsFile = ".verbs.txt";
const inventoryFile = ".inventory.txt";
const placesFile = ".places.txt";
const terrainFile = ".terrain.txt";
//globals
//blessed-specific, to write to screen
let screen, box;
let gameTitle = "Embarassed Cake Watering Hole";
// game vars
const width = 30;
const height = 8;
const movesInADay = 24;
let dungeon;
let roomItems = [];
let today;
let itemDescriptions = [];
let verbs = [];
let inventory = [];
let terrain = [];
let places = [];
let player = {"position": {
"x": null,
"y": null,
},
"avatar": "@",
"movedToday": false,
"life": null
};
let parseArgs = () => {
if (argv.new){
console.log('new game');
createDungeon();
createTerrain();
createPlayer();
loadTextFiles();
createItems();
createBooks();
createPlaces();
createGraves();
} else {
loadDungeon();
loadTerrain();
loadItems();
loadPlaces();
loadPlayer();
loadInventory();
grab();
movePlayer();
checkCollision();
}
}
let loadDungeon = () => {
const dungeonInput = fs.readFileSync(dungeonFile,'utf8')
let dungeonRows = dungeonInput.split("\n");
dungeon = Array.from(Array(height), () => new Array(width))
for (let y = 0; y < dungeonRows.length-1; y++){
for (x = 0; x < dungeonRows[0].length; x++){ //length of any string (width of dungeon)
let dungeonRowArr = dungeonRows[y].split("");
dungeon[y][x] = dungeonRowArr[x];
}
}
}
let loadTerrain = () => {
const terrainStr = fs.readFileSync(terrainFile, 'utf8');
terrain = JSON.parse(terrainStr);
}
let loadItems = () => {
const itemsStr = fs.readFileSync(itemsFile, 'utf8');
roomItems = JSON.parse(itemsStr)
//console.log(roomItems);
}
let loadPlayer = () => {
const playerFile = fs.readFileSync(gameFile,'utf8');
player = JSON.parse(playerFile);
//player.life--;
//console.log('life: '+player.life);
}
let loadInventory = () => {
const invFile = fs.readFileSync(inventoryFile,'utf8');
inventory = JSON.parse(invFile);
}
let loadPlaces = () => {
const placesStr = fs.readFileSync(placesFile, 'utf8');
places = JSON.parse(placesStr)
//console.log(roomItems);
}
let movePlayer = () => {
//if moving
//if (argv.r || argv.right || argv.l || argv.left || argv.u || argv.up || argv.d || argv.down) { //check to see if moving first
//if(player.life > 0){ //check to see if player did not move yet
if (argv.r || argv.right){
if (player.position.x < width-1){
//console.log('moved right');
player.position.x++;
player.movedToday = true;
} else {
console.log("can't go that way");
}
} else if (argv.l || argv.left){
if (player.position.x > 0){
//console.log('moved left');
player.position.x--;
player.movedToday = true;
} else {
console.log("can't go that way");
}
} else if (argv.u || argv.up){
if (player.position.y>0){
//console.log('moved up');
player.position.y--;
player.movedToday = true;
} else {
console.log("can't go that way");
}
} else if (argv.d || argv.down){
if (player.position.y<height-1){
//console.log('moved down');
player.position.y++;
player.movedToday = true;
} else {
console.log("can't go that way");
}
}
//} else { //you are asleep
// console.log("You've drifted off asleep for the night and will wake tomorrow refreshed.");
//player.avatar = '😴'
//}
//}
}
let checkCollision = () => {
//check Collision with Items
for (let i = 0; i < roomItems.length; i++){
if ((player.position.x == roomItems[i].position.x) && (player.position.y == roomItems[i].position.y)){
//console.log("you collided with "+roomItems[i].name);
console.log(roomItems[i].name);
console.log(roomItems[i].description);
}
}
//check Collision with Places
for (let i = 0; i < places.length; i++){
if ((player.position.x == places[i].position.x) && (player.position.y == places[i].position.y)){
console.log(places[i].name); //player is standing at this place
}
}
}
let grab = () => {
if (argv.g){
for (let i = 0; i < roomItems.length; i++){
if ((player.position.x == roomItems[i].position.x) && (player.position.y == roomItems[i].position.y)){
console.log("you take "+roomItems[i].name);
//add to player inventory
inventory.push(roomItems[i]);
//now remove from room since owned by player now
roomItems.splice(i, 1);
}
}
}
}
//---------------CREATE DUNGEON -----------------------------------
let createDungeon = () => {
dungeon = Array.from(Array(height), () => new Array(width))
}
//---------------CREATE PLAYER ----------------------------------
let createPlayer = () => {
//const avatars = ['👳','👶','🧛','🕵','👲','🧕','👵','👧','🧔','👸','🤠','']
//const avatars = ['☺','☺'];
//let chooseAvatar = Math.floor(Math.random() * avatars.length);
//player.avatar = avatars[chooseAvatar];
player.avatar = '@';
player.life = movesInADay;
player.position.x = Math.floor(Math.random()*width)
player.position.y = Math.floor(Math.random()*height)
}
//-------------CREATE ITEMS---------------------------------------
let loadTextFiles = () => {
itemDescriptions = fs.readFileSync(descriptionsFile).toString().split("\n");
verbs = fs.readFileSync(verbsFile).toString().split("\n");
}
let createItems = () => {
let numToSpawn = Math.round(Math.random() * 2)
let currentItemsInRoom = [];
for (let i = 0; i < numToSpawn; i++){
//east asian wide unicode
//const items = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.:;!?"'`^~ ̄_&#%+-*=<>()[]{}⦅⦆|¦/\¬$£¢₩¥".split("");
const items = "123456789ABCDEFHIJKLMNOPQRSTUVWXYZabcdefghijklmonqrsntuvwxyz,.:!?\"'`^~_&#%+-*=<>(){}/\$".split("");
//make sure it wasn't chosen previously in this board
do {
whichItem = Math.floor(Math.random()*items.length);
} while (currentItemsInRoom.includes(items[whichItem]))
currentItemsInRoom.push(items[whichItem]);
// console.log(items[whichItem]);
let whichItemText = Math.floor(Math.random() * itemDescriptions.length);
itemDescrip = itemDescriptions[whichItemText];
roomItems.push(
{
"name": "",
"symbol": items[whichItem],
"description": itemDescrip,
"position":
{
"x":Math.floor(Math.random()*width),
"y":Math.floor(Math.random()*height)
}
}
)
//roomItems[i].char = items[whichItem];
//roomItems[i].position.x = Math.floor(Math.random()*width)
//roomItems[i].position.y = Math.floor(Math.random()*height)
}
//console.log(roomItems);
}
let createBooks = () => {
let numToSpawn = Math.round(Math.random() * 2)
for (let i = 0; i < numToSpawn; i++){
//const items = ["📗","📕","📘","📙","📒","📓","📔"];
const items = ['⎅','⏍','◫'];
whichItem = Math.floor(Math.random()*items.length);
let firstSubject = generator.generate().spaced;
let secondSubject = generator.generate({ words: 2, alliterative: true }).spaced;
let pre = ["My Year of","A Guide To","Simply","A Cook's Guide to","The Book of","A tale of","To","A","The","The","The","One","Beginning","My","A Manual of","",""];
let connector = [" of"," for"," for"," with"," and"," on",":",": on"," OR"];
let descriptionWords = ["Probably the most complete book on","A treatise on","One of the few books to delve into","An expert description of","Mainly dealing with","An expert's guide to","A gentle introduction to the subject of","This book deals with","A compendium on","A poorly-written book on","A translated tale of","A well-written book on","A turgid tale of","A plain volume on","An old volume. One of a series on","Only the latest of many books about","An informative book on"];
let secondDescrip = ["Highly recommended.","It's rotting","It's cover is torn off.","It's a shoddy reprint.","This is a well-loved volume.","There is a faint musty odor.","Original edition","A Reprint.","Second edition.","This appears to be a reproduction.","Translated from the original.","Originally a movie and adapted for print.","Based on the film.","Several pages have been torn out.","Several pages are stuck together.","The cover has fallen off.","The spine is cracked.","The book is full of highlighting and notes from a previous reader.","The book has hand-written notes throughout.","The print has faded but it is barely readable.","This volume is quite old.","A great guide for those interested in this timely subject."];
//title of book
let book;
if (Math.random()<0.4){
book = choose(pre) + " " + capitalize(firstSubject) + choose(connector) + " " + capitalize(secondSubject);
} else if (Math.random()<0.5){
book = choose(pre) + " " + capitalize(firstSubject);
} else {
book = choose(pre) + " " + capitalize(secondSubject);
}
//description
let name = Charlatan.Name.name();
let bookDescrip = choose(descriptionWords) + " " + capitalize(firstSubject) + ". " + choose(secondDescrip) + "\n By " + name + ".";
roomItems.push(
{
"name": book,
"symbol": items[whichItem],
"description": bookDescrip,
"position":
{
"x":Math.floor(Math.random()*width),
"y":Math.floor(Math.random()*height)
}
}
)
}
}
let choose = arr => arr[Math.floor(Math.random()*arr.length)];
let capitalize = (str) => {
let arr = str.split(' ');
for(let i = 0; i < arr.length; i++ ) {
arr[i] = arr[i].replace(arr[i].charAt(0), arr[i].charAt(0).toUpperCase());
}
return arr.join(' ');
}
let createPlaces = () => {
let numToSpawn = Math.round(Math.random() * 3) + 3
for (let i = 0; i < numToSpawn; i++){
//const buildings = ['🏛️','⛺','🏚️','⛩️','🗿']
const buildings = ['⌂','⏏','☖','☗']
const placeTypes = ['Village','Village','Village','House','House','Market','Market','Market','Crossroads','Place','Outpost','Trading Post','House','Shack','Meeting Place','Saloon','Watering Hole','Stall','Hideout','Cart','Camp','Camp','Camp','Camp','','','','Zone of Ill Repute']
let whichBuilding = buildings[Math.floor(Math.random()*3)];
let loc = generator.generate().spaced;
let suffix = placeTypes[Math.floor(Math.random() * placeTypes.length)];
let locName = capitalize(loc + ' ' + suffix);
places.push(
{
"name": locName,
"symbol": whichBuilding,
"position":
{
"x":Math.floor(Math.random()*width),
"y":Math.floor(Math.random()*height)
}
}
)
}
//let's also create some tombstones?
//console.log(places)
}
let createGraves = () => {
let name = Charlatan.Name.name();
let gravestones = ['✝','✟','☨','✞'];
let whichGravestone = choose(gravestones);
let prefix = ["Here lies","RIP","","","","Resting place of ","Beloved"]
let reason = ["Made an enemy","Wasn't afraid to be","Tried","Died while","Passed while performing","Tried out","Dissapeared investingating","Wandered off while looking for","Last seen","Loved","Adored","A lifelong fan of","Our favorite at","The best at","Always in our hearts","Keep","Always be","Always","Just","Tried","Couldn't stop","Only ever found","Died","Passed while","Couldn't stop","Forgot to try","Never stopped","We'll always think of you when we're","It's not the same"]
let epitaph = prefix[Math.floor(Math.random() * prefix.length)] + " " + name +"\n"+reason[Math.floor(Math.random() * reason.length)]+" " + verbs[Math.floor(Math.random()*verbs.length)] + ".";
places.push(
{
"name": epitaph,
// "symbol": "⚰️",
"symbol": whichGravestone,
"position":
{
"x":Math.floor(Math.random()*width),
"y":Math.floor(Math.random()*height)
}
}
)
}
//----------------create zone---------------
/*
let landscapes = ['🏔️','🌿','🌱','🌾','🌻','🌵']
let plants = ['🌹','🌺','🌻','🌼','🌷','🎋']
let shrines = ['⛩️','🗿']
let emojiitems = ['🍄','🌰','🦴','🧵','🧶','👓','🕶','🥽','👞','👟','🥾','🥿','👑','💼']
*/
let debug = () => {
//console.log(dungeon)
console.log("terminal width: "+process.stdout.columns+" height: "+process.stdout.rows);
console.log("game width: "+width+" height: "+height);
console.log("x: "+player.position.x+" y: "+player.position.y)
}
let dungeonToStrings = () => {
let dungeonStr = "";
for (let i = 0; i < height; i++){
dungeonStr+=(dungeon[i].join("")+"\n");
}
return dungeonStr;
}
let createTerrain = () => {
let forestTerrain = ['ᚠ','ᚡ','ᚴ','ᚵ','ᚶ','ᛉ','ᛘ','ᛠ'];
//let forestTerrain = ['🎄', '🌳','🌲']
let plants = ['ሥ','ሥ','ቂ','ቁ','ቄ','ቃ','ቅ','ቆ','ቇ','ቈ','ቊ','ቋ','ቌ','ቍ','ቜ','ቝ']
//let plants = ['🌹','🌺','🌻','🌼','🌷','🎋','🍄','🌰','🌿','🌱','🌾','🏔️']
//
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
let symbol;
if (Math.random()<0.95){
symbol = choose(forestTerrain)
} else {
symbol = choose(plants);
}
terrain.push(
{
"symbol": symbol,
"position":
{
"x": x,
"y": y
}
}
)
}
}
// console.log(terrain)
}
let dungeonWithItemsToStrings = () => {
//start with dots
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
dungeon[y][x] = ".";
}
}
//specify terrain locations
for (let i = 0; i < terrain.length; i++){
dungeon[terrain[i].position.y][terrain[i].position.x] = terrain[i].symbol;
}
//specify item locations
for (let i = 0; i < roomItems.length; i++){
dungeon[roomItems[i].position.y][roomItems[i].position.x] = roomItems[i].symbol;
}
//specify places location
for (let i = 0; i < places.length; i++){
dungeon[places[i].position.y][places[i].position.x] = places[i].symbol;
}
//specify player location
dungeon[player.position.y][player.position.x] = player.avatar;
let dungeonStr = "";
for (let i = 0; i < height; i++){
dungeonStr+=(dungeon[i].join("")+"\n");
}
return dungeonStr;
}
let drawDungeon = dungeonStr => {
console.log(dungeonStr); //display
}
let writeToFile = dungeonStr => { //save to disk
//this is very inefficient, just loop it swoop it
//write board to file, although it's all empty save for .....dots
fs.writeFileSync(dungeonFile, dungeonStr);
//write terrain to file
let terrainStr = JSON.stringify(terrain)
fs.writeFileSync(terrainFile, terrainStr);
//save player info to file
let playerData = JSON.stringify(player);
fs.writeFileSync(gameFile, playerData);
//write dates to dateFile
fs.appendFileSync(dateFile, today+'\n');
//write items in current dungeon to file
let itemsData = JSON.stringify(roomItems);
fs.writeFileSync(itemsFile, itemsData);
//list of items saved to player's inventory
console.log('inventory: '+inventory)
let inventoryData = JSON.stringify(inventory);
fs.writeFileSync(inventoryFile, inventoryData);
//save places to file
let placesData = JSON.stringify(places);
fs.writeFileSync(placesFile, placesData);
}
let checkDay = () => {
const savedDays = fs.readFileSync(dateFile).toString().split("\n");;
const lastDay = savedDays[savedDays.length-2]; //loads day last played
console.log("Last movement: "+lastDay);
const fullDate = new Date();
today = fullDate.getFullYear()+"-"+fullDate.getMonth()+"-"+fullDate.getDate();
console.log("Today: "+today);
//TURN OFF RESTRICTION
if (lastDay === today){
//console.log('already moved today');
} else {
player.movedToday = false
player.life = movesInADay;
}
}
//----------------------------SET UP BLESSED----------------------
let initBlessed = () => {
screen = blessed.screen({
smartCSR: true
});
screen.title = gameTitle;
// Create a box perfectly centered horizontally and vertically.
box = blessed.box({
top: 'center',
left: 'center',
width: width+2,
height: height+2,
content: "Welcome to "+gameTitle,
tags: true,
border: {
type: 'line'
},
style: {
fg: 'white',
bg: 'cyan',
border: {
fg: '#f0f0f0'
},
hover: {
bg: 'green'
}
}
});
// Append our box to the screen.
screen.append(box);
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
screen.key(['right', 'l'], function(ch, key) {
console.log('moved right');
player.position.x++;
//loop();
update()
screen.render();
});
screen.key(['left', 'h'], function(ch, key) {
console.log('moved left');
player.position.x--;
//loop();
update()
screen.render();
});
screen.key(['down', 'j'], function(ch, key) {
console.log('moved down');
player.position.y++;
//loop();
update()
screen.render();
});
screen.key(['up', 'k'], function(ch, key) {
player.position.y--;
console.log('moved up');
//loop();
update()
screen.render();
});
screen.render();
}
let drawToBox = () => {
//start with dots
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
dungeon[y][x] = ".";
}
}
//specify terrain locations
for (let i = 0; i < terrain.length; i++){
dungeon[terrain[i].position.y][terrain[i].position.x] = terrain[i].symbol;
}
//specify item locations
for (let i = 0; i < roomItems.length; i++){
dungeon[roomItems[i].position.y][roomItems[i].position.x] = roomItems[i].symbol;
}
//specify places location
for (let i = 0; i < places.length; i++){
dungeon[places[i].position.y][places[i].position.x] = places[i].symbol;
}
//specify player location
dungeon[player.position.y][player.position.x] = player.avatar;
let dungeonStr = "";
for (let i = 0; i < height; i++){
dungeonStr+=(dungeon[i].join("")+"\n");
}
//return dungeonStr;
//
box.setContent(dungeonStr);
/*
for (let i = 0; i < dungeon.length; i++){
box.setLine(i, dungeonStr[i]);
}
*/
}
//--------------------------MAIN GAME LOOP-------------------------
let main = () => {
//----INIT
initBlessed();
checkDay();
parseArgs();
let dungeonStr = dungeonToStrings();
writeToFile(dungeonStr);
//createTerrain(); //do this after writing to file
let dungeonWithItems = dungeonWithItemsToStrings();
drawDungeon(dungeonWithItems);
//debug();
}
let loop = () => {
//
//Probably need to reload these things as you move
//
//
loadDungeon();
loadTerrain();
loadItems();
loadPlaces();
loadPlayer();
loadInventory();
grab();
movePlayer();
checkCollision();
let dungeonStr = dungeonToStrings();
writeToFile(dungeonStr);
let dungeonWithItems = dungeonWithItemsToStrings();
drawDungeon(dungeonWithItems);
//blessed
//
//screen.realloc();
drawToBox();
screen.render();
}
let start = () => {
initBlessed();
createDungeon();
createTerrain();
createPlayer();
loadTextFiles();
createItems();
createBooks();
createPlaces();
createGraves();
}
let update = () => {
loadDungeon();
loadTerrain();
loadItems();
loadPlaces();
loadPlayer();
loadInventory();
grab();
movePlayer();
checkCollision();
screen.realloc();
drawToBox();
screen.render();
}
//main()
start();
update();