Repository navigation
Fleet Scripts
Abstrct edited this page Feb 22, 2011
·
1 revision
Here is an example of a script that automatically finds a new planet, goes there, builds a fleet of miners, upgrades them and then moves on to the next planet.
UPDATE my_fleets SET script_declarations = 'current_money integer;
current_fuel integer;
next_planet_id integer;
current_planet_id integer;
current_planet_name character varying;
current_planet_limit integer;
current_planet_location_x integer;
current_planet_location_y integer;
current_planet_conqueror_id integer;
target_ships integer;
required_fuel integer;
enemy_ships RECORD;
miners RECORD;
conquer RECORD;
upgrades RECORD; ',
script='
SELECT INTO current_money, current_fuel balance, fuel_reserve from my_player;
If (SELECT distinct speed FROM my_ships WHERE name = ''Dog'' ORDER by speed DESC LIMIT 1) = 0 THEN
SELECT DISTINCT INTO current_planet_id, current_planet_name, current_planet_limit, current_planet_location_x, current_planet_location_y, current_planet_conqueror_id p.id, p.name, p.mine_limit, p.location_x, p.location_y, p.conqueror_id
FROM
my_ships, planets p
WHERE
p.id = (
SELECT planets.id FROM planets ORDER BY SQRT(((planets.location_x+-my_ships.location_x)^2) + ((my_ships.location_y-planets.location_y)^2)) ASC OFFSET 0 LIMIT 1
)
AND my_ships.name=''Dog'';
IF current_planet_conqueror_id = 1 THEN
target_ships := (current_planet_limit / 2)::integer + 1;
IF target_ships > (SELECT COUNT(id) from my_ships WHERE my_ships.name = current_planet_name) THEN
--1000 = current ship cost
required_fuel := (target_ships * 1000 * 0.95)::integer;
IF required_fuel < current_fuel THEN
PERFORM CONVERT_RESOURCE(''FUEL'', required_fuel);
WHILE target_ships > 0 LOOP
INSERT INTO my_ships(name, location_x, location_y) VALUES(current_planet_name, current_planet_location_x,current_planet_location_y);
target_ships := target_ships - 1;
END LOOP;
END IF;
END IF;
END IF;
SELECT into upgrades UPGRADE_SHIP(my_ships.id, ''PROSPECTING'', 10), UPGRADE_SHIP(my_ships.id, ''DEFENSE'', 10) FROM my_ships WHERE my_ships.name=current_planet_name AND attack+defense+prospecting+engineering < 400 ORDER BY my_ships.prospecting LIMIT 5;
SELECT into upgrades UPGRADE_SHIP(my_ships.id, ''ATTACK'', 10), UPGRADE_SHIP(my_ships.id, ''DEFENSE'', 10), UPGRADE_SHIP(my_ships.id, ''ENGINEERING'', 10) FROM my_ships WHERE my_ships.name=current_planet_name AND attack+defense+prospecting+engineering < 400 ORDER BY attack DESC LIMIT 5;
END IF;
SELECT INTO upgrades REPAIR(id, id) from my_ships WHERE current_health < 100;
--FOR enemy_ships IN SELECT id, ship_in_range_of from ships_in_range WHERE health > 0 ORDER BY health ASC LOOP
-- PERFORM ATTACK(enemy_ships.ship_in_range_of, enemy_ships.id);
--END LOOP;
FOR miners IN SELECT my_ships.id as ship_id, planets.id as planet_id from my_ships, planets WHERE
my_ships.name != ''Dog'' AND planets.name=my_ships.name LOOP
PERFORM MINE(miners.ship_id, miners.planet_id);
END LOOP;
If (SELECT distinct speed FROM my_ships WHERE name = ''Dog'' ORDER by speed DESC LIMIT 1) = 0 THEN
IF ((SELECT SUM((my_ships.max_speed * 2) + 180) FROM my_ships WHERE my_ships.name=''Dog'') < current_fuel ) AND
((SELECT MIN(attack+defense+engineering+prospecting) FROM my_ships WHERE my_ships.name=current_planet_name) > 400)
AND ((SELECT last_value FROM tic_seq) != (SELECT MAX(last_action_tic) from my_ships WHERE name=''Dog'')) THEN
FOR conquer IN SELECT my_ships.id as ship_id, my_ships.max_speed as max_speed, p.location_x as location_x, p.location_y as location_y, p.conqueror_id as conqueror_id FROM
my_ships, planets p
WHERE
p.id = (
SELECT planets.id FROM planets WHERE planets.conqueror_id is null ORDER BY SQRT(((planets.location_x+-my_ships.location_x)^2) + ((my_ships.location_y-planets.location_y)^2)) ASC OFFSET 1 LIMIT 1
) AND
my_ships.name=''Dog'' LOOP
PERFORM UPGRADE_SHIP(conquer.ship_id, ''REFUEL'', (conquer.max_speed * 2) + 180);
PERFORM MOVE(conquer.ship_id, conquer.max_speed, NULL, conquer.location_x, conquer.location_y);
END LOOP;
ELSE
FOR miners IN SELECT my_ships.id as ship_id, planets.id as planet_id from my_ships, planets WHERE
my_ships.name = ''Dog'' AND planets.name=current_planet_name AND my_ships.last_action_tic != (SELECT last_value FROM tic_seq) LOOP
PERFORM MINE(miners.ship_id, miners.planet_id);
END LOOP;
END IF;
END IF;
';