Beispiel #1
0
/* editCom: modify the command cmd with newResp and newcd */
bool CustomHandler::editcom(const char *cmd, const char *response, time_t newcd)
{
	Json::Value *com;
	char resp[MAX_MSG];

	if (!(com = getcom(cmd))) {
		snprintf(err, MAX_LEN, "not a command: $%s", cmd);
		return false;
	}
	if (response) {
		resp[0] = '\0';
		if (*response == '/')
			strcat(resp, " ");
		strcat(resp, response);

		if (!valid_resp(resp, err))
			return false;

		(*com)["response"] = resp;
	}
	if (newcd != -1) {
		(*com)["cooldown"] = (Json::Int64)newcd;
		cooldowns->remove(cmd);
		cooldowns->add(cmd, newcd);
	}
	(*com)["mtime"] = (Json::Int64)time(nullptr);
	write();
	return true;
}
Beispiel #2
0
/* valid_name: check if cmd is a valid command name */
bool CustomHandler::valid_name(const char *cmd, bool loading)
{
	/* when loading commands from file, don't check against stored cmds */
	return default_cmds->find(cmd) == default_cmds->end()
		&& strcmp(cmd, wheel_cmd) != 0
		&& strlen(cmd) < 32 && (loading || !getcom(cmd));
}
Beispiel #3
0
/* deactivate: deactivate the command cmd */
bool CustomHandler::deactivate(const char *cmd)
{
	Json::Value *com;

	if (!(com = getcom(cmd))) {
		snprintf(err, MAX_LEN, "not a command: $%s", cmd);
		return false;
	}

	(*com)["active"] = false;
	write();
	return true;
}
Beispiel #4
0
/* activate: activate the command cmd */
bool CustomHandler::activate(const char *cmd)
{
	Json::Value *com;

	if (!(com = getcom(cmd))) {
		snprintf(err, MAX_LEN, "not a command: $%s", cmd);
		return false;
	}
	if (!valid_resp((*com)["response"].asCString(), err))
		return false;

	(*com)["active"] = true;
	write();
	return true;
}
Beispiel #5
0
int
main(int argc, char **argv)
{
	char mainbuf[LINELENGTH];
	char *next;

	/* Open the score file then revoke setgid privileges */
	open_score_file();
	setgid(getgid());

	initialize(argc < 2 || strcmp(argv[1], "-r"));
start:
	news();
	beenthere[position]++;
	if (notes[LAUNCHED])
		crash();	/* decrements fuel & crash */
	if (matchlight) {
		puts("Your match splutters out.");
		matchlight = 0;
	}
	if (!notes[CANTSEE] || testbit(inven, LAMPON) ||
	    testbit(location[position].objects, LAMPON)) {
		writedes();
		printobjs();
	} else
		puts("It's too dark to see anything in here!");
	whichway(location[position]);
run:
	next = getcom(mainbuf, sizeof mainbuf, ">-: ",
	    "Please type in something.");
	for (wordcount = 0; next && wordcount < 20; wordcount++)
		next = getword(next, words[wordcount], -1);
	parse();
	switch (cypher()) {
	case -1:
		goto run;
	case 0:
		goto start;
	default:
		exit(1); /* Shouldn't happen */
	}
	return (1);
}
Beispiel #6
0
/* rename: rename custom command cmd to newcmd */
bool CustomHandler::rename(const char *cmd, const char *newcmd)
{
	Json::Value *com;

	if (!(com = getcom(cmd))) {
		snprintf(err, MAX_LEN, "not a command: $%s", cmd);
		return false;
	}
	if (!valid_name(newcmd)) {
		snprintf(err, MAX_LEN, "invalid command name: $%s", newcmd);
		return false;
	}

	(*com)["cmd"] = newcmd;
	(*com)["mtime"] = (Json::Int64)time(nullptr);
	cooldowns->remove(cmd);
	cooldowns->add(newcmd, (*com)["cooldown"].asInt64());
	write();
	return true;
}
Beispiel #7
0
int
fight(int enemy, int strength)
{
	int lifeline;
	int hurt;
	char auxbuf[LINELENGTH];
	char *next;
	int i;
	int exhaustion;

	exhaustion = lifeline = 0;
fighton:
	gtime++;
	snooze -= 5;
	if (snooze > gtime)
		exhaustion = CYCLE / (snooze - gtime);
	else {
		puts("You collapse exhausted, and he pulverizes your skull.");
		die(0);
	}
	if (snooze - gtime < 20)
		puts("You look tired! I hope you're able to fight.");
	next = getcom(auxbuf, LINELENGTH, "<fight!>-: ", 0);
	for (i = 0; next && i < 10; i++)
		next = getword(next, words[i], -1);
	parse();
	switch (wordvalue[wordnumber]) {
	case KILL:
	case SMITE:
		if (testbit(inven, TWO_HANDED))
			hurt = rnd(70) - 2 * card(injuries, NUMOFINJURIES) -
			    ucard(wear) - exhaustion;
		else if (testbit(inven, SWORD) || testbit(inven, BROAD))
			hurt = rnd(50) % (WEIGHT - carrying) -
			    card(injuries, NUMOFINJURIES) - encumber -
			    exhaustion;
		else if (testbit(inven, KNIFE) || testbit(inven, MALLET) ||
		    testbit(inven, CHAIN) || testbit(inven, MACE) ||
		    testbit(inven, HALBERD))
			hurt = rnd(15) - card(injuries, NUMOFINJURIES) -
			    exhaustion;
		else
			hurt = rnd(7) - encumber;
		if (hurt < 5)
			switch (rnd(3)) {
			case 0:
				puts("You swung wide and missed.");
				break;
			case 1:
				puts("He checked your blow. CLASH! CLANG!");
				break;
			case 2:
				puts("His filthy tunic hangs by one less thread.");
				break;
			}
		else if (hurt < 10) {
			switch (rnd(3)) {
			case 0:
				puts("He's bleeding.");
				break;
			case 1:
				puts("A trickle of blood runs down his face.");
				break;
			case 2:
				puts("A huge purple bruise is forming on the side of his face.");
				break;
			}
			lifeline++;
		} else if (hurt < 20) {
			switch (rnd(3)) {
			case 0:
				puts("He staggers back quavering.");
				break;
			case 1:
				puts("He jumps back with his hand over the wound.");
				break;
			case 2:
				puts("His shirt falls open with a swath across the chest.");
				break;
			}
			lifeline += 5;
		} else if (hurt < 30) {
			switch (rnd(3)) {
			case 0:
				printf("A bloody gash opens up on his %s side.\n",
				    (rnd(2) ? "left" : "right"));
				break;
			case 1:
				puts("The steel bites home and scrapes along his ribs.");
				break;
			case 2:
				puts("You pierce him, and his breath hisses through clenched teeth.");
				break;
			}
			lifeline += 10;
		} else if (hurt < 40) {
			switch (rnd(3)) {
			case 0:
				puts("You smite him to the ground.");
				if (strength - lifeline > 20)
					puts("But in a flurry of steel he regains his feet!");
				break;
			case 1:
				puts("The force of your blow sends him to his knees.");
				puts("His arm swings lifeless at his side.");
				break;
			case 2:
				puts("Clutching his blood drenched shirt, he collapses stunned.");
				break;
			}
			lifeline += 20;
		} else {
			switch (rnd(3)) {
			case 0:
				puts("His ribs crack under your powerful swing, flooding his lungs with blood.");
				break;
			case 1:
				puts("You shatter his upheld arm in a spray of blood.  The blade continues deep");
				puts("into his back, severing the spinal cord.");
				lifeline += 25;
				break;
			case 2:
				puts("With a mighty lunge the steel slides in, and gasping, he falls to the ground.");
				lifeline += 25;
				break;
			}
			lifeline += 30;
		}
		break;

	case BACK:
		if (enemy == DARK && lifeline > strength * 0.33) {
			puts("He throws you back against the rock and pummels your face.");
			if (testbit(inven, AMULET) || testbit(wear, AMULET)) {
				printf("Lifting the amulet from you, ");
				if (testbit(inven, MEDALION) ||
				    testbit(wear, MEDALION)) {
					puts("his power grows and the walls of\nthe earth tremble.");
					puts("When he touches the medallion, your chest explodes and the foundations of the\nearth collapse.");
					puts("The planet is consumed by darkness.");
					die(0);
				}
				if (testbit(inven, AMULET)) {
					clearbit(inven, AMULET);
					carrying -= objwt[AMULET];
					encumber -= objcumber[AMULET];
				} else
					clearbit(wear, AMULET);
				puts("he flees down the dark caverns.");
				clearbit(location[position].objects, DARK);
				injuries[SKULL] = 1;
				followfight = gtime;
				return (0);
			} else {
				puts("I'm afraid you have been killed.");
				die(0);
			}
		} else {
			puts("You escape stunned and disoriented from the fight.");
			puts("A victorious bellow echoes from the battlescene.");
			if (back && position != back)
				battlestar_move(back, BACK);
			else if (ahead && position != ahead)
				battlestar_move(ahead, AHEAD);
			else if (left && position != left)
				battlestar_move(left, LEFT);
			else if (right && position != right)
				battlestar_move(right, RIGHT);
			else
				battlestar_move(location[position].down, AHEAD);
			return (0);
		}

	case SHOOT:
		if (testbit(inven, LASER)) {
			if (strength - lifeline <= 50) {
				printf("The %s took a direct hit!\n",
				    objsht[enemy]);
				lifeline += 50;
			} else {
				puts("With his bare hand he deflects the laser blast and whips the pistol from you!");
				clearbit(inven, LASER);
				setbit(location[position].objects, LASER);
				carrying -= objwt[LASER];
				encumber -= objcumber[LASER];
			}
		} else
			puts("Unfortunately, you don't have a blaster handy.");
		break;

	case DROP:
	case DRAW:
		cypher();
		gtime--;
		break;

	default:
		puts("You don't have a chance, he is too quick.");
		break;
	}
	if (lifeline >= strength) {
		printf("You have killed the %s.\n", objsht[enemy]);
		if (enemy == ELF || enemy == DARK)
			puts("A watery black smoke consumes his body and then vanishes with a peal of thunder!");
		clearbit(location[position].objects, enemy);
		power += 2;
		notes[JINXED]++;
		return (0);
	}
	puts("He attacks...");
	/* Some embellishments. */
	hurt = rnd(NUMOFINJURIES) - (testbit(inven, SHIELD) != 0) -
	    (testbit(wear, MAIL) != 0) - (testbit(wear, HELM) != 0);
	hurt += (testbit(wear, AMULET) != 0) +
	    (testbit(wear, MEDALION) != 0) + (testbit(wear, TALISMAN) != 0);
	hurt = hurt < 0 ? 0 : hurt;
	hurt = hurt >= NUMOFINJURIES ? NUMOFINJURIES - 1 : hurt;
	if (!injuries[hurt]) {
		injuries[hurt] = 1;
		printf("I'm afraid you have suffered %s.\n", ouch[hurt]);
	} else
		puts("You emerge unscathed.");
	if (injuries[SKULL] && injuries[INCISE] && injuries[NECK]) {
		puts("I'm afraid you have suffered fatal injuries.");
		die(0);
	}
	goto fighton;
}
Beispiel #8
0
static void
getpsbb(const char *name, double bb[4])
{
	struct fg	*fp;
	char	*buf = NULL;
	char	*cp;
	size_t	size = 0;
	int	fd, n, k;
	int	lineno = 0;
	int	found = 0;
	int	atend = 0;
	int	state = 0;
	int	indoc = 0;

	if ((fd = open(name, O_RDONLY)) < 0) {
		errprint("can't open %s", name);
		return;
	}
	fp = calloc(1, sizeof *fp);
	fp->fd = fd;
	for (;;) {
		n = psgetline(fp, &buf, &size);
		if (++lineno == 1 && (n == 0 || strncmp(buf, "%!PS-", 5))) {
			errprint("%s is not a DSC-conforming "
					"PostScript document", name);
			break;
		}
		if (n > 0 && state != 1 &&
				(cp = getcom(buf, "%%BoundingBox:")) != NULL) {
			while (*cp == ' ' || *cp == '\t')
				cp++;
			if (strncmp(cp, "(atend)", 7) == 0) {
				atend++;
				continue;
			}
			bb[0] = strtod(cp, &cp);
			if (*cp)
				bb[1] = strtod(cp, &cp);
			if (*cp)
				bb[2] = strtod(cp, &cp);
			if (*cp) {
				bb[3] = strtod(cp, &cp);
				found = 1;
			} else
				errprint("missing arguments to "
					"%%%%BoundingBox: in %s, line %d\n",
					name, lineno);
			continue;
		}
		if (n > 0 && state != 1 &&
				(cp = getcom(buf, "%%HiResBoundingBox:"))
				!= NULL) {
			while (*cp == ' ' || *cp == '\t')
				cp++;
			if (strncmp(cp, "(atend)", 7) == 0) {
				atend++;
				continue;
			}
			bb[0] = strtod(cp, &cp);
			if (*cp)
				bb[1] = strtod(cp, &cp);
			if (*cp)
				bb[2] = strtod(cp, &cp);
			if (*cp) {
				bb[3] = strtod(cp, &cp);
				break;
			} else {
				errprint("missing arguments to "
					"%%%%HiResBoundingBox: in %s, "
					"line %d\n",
					name, lineno);
				continue;
			}
		}
		if (n == 0 || (state == 0 &&
				(getcom(buf, "%%EndComments") != NULL ||
				 buf[0] != '%' || buf[1] == ' ' ||
				 buf[1] == '\t' || buf[1] == '\r' ||
				 buf[1] == '\n'))) {
		eof:	if (found == 0 && (atend == 0 || n == 0))
				errprint("%s lacks a %%%%BoundingBox: DSC "
					"comment", name);
			if (atend == 0 || n == 0)
				break;
			state = 1;
			continue;
		}
		if (indoc == 0 && getcom(buf, "%%EOF") != NULL) {
			n = 0;
			goto eof;
		}
		if (state == 1 && indoc == 0 &&
				getcom(buf, "%%Trailer") != NULL) {
			state = 2;
			continue;
		}
		if (state == 1 && getcom(buf, "%%BeginDocument:") != NULL) {
			indoc++;
			continue;
		}
		if (state == 1 && indoc > 0 &&
				getcom(buf, "%%EndDocument") != NULL) {
			indoc--;
			continue;
		}
		if (state == 1 &&
				(cp = getcom(buf, "%%BeginBinary:")) != NULL) {
			if ((k = strtol(cp, &cp, 10)) > 0)
				psskip(fp, k);
			continue;
		}
		if (state == 1 && (cp = getcom(buf, "%%BeginData:")) != NULL) {
			if ((k = strtol(cp, &cp, 10)) > 0) {
				while (*cp == ' ' || *cp == '\t')
					cp++;
				while (*cp && *cp != ' ' && *cp != '\t')
					cp++;
				while (*cp == ' ' || *cp == '\t')
					cp++;
				if (strncmp(cp, "Bytes", 5) == 0)
					psskip(fp, k);
				else if (strncmp(cp, "Lines", 5) == 0) {
					while (k--) {
						n = psgetline(fp, &buf, &size);
						if (n == 0)
							goto eof;
					}
				}
			}
			continue;
		}
	}
	free(fp);
	free(buf);
	close(fd);
}