updated and minimal working prototype!

This commit is contained in:
lee2sman 2021-03-28 04:18:54 -04:00
parent 40099336a2
commit 61490b5d00
1 changed files with 527 additions and 0 deletions

527
village.js Normal file
View File

@ -0,0 +1,527 @@
#!/usr/bin/env node
//libraries
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");
//save/load files
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";
// game vars
program = blessed.program();
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
};
//----------------------START and LOOP-------------------------------
function start(){
if (argv.new){
createMap();
}
loadAll();
program.alternateBuffer();
program.clear();
program.move(1, 1);
program.bg('blue');
program.write('A Visit to Moaning Cream Camp, and Other Villages', 'red fg');
//program.setx((program.cols / 4 | 0) );
program.setx(1);
program.down(3);
program.write('Directions: arrow keys (or vimkeys)');
program.setx(1);
program.down(1);
program.write('Q to quit');
program.bg('!black');
program.feed();
}
function loop(){
program.on('keypress', function(ch, key) {
resetScreen();
checkKeys(key);
checkCollision();
updateDungeon();
drawDungeon();
program.setx(1);
});
}
//----------------------------functions-------------------------------------
function checkKeys(key){
switch(key.name) {
case 'q':
program.disableMouse();
program.showCursor();
program.normalBuffer();
process.exit(0);
case 'right': //vim key 'l' or right-arrow
case 'l':
//program.write('moved right');
player.position.x++;
break;
case 'left': //vim key 'h' OR left-arrow
case 'h':
//program.write('moved left');
player.position.x--;
break;
case 'up': //vim key 'k' or up-arrow
case 'k':
//program.write('moved up');
player.position.y--;
break;
case 'down': //vim key 'j' or down-arrow
case 'j':
//program.write('moved down');
player.position.y++;
break;
case 'p':
program.write('picked up something');
pickup();
break;
case 'i':
//program.write('list inventory');
inv();
break;
default: //else
program.write('hit another key');
}
}
function pickup(){
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);
}
}
}
function inv(){
program.setx(1)
for (let i = 0; i < inventory.length; i++){
program.setx(0);
program.down(10);
program.write(inventory[i].symbol+": "+inventory[i].description);
}
}
function 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);
//program.down(5);
//program.write(roomItems[i].name);
//program.write(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
//program.write(places[i].name);
}
}
}
function resetScreen(){
program.setx(1);
program.bg('!green');
program.clear();
program.bg('green');
}
function updateDungeon(){
//start with dots
for (let y = 0; y < height; y++){
for (let x = 0; x < width; x++){
dungeon[y][x] = ".";
}
}
//add terrain for drawing
for (let i = 0; i < terrain.length; i++){
dungeon[terrain[i].position.y][terrain[i].position.x] = terrain[i].symbol;
}
//add items to map
for (let i = 0; i < roomItems.length; i++){
dungeon[roomItems[i].position.y][roomItems[i].position.x] = roomItems[i].symbol;
}
//add places to map
for (let i = 0; i < places.length; i++){
dungeon[places[i].position.y][places[i].position.x] = places[i].symbol;
}
//add player for drawing
dungeon[player.position.y][player.position.x] = player.avatar;
}
function drawDungeon(){
for (let y=0; y < dungeon.length; y++){
let dungeonStr = '';
for (let x=0; x < dungeon[0].length; x++){
dungeonStr+=dungeon[y][x];
}
program.setx(1)
program.write(dungeonStr);
program.down(1)
//console.log(dungeonStr);
}
}
//--------------------------CREATE-MAP---------------------------------------
function createMap(){
createDungeon();
createTerrain();
createPlayer();
loadTextFiles();
createItems();
createBooks();
createPlaces();
createGraves();
saveMap();
}
function createDungeon(){
let createDungeon = () => {
dungeon = Array.from(Array(height), () => new Array(width))
}
}
let createTerrain = () => {
let forestTerrain = ['ᚠ','ᚡ','ᚴ','ᚵ','ᚶ','ᛉ','ᛘ','ᛠ'];
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
}
}
)
}
}
}
let createPlayer = () => {
player.avatar = '@';
player.position.x = Math.floor(Math.random()*width)
player.position.y = Math.floor(Math.random()*height)
}
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)
}
}
)
}
function saveMap(){
//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);
}
//-------------------------LOADING HELPERS------------------------------------
//
function loadAll(){
loadDungeon();
loadTerrain();
loadItems();
loadPlaces();
loadPlayer();
loadInventory();
}
function loadDungeon(){
dungeon = Array.from(Array(height), () => new Array(width))
}
function loadTerrain() {
const terrainStr = fs.readFileSync(terrainFile, 'utf8');
terrain = JSON.parse(terrainStr);
}
let loadPlayer = () => {
const playerFile = fs.readFileSync(gameFile,'utf8');
player = JSON.parse(playerFile);
}
let loadItems = () => {
const itemsStr = fs.readFileSync(itemsFile, 'utf8');
roomItems = JSON.parse(itemsStr)
}
let loadPlaces = () => {
const placesStr = fs.readFileSync(placesFile, 'utf8');
places = JSON.parse(placesStr)
}
let loadInventory = () => {
const invFile = fs.readFileSync(inventoryFile,'utf8');
inventory = JSON.parse(invFile);
}
//--------------------------MAIN-------------------------------
start();
loop();