Skip to content

Instantly share code, notes, and snippets.

@KWKdesign
Last active August 29, 2015 14:13
Show Gist options
  • Save KWKdesign/3e573d619cf858e9516d to your computer and use it in GitHub Desktop.
Save KWKdesign/3e573d619cf858e9516d to your computer and use it in GitHub Desktop.
Schemaverse Example Fleet Script
insert into my_fleets ( name ) values ( 'main' );
update my_fleets set
script_declarations = '
my_player_id int := get_player_id( session_user );
cash bigint;
ship record;
planet record;
target record;
',
script = '
-- Convert all fuel to cash
perform convert_resource( ''FUEL'', fuel_reserve ) from my_player;
-- Get our homeworld''s details
select id, location from planets where conqueror_id = my_player_id into planet;
-- Check to see if this is a new round.
if( select last_value > coalesce( get_numeric_variable( ''round'' ), 0 ) from round_seq ) then
-- New round handling.
-- Update game variable with the current round number.
perform set_numeric_variable( ''round'', ( select last_value from round_seq )::int );
-- Much more can be done with the game''s variable system that is not covered here.
-- We want to spend all starting cash building miners.
for i in 1 .. 30 loop -- Each starting planet has mine limit of 30, so we''ll use it up.
-- Basic method for creating a ship, 20 skill points will be by default assigned 5 to each skill.
insert into my_ships ( name, location ) values ( ''miner'', planet.location ) returning * into ship;
-- Improve the ships drills or whatever. This gets more fuel for our future fleet per miner.
perform upgrade( ship.id, ''PROSPECTING'', 100 - ship.prospecting );
-- Rather than instruct the miners to mine() each turn, we can set it so the game does it for us. How nice.
update my_ships set action = ''MINE'', action_target_id = planet.id where id = ship.id;
end loop;
-- end new round handling
end if;
-- New tic, time to refuel the fleet ( if extant ).
-- Convert all cash into fuel.
perform convert_resource( ''MONEY'', balance ) from my_player;
-- Use perform instead of select, since we don''t want any return values.
perform refuel_ship( id ) from my_ships where name = ''mover'';
-- Convert all fuel to cash, and this time assign the result to our cash
-- cash variable declared above.
cash := ( select convert_resource( ''FUEL'', fuel_reserve ) from my_player );
-- If enough is left over after refueling to build a ship, then build one.
if cash > 30000 then
insert into my_ships ( name, location ) values ( ''mover'', planet.location ) returning * into ship;
-- This time we will upgrade max_speed and max_fuel
-- Max Speed is the top speed, while Max Fuel is max acceleration per tic
perform upgrade( ship.id, ''MAX_SPEED'', 10000 - ship.max_speed );
-- Keep them equal to each other for now, for the sake of simplicity
perform upgrade( ship.id, ''MAX_FUEL'', 10000 - ship.max_fuel );
-- Our shiny, brand new ship needs a target to conquer
select id, location from planets
-- First, filter out planets we already own
where conqueror_id <> my_player_id
-- We don''t want to target the same planets over and over again, so filter planets with ships assigned out
and id not in ( select action_target_id from my_ships where name = ''mover'' and action_target_id is not null )
-- Order by distance from home, in ascending order
order by planet.location <-> location
into target; -- Assign the first result to our record variable, discard the rest
-- Convert just enough cash into fuel for the new ship to refuel with
perform convert_resource( ''MONEY'', ship.max_fuel - ship.current_fuel ) from my_player;
perform refuel_ship( ship.id );
-- We want to order the ship to mine the planet as soon as it is in range
update my_ships set action = ''MINE'', action_target_id = target.id,
destination = target.location, target_speed = max_speed where id = ship.id;
-- When max_speed is equal to max_fuel, controlling the ships speed can
-- be trusted to the game. Greater speed requires deliberate handling or
-- ships will overshoot their targets substantially. This is intentional, do not try fight it. Submit.
end if;
',
enabled = true where name = 'main';
select upgrade( ( select id from my_fleets where name = 'main' ), upper( 'fleet_runtime' ), 1 );
update my_player set starting_fleet = ( select id from my_fleets where name = 'main' );
DO $$ DECLARE
my_player_id int := get_player_id( session_user );
cash bigint;
ship record;
planet record;
target record;
BEGIN
-- Convert all fuel to cash
perform convert_resource( 'FUEL', fuel_reserve ) from my_player;
-- Get our homeworld's details
select id, location from planets where conqueror_id = my_player_id into planet;
-- Check to see if this is a new round.
if( select last_value > coalesce( get_numeric_variable( 'round' ), 0 ) from round_seq ) then
-- New round handling.
-- Update game variable with the current round number.
perform set_numeric_variable( 'round', ( select last_value from round_seq )::int );
-- Much more can be done with the game's variable system that is not covered here.
-- We want to spend all starting cash building miners.
for i in 1 .. 30 loop -- Each starting planet has mine limit of 30, so we'll use it up.
-- Basic method for creating a ship, 20 skill points will be by default assigned 5 to each skill.
insert into my_ships ( name, location ) values ( 'miner', planet.location ) returning * into ship;
-- Improve the ships drills or whatever. This gets more fuel for our future fleet per miner.
perform upgrade( ship.id, 'PROSPECTING', 100 - ship.prospecting );
-- Rather than instruct the miners to mine() each turn, we can set it so the game does it for us. How nice.
update my_ships set action = 'MINE', action_target_id = planet.id where id = ship.id;
end loop;
-- end new round handling
end if;
-- New tic, time to refuel the fleet ( if extant ).
-- Convert all cash into fuel.
perform convert_resource( 'MONEY', balance ) from my_player;
-- Use perform instead of select, since we don't want any return values.
perform refuel_ship( id ) from my_ships where name = 'mover';
-- Convert all fuel to cash, and this time assign the result to our cash
-- cash variable declared above.
cash := ( select convert_resource( 'FUEL', fuel_reserve ) from my_player );
-- If enough is left over after refueling to build a ship, then build one.
if cash > 30000 then
insert into my_ships ( name, location ) values ( 'mover', planet.location ) returning * into ship;
-- This time we will upgrade max_speed and max_fuel
-- Max Speed is the top speed, while Max Fuel is max acceleration per tic
perform upgrade( ship.id, 'MAX_SPEED', 10000 - ship.max_speed );
-- Keep them equal to each other for now, for the sake of simplicity
perform upgrade( ship.id, 'MAX_FUEL', 10000 - ship.max_fuel );
-- Our shiny, brand new ship needs a target to conquer
select id, location from planets
-- First, filter out planets we already own
where conqueror_id <> my_player_id
-- We don't want to target the same planets over and over again, so filter planets with ships assigned out
and id not in ( select action_target_id from my_ships where name = 'mover' and action_target_id is not null )
-- Order by distance from home, in ascending order
order by planet.location <-> location
into target; -- Assign the first result to our record variable, discard the rest
-- Convert just enough cash into fuel for the new ship to refuel with
perform convert_resource( 'MONEY', ship.max_fuel - ship.current_fuel ) from my_player;
perform refuel_ship( ship.id );
-- We want to order the ship to mine the planet as soon as it is in range
update my_ships set action = 'MINE', action_target_id = target.id,
destination = target.location, target_speed = max_speed where id = ship.id;
-- When max_speed is equal to max_fuel, controlling the ships speed can
-- be trusted to the game. Greater speed requires deliberate handling or
-- ships will overshoot their targets substantially. This is intentional, do not try fight it. Submit.
end if;
END $$ ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment