Skip to content

Commit

Permalink
implemented tritium scaling factor based on avg planet temp
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Oct 28, 2023
1 parent 9272d3e commit 7f2b55d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
43 changes: 40 additions & 3 deletions src/game/main.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODOS:
// - Implement modifier for tritium production based on orbit
// - Make contract upgradable
// - Add events for battle reports

Expand Down Expand Up @@ -829,13 +828,17 @@ mod NoGame {

fn get_collectible_resources(self: @ContractState, planet_id: u16) -> ERC20s {
let time_elapsed = self.time_since_last_collection(planet_id);
let position = self.planet_position.read(planet_id);
let temp = self.calculate_avg_temperature(position.orbit);
let steel = Compounds::steel_production(self.steel_mine_level.read(planet_id))
* time_elapsed.into()
/ 3600;
let quartz = Compounds::quartz_production(self.quartz_mine_level.read(planet_id))
* time_elapsed.into()
/ 3600;
let tritium = Compounds::tritium_production(self.tritium_mine_level.read(planet_id))
let tritium = Compounds::tritium_production(
self.tritium_mine_level.read(planet_id), temp
)
* time_elapsed.into()
/ 3600;
ERC20s { steel: steel, quartz: quartz, tritium: tritium }
Expand Down Expand Up @@ -1162,6 +1165,8 @@ mod NoGame {
let last_collection_time = self.resources_timer.read(planet_id);
let time_elapsed = time_now - last_collection_time;
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 steel_available = Compounds::steel_production(mines_levels.steel)
* time_elapsed.into()
/ 3600;
Expand All @@ -1170,7 +1175,7 @@ mod NoGame {
* time_elapsed.into()
/ 3600;

let tritium_available = Compounds::tritium_production(mines_levels.tritium)
let tritium_available = Compounds::tritium_production(mines_levels.tritium, temp)
* time_elapsed.into()
/ 3600;
let energy_available = Compounds::energy_plant_production(mines_levels.energy);
Expand Down Expand Up @@ -1545,6 +1550,38 @@ mod NoGame {
return 11;
}
}

fn calculate_avg_temperature(self: @ContractState, orbit: u8) -> u16 {
if orbit == 1 {
return 230;
}
if orbit == 2 {
return 170;
}
if orbit == 3 {
return 120;
}
if orbit == 4 {
return 70;
}
if orbit == 5 {
return 60;
}
if orbit == 6 {
return 50;
}
if orbit == 7 {
return 40;
}
if orbit == 8 {
return 40;
}
if orbit == 9 {
return 20;
} else {
return 10;
}
}
}
}

16 changes: 13 additions & 3 deletions src/libraries/compounds.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use core::integer::U256Mul;
use integer::U8Div;
use cubit::f128::types::fixed::{Fixed, FixedTrait, ONE_u128 as ONE};

use nogame::libraries::types::ERC20s;
use nogame::libraries::math::{power, BitShift};

const UNI_SPEED: u128 = 1;

const _1_36: u128 = 25087571940244990000;
const _0_004: u128 = 73786976294838210;


#[generate_trait]
impl Compounds of CompoundsTrait {
Expand Down Expand Up @@ -141,14 +145,20 @@ impl Compounds of CompoundsTrait {
}

#[inline(always)]
fn tritium_production(current_level: u8) -> u128 {
fn tritium_production(current_level: u8, avg_temp: u16) -> u128 {
let base: u256 = 10;
(base
let raw_production = (base
* current_level.into()
* BitShift::fpow(11.into(), current_level.into())
/ BitShift::fpow(10.into(), current_level.into()))
.low
* UNI_SPEED
* UNI_SPEED;

let production_fp = FixedTrait::new_unscaled(raw_production, false);
let temp = FixedTrait::new_unscaled(avg_temp.into(), false);
let f1 = FixedTrait::new(_1_36, false);
let f2 = FixedTrait::new(_0_004, false);
(production_fp * (f1 - f2 * temp)).mag / ONE
}

#[inline(always)]
Expand Down
27 changes: 14 additions & 13 deletions tests/internal_functions/mine_production.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use nogame::libraries::compounds::Compounds;
use snforge_std::PrintTrait;

#[test]
fn steel_production_test() {
Expand Down Expand Up @@ -37,20 +38,20 @@ fn quartz_production_test() {
}
#[test]
fn tritium_production_test() {
let production = Compounds::tritium_production(0);
let production = Compounds::tritium_production(0, 20);
assert(production == 0, 'wrong result');
let production = Compounds::tritium_production(1);
assert(production == 11, 'wrong result');
let production = Compounds::tritium_production(5);
assert(production == 80, 'wrong result');
let production = Compounds::tritium_production(10);
assert(production == 259, 'wrong result');
let production = Compounds::tritium_production(20);
assert(production == 1345, 'wrong result');
let production = Compounds::tritium_production(31);
assert(production == 5950, 'wrong result');
let production = Compounds::tritium_production(60);
assert(production == 182688, 'wrong result');
let production = Compounds::tritium_production(1, 20);
assert(production == 14, 'wrong result');
let production = Compounds::tritium_production(5, 20);
assert(production == 102, 'wrong result');
let production = Compounds::tritium_production(10, 20);
assert(production == 331, 'wrong result');
let production = Compounds::tritium_production(20, 20);
assert(production == 1721, 'wrong result');
let production = Compounds::tritium_production(31, 20);
assert(production == 7615, 'wrong result');
let production = Compounds::tritium_production(60, 20);
assert(production == 233840, 'wrong result');
}
#[test]
fn energy_plant_production_test() {
Expand Down
13 changes: 9 additions & 4 deletions tests/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use array::ArrayTrait;
use starknet::{ContractAddress, contract_address_const, get_block_timestamp, get_contract_address};
use starknet::{
ContractAddress, contract_address_const, get_block_timestamp, get_contract_address,
get_caller_address
};
use openzeppelin::token::erc20::erc20::ERC20;
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};

Expand Down Expand Up @@ -149,7 +152,7 @@ fn build_basic_mines(game: INoGameDispatcher) {
game.steel_mine_upgrade();
game.steel_mine_upgrade();
game.quartz_mine_upgrade();
// warp_multiple(game.contract_address, get_contract_address(), get_block_timestamp() + YEAR);
warp_multiple(game.contract_address, get_contract_address(), get_block_timestamp() + YEAR);
game.energy_plant_upgrade();
game.energy_plant_upgrade();
game.energy_plant_upgrade();
Expand All @@ -161,11 +164,14 @@ fn build_basic_mines(game: INoGameDispatcher) {
game.tritium_mine_upgrade();
game.energy_plant_upgrade();
game.tritium_mine_upgrade();
game.tritium_mine_upgrade();
game.tritium_mine_upgrade();
game.tritium_mine_upgrade();
game.steel_mine_upgrade();
}

fn advance_game_state(game: INoGameDispatcher) {
warp_multiple(game.contract_address, get_contract_address(), get_block_timestamp() + YEAR);
warp_multiple(game.contract_address, get_contract_address(), get_block_timestamp() + YEAR * 3);
game.dockyard_upgrade();
game.dockyard_upgrade();
game.dockyard_upgrade();
Expand Down Expand Up @@ -236,7 +242,6 @@ fn advance_game_state(game: INoGameDispatcher) {
game.weapons_development_upgrade();
game.weapons_development_upgrade();
game.weapons_development_upgrade(); // weapons #3
warp_multiple(game.contract_address, get_contract_address(), get_block_timestamp() + YEAR * 3);
}


Expand Down

0 comments on commit 7f2b55d

Please sign in to comment.