コード例 #1
0
ファイル: opencore.cpp プロジェクト: extreme-games/opencore
void
cmd_startbot(CORE_DATA *cd)
{
	if (cd->cmd_argc != 3) {
		RmtMessageFmt(cd->cmd_name, "Usage: %s <type> <arena>", cd->cmd_argv[0]);
		return;
	}

	char *err = StartBot(cd->cmd_argv[1], cd->cmd_argv[2], cd->cmd_name);
	if (err) {
		RmtMessageFmt(cd->cmd_name, "Couldn't start bot: %s", err);
	} else {
		RmtMessageFmt(cd->cmd_name, "Started %s in %s", cd->cmd_argv[1], cd->cmd_argv[2]);
	}
}
コード例 #2
0
ファイル: opencore.cpp プロジェクト: extreme-games/opencore
/*
 * Check the get_file() queue and if there are entries, initiate
 * a file transfer.
 */
void
try_get_next_file(THREAD_DATA *td)
{
	THREAD_DATA::net_t *n = td->net;
	THREAD_DATA::net_t::chunk_in_t *ci = n->chunk_i;

	if (ci->in_use == 0 && ci->file_list->empty() == false) {
		DOWNLOAD_ENTRY *fe = &*ci->file_list->begin();
		LogFmt(OP_SMOD, "Initiating file download: %s", fe->name);
		if (strlen(fe->initiator) > 0) {
			RmtMessageFmt(fe->initiator, "Initiating file download: %s", fe->name);
		}
		PubMessageFmt("*getfile %s", fe->name);
		strlcpy(ci->initiator, fe->initiator, 24);
		ci->file_list->erase(ci->file_list->begin());
		ci->in_use = 1;
	}
}
コード例 #3
0
ファイル: opencore.cpp プロジェクト: extreme-games/opencore
void
try_send_next_file(THREAD_DATA *td)
{
	THREAD_DATA::net_t::send_file_data_t *sfd = td->net->send_file_data;

	if (sfd->in_use == 0 && !sfd->upload_list->empty()) {
		UPLOAD_DATA *ud = *sfd->upload_list->begin();

		sfd->in_use = 1;

		strlcpy(sfd->cur_initiator, ud->initiator, 64);
		strlcpy(sfd->cur_filename, ud->filename, 24);

		PubMessageFmt("*putfile %s", sfd->cur_filename);

		LogFmt(OP_SMOD, "Initiated file upload: %s", sfd->cur_filename);
		if (strlen(sfd->cur_initiator) > 0) {
			RmtMessageFmt(sfd->cur_initiator, "Initiated file upload: %s", sfd->cur_filename);
		}

		sfd->upload_list->erase(sfd->upload_list->begin());
		free(ud);
	} 
}
コード例 #4
0
ファイル: opencore.cpp プロジェクト: extreme-games/opencore
void
do_send_file(THREAD_DATA *td)
{
	THREAD_DATA::net_t::send_file_data_t *sfd = td->net->send_file_data;
	char *filename = sfd->cur_filename;
	const char *initiator = (strlen(sfd->cur_initiator) > 0) ? sfd->cur_initiator : NULL;

	char full_filename[64];

	snprintf(full_filename, 64, "files/%s", filename);

	initiator = initiator ? initiator : "";

	FILE *f = fopen(full_filename, "rb");
	if (f == NULL) {
		LogFmt(OP_SMOD, "Couldn't open file for sending: %s", filename);
		if (initiator) {
			RmtMessageFmt(initiator, "Couldn't open file for sending: %s", filename);
		}
		sfd->in_use = 0;
		return;
	}

	struct stat s;
	memset(&s, 0, sizeof(s));
	fstat(fileno(f), &s);
	int bufl = s.st_size + 17;
	uint8_t *buf = (uint8_t*)xmalloc(bufl);
	if (fread(&buf[17], bufl - 17, 1, f) != 1) {
		LogFmt(OP_SMOD, "Couldn't read file for sending: %s", filename);
		if (initiator) {
			RmtMessageFmt(initiator, "Couldn't read file for sending: %s", filename);
		}
		fclose(f);
		free(buf);
		sfd->in_use = 0;
		return;
	}

	/* add file transfer header */
	buf[0] = 0x16;
	strlcpy((char*)&buf[1], filename, 16);

	int bytes_left = bufl;

	/* send initial stream start */
	while (bytes_left > 0) {
		int nout = MIN(bytes_left, 220); // must fit into a cluster header
		PACKET *p = allocate_packet(6 + nout);
		build_packet(p->data, "AACZ",
		    0x00,
		    0x0A,
		    bufl,
		    &buf[bufl - bytes_left], nout);
		bytes_left -= nout;

		queue_packet_reliable(p, SP_DEFERRED);
	}

	free(buf);
}
コード例 #5
0
ファイル: rabbit.cpp プロジェクト: harveyharvey/opencore
void
GameEvent(CORE_DATA *cd)
{
	switch (cd->event) {
	case EVENT_START:
		RegisterPlugin(OPENCORE_VERSION, "rabbit", "cycad", "1.0", __DATE__, __TIME__, "A rabbit bot", sizeof(USER_DATA), 0);

		/* set rabbit pid to nobody */
		ud(cd)->rabbit_pid = PID_NONE;

		/* register rabbit command */
		RegisterCommand(COMMAND_RABBIT, "!rabbit", "Rabbit", 2, CMD_PRIVATE, "<name>:<bounty>[:<special>]", "Start the rabbit game", NULL); break;
	case EVENT_LOGIN:
		/* set the bot's location to center */
		SetPosition(16 * 512, 16 * 512, 0, 0);
		break;
	case EVENT_COMMAND:
		switch (cd->cmd_id) {
		case COMMAND_RABBIT: {
			int show_usage = 0;

			PLAYER_ID rpid = ud(cd)->rabbit_pid;

			if (rpid == PID_NONE) {	/* rabbit game is not runnig */
				if (cd->cmd_argc <= 1) {
					show_usage = 1;
				} else {
					char *cmd = cd->cmd_argr[1];
					int nargs = ArgCount(cmd, ':');

					if (nargs != 2 && nargs != 3) {
						show_usage = 1;
					} else {	/* args to command are valid */
						/* copy name out of command */
						char rname[20];
						DelimArgs(rname, sizeof(rname), cmd, 0, ':', 0);

						/* copy bounty out of command */
						int bty = AtoiArg(cmd, 1, ':');

						/* copy special out of command if it was specified */
						int special = 0;
						if (nargs == 3)
							special = AtoiArg(cmd, 2, ':');

						/* find the player specified on the command line */
						PLAYER *rabbit = FindPlayerName(rname, MATCH_HERE | MATCH_PREFIX);
						if (rabbit) {
							/* player found, start the game */
							start_rabbit_game(cd, rabbit, bty, special);
						} else {
							RmtMessageFmt(cd->cmd_name, "Player not found: %s", rname);
						}
					}
				}
			} else {
				/* rabbit game is already running */
				PLAYER *p = FindPlayerPid(rpid, MATCH_HERE);

				RmtMessageFmt(cd->cmd_name, "Rabbit is already running (%s is the rabbit)",
				    p ? p->name : "**UNKNOWN**");
			}

			/* display usage if necessary */
			if (show_usage) RmtMessageFmt(cd->cmd_name, "Usage: %s <name>:<bounty>[:<special>]", cd->cmd_argv[0]);
		}
		default: break;
		}
		break;
	case EVENT_UPDATE:
		if (ud(cd)->rabbit_pid == cd->p1->pid) {	/* if the update is for the rabbit */
			if (cd->p1->status & STATUS_SAFE) {	/* check if the rabbit is in safe */
				/* the rabbit entered safe, end the game */
				ArenaMessageFmt("%s has lost the rabbit game by entering safe!", cd->p1->name);
				ud(cd)->rabbit_pid = PID_NONE;
			}
		}
		break;
	case EVENT_TIMER:
		if (ud(cd)->rabbit_pid != PID_NONE) {	/* if the rabbit game is running */
			/* find the rabbit */
			PLAYER *p = FindPlayerPid(ud(cd)->rabbit_pid, MATCH_HERE);
			if (p) {
				if (ud(cd)->where_timer == cd->timer_id) {
					/* a *where timer expired, send the next one and look for response */
					PrivMessage(p, "*where");
					ud(cd)->expecting_where = 1;
				} else if (ud(cd)->gameover_timer == cd->timer_id) {
					/* the game over timer expired, the rabbit didnt die and he won */
					ArenaMessageFmt("%s wins the rabbit game by staying alive!", p->name);
					ud(cd)->rabbit_pid = PID_NONE;
				}
			} else {
				/* rabbit not found, this should never happen! */
			}
		}
	case EVENT_MESSAGE:
		/*
		 * Only look for a response of the game is running, the bot is expecting *where,
		 * and the message type is an arena message.
		 */
		if (ud(cd)->rabbit_pid != PID_NONE && ud(cd)->expecting_where && 
		    cd->msg_type == MSG_ARENA) {
			/* message is a possible *where response */

			/* find the rabbit */
			PLAYER *p = FindPlayerPid(ud(cd)->rabbit_pid, MATCH_HERE);
			if (p) {
				char where_prefix[32];
				snprintf(where_prefix, 32, "%s: ", p->name);
				where_prefix[32 - 1] = '\0';

				/*
				 * Verify that this is *where output by looking for "rabbit: " at the
				 * beginning of the string.
				 */
				if (strncasecmp(where_prefix, cd->msg, strlen(where_prefix)) == 0) {
					/* this must be a *where response, process it */
					char loc[4];
					snprintf(loc, 4, "%s", &cd->msg[strlen(where_prefix)]);
					loc[4 - 1] = '\0';

					/* announce the rabbits location */
					ArenaMessageFmt(">>> Rabbit %s is at %-3s <<<", p->name, loc);

					/* set the next *where timer */
					ud(cd)->where_timer = SetTimer(30 * 1000, 0, 0);

					/*
					 * The bot wont be looking for *where responses until the
					 * next time it sends the command, so turn parsing off
					 * for now.
					 */
					ud(cd)->expecting_where = 0;
				}
			}
		}
		break;
	case EVENT_KILL:
		if (cd->p1->pid == ud(cd)->rabbit_pid) {
			/* the rabbit died */
			ArenaMessageFmt("%s wins the rabbit game by killing Rabbit %s!", cd->p2->name, cd->p1->name);
			ud(cd)->rabbit_pid = PID_NONE;
		}
		break;
	case EVENT_LEAVE:
		if (ud(cd)->rabbit_pid == cd->p1->pid) {
			/* the rabbit left */
			ArenaMessageFmt("%s has lost the rabbit game by leaving the arena!", cd->p1->name);
			ud(cd)->rabbit_pid = PID_NONE;
		}
		break;
	case EVENT_CHANGE:
		if (ud(cd)->rabbit_pid == cd->p1->pid) {
			/* the rabbit changed ship */
			ArenaMessageFmt("%s has lost the rabbit game by changing ship/freq!", cd->p1->name);
			ud(cd)->rabbit_pid = PID_NONE;
		}
		break;
	}
}