static void
parse_states(const char *arg, struct ipt_conntrack_info *sinfo)
{
	const char *comma;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg || !parse_state(arg, comma-arg, sinfo))
			exit_error(PARAMETER_PROBLEM, "Bad ctstate `%s'", arg);
		arg = comma+1;
	}

	if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo))
		exit_error(PARAMETER_PROBLEM, "Bad ctstate `%s'", arg);
}
Beispiel #2
0
TEST(SimulatorTest, Simulate4) {
    MyBot bot;
    string state[] = {"P 10.8 11.5 0   1 1",
                      "P 19.4  6.6 1 110 5",
                      "P  2.2 16.3 2  33 5",
                      "F 2 50 2 0 10 8",
                      "F 2 27 2 0 10 9",
                      "F 1 51 2 1 10 8",
                      "F 1 51 2 1 10 8"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);
    vector<plstate_t> const * states = sim.planet_states(0);

    plstate_t s = states->at(8);
    EXPECT_EQ(enemy, s._owner)  << "turn 8 state 49:enemy";
    EXPECT_EQ(49, s._ships)     << "turn 8 state 49:enemy";

    s = states->at(9);
    EXPECT_EQ(enemy, s._owner)  << "turn 9 state 77:enemy";
    EXPECT_EQ(77, s._ships)     << "turn 9 state 77:enemy";

    // re-simulation
    sim.simulate(9, true);
    states = sim.planet_states(0);
    s = states->at(8);
    EXPECT_EQ(enemy, s._owner)  << "turn 8 state 49:enemy after re-sim";
    EXPECT_EQ(49, s._ships)     << "turn 8 state 49:enemy after re-sim";
}
Beispiel #3
0
static void
parse_states(const char *arg, struct xt_conntrack_info *sinfo)
{
	const char *comma;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg || !parse_state(arg, comma-arg, sinfo))
			xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
		arg = comma+1;
	}
	if (!*arg)
		xtables_error(PARAMETER_PROBLEM, "\"--ctstate\" requires a list of "
					      "states with no spaces, e.g. "
					      "ESTABLISHED,RELATED");
	if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo))
		xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
}
TEST(TargetTest, TargetSelect) {
    MyBot bot;
    string state[] = {"P 14.4 3.3 0 20 5",
                      "F 1 30 42 0 10 3",
                      "F 2 23 42 0 10 3"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);
    vector<target_t> targets = sim.select_targets(enemy);
    EXPECT_TRUE(std::find(targets.begin(), targets.end(), target_t(0, ally, 13, 4, attack)) != targets.end())
        << "sneak attack for enemy";
}
Beispiel #5
0
TEST(SimulatorTest, Simulate2) {
    MyBot bot;
    string state[] = {"P 14.4 3.3 0 20 5",
                      "F 1 30 42 0 10 3",
                      "F 2 23 42 0 10 3"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);

    EXPECT_EQ((ships_t)0, sim.ships_avail(0, 3, enemy)) << "0 enemy ships at 3rd turn";
    EXPECT_EQ((ships_t)0, sim.ships_avail(0, 4, enemy)) << "0 enemy ships at 4th turn";
    EXPECT_EQ((ships_t)0, sim.ships_avail(0, 2, ally))  << "0 ally ships at 2nd turn";
    EXPECT_EQ((ships_t)7, sim.ships_avail(0, 3, ally))  << "7 ally ships at 3rd turn";
}
Beispiel #6
0
static int parse_msrp_query(char *sz, size_t len, struct mrpdhelper_notify *n)
{
	char *r;

	if (parse_msrp_string(sz, len, n) < 0)
		return -1;

	if (parse_registrar(sz, n, &r) < 0)
		return -1;

	if (parse_app_state(&r[15], n) < 0)
		return -1;

	return parse_state(&r[18], n);
}
Beispiel #7
0
static int parse_mmrp(char *sz, size_t len, struct mrpdhelper_notify *n)
{
	if (len < 9)	/* protect against sscanf(&sz[8],...) runaway */
		return -1;

	if (parse_notification(&sz[1], n) < 0)
		return -1;

	if (parse_state(&sz[5], n) < 0)
		return -1;

	n->attrib = mrpdhelper_attribtype_mvrp;
	if (sscanf(&sz[8], "M=%" SCNx64, &n->u.m.mac) != 1)
		return -1;
	return parse_registrar(sz, n, NULL);
}
Beispiel #8
0
static int parse_msrp(char *sz, size_t len, struct mrpdhelper_notify *n)
{
	if (parse_notification(&sz[1], n) < 0)
		return -1;

	if (parse_msrp_string(&sz[4], len - 4, n) < 0)
		return -1;

	if (parse_registrar(sz, n, NULL) < 0)
		return -1;

	if (parse_app_state(sz, n) < 0)
		return -1;

	return parse_state(sz, n);
}
Beispiel #9
0
TEST(SimulatorTest, SimulateNoChange) {
    MyBot bot;
    string state[] = {"P 16.8 20.3 2 5 2",
                      "F 1 1  1 0 5 1",
                      "F 1 1  1 0 5 2",
                      "F 1 1  1 0 5 3",
                      "F 1 1 21 0 6 4",
                      "F 1 1  1 0 5 4",
                      "F 1 1 15 0 2 1"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);

    vector<plstate_t> const * states = sim.planet_states(0);
    for (vector<plstate_t>::const_iterator i=states->begin(); i<states->end(); i++)
        EXPECT_EQ(enemy, i->_owner) << "ownership doesn't change";
}
Beispiel #10
0
TEST(SimulatorTest, Simulate) {
    MyBot bot;
    string state[] = {"P 14.4 3.3 0 10 1",
                      "F 1 10 42 0 10 3",
                      "F 2  1 42 0 10 4"};
    parse_state(bot, state, LENGTH(state));

    EXPECT_TRUE(bot.game()->fleets()->find(10) == bot.game()->fleets()->end()) << "check fleets searching";
    EXPECT_EQ((unsigned int)1, bot.game()->fleets()->find(3)->second.size()) << "1 fleet at turn 3";
    EXPECT_EQ((unsigned int)1, bot.game()->fleets()->find(4)->second.size()) << "1 fleet at turn 4";

    Simulator sim(bot.game(), bot.map(), 10);

    EXPECT_EQ((ships_t)0, sim.ships_avail(0, 3, enemy)) << "0 enemy ships at 3rd turn";
    EXPECT_EQ((ships_t)1, sim.ships_avail(0, 4, enemy)) << "1 enemy ships at 4th turn";
    EXPECT_EQ((ships_t)0, sim.ships_avail(0, 3, ally))  << "0 ally ships at 3rd turn";
    EXPECT_EQ((ships_t)0, sim.ships_avail(0, 4, ally))  << "0 ally ships at 4th turn";
}
Beispiel #11
0
void
fn_state(char **args)
{
    char *state, *line;
    enum state st;
    int freestatep;

    freestatep = 0;
    line = NULL;

    if (args) {
	if (args[0][0] != '<') {
	    if ((state=(char *)malloc(strlen(args[0])+3)) == NULL)
		return;
	    sprintf(state, "<%s>", args[0]);
	    freestatep = 1;
	}
	else
	    state = args[0];
    }
    else {
	line = read_string("State: ", 1);
	if (line == NULL || line[0] == '\0')
	    return;
	if (line[0] != '<') {
	    if ((state=(char *)malloc(strlen(line)+3)) == NULL)
		return;
	    sprintf(state, "<%s>", line);
	    freestatep = 1;
	}
	else
	    state = line;
    }

    st = parse_state(state);
    if (st == binding_state)
	return;
    
    enter_state(st);

    if (freestatep)
	free(state);
    free(line);
}	
Beispiel #12
0
TEST(SimulatorTest, SimulateNoChange2) {
    MyBot bot;
    string state[] = {"P 1.1 14.3 1 25 1",
                      "F 2 14  1 0 7 1",
                      "F 2 18  1 0 7 4",
                      "F 1  2 21 0 4 1",
                      "F 1  5  9 0 4 1",
                      "F 1  4 12 0 2 1"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 50);

    vector<plstate_t> const * states = sim.planet_states(0);
    for (vector<plstate_t>::const_iterator i=states->begin(); i<states->end(); i++)
        EXPECT_EQ(ally, i->_owner) << "ownership doesn't change";

    sim.simulate(10, true);
    EXPECT_EQ((unsigned int)10, sim.planet_states(0)->size()) << "number of states after re-sim";
}
Beispiel #13
0
static int parse_mmrp(char *sz, size_t len, struct mrpdhelper_notify *n)
{
	/* format
	   MIN MJO M=112233445566 R=112233445566
	   len = 38
	 */
	if (len < 38)
		return -1;

	if (parse_notification(&sz[1], n) < 0)
		return -1;

	if (parse_state(&sz[5], n) < 0)
		return -1;

	n->attrib = mrpdhelper_attribtype_mvrp;
	if (sscanf(&sz[8], "M=%" SCNx64, &n->u.m.mac) != 1)
		return -1;
	return parse_registrar(sz, n, NULL);
}
Beispiel #14
0
static int parse_mvrp(char *sz, size_t len, struct mrpdhelper_notify *n)
{
	/* format
	   VIN VJO 1234 R=112233445566
	   len = 28
	 */
	if (len < 28)
		return -1;

	if (parse_notification(&sz[1], n) < 0)
		return -1;

	if (parse_state(&sz[5], n) < 0)
		return -1;

	n->attrib = mrpdhelper_attribtype_mvrp;
	if (sscanf(&sz[8], "%04x ", &n->u.v.vid) != 1)
		return -1;
	return parse_registrar(sz, n, NULL);
}
Beispiel #15
0
TEST(TargetTest, TargetSelect2) {
    MyBot bot;
    string state[] = {"P 14.4 3.3 1 10 1",
                      "F 2 13 42 0 10 3",
                      "F 2  2 42 0 10 4",
                      "F 1  3 42 0 10 6",
                      "F 1  3 42 0 10 8"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);

    vector<target_t> targets = sim.select_targets(enemy);
    cerr << targets;
    EXPECT_TRUE(std::find(targets.begin(), targets.end(), target_t(0, enemy, 1, 8, defence)) != targets.end())
        << "sneak defence for enemy";

    targets = sim.select_targets(ally);
    EXPECT_TRUE(std::find(targets.begin(), targets.end(), target_t(0, ally, 1, 4, defence)) != targets.end())
        << "sneak defence for ally";
}
Beispiel #16
0
TEST(SimulatorTest, Simulate3) {
    MyBot bot;
    string state[] = {"P 14.4 3.3 1 10 1",
                      "F 2 13 42 0 10 3",
                      "F 2  2 42 0 10 4",
                      "F 1  3 42 0 10 6",
                      "F 1  3 42 0 10 8"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);

    vector<plstate_t> const * states = sim.planet_states(0);

    plstate_t s = states->at(4);
    EXPECT_EQ(enemy, s._owner) << "turn 4 ownership change to 1:enemy";
    EXPECT_EQ(1, s._ships)     << "turn 4 ownership change to 1:enemy";

    s = states->at(8);
    EXPECT_EQ(ally, s._owner)  << "turn 8 ownership change to 1:ally";
    EXPECT_EQ(1, s._ships)     << "turn 8 ownership change to 1:ally";
}
Beispiel #17
0
TEST(SimulatorTest, SimulateFuture) {
    MyBot bot;
    string state[] = {"P 14.425990 3.301771 0 10 1",
                      "P 14.290286 8.040786 0  0 0",
                      "F 1 10 42 0 10 3",
                      "F 2  1 42 0 10 4"};
    parse_state(bot, state, LENGTH(state));

    EXPECT_EQ(5, bot.map()->distance(0, 1)) << "check 0-1 distance";

    Simulator sim(bot.game(), bot.map(), 0); // no initial simulation
    Move move;
    move.add(FutureOrder(enemy, 1, 0, 1, 4)); // 1 enemy ship from 0 to 1 at turn 4

    sim.simulate(move, 10, true);

    EXPECT_TRUE(plstate_t(enemy, 5) == sim.planet_states(0)->at(9)) << "planet 0 - 5:enemy at turn 9";
    EXPECT_TRUE(plstate_t(enemy, 1) == sim.planet_states(1)->at(9)) << "planet 1 - 1:enemy at turn 9";

    sim.simulate(move, 21, true);
    EXPECT_TRUE(plstate_t(enemy, 1) == sim.planet_states(1)->at(19)) << "planet 1 - 1:enemy at turn 19";
}
Beispiel #18
0
TEST(TargetTest, TargetSelect3) {
    MyBot bot;
    string state[] = {"P 10.8 11.5 0   1 1",
                      "P 19.4  6.6 1 110 5",
                      "P  2.2 16.3 2  33 5",
                      "F 2 50 2 0 10 8",
                      "F 2 27 2 0 10 9",
                      "F 1 51 2 1 10 8",
                      "F 1 51 2 1 10 8"};
    parse_state(bot, state, LENGTH(state));

    Simulator sim(bot.game(), bot.map(), 10);

    vector<target_t> targets = sim.select_targets(enemy);
    EXPECT_TRUE(std::find(targets.begin(), targets.end(), target_t(1, ally, 116, 1, expand)) != targets.end())
        << "earliest attack on opposite";

    targets = sim.select_targets(ally);
    EXPECT_TRUE(std::find(targets.begin(), targets.end(), target_t(0, enemy, 78, 9, attack)) != targets.end())
        << "ally sneak attack";
    EXPECT_TRUE(std::find(targets.begin(), targets.end(), target_t(2, enemy, 39, 1, expand)) != targets.end())
        << "earliest attack on opposite";
}
static bool
read_values(const char *file_path, Context *ctx)
{
    FILE *f;
    int line_num;
    char line[LINE_SIZE];
    Expectation_Block *eblock = NULL;

    f = fopen(file_path, "r");
    if (!f) {
        fprintf(stderr, "Failed to open %s\n", file_path);
        return false;
    }

    if (ctx->debug)
        printf("=== Parsing ===\n");

    line_num = 0;
    // mark all events on values and expectations vectors
    while (fgets(line, LINE_SIZE, f)) {
        Variable *var;
        Event *event;
        Status_Event *status_event;
        Term *term;
        Expectation *expec = NULL;
        int time, ret;
        char sensor[STR_PARSE_LEN], state[STR_PARSE_LEN];
        float value = 0;

        ret = parse_value(ctx, line, &time, sensor, state);
        if (ret == LINE_ERROR) {
            fprintf(stderr, "Failed to read line %d: %s\n", line_num, line);
            goto error;
        }

        line_num++;

        if (ret == LINE_EMPTY)
            continue;

        if (time >= ctx->reads) {
            fprintf(stderr, "Time %d exceeds reads %d on line %d\n",
                time, ctx->reads, line_num - 1);
            goto error;
        }

        if (!strcmp(sensor, BEGIN_EXPECTATIONS)) {
            if (eblock) {
                fprintf(stderr, "BEGIN_EXPECTATIONS block on line %d is inside"
                    " another BEGIN_EXPECTATIONS block\n", line_num - 1);
                goto error;
            }
            eblock = add_expectation_block(&ctx->expectation_blocks, state,
                time, 0);
            continue;
        } else if (!strcmp(sensor, END_EXPECTATIONS)) {
            if (!eblock) {
                fprintf(stderr, "Missing BEGIN_EXPECTATIONS block before "
                    "END_EXPECTATIONS block on line %d\n", line_num - 1);
                goto error;
            }
            eblock->end = time;
            eblock = NULL;
            continue;
        }

        var = parse_sensor(ctx, sensor);
        if (!var) {
            expec = parse_expectation(ctx, sensor);
            if (expec)
                var = expec->output;
            else
                goto error;
        }

        status_event = parse_status_event(time, state);
        if (status_event) {
            if (expec) {
                fprintf(stderr, "Error: EXP_%s is not suppose to have status "
                    "changes\n", var->name);
                free(status_event);
                goto error;
            }
            var->status_events = g_list_append(var->status_events,
                status_event);
        } else {
            term = parse_state(var, state);
            if (!term)
                value = atof(state);

            event = new_event(time, term, value);

            if (expec)
                expec->events = g_list_append(expec->events, event);
            else
                var->events = g_list_append(var->events, event);
        }

    }

    ctx->cur_expectation_block = ctx->expectation_blocks;
    if (ctx->debug)
        printf("=== Parsing concluded ===\n");

    fclose(f);
    return true;

error:
    fclose(f);
    return false;
}
Beispiel #20
0
void
fn_bind(char **args)
{
    enum state state, s2;
    int key, fn, i;
    char *line = NULL;
    char *staten, *kname, *cmd, **list, *p;
    struct binding *b;

    state = bs_none;

    if (args) {
	if ((s2=parse_state(args[0])) != bs_nostate) {
	    staten = args[0];
	    state = s2;
	    args++;
	}
	kname = args[0];
	cmd = args[1];
	args += 2;
    }
    else {
	if (rc_inrc) {
	    rc_error("bind: no arguments given");
	    return;
	}
	line = p = read_string("bind ", 1);

	if ((kname=rc_token(&p)) == NULL) {
	    disp_status(DISP_STATUS, "no key");
	    free(line);
	    return;
	}
	if ((s2=parse_state(kname)) != bs_nostate) {
	    staten = kname;
	    state = s2;

	    if ((kname=rc_token(&p)) == NULL) {
		disp_status(DISP_STATUS, "no key");
		free(line);
		return;
	    }
	}
	cmd = rc_token(&p);
    }

    if (state == bs_unknown) {
	if (rc_inrc)
	    rc_error("unknown state: %s", staten);
	else
	    disp_status(DISP_STATUS, "unknown state: %s", staten);
	if (line)
	    free(line);
	return;
    }

    if ((key=parse_key(kname)) < 0) {
	if (rc_inrc)
	    rc_error("unknown key: %s", kname);
	else
	    disp_status(DISP_STATUS, "unknown key: %s", kname);
	if (line)
	    free(line);
	return;
    }
	
    if (cmd) {
	if ((fn=find_function(cmd)) < 0) {
	    if (rc_inrc)
		rc_error("unknown function: %s", cmd);
	    else
		disp_status(DISP_STATUS, "unknown function: %s", cmd);
	    if (line)
		free(line);
	    return;
	}
    }

    if (!rc_inrc)
	disp_status(DISP_STATUS, "");
	
    b = get_function(key, state);

    if (b->state == state) {
	b->fn = -1;
	if (b->args) {
	    if (b->args < binding_argpool
		|| (b->args >= binding_argpool+binding_nargpool)) {
		for (i=0; b->args[i]; i++)
		    free(b->args[i]);
		free(b->args);
		b->args = NULL;
	    }
	}
    }
    else {
	if ((b=(struct binding *)malloc(sizeof(struct binding))) == NULL)
	    return;
	b->fn = -1;
	b->args = NULL;
	b->state = state;

	b->next = binding[key].next;
	binding[key].next = b;
    }

    if (cmd) {
	if (line)
	    list = rc_list(p);
	else {
	    for (i=0; args[i]; i++)
		;
	    if (i > 0) {
		list = (char **)malloc(sizeof(char *)*(i+1));
		memcpy(list, args, sizeof(char *)*(i+1));
	    }
	    else
		list = NULL;
	}

	b->fn = fn;
	b->args = list;
    }

    return;
}
Beispiel #21
0
Common::Error GagEngine::StateScript()
{
	Common::Error status(Common::kNoError);

	Common::File file;
	if(file.open(_script, *_archive))
	{
		// simple script parsing: skim through, find section and execute commands
		ParserState parse_state(PS_SECTION_SEARCH);
		Common::String buffer;
		Common::String command_name;

		bool done(false);
		while(!file.eos())
		{
			// add space for multiline command support
			Common::String line = file.readLine() + ' ';
			if(file.err())
			{
				status = Common::Error(Common::kReadingFailed, _script + ", readLine()");
				break;
			}

			bool skip_line(false);
			for(Common::String::const_iterator it = line.begin(); it != line.end(); ++it)
			{
				switch(parse_state)
				{
				case PS_SECTION_SEARCH:
					// section
					if(*it == '[')
					{
						buffer.clear();

						parse_state = PS_SECTION_NAME;
					}
					break;

				case PS_SECTION_NAME:
					// section end
					if(*it == ']')
					{
#ifdef DEBUG_SKIM_SCRIPT
						parse_state = PS_SECTION_BODY;
#else
						if(buffer == _section)
							parse_state = PS_SECTION_BODY;
						else if(buffer == _END_SECTION)
						{
							status = Common::Error(Common::kUnknownError, "[" + _section + "] script section not found");
							skip_line = true;
							done = true;
						}
						else
							parse_state = PS_SECTION_SEARCH;
#endif
					}
					else
						buffer += *it;
					break;

				case PS_SECTION_BODY:
					// section
					if(*it == '[')
					{
#ifdef DEBUG_SKIM_SCRIPT
						buffer.clear();
						parse_state = PS_SECTION_NAME;
#else
						skip_line = true;
						done = true;
#endif
					}
					// comment
					else if(*it == '*')
					{
						skip_line = true;
					}
					//NOTE: invalid syntax
					else if(*it == '-' || *it == '/' || (*it >= 'A' && *it <= 'Z'))
					{
#ifndef DEBUG_SKIM_SCRIPT
						warning("invalid script syntax [file: %s, section: %s, line: \"%s\"], skipped", _script.c_str(), _section.c_str(), line.c_str());
#endif
						skip_line = true;
					}
					// command name
					else if((*it >= 'a' && *it <= 'z'))
					{
						buffer.clear();
						buffer += *it;

						parse_state = PS_COMMAND_NAME;
					}
					break;

				case PS_COMMAND_NAME:
					// command value
					if(*it == '=')
					{
						command_name = buffer;
						buffer.clear();

						parse_state = PS_COMMAND_VALUE;
					}
					else
						buffer += *it;
					break;

				case PS_COMMAND_VALUE:
					// command value end
					if(*it == ';')
					{
						command_name.trim();
						buffer.trim();

#ifndef DEBUG_SKIM_SCRIPT
						debug("%s=%s;", command_name.c_str(), buffer.c_str());
#endif

						Common::HashMap<Common::String, Common::Error (GagEngine::*)(const Common::String &)>::const_iterator f = _commandCallbacks.find(command_name);
						if(f != _commandCallbacks.end())
						{
							status = (this->*f->_value)(buffer);
							if(status.getCode() != Common::kNoError)
							{
								skip_line = true;
								done = true;
							}
						}

						parse_state = PS_SECTION_BODY;
					}
					else
						buffer += *it;
					break;
				}

				if(skip_line)
					break;
			}

			if(done)
				break;
		}
	}
	else
	{
		status = Common::Error(Common::kReadingFailed, _script + ", open()");
	}

	return status;
}