User:Rhebus/Gem table generator

From NetHackWiki
Jump to: navigation, search

Also see /Gem table output

#include <string>
#include <iostream>
#include <cstdlib>
#include <sstream>

using std::string;

using std::cout;
using std::endl;

using std::atoi;

using std::stringstream;

#define GEM(name,desc,prob,wt,gval,nutr,mohs,glass,color) \
	printrow(name,desc,prob,wt,gval,nutr,mohs,glass,color)

#define ROCK(name,desc,kn,prob,wt,gval,sdam,ldam,mgc,nutr,mohs,glass,color) \
	printrow(name,desc,prob,wt,gval,nutr,mohs,glass,color)

const char *GEMSTONE = "gemstone";
const char *GLASS    = "glass";
const char *MINERAL  = "mineral";

const char *CLR_WHITE   = "white";
const char *CLR_RED     = "red";
const char *CLR_ORANGE  = "orange";
const char *CLR_BLUE    = "blue";
const char *CLR_BLACK   = "black";
const char *CLR_GREEN   = "green";
const char *CLR_YELLOW  = "yellow";
const char *CLR_MAGENTA = "magenta";
const char *CLR_BROWN   = "brown";
const char *CLR_GRAY    = "gray";

/* Need forward declaration. */
string mkdesc(const char *name, const char *desc, const char *material);

string mksymbol(const char *name,
				const char *desc,
				const char *material,
				const char *color) {
	string ascii = string("{{") + color + string("|*}}");
	string tile  = "[[Image:" + mkdesc(name, desc, material) + ".png]]";

	return ascii + " " + tile;
}


/* Don't display "stone" after the name. */
const string NO_STONE[] = {
	"dilithium crystal",
		"ruby",
		"diamond",
		"sapphire",
		"black opal",
		"emerald",
		"opal",
		""
};

string stone(const char *name, const char *material) {
	if (name == "flint") { return " stone"; }
	else if (material == "glass"
		|| material == "mineral") { return ""; }
	else {
		for (const string *sp = NO_STONE; *sp != ""; ++sp) {
			if (*sp == name) { return ""; }
		}

		return " stone";
	}
}

/* Name should be a wikilink. */
const string LINK_NAME[] = {
	"dilithium crystal",
	"luckstone",
	"loadstone",
	"touchstone",
	"flint",
	"rock",
	""
};

bool link(const char *name) {
	for (const string *sp = LINK_NAME; *sp != ""; ++sp) {
		if (*sp == name) {
			return true;
		}
	}
	return false;
}

string mkname(const char *name, const char *material) {
	bool dolink = link(name);

	return string("'''")
		+ (dolink ? "[[" : "")
		+ name
		+ stone(name, material)
		+ (dolink ? "]]" : "")
		+ string("'''");
}

string mkdesc(const char *name, const char *desc, const char *material) {
	if (!desc) { return string(name); }
	else {
		string gem = (material == string("mineral")) ? "stone" : "gem";
		return string(desc) + " " + gem;
	}
}

string mkcost(int gval) {
	stringstream ss;
	string sgval;
	ss << gval;
	ss >> sgval;

	return sgval + " [[Zorkmid|zm]]";
}

string mkweight(int wt) {
	stringstream ss;
	string swt;
	ss << wt;
	ss >> swt;

	return string("[[Weight|") + swt + string("]]");
}

string mkhard(int mohs) {
	return (mohs >= 8) ? string("HARD") : string("soft");
}

string mkmaterial(const char *material) {
	return string("[[") + material + string("]]");
}

typedef const char *S;
typedef int I;

void pre(void) {
	cout << "<!-- This table was generated by a program; see the talk page. -->\r\n";
	cout << "{| class=\"prettytable sortable\"\r\n";
	cout << "! class=\"unsortable\" | * !! Name !! Description !! Cost !! Weight !! Hardness !! class=\"unsortable\" | Material\r\n";
}

void printrow(S name, S desc, I prob, I wt, I gval, I nutr, I mohs, S glass, S color) {
	cout << "|-" << "\r\n";
	cout << "| " << mksymbol(name, desc, glass, color);
	//printf("|| %s ", mkname(name, glass));
	cout << "|| " << mkname(name, glass);
	//printf("|| %s ", mkdesc(name, desc, glass));
	cout << "|| " << mkdesc(name, desc, glass);
	//printf("|| %s ", mkcost(gval));
	cout << "|| " << mkcost(gval);
	//printf("|| %s ", mkweight(wt));
	cout << "|| " << mkweight(wt);
	//printf("|| %s ", mkhard(mohs));
	cout << "|| " << mkhard(mohs);
	//printf("|| %s ", mkmaterial(glass));
	cout << "|| " << mkmaterial(glass);

	printf("\r\n");
}

void mid(void) {
	cout << "|-\r\n";
	cout << "| colspan=7 |\r\n";
}

void post(void) {
	cout << "|}\r\n";
	cout << "<!-- End generated table. -->\r\n";
}

int main() {

	pre();

GEM("dilithium crystal", "white",      2,  1, 4500, 15,  5, GEMSTONE, CLR_WHITE),
GEM("diamond", "white",                3,  1, 4000, 15, 10, GEMSTONE, CLR_WHITE),
GEM("ruby", "red",                     4,  1, 3500, 15,  9, GEMSTONE, CLR_RED),
GEM("jacinth", "orange",               3,  1, 3250, 15,  9, GEMSTONE, CLR_ORANGE),
GEM("sapphire", "blue",                4,  1, 3000, 15,  9, GEMSTONE, CLR_BLUE),
GEM("black opal", "black",             3,  1, 2500, 15,  8, GEMSTONE, CLR_BLACK),
GEM("emerald", "green",                5,  1, 2500, 15,  8, GEMSTONE, CLR_GREEN),
GEM("turquoise", "green",              6,  1, 2000, 15,  6, GEMSTONE, CLR_GREEN),
GEM("citrine", "yellow",               4,  1, 1500, 15,  6, GEMSTONE, CLR_YELLOW),
GEM("aquamarine", "green",             6,  1, 1500, 15,  8, GEMSTONE, CLR_GREEN),
GEM("amber", "yellowish brown",        8,  1, 1000, 15,  2, GEMSTONE, CLR_BROWN),
GEM("topaz", "yellowish brown",       10,  1,  900, 15,  8, GEMSTONE, CLR_BROWN),
GEM("jet", "black",                    6,  1,  850, 15,  7, GEMSTONE, CLR_BLACK),
GEM("opal", "white",                  12,  1,  800, 15,  6, GEMSTONE, CLR_WHITE),
GEM("chrysoberyl", "yellow",           8,  1,  700, 15,  5, GEMSTONE, CLR_YELLOW),
GEM("garnet", "red",                  12,  1,  700, 15,  7, GEMSTONE, CLR_RED),
GEM("amethyst", "violet",             14,  1,  600, 15,  7, GEMSTONE, CLR_MAGENTA),
GEM("jasper", "red",                  15,  1,  500, 15,  7, GEMSTONE, CLR_RED),
GEM("fluorite", "violet",             15,  1,  400, 15,  4, GEMSTONE, CLR_MAGENTA),
GEM("obsidian", "black",               9,  1,  200, 15,  6, GEMSTONE, CLR_BLACK),
GEM("agate", "orange",                12,  1,  200, 15,  6, GEMSTONE, CLR_ORANGE),
GEM("jade", "green",                  10,  1,  300, 15,  6, GEMSTONE, CLR_GREEN),

	mid();

GEM("worthless piece of white glass", "white",   77, 1, 0, 6, 5, GLASS, CLR_WHITE),
GEM("worthless piece of blue glass", "blue",     77, 1, 0, 6, 5, GLASS, CLR_BLUE),
GEM("worthless piece of red glass", "red",       77, 1, 0, 6, 5, GLASS, CLR_RED),
GEM("worthless piece of yellowish brown glass", "yellowish brown", 77, 1, 0, 6, 5, GLASS, CLR_BROWN),
GEM("worthless piece of orange glass", "orange", 76, 1, 0, 6, 5, GLASS, CLR_ORANGE),
GEM("worthless piece of yellow glass", "yellow", 77, 1, 0, 6, 5, GLASS, CLR_YELLOW),
GEM("worthless piece of black glass",  "black",  76, 1, 0, 6, 5, GLASS, CLR_BLACK),
GEM("worthless piece of green glass", "green",   77, 1, 0, 6, 5, GLASS, CLR_GREEN),
GEM("worthless piece of violet glass", "violet", 77, 1, 0, 6, 5, GLASS, CLR_MAGENTA),

	mid();

ROCK("luckstone", "gray",	0, 10,  10, 60, 3, 3, 1, 10, 7, MINERAL, CLR_GRAY),
ROCK("loadstone", "gray",	0, 10, 500,  1, 3, 3, 1, 10, 6, MINERAL, CLR_GRAY),
ROCK("touchstone", "gray",	0,  8,  10, 45, 3, 3, 1, 10, 6, MINERAL, CLR_GRAY),
ROCK("flint", "gray",		0, 10,  10,  1, 6, 6, 0, 10, 7, MINERAL, CLR_GRAY),
ROCK("rock", (char *)0,		1,100,  10,  0, 3, 3, 0, 10, 7, MINERAL, CLR_GRAY),

	post();
}