From: zer0 Date: Mon, 10 May 2010 19:59:22 +0000 (+0200) Subject: track opponent X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=commitdiff_plain;h=0b7e368f3918ec09a82f73ed2f2a86a4a6957ee0 track opponent --- diff --git a/projects/microb2010/mainboard/strat.c b/projects/microb2010/mainboard/strat.c index dd7b23c..dd06751 100644 --- a/projects/microb2010/mainboard/strat.c +++ b/projects/microb2010/mainboard/strat.c @@ -336,6 +336,38 @@ static void check_corn(void) } } +/* check opponent position */ +void check_opponent(void) +{ + int16_t x, y; + uint8_t i, j; + + if (get_opponent_xy(&x, &y) < 0) + return; + + /* check for oranges after 5 seconds */ + if (time_get_s() > 5) { + if (mainboard.our_color == I2C_COLOR_YELLOW) { + if (y < 500 && x < 500) + strat_db.our_oranges_count = 0; + if (y < 500 && x > AREA_X - 500) + strat_db.opp_oranges_count = 0; + } + else { + if (y > AREA_Y - 500 && x < 500) + strat_db.our_oranges_count = 0; + if (y > AREA_Y - 500 && x > AREA_X - 500) + strat_db.opp_oranges_count = 0; + } + } + + /* malus for some tomatoes and cobs, visited by opponent */ + if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) + return; + + strat_db.wp_table[i][j].opp_visited = 1; +} + /* called periodically (10ms) */ void strat_event(void *dummy) { @@ -345,6 +377,7 @@ void strat_event(void *dummy) check_tomato(); check_corn(); + check_opponent(); /* limit speed when opponent is near */ /* disabled for 2010, we are already slow :) */ @@ -360,7 +393,7 @@ static uint8_t robot_is_on_eject_line(void) x = position_get_x_s16(&mainboard.pos); y = position_get_y_s16(&mainboard.pos); - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) return 0; if (!wp_belongs_to_line(i, j, 5, LINE_UP) && @@ -384,7 +417,7 @@ static uint8_t eject_select_speed(void) return 0; /* fast */ } - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) { + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) { DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d", __FUNCTION__, x, y); return 1; /* slow */ @@ -566,7 +599,7 @@ uint8_t get_opp_oranges(void) x = position_get_x_s16(&mainboard.pos); y = position_get_y_s16(&mainboard.pos); - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) return END_ERROR; /* not on eject point */ @@ -623,7 +656,7 @@ uint8_t get_orphan_tomatoes(void) x = position_get_x_s16(&mainboard.pos); y = position_get_y_s16(&mainboard.pos); - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) return END_ERROR; /* not on eject point */ diff --git a/projects/microb2010/mainboard/strat_avoid.c b/projects/microb2010/mainboard/strat_avoid.c index de85e30..3d52188 100644 --- a/projects/microb2010/mainboard/strat_avoid.c +++ b/projects/microb2010/mainboard/strat_avoid.c @@ -665,6 +665,7 @@ static int8_t get_path(const struct circuit *circuit, static int16_t get_score(uint32_t wcorn_retrieved, uint32_t ucorn_retrieved, uint16_t tomato_retrieved, + uint16_t utomato_retrieved, uint8_t len, uint8_t opp_on_path) { int16_t score = 0; @@ -763,11 +764,12 @@ static int8_t evaluate_one_face(const struct circuit *circuit, uint32_t wcorn_retrieved = 0; /* bit mask */ uint32_t ucorn_retrieved = 0; /* bit mask */ uint16_t tomato_retrieved = 0; /* bit mask */ + uint16_t utomato_retrieved = 0; /* bit mask */ uint8_t opponent_on_path = 0; uint8_t len = 0, found = 0; uint8_t i, j, prev_i, prev_j; uint8_t ni = 0, nj = 0; - uint8_t dir, color, idx; + uint8_t dir, color, idx, visited; int8_t step = faceA ? 1 : -1; int16_t x, y; int32_t d, prev_d = 0; @@ -840,18 +842,29 @@ static int8_t evaluate_one_face(const struct circuit *circuit, /* is there a tomato ? */ if (strat_db.wp_table[i][j].type == WP_TYPE_TOMATO && strat_db.wp_table[i][j].present) { - DPR(" TOMATO\n"); - tomato_retrieved |= (1UL << strat_db.wp_table[i][j].tomato.idx); + if (strat_db.wp_table[i][j].opp_visited) { + DPR(" TOMATO (opp visited)\n"); + utomato_retrieved |= (1UL << strat_db.wp_table[i][j].tomato.idx); + } + else { + DPR(" TOMATO\n"); + tomato_retrieved |= (1UL << strat_db.wp_table[i][j].tomato.idx); + } } /* behind left */ if (wp_get_neigh(i, j, &ni, &nj, (dir + 2) % 6) == 0) { color = get_corn_type(ni, nj); idx = strat_db.wp_table[ni][nj].corn.idx; - if (color == I2C_COB_WHITE) { + visited = strat_db.wp_table[ni][nj].opp_visited; + if (color == I2C_COB_WHITE && !visited) { DPR(" LEFT WCORN (%d)\n", idx); wcorn_retrieved |= (1UL << idx); } + else if (color == I2C_COB_WHITE && visited) { + DPR(" LEFT CORN visited (%d)\n", idx); + ucorn_retrieved |= (1UL << idx); + } else if (color == I2C_COB_UNKNOWN) { DPR(" LEFT UCORN (%d)\n", idx); ucorn_retrieved |= (1UL << idx); @@ -862,10 +875,15 @@ static int8_t evaluate_one_face(const struct circuit *circuit, if (wp_get_neigh(i, j, &ni, &nj, (dir + 4) % 6) == 0) { color = get_corn_type(ni, nj); idx = strat_db.wp_table[ni][nj].corn.idx; - if (color == I2C_COB_WHITE) { + visited = strat_db.wp_table[ni][nj].opp_visited; + if (color == I2C_COB_WHITE && !visited) { DPR(" RIGHT WCORN (%d)\n", idx); wcorn_retrieved |= (1UL << idx); } + else if (color == I2C_COB_WHITE && visited) { + DPR(" RIGHT CORN visited (%d)\n", idx); + ucorn_retrieved |= (1UL << idx); + } else if (color == I2C_COB_UNKNOWN) { DPR(" RIGHT UCORN (%d)\n", idx); ucorn_retrieved |= (1UL << idx); @@ -878,7 +896,8 @@ static int8_t evaluate_one_face(const struct circuit *circuit, /* write score and exit */ *score = get_score(wcorn_retrieved, ucorn_retrieved, - tomato_retrieved, len, opponent_on_path); + tomato_retrieved, utomato_retrieved, + len, opponent_on_path); return 0; } @@ -1004,7 +1023,7 @@ uint8_t strat_harvest_circuit(void) x = position_get_x_s16(&mainboard.pos); y = position_get_y_s16(&mainboard.pos); - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) { + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) { DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d", __FUNCTION__, x, y); err = END_ERROR; @@ -1096,7 +1115,7 @@ uint8_t strat_unblock(void) x = posx; y = posy; - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) x = -1; else if (strat_db.wp_table[i][j].on_circuit == 0) x = -1; diff --git a/projects/microb2010/mainboard/strat_corn.c b/projects/microb2010/mainboard/strat_corn.c index e64871e..db672bf 100644 --- a/projects/microb2010/mainboard/strat_corn.c +++ b/projects/microb2010/mainboard/strat_corn.c @@ -162,7 +162,7 @@ static uint8_t clitoid_select_speed(uint8_t num1, uint8_t dir1, if (get_cob_count() >= 5) return 0; /* fast */ - if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) { + if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) { DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d", __FUNCTION__, x, y); return 1; diff --git a/projects/microb2010/mainboard/strat_db.c b/projects/microb2010/mainboard/strat_db.c index 0153794..dfc5693 100644 --- a/projects/microb2010/mainboard/strat_db.c +++ b/projects/microb2010/mainboard/strat_db.c @@ -160,10 +160,42 @@ int8_t ijcoord_to_xycoord(uint8_t i, uint8_t j, int16_t *x, int16_t *y) return 0; } +/* return the nearest waypoint (any type) */ +int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp) +{ + int16_t x, y; + uint8_t i, j; + + x = *xp; + y = *yp; + + x -= OFFSET_CORN_X; + x += (STEP_CORN_X/2); + i = x / STEP_CORN_X; + + y = COLOR_Y(y); /* Y depends on color */ + y -= OFFSET_CORN_Y; + y += STEP_CORN_Y/2; + + if ((i & 1) == 1) + y -= STEP_CORN_Y/2; + j = y / STEP_CORN_Y; + + if (ijcoord_to_xycoord(i, j, &x, &y) < 0) + return -1; + + *xp = x; + *yp = y; + *ip = i; + *jp = j; + + return 0; +} + /* return the nearest waypoint that is not a corn: xp and yp contains * the input and output, and ip, jp are only outputs. return 0 on * success. */ -int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp) +int8_t xycoord_to_ijcoord_not_corn(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp) { int16_t x, y; uint8_t i, j; diff --git a/projects/microb2010/mainboard/strat_db.h b/projects/microb2010/mainboard/strat_db.h index 61ee6bf..e377ceb 100644 --- a/projects/microb2010/mainboard/strat_db.h +++ b/projects/microb2010/mainboard/strat_db.h @@ -90,10 +90,13 @@ struct strat_db { /* global structure storing the database */ extern struct strat_db strat_db; +/* return the nearest waypoint */ +int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp); + /* return the nearest waypoint that is not a corn: xp and yp contains * the input and output, and ip, jp are only outputs. return 0 on * success. */ -int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp); +int8_t xycoord_to_ijcoord_not_corn(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp); /* convert i,j coords to x,y coords */ int8_t ijcoord_to_xycoord(uint8_t i, uint8_t j, int16_t *x, int16_t *y);