Skip to content

Commit

Permalink
Improve Rust API
Browse files Browse the repository at this point in the history
  • Loading branch information
Tye singwa committed Oct 15, 2021
1 parent 042272e commit f748904
Show file tree
Hide file tree
Showing 18 changed files with 738 additions and 328 deletions.
3 changes: 2 additions & 1 deletion clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ rm -fv $(find "./kits/ts" -name "*.js")
rm -rfv $(find "./kits/python" -name "__pycache__")
rm -fv $(find "./kits" -name ".DS_Store")
rm -rf ./.nyc_output
rm -rf ./coverage
rm -rf ./coverage
rm -rf "./kits/rust/simple/target" "./kits/rust/simple/main"
1 change: 0 additions & 1 deletion kits/rust/simple/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
target/
build/
main
Cargo.lock
124 changes: 124 additions & 0 deletions kits/rust/simple/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions kits/rust/simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cargo-features = ["strip"]

[package]
authors = ["Tye singwa"]
edition = "2021"
Expand All @@ -8,5 +10,12 @@ version = "0.1.0"
name = "solution"
path = "./src/main.rs"

[dependencies]
lux-ai-api = {path = "./lux"}
[dependencies.lux-ai]
path = "./lux"
package = "lux-ai-api"

[profile.release]
opt-level = 3
lto = true
panic = "abort"
strip = "symbols"
38 changes: 19 additions & 19 deletions kits/rust/simple/lux/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::*;
#[derive(Clone, fmt::Debug)]
pub struct Agent {
/// Team id of our Bot's Player
pub team_id: TeamId,
pub team: TeamId,

/// Turn index
pub turn: TurnAmount,
Expand All @@ -29,28 +29,28 @@ impl Agent {
///
/// # Parameters
///
/// - `environment` - mutable reference to `Environment`
/// - `environment` - mutable reference to [`Environment`]
///
/// # Returns
/// Initialized `Agent` or error
pub fn new(environment: &mut Environment) -> LuxAiResult<Self> {
let team_id = Self::read_team_id(environment)?;
let dimensions = Self::read_map_dimensions(environment)?;
let game_map = GameMap::new(dimensions);
let team = Self::read_team(environment)?;
let (width, height) = Self::read_map_dimensions(environment)?;
let game_map = GameMap::new(width, height);
let turn = 0;
let players = (0..TEAM_COUNT)
.map(|team_id| Player::new(team_id))
.collect();

Ok(Self {
team_id,
team,
turn,
game_map,
players,
})
}

/// Returns our Bot's Player
/// Returns our Bot's [`Player`]
///
/// # Parameters
///
Expand All @@ -59,7 +59,7 @@ impl Agent {
/// # Returns
///
/// `Player` reference
pub fn player(&self) -> &Player { &self.players[self.team_id as usize] }
pub fn player(&self) -> &Player { &self.players[self.team as usize] }

/// Updates Agent's map for current turn
/// - updates turn
Expand Down Expand Up @@ -103,15 +103,15 @@ impl Agent {
}
}

fn read_team_id(environment: &mut Environment) -> LuxAiResult<TeamId> {
fn read_team(environment: &mut Environment) -> LuxAiResult<TeamId> {
let command = environment.read_len_command(1)?;

let team_id = command.argument(0)?;

Ok(team_id)
}

fn read_map_dimensions(environment: &mut Environment) -> LuxAiResult<GameMapDimensions> {
fn read_map_dimensions(environment: &mut Environment) -> LuxAiResult<(Coordinate, Coordinate)> {
let command = environment.read_len_command(2)?;

let (width, height) = (command.argument(0)?, command.argument(1)?);
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Agent {

fn update_unit(&mut self, command: &Command) -> LuxAiResult {
command.expect_arguments(10)?;
let (unit_type, team_id, unit_id, x, y, cooldown) = (
let (unit_type, team, unit_id, x, y, cooldown) = (
command.argument(1)?,
command.argument(2)?,
command.argument(3)?,
Expand All @@ -158,12 +158,12 @@ impl Agent {
let argument_offset = 7;

let position = Position::new(x, y);
let mut unit = Unit::new(team_id, unit_type, unit_id, position, cooldown);
let mut unit = Unit::new(team, unit_type, unit_id, position, cooldown);
for (index, resource_type) in ResourceType::VALUES.iter().enumerate() {
let amount = command.argument(argument_offset + index)?;
unit.cargo.insert(resource_type.clone(), amount);
unit.cargo[*resource_type] = amount;
}
self.players[team_id as usize].units.push(unit);
self.players[team as usize].units.push(unit);

Ok(())
}
Expand Down Expand Up @@ -206,25 +206,25 @@ impl Agent {

fn update_road(&mut self, command: &Command) -> LuxAiResult {
command.expect_arguments(4)?;
let (x, y, road_progress) = (
let (x, y, road) = (
command.argument::<Coordinate>(1)?,
command.argument::<Coordinate>(2)?,
command.argument(3)?,
);

let position = Position::new(x, y);
let cell = &mut self.game_map[position];
cell.road_progress = road_progress;
cell.road = road;

Ok(())
}

fn fix_dependencies(&mut self) {
for player in self.players.iter_mut() {
for (_city_id, city) in player.cities.iter_mut() {
for city_tile in city.city_tiles.iter() {
let position = city_tile.borrow().position;
self.game_map[position].city_tile = Some(city_tile.clone());
for city_tile in city.citytiles.iter() {
let position = city_tile.borrow().pos;
self.game_map[position].citytile = Some(city_tile.clone());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions kits/rust/simple/lux/src/amounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub type DayAmount = i32;
/// Check <https://www.lux-ai.org/specs-2021#Day/Night%20Cycle>
pub type TurnAmount = i32;

pub type Cooldown = f32;

/// Fuel amount
pub type FuelAmount = f32;

Expand Down
Loading

0 comments on commit f748904

Please sign in to comment.