Skip to content

Commit

Permalink
fixed varios bugs on fleet movements
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Dec 31, 2023
1 parent 0ff08c6 commit 425f8ef
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ scripts/goerli/
scripts/sepolia/

scripts/

.vscode/
68 changes: 38 additions & 30 deletions src/game/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod NoGame {

use nogame::libraries::auction::{LinearVRGDA, LinearVRGDATrait};

use snforge_std::PrintTrait;

#[storage]
struct Storage {
initialized: bool,
Expand Down Expand Up @@ -1015,8 +1017,14 @@ mod NoGame {
self.active_missions.write((origin, mission_id), Zeroable::zero());
} else {
let spendable = self.get_spendable_resources(mission.destination);
let collectible = self.get_collectible_resources(mission.destination);
let mut available_to_loot: ERC20s = Default::default();
available_to_loot.steel = available_to_loot.steel + spendable.steel / 2;
available_to_loot.quartz =available_to_loot.quartz + spendable.quartz / 2;
available_to_loot.tritium = available_to_loot.tritium + spendable.tritium / 2;
available_to_loot =available_to_loot + collectible;
let storage = fleet::get_fleet_cargo_capacity(f1);
loot_amount = fleet::load_resources(spendable, storage);
loot_amount = fleet::load_resources(available_to_loot, storage);
self.resources_timer.write(mission.destination, time_now);
self
.pay_resources_erc20(
Expand All @@ -1036,6 +1044,8 @@ mod NoGame {
self.update_points_after_attack(origin, attacker_loss, Zeroable::zero());
self.update_points_after_attack(mission.destination, defender_loss, defences_loss);

let active_missions = self.active_missions_len.read(origin);
self.active_missions_len.write(origin, active_missions - 1);
self
.emit_battle_report(
time_now,
Expand Down Expand Up @@ -1181,17 +1191,14 @@ mod NoGame {
let speed = self.uni_speed.read();
let steel = Production::steel(self.steel_mine_level.read(planet_id))
* speed
* time_elapsed.into()
/ HOUR.into();
* time_elapsed.into();
let quartz = Production::quartz(self.quartz_mine_level.read(planet_id))
* speed
* time_elapsed.into()
/ HOUR.into();
* time_elapsed.into();
let tritium = Production::tritium(
self.tritium_mine_level.read(planet_id), temp, self.uni_speed.read()
)
* time_elapsed.into()
/ HOUR.into();
* time_elapsed.into();
ERC20s { steel: steel, quartz: quartz, tritium: tritium }
}

Expand Down Expand Up @@ -1502,45 +1509,46 @@ mod NoGame {
/// is then scaled based on available and required energy, and the result is returned
/// as a `Resources` structure.
fn calculate_production(self: @ContractState, planet_id: u16) -> ERC20s {
let time_now = get_block_timestamp();
let last_collection_time = self.resources_timer.read(planet_id);
let time_elapsed = time_now - last_collection_time;
// let time_now = get_block_timestamp();
// let last_collection_time = self.resources_timer.read(planet_id);
// let time_elapsed = time_now - last_collection_time;
// let position = self.planet_position.read(planet_id);
// let temp = self.calculate_avg_temperature(position.orbit);
// let speed = self.uni_speed.read();
// let steel_available = Production::steel(mines_levels.steel)
// * speed
// * time_elapsed.into()
// / HOUR.into();

// let quartz_available = Production::quartz(mines_levels.quartz)
// * speed
// * time_elapsed.into()
// / HOUR.into();

// let tritium_available = Production::tritium(mines_levels.tritium, temp, speed)
// * time_elapsed.into()
// / HOUR.into();
let mines_levels = NoGame::get_compounds_levels(self, planet_id);
let position = self.planet_position.read(planet_id);
let temp = self.calculate_avg_temperature(position.orbit);
let speed = self.uni_speed.read();
let steel_available = Production::steel(mines_levels.steel)
* speed
* time_elapsed.into()
/ HOUR.into();

let quartz_available = Production::quartz(mines_levels.quartz)
* speed
* time_elapsed.into()
/ HOUR.into();

let tritium_available = Production::tritium(mines_levels.tritium, temp, speed)
* time_elapsed.into()
/ HOUR.into();
let raw = self.get_collectible_resources(planet_id);
let energy_available = Production::energy(mines_levels.energy);
let energy_required = Consumption::base(mines_levels.steel)
+ Consumption::base(mines_levels.quartz)
+ Consumption::base(mines_levels.tritium);
if energy_available < energy_required {
let _steel = Compounds::production_scaler(
steel_available, energy_available, energy_required
raw.steel / 3600, energy_available, energy_required
);
let _quartz = Compounds::production_scaler(
quartz_available, energy_available, energy_required
raw.quartz / 3600, energy_available, energy_required
);
let _tritium = Compounds::production_scaler(
tritium_available, energy_available, energy_required
raw.tritium / 3600, energy_available, energy_required
);

return ERC20s { steel: _steel, quartz: _quartz, tritium: _tritium, };
}

ERC20s { steel: steel_available, quartz: quartz_available, tritium: tritium_available, }
ERC20s { steel: raw.steel / 3600, quartz: raw.quartz / 3600, tritium: raw.tritium / 3600}
}

fn calculate_energy_consumption(self: @ContractState, compounds: CompoundsLevels) -> u128 {
Expand Down
17 changes: 11 additions & 6 deletions src/libraries/fleet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn unit_combat(ref unit1: Unit, ref unit2: Unit) {
break;
}

rapid_fire(ref unit1, ref unit2);
if unit1.weapon < unit2.shield / 100 {
continue;
} else if unit1.weapon < unit2.shield {
Expand All @@ -127,6 +128,7 @@ fn unit_combat(ref unit1: Unit, ref unit2: Unit) {
unit2.hull -= unit1.weapon - unit2.shield;
}

rapid_fire(ref unit2, ref unit1);
if unit2.weapon < unit1.shield / 100 {
continue;
} else if unit2.weapon < unit1.shield {
Expand All @@ -141,6 +143,12 @@ fn unit_combat(ref unit1: Unit, ref unit2: Unit) {
};
}

fn rapid_fire(ref unit1: Unit, ref unit2: Unit) {
if unit1.id == 3 && unit2.id == 2 {
unit2.hull = 0
}
}

fn build_fleet_struct(ref a: Array<Unit>) -> (Fleet, DefencesLevels) {
let mut fleet: Fleet = Default::default();
let mut d: DefencesLevels = Default::default();
Expand Down Expand Up @@ -493,8 +501,7 @@ fn load_resources(mut resources: ERC20s, mut storage: u128) -> ERC20s {
let mut tritium_loaded = 0;

// # Step 1: Load Metal
// metal_to_load = metal / 2
let mut steel_to_load = resources.steel / 2;
let mut steel_to_load = resources.steel;
// if metal_to_load <= storage / 3:
if steel_to_load <= storage / 3 {
// metal_loaded += metal_to_load
Expand All @@ -511,8 +518,7 @@ fn load_resources(mut resources: ERC20s, mut storage: u128) -> ERC20s {
storage -= steel_loaded;

// # Step 2: Load Crystal
// crystal_to_load = crystal / 2
let mut quartz_to_load = resources.quartz / 2;
let mut quartz_to_load = resources.quartz;
// if crystal_to_load <= storage / 2:
if quartz_to_load <= storage / 2 {
// crystal_loaded += crystal_to_load
Expand All @@ -530,8 +536,7 @@ fn load_resources(mut resources: ERC20s, mut storage: u128) -> ERC20s {
storage -= quartz_loaded;

// # Step 3: Load Deuterium
// deuterium_to_load = deuterium / 2
let tritium_to_load = resources.tritium / 2;
let tritium_to_load = resources.tritium;
// if deuterium_to_load <= storage:
if tritium_to_load <= storage {
// deuterium_loaded += deuterium_to_load
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/research.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Lab of LabTrait {
spacetime: ERC20s { steel: 0, quartz: 4000, tritium: 2000 },
combustion: ERC20s { steel: 400, quartz: 0, tritium: 600 },
thrust: ERC20s { steel: 2000, quartz: 4000, tritium: 600 },
warp: ERC20s { steel: 10000, quartz: 2000, tritium: 6000 },
warp: ERC20s { steel: 10000, quartz: 20000, tritium: 6000 },
}
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/tests/view_tests/general_view.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use starknet::testing::cheatcode;
use starknet::info::get_contract_address;
use starknet::{ContractAddress, contract_address_const, get_block_timestamp};
use starknet::{ContractAddress, contract_address_const, get_block_timestamp, get_contract_address};
use snforge_std::PrintTrait;

use snforge_std::{start_prank, start_warp, CheatTarget};
Expand Down Expand Up @@ -169,12 +168,25 @@ fn test_get_collectible_resources() {
init_game(dsp);
start_prank(CheatTarget::One(dsp.game.contract_address), ACCOUNT1());
dsp.game.generate_planet();
build_basic_mines(dsp.game);

start_warp(CheatTarget::All, HOUR * 3);
start_warp(CheatTarget::All, get_block_timestamp() + HOUR / 6);
let collectible = dsp.game.get_collectible_resources(1);
assert(collectible.steel == 30, 'wrong collectible ');
assert(collectible.quartz == 30, 'wrong collectible ');
assert(collectible.tritium == 0, 'wrong collectible ');
collectible.print();
// assert(collectible.steel == 30, 'wrong collectible ');
// assert(collectible.quartz == 30, 'wrong collectible ');
// assert(collectible.tritium == 0, 'wrong collectible ');
}

#[test]
#[fork(url: "https://starknet-sepolia.blastapi.io/e88cff07-b7b6-48d0-8be6-292f660dc735/rpc/v0_6", block_id: BlockId::Number(12196))]
fn test_fork_get_collectible_resources() {
let contract_address = contract_address_const::<0x07287f2df129f8869638b5e7bf1b9e5961e57836f9762c8caa80e9e7831eeadc>();
let account = contract_address_const::<0x02e492bffa91eb61dbebb7b70c4520f9a1ec2a66ec8559a943a87d299b2782c7>();
let collectible = INoGameDispatcher{contract_address}.get_collectible_resources(1);
collectible.print();
start_prank(CheatTarget::One(contract_address), account);
let collectible = INoGameDispatcher{contract_address}.collect_resources();
}

#[test]
Expand Down

0 comments on commit 425f8ef

Please sign in to comment.