Skip to content

Commit

Permalink
bumped cairo to 2.3.0 and snforge to 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Oct 27, 2023
1 parent 3092b1b commit 6a61cb6
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 421 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = 1
[[package]]
name = "cubit"
version = "1.2.0"
source = "git+https://github.com/influenceth/cubit.git?rev=2ccb253#2ccb2536dffa3f15ebd38b755c1be65fde1eab0c"
source = "git+https://github.com/ametel01/cubit?rev=72784d8#72784d843630a9b216045631aca87b86d4bf5e10"

[[package]]
name = "nogame"
Expand All @@ -18,8 +18,8 @@ dependencies = [

[[package]]
name = "openzeppelin"
version = "0.7.0"
source = "git+https://github.com/openzeppelin/cairo-contracts?tag=v0.7.0#61a2505fe0c0f19b5de2b3f8dedf421ba2cff657"
version = "0.8.0-beta.0"
source = "git+https://github.com/openzeppelin/cairo-contracts?rev=4c95981#4c95981a7178e43730f152c85a92336dc6a22d62"

[[package]]
name = "snforge_std"
Expand Down
8 changes: 5 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ authors = ["ametel01"]
license = "MIT"

[dependencies]
openzeppelin = { git = "https://github.com/openzeppelin/cairo-contracts", tag = "v0.7.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.8.3" }
openzeppelin = { git = "https://github.com/openzeppelin/cairo-contracts", rev = "4c95981" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.9.0" }
xoroshiro = { git ="https://github.com/ametel01/xoroshiro-cairo.git" }
cubit = {git ="https://github.com/influenceth/cubit.git", rev="2ccb253"}
# cubit = {git ="https://github.com/influenceth/cubit.git", rev="2ccb253"}
cubit = {git ="https://github.com/ametel01/cubit", rev="72784d8"}

starknet = "^2.2.0"

[[target.starknet-contract]]
Expand Down
14 changes: 7 additions & 7 deletions src/game/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use starknet::ContractAddress;
use nogame::libraries::types::{
DefencesCost, DefencesLevels, EnergyCost, ERC20s, CompoundsCost, CompoundsLevels, ShipsLevels,
ShipsCost, TechLevels, TechsCost, Tokens, PlanetPosition, Cargo, Debris, Fleet, Mission
ShipsCost, TechLevels, TechsCost, Tokens, PlanetPosition, Cargo, Debris, Fleet, Mission,
HostileMission
};
use nogame::libraries::i128::I128Serde;

#[starknet::interface]
trait INoGame<TState> {
Expand Down Expand Up @@ -54,9 +54,9 @@ trait INoGame<TState> {
ref self: TState, f: Fleet, destination: PlanetPosition, is_debris_collection: bool
);
// fn dock_fleet(ref self: TState, mission_id: u8);
fn attack_planet(ref self: TState, mission_id: u8);
fn recall_fleet(ref self: TState, mission_id: u8);
fn collect_debris(ref self: TState, mission_id: u8);
fn attack_planet(ref self: TState, mission_id: usize);
fn recall_fleet(ref self: TState, mission_id: usize);
fn collect_debris(ref self: TState, mission_id: usize);
// View functions
fn get_owner(self: @TState) -> ContractAddress;
fn get_token_addresses(self: @TState) -> Tokens;
Expand All @@ -83,8 +83,8 @@ trait INoGame<TState> {
fn get_defences_levels(self: @TState, planet_id: u16) -> DefencesLevels;
fn get_defences_cost(self: @TState) -> DefencesCost;
fn is_noob_protected(self: @TState, planet1_id: u16, planet2_id: u16) -> bool;
fn get_mission_details(self: @TState, planet_id: u16, mission_id: u8) -> Mission;
fn get_hostile_missions(self: @TState, planet_id: u16) -> (PlanetPosition, u32);
fn get_mission_details(self: @TState, planet_id: u16, mission_id: usize) -> Mission;
fn get_hostile_missions(self: @TState, planet_id: u16) -> Array<HostileMission>;
fn get_active_missions(self: @TState, planet_id: u16) -> Array<Mission>;
fn get_travel_time(
self: @TState,
Expand Down
138 changes: 102 additions & 36 deletions src/game/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ mod NoGame {
use nogame::libraries::types::{
ETH_ADDRESS, BANK_ADDRESS, E18, DefencesCost, DefencesLevels, EnergyCost, ERC20s,
CompoundsCost, CompoundsLevels, ShipsLevels, ShipsCost, TechLevels, TechsCost, Tokens,
PlanetPosition, Debris, Mission, Fleet, MAX_NUMBER_OF_PLANETS, _0_05, PRICE, DAY
PlanetPosition, Debris, Mission, HostileMission, Fleet, MAX_NUMBER_OF_PLANETS, _0_05, PRICE,
DAY
};
use nogame::libraries::compounds::Compounds;
use nogame::libraries::defences::Defences;
use nogame::libraries::dockyard::Dockyard;
use nogame::libraries::fleet;
use nogame::libraries::research::Lab;
use nogame::token::erc20::{INGERC20DispatcherTrait, INGERC20Dispatcher};
use nogame::token::erc721::{INGERC721DispatcherTrait, INGERC721Dispatcher};
use nogame::libraries::i128::{I128Serde, to_signed};
use nogame::token::erc721::{IERC721NoGameDispatcherTrait, IERC721NoGameDispatcher};
use nogame::libraries::i128::{to_signed};

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

Expand All @@ -45,7 +46,7 @@ mod NoGame {
resources_spent: LegacyMap::<u16, u128>,
last_active: LegacyMap::<u16, u64>,
// Tokens.
erc721: INGERC721Dispatcher,
erc721: IERC721NoGameDispatcher,
steel: INGERC20Dispatcher,
quartz: INGERC20Dispatcher,
tritium: INGERC20Dispatcher,
Expand Down Expand Up @@ -86,9 +87,10 @@ mod NoGame {
astral_available: LegacyMap::<u16, u32>,
plasma_available: LegacyMap::<u16, u32>,
// Fleet
active_missions: LegacyMap::<(u16, u8), Mission>,
active_missions_len: LegacyMap<u16, u8>,
hostile_missions: LegacyMap<u16, (PlanetPosition, u32)>,
active_missions: LegacyMap::<(u16, u32), Mission>,
active_missions_len: LegacyMap<u16, usize>,
hostile_missions: LegacyMap<(u16, u32), HostileMission>,
hostile_missions_len: LegacyMap<u16, usize>,
}

#[event]
Expand Down Expand Up @@ -136,7 +138,7 @@ mod NoGame {
) {
// NOTE: uncomment the following after testing with katana.
assert(get_caller_address() == self.owner.read(), 'caller is not owner');
self.erc721.write(INGERC721Dispatcher { contract_address: erc721 });
self.erc721.write(IERC721NoGameDispatcher { contract_address: erc721 });
self.steel.write(INGERC20Dispatcher { contract_address: steel });
self.quartz.write(INGERC20Dispatcher { contract_address: quartz });
self.tritium.write(INGERC20Dispatcher { contract_address: tritium });
Expand All @@ -156,7 +158,7 @@ mod NoGame {
let number_of_planets = self.number_of_planets.read();
assert(number_of_planets <= MAX_NUMBER_OF_PLANETS, 'max number of planets');
let token_id = number_of_planets + 1;
self.erc721.read().mint(to: caller, token_id: token_id.into());
self.erc721.read().mint(caller, token_id.into());
let position = self.calculate_planet_position(token_id);
self.planet_position.write(token_id, position);
self.position_to_planet_id.write(self.get_raw_from_position(position), token_id);
Expand Down Expand Up @@ -656,7 +658,7 @@ mod NoGame {
let travel_time = fleet::get_flight_time(speed, distance);
// Check numeber of mission
let active_missions = self.active_missions_len.read(planet_id);
assert(active_missions < techs.digital + 1, 'max active missions');
assert(active_missions < techs.digital.into() + 1, 'max active missions');
// Pay for fuel
let consumption = fleet::get_fuel_consumption(f, distance);
let mut cost: ERC20s = Default::default();
Expand All @@ -672,27 +674,25 @@ mod NoGame {
mission.fleet = f;
if is_debris_collection {
mission.is_debris = true;
self.add_active_mission(planet_id, mission);
} else {
let id = self.add_active_mission(planet_id, mission);
mission.is_debris = false;
self
.hostile_missions
.write(
self.position_to_planet_id.read(self.get_raw_from_position(destination)),
(
self.get_planet_position(planet_id),
fleet::calculate_number_of_ships(f, Zeroable::zero())
)
);
let mut hostile_mission: HostileMission = Default::default();
hostile_mission.origin = planet_id;
hostile_mission.id_at_origin = id;
hostile_mission.time_arrival = mission.time_arrival;
hostile_mission
.number_of_ships = fleet::calculate_number_of_ships(f, Zeroable::zero());

self.add_hostile_mission(destination_id, hostile_mission);
}
self.active_missions.write((planet_id, active_missions + 1), mission);
self.active_missions_len.write(planet_id, active_missions + 1);
// Write new fleet levels
self.fleet_leave_planet(planet_id, f);

self.last_active.write(planet_id, get_block_timestamp());
}

fn attack_planet(ref self: ContractState, mission_id: u8) {
fn attack_planet(ref self: ContractState, mission_id: usize) {
let origin = self.get_owned_planet(get_caller_address());
let mut mission = self.active_missions.read((origin, mission_id));
assert(!mission.is_zero(), 'the mission is empty');
Expand Down Expand Up @@ -722,27 +722,26 @@ mod NoGame {
self.resources_timer.write(mission.destination, time_now);
self
.pay_resources_erc20(
self.erc721.read().owner_of(mission.destination.into()), loot_amount
self.erc721.read().ng_owner_of(mission.destination.into()), loot_amount
);
self.receive_resources_erc20(get_caller_address(), loot_amount);
self.fleet_return_planet(origin, mission.fleet);
mission.fleet = f1;
self.active_missions_len.write(origin, self.active_missions_len.read(origin) - 1);
self.active_missions.write((origin, mission_id), Zeroable::zero());
}
self.hostile_missions.write(mission.destination, (Zeroable::zero(), Zeroable::zero()));
self.remove_hostile_mission(mission.destination, mission_id);
}

fn recall_fleet(ref self: ContractState, mission_id: u8) {
fn recall_fleet(ref self: ContractState, mission_id: usize) {
let origin = self.get_owned_planet(get_caller_address());
let mission = self.active_missions.read((origin, mission_id));
assert(!mission.is_zero(), 'no fleet to recall');
self.fleet_return_planet(origin, mission.fleet);
self.active_missions.write((origin, mission_id), Zeroable::zero());
self.active_missions_len.write(origin, self.active_missions_len.read(origin) - 1);
self.remove_hostile_mission(mission.destination, mission_id);
}

fn collect_debris(ref self: ContractState, mission_id: u8) {
fn collect_debris(ref self: ContractState, mission_id: usize) {
let caller = get_caller_address();
let origin = self.get_owned_planet(caller);
let mut mission = self.active_missions.read((origin, mission_id));
Expand Down Expand Up @@ -822,7 +821,7 @@ mod NoGame {
}

fn get_spendable_resources(self: @ContractState, planet_id: u16) -> ERC20s {
let planet_owner = self.erc721.read().owner_of(planet_id.into());
let planet_owner = self.erc721.read().ng_owner_of(planet_id.into());
let steel = self.steel.read().balance_of(planet_owner).low / E18;
let quartz = self.quartz.read().balance_of(planet_owner).low / E18;
let tritium = self.tritium.read().balance_of(planet_owner).low / E18;
Expand Down Expand Up @@ -972,26 +971,42 @@ mod NoGame {
}
}

fn get_mission_details(self: @ContractState, planet_id: u16, mission_id: u8) -> Mission {
fn get_mission_details(self: @ContractState, planet_id: u16, mission_id: usize) -> Mission {
self.active_missions.read((planet_id, mission_id))
}

fn get_active_missions(self: @ContractState, planet_id: u16) -> Array<Mission> {
let mut arr: Array<Mission> = array![];
let len = self.active_missions_len.read(planet_id);
let mut i: u8 = 1;
let mut i = 1;
loop {
if i.into() > len {
if i > len {
break;
}
arr.append(self.active_missions.read((planet_id, i)));
let mission = self.active_missions.read((planet_id, i));
if !mission.is_zero() {
arr.append(mission);
}
i += 1;
};
arr
}

fn get_hostile_missions(self: @ContractState, planet_id: u16) -> (PlanetPosition, u32) {
self.hostile_missions.read(planet_id)
fn get_hostile_missions(self: @ContractState, planet_id: u16) -> Array<HostileMission> {
let mut arr: Array<HostileMission> = array![];
let len = self.hostile_missions_len.read(planet_id);
let mut i = 1;
loop {
if i > len {
break;
}
let mission = self.hostile_missions.read((planet_id, i));
if !mission.is_zero() {
arr.append(mission);
}
i += 1;
};
arr
}

fn get_travel_time(
Expand Down Expand Up @@ -1443,6 +1458,57 @@ mod NoGame {
self.plasma_available.write(planet_id, d.plasma);
}

fn add_active_mission(ref self: ContractState, planet_id: u16, mission: Mission) -> usize {
let len = self.active_missions_len.read(planet_id);
let mut i = 1;
loop {
if i > len {
self.active_missions.write((planet_id, i), mission);
self.active_missions_len.write(planet_id, i);
break;
}
i.print();
let mission = self.active_missions.read((planet_id, i));
if mission.is_zero() {
self.active_missions.write((planet_id, i), mission);
}
i += 1;
};
i
}

fn add_hostile_mission(ref self: ContractState, planet_id: u16, mission: HostileMission) {
let len = self.hostile_missions_len.read(planet_id);
let mut i = 1;
loop {
if i > len {
self.hostile_missions.write((planet_id, i), mission);
self.hostile_missions_len.write(planet_id, i);
break;
}
let mission = self.hostile_missions.read((planet_id, i));
if mission.is_zero() {
self.hostile_missions.write((planet_id, i), mission);
}
i += 1;
};
}

fn remove_hostile_mission(ref self: ContractState, planet_id: u16, id_to_remove: usize) {
let len = self.hostile_missions_len.read(planet_id);
let mut i = 1;
loop {
if i > len {
break;
}
let mission = self.hostile_missions.read((planet_id, i));
if mission.id_at_origin == id_to_remove {
self.hostile_missions.write((planet_id, i), Zeroable::zero());
}
i += 1;
}
}

fn position_to_celestia_production(self: @ContractState, orbit: u8) -> u16 {
if orbit == 1 {
return 48;
Expand Down
20 changes: 10 additions & 10 deletions src/libraries/i128.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ fn to_signed(a: u128, is_positive: bool) -> i128 {
-a_signed
}
}
// impl I128Serde of Serde<i128> {
// fn serialize(self: @i128, ref output: Array<felt252>) {
// output.append((*self).into());
// }
// fn deserialize(ref serialized: Span<felt252>) -> Option<i128> {
// let felt_val = *(serialized.pop_front().expect('i128 deserialize'));
// let i128_val = felt_val.try_into().expect('i128 Overflow');
// Option::Some(i128_val)
// }
// }


impl I128Serde of Serde<i128> {
fn serialize(self: @i128, ref output: Array<felt252>) {
output.append((*self).into());
}
fn deserialize(ref serialized: Span<felt252>) -> Option<i128> {
let felt_val = *(serialized.pop_front().expect('i128 deserialize'));
let i128_val = felt_val.try_into().expect('i128 Overflow');
Option::Some(i128_val)
}
}
34 changes: 34 additions & 0 deletions src/libraries/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,40 @@ impl PrintUnit of PrintTrait<Unit> {
}
}

#[derive(Copy, Default, PartialEq, Drop, Serde, starknet::Store)]
struct HostileMission {
origin: u16,
id_at_origin: usize,
time_arrival: u64,
number_of_ships: u32,
}

impl HostileMissionPrint of PrintTrait<HostileMission> {
fn print(self: HostileMission) {
self.origin.print();
self.id_at_origin.print();
self.time_arrival.print();
self.number_of_ships.print();
}
}

impl HostileMissionZeroable of Zeroable<HostileMission> {
fn zero() -> HostileMission {
HostileMission {
origin: Zeroable::zero(),
id_at_origin: Zeroable::zero(),
time_arrival: Zeroable::zero(),
number_of_ships: Zeroable::zero(),
}
}
fn is_zero(self: HostileMission) -> bool {
self.origin.is_zero() || self.number_of_ships.is_zero() || self.time_arrival.is_zero()
}
fn is_non_zero(self: HostileMission) -> bool {
!self.is_zero()
}
}

#[derive(Copy, Default, PartialEq, Drop, Serde, starknet::Store)]
struct Mission {
time_start: u64,
Expand Down
Loading

0 comments on commit 6a61cb6

Please sign in to comment.