static inline void tur_rotate(turret_t *turret) { fixed dx, dy; int i; int dir_n, dir_p; int x, y; int rotate_direction; x = turret->x * 32; y = turret->y * 32; rotate_direction = 0; /* rotate: get desired angle from target */ if (turret->target_monster != NULL) { dx = itofix((x + 16) - (int)(turret->target_monster->x + 16)); dy = itofix((y + 16) - (int)(turret->target_monster->y + 16)); turret->desired_angle = fatan2(dy, dx) - itofix(64); } else turret->desired_angle = itofix(-420); if (turret->target_monster != NULL) { if (fixtoi(turret->angle) != fixtoi(turret->desired_angle) && tur_target_within_range(turret,(int)turret->target_monster->x + 16,(int)turret->target_monster->y + 16)) { dir_n = 0; dir_p = 0; /* calculate travel distances */ i = fixtoi(turret->angle); for (;;) { i--; dir_n++; if (i < -192) i = 64; if (i > 64) i = -192; if (i == fixtoi(turret->desired_angle)) break; } i = fixtoi(turret->angle); for (;;) { i++; dir_p++; if (dir_p > dir_n) break; if (i < -192) i = 64; if (i > 64) i = -192; if (i == fixtoi(turret->desired_angle)) break; } /* rotate */ if (dir_n < dir_p) rotate_direction = 1; else rotate_direction = 2; if (rotate_direction == 1) turret->angle = itofix(fixtoi(turret->angle) - 1); if (rotate_direction == 2) turret->angle = itofix(fixtoi(turret->angle) + 1); if (fixtoi(turret->angle) > 64) turret->angle = itofix(-192); if (fixtoi(turret->angle) < -192) turret->angle = itofix(64); } } /* rotate */ }
int tur_target_within_sight(turret_t *turret, int target_x, int target_y) { int x, y; int map_x, map_y; turret_t *tp; game_data_t *gd; fixed angle, dx, dy; float bx, by; int i; int px, py, pc; dx = itofix((turret->x * 32 + 16) - target_x); dy = itofix((turret->y * 32 + 16) - target_y); angle = fatan2(dy, dx) - itofix(64); x = turret->x * 32; y = turret->y * 32; bx = x + turret->bitmap->w/2; by = y + turret->bitmap->h/2; gd = get_game_data_pointer(); for (i = 0; i < turret->range / 15; i++) { float result; result = fixtof(fixcos(angle)); by -= result * 15; result = fixtof(fixsin(angle)); bx += result * 15; map_x = (int) bx / 32; map_y = (int) by / 32; if (map_x == target_x/32 && map_y == target_y/32) return 1; //putpixel(screen, bx, by, makecol(255,255,255)); tp = gd->turret; for (;;) { if (tp == NULL) break; if (tp != turret && (tp->x == map_x && tp->y == map_y)) { /* found a turret in this tile, check for pixel value */ px = (int)bx - (map_x * 32); py = (int)by - (map_y * 32); pc = getpixel(tp->bitmap, px, py); if (pc != makecol(255, 0, 255)) { return 0; } } tp = tp->next; } } return 1; }
void alienrocket::fireRocket(int dx, int dy, int a){ angle = rand()% 360; targetX = itofix(dx); targetY = itofix(dy); fired = true; int r1; //randomly picks an x coorinate in the general direction of the target int r2; //randomly picks a y coordinate in the general direction of the target r1 = (dx - 100) + int(200.0 * rand()/(RAND_MAX+1.0)); r2 = (dy - 100)+ int(200.0 * rand()/(RAND_MAX+1.0)); angle = a; angle = fatan2(itofix(r2) - y, itofix(r1)- x); }