Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dfdao community round additions #3

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d6f2221
.toml base and localhost
Dec 29, 2021
47494cd
update zkSNARK for move
Dec 29, 2021
f88d0ff
Merge branch 'base' of https://github.com/cha0sg0d/eth into shrink
Dec 29, 2021
db78481
inital world radius
Dec 30, 2021
fef9cf9
starting subgraph stuff
Dec 30, 2021
3df1ee3
working destroy logic and tests
Dec 30, 2021
0efc429
remove hats due to contract size
Dec 30, 2021
45bfbe3
update black domain test to account for no-op on move to destroy
Dec 30, 2021
a63e14f
update .toml for testing destroy
Dec 30, 2021
01d4e7e
Merge pull request #7 from cha0sg0d/destroy
cha0sg0d Dec 30, 2021
c693c55
set reveal cooldown
Dec 30, 2021
bb50c71
shrink logic, constants, and test
Dec 30, 2021
5d374bc
clean up variable name, add js shrink algorithm, and update settings
Dec 30, 2021
2ef5d21
Merge branch 'community' into shrink
cha0sg0d Dec 30, 2021
754ec08
Merge pull request #8 from cha0sg0d/shrink
cha0sg0d Dec 30, 2021
a944c51
set token end timestamp
Dec 30, 2021
3e82a65
working whitelist flow
Dec 30, 2021
0a99ef6
Merge pull request #9 from cha0sg0d/whitelist
cha0sg0d Dec 30, 2021
0d16a9c
update .toml with correct timestamps
Dec 30, 2021
9f1c197
add lowercase for whitelisting
Dec 31, 2021
bacdfa2
add my address
Dec 31, 2021
c00e150
Merge pull request #10 from cha0sg0d/whitelist
cha0sg0d Dec 31, 2021
6158997
subgraph edits
Dec 31, 2021
cd94499
working subgraph for death of the universe
Dec 31, 2021
1b38262
Merge pull request #11 from cha0sg0d/subg
cha0sg0d Dec 31, 2021
3360069
make _updateWorldRadius public in case of emergency
Dec 31, 2021
9bf7544
remove console.log
Dec 31, 2021
d83af13
remove log
Dec 31, 2021
4a6c3e4
fix silver withdrawal test
Dec 31, 2021
4f7d92d
update admin tasks
Jan 1, 2022
520944f
prod version of Verifier.sol
Jan 1, 2022
0ff7e1e
small whitelist taskupdate
Jan 1, 2022
11d3576
needed constants
Jan 1, 2022
cd3e630
final round .toml and Verification
Jan 1, 2022
c535501
curr addresses
Jan 1, 2022
8caf297
1/1/3pm addresses
Jan 1, 2022
344dd80
whitelist task typo
Jan 1, 2022
50451aa
1/1/3:20 whitelist
Jan 1, 2022
4993adb
1/1/6:37 whitelist
Jan 2, 2022
029ff21
1/1/6:41 whitelist for cecsk
Jan 2, 2022
bc4e543
auto-whitelist
Jan 2, 2022
f7d6e24
reduce DarkForestCore size by removing changeAdmin
Jan 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions addresses.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0x3097403b64fe672467345bf159f4c9c5464bd89e,
0x1c0f0af3262a7213e59be7f1440282279d788335,
0xe8170282c5bc6e7c5b2d984cd5d897a05e0afafb,
0xb97eea0763611d1abaae833b3e99a5e269ec314e
60 changes: 42 additions & 18 deletions contracts/DarkForestCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
event ArtifactWithdrawn(address player, uint256 artifactId, uint256 loc);
event ArtifactActivated(address player, uint256 artifactId, uint256 loc); // emitted in DFPlanet library
event ArtifactDeactivated(address player, uint256 artifactId, uint256 loc); // emitted in DFPlanet library
event PlanetDestroyed(address player, uint256 loc); // emitted in DFPlanet library
event RadiusUpdated(uint256 radius);

event PlanetSilverWithdrawn(address player, uint256 loc, uint256 amount);

Expand Down Expand Up @@ -88,6 +90,8 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
PERLIN_LENGTH_SCALE: initArgs.PERLIN_LENGTH_SCALE
});
s.gameConstants = DarkForestTypes.GameConstants({
DESTROY_THRESHOLD: initArgs.DESTROY_THRESHOLD,
INITIAL_WORLD_RADIUS: initArgs.INITIAL_WORLD_RADIUS,
MAX_NATURAL_PLANET_LEVEL: initArgs.MAX_NATURAL_PLANET_LEVEL,
TIME_FACTOR_HUNDREDTHS: initArgs.TIME_FACTOR_HUNDREDTHS,
PERLIN_THRESHOLD_1: initArgs.PERLIN_THRESHOLD_1,
Expand All @@ -102,7 +106,13 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
PHOTOID_ACTIVATION_DELAY: initArgs.PHOTOID_ACTIVATION_DELAY,
LOCATION_REVEAL_COOLDOWN: initArgs.LOCATION_REVEAL_COOLDOWN,
PLANET_TYPE_WEIGHTS: initArgs.PLANET_TYPE_WEIGHTS,
ARTIFACT_POINT_VALUES: initArgs.ARTIFACT_POINT_VALUES
ARTIFACT_POINT_VALUES: initArgs.ARTIFACT_POINT_VALUES,
SHRINK: initArgs.SHRINK,
SHRINK_START: initArgs.SHRINK_START,
ROUND_END: initArgs.ROUND_END,
MIN_RADIUS: initArgs.MIN_RADIUS,
DISC_LOWER_BOUND: initArgs.DISC_LOWER_BOUND,
DISC_UPPER_BOUND: initArgs.DISC_UPPER_BOUND
});

s.worldRadius = initArgs.INITIAL_WORLD_RADIUS; // will be overridden by TARGET4_RADIUS if !WORLD_RADIUS_LOCKED
Expand Down Expand Up @@ -160,9 +170,10 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
//////////////

// Private helpers that modify state
function _updateWorldRadius() private {
function _updateWorldRadius() public {
if (!s.WORLD_RADIUS_LOCKED) {
s.worldRadius = DarkForestUtils._getRadius();
emit RadiusUpdated(s.worldRadius);
}
}

Expand All @@ -178,10 +189,10 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
/// Administrative Engine ///
/////////////////////////////

function changeAdmin(address _newAdmin) public onlyAdmin {
require(_newAdmin != address(0), "newOwner cannot be 0x0");
s.adminAddress = _newAdmin;
}
// function changeAdmin(address _newAdmin) public onlyAdmin {
// require(_newAdmin != address(0), "newOwner cannot be 0x0");
// s.adminAddress = _newAdmin;
// }

function pause() public onlyAdmin {
require(!s.paused, "Game is already paused");
Expand All @@ -204,6 +215,7 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {

function adminSetWorldRadius(uint256 _newRadius) public onlyAdmin {
s.worldRadius = _newRadius;
emit RadiusUpdated(s.worldRadius);
}

function changeLocationRevealCooldown(uint256 newCooldown) public onlyAdmin {
Expand Down Expand Up @@ -242,6 +254,18 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
emit AdminPlanetCreated(args.location);
}

function changeDestroyThreshold(uint256 newThreshold) public onlyAdmin {
s.gameConstants.DESTROY_THRESHOLD = newThreshold;
}

function setShrinkStart(uint256 newShrinkStart) public onlyAdmin {
s.gameConstants.SHRINK_START = newShrinkStart;
}

function setRoundEnd(uint256 newEndTime) public onlyAdmin {
s.gameConstants.ROUND_END = newEndTime;
}

//////////////////////
/// Game Mechanics ///
//////////////////////
Expand Down Expand Up @@ -449,22 +473,22 @@ contract DarkForestCore is Initializable, DarkForestStorageV1 {
emit PlanetTransferred(msg.sender, _location, _player);
}

function buyHat(uint256 _location) public payable {
require(
s.planetsExtendedInfo[_location].isInitialized == true,
"Planet is not initialized"
);
refreshPlanet(_location);
// function buyHat(uint256 _location) public payable {
// require(
// s.planetsExtendedInfo[_location].isInitialized == true,
// "Planet is not initialized"
// );
// refreshPlanet(_location);

require(s.planets[_location].owner == msg.sender, "Only owner can buy hat for planet");
// require(s.planets[_location].owner == msg.sender, "Only owner can buy hat for planet");

uint256 cost = (1 << s.planetsExtendedInfo[_location].hatLevel) * 1 ether;
// uint256 cost = (1 << s.planetsExtendedInfo[_location].hatLevel) * 1 ether;

require(msg.value == cost, "Wrong value sent");
// require(msg.value == cost, "Wrong value sent");

s.planetsExtendedInfo[_location].hatLevel += 1;
emit PlanetHatBought(msg.sender, _location, s.planetsExtendedInfo[_location].hatLevel);
}
// s.planetsExtendedInfo[_location].hatLevel += 1;
// emit PlanetHatBought(msg.sender, _location, s.planetsExtendedInfo[_location].hatLevel);
// }

function findArtifact(
uint256[2] memory _a,
Expand Down
31 changes: 25 additions & 6 deletions contracts/DarkForestLazyUpdate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ library DarkForestLazyUpdate {
function applyArrival(
DarkForestTypes.Planet memory planet,
DarkForestTypes.ArrivalData memory arrival
) private pure returns (uint256 newArtifactOnPlanet, DarkForestTypes.Planet memory) {
) private view returns (DarkForestTypes.ApplyArrivalData memory) {
// checks whether the planet is owned by the player sending ships
bool isDestroyed = false;

if (arrival.player == planet.owner) {
// simply increase the population if so
planet.population = planet.population + arrival.popArriving;
} else {

if (arrival.arrivalType == DarkForestTypes.ArrivalType.Wormhole) {
// if this is a wormhole arrival to a planet that isn't owned by the initiator of
// the move, then don't move any energy
Expand All @@ -147,6 +150,18 @@ library DarkForestLazyUpdate {
// reduce the arriving ships amount with the current population and the
// result is the new population of the planet now owned by the attacking
// player

// If moving to enemy planet, destroy if arriving energy > threshold.
// If threshold = 0, no destroy.
if (planet.owner != address(0) && s().gameConstants.DESTROY_THRESHOLD > 0) {
if(
((arrival.popArriving * 100) / planet.defense) >
(planet.population * s().gameConstants.DESTROY_THRESHOLD)
) {
isDestroyed = true;
}
}

planet.owner = arrival.player;
planet.population =
arrival.popArriving -
Expand All @@ -170,7 +185,7 @@ library DarkForestLazyUpdate {
uint256 _nextSilver = planet.silver + arrival.silverMoved;
planet.silver = _maxSilver < _nextSilver ? _maxSilver : _nextSilver;

return (arrival.carriedArtifactId, planet);
return DarkForestTypes.ApplyArrivalData(arrival.carriedArtifactId, planet, isDestroyed);
}

function applyPendingEvents(
Expand Down Expand Up @@ -246,14 +261,18 @@ library DarkForestLazyUpdate {
) {
eventIdsToRemove[numEventsToRemove++] = events[earliestEventIndex].id;

uint256 newArtifactId;
(newArtifactId, planet) = applyArrival(
DarkForestTypes.ApplyArrivalData memory arrivalData = applyArrival(
planet,
s().planetArrivals[events[earliestEventIndex].id]
);

if (newArtifactId != 0) {
newArtifactsOnPlanet[numNewArtifactsOnPlanet++] = newArtifactId;
if (arrivalData.newArtifactId != 0) {
newArtifactsOnPlanet[numNewArtifactsOnPlanet++] = arrivalData.newArtifactId;
}

if(arrivalData.destroyed) {
planetExtendedInfo.destroyed = true;
break;
}
}
}
Expand Down
27 changes: 22 additions & 5 deletions contracts/DarkForestPlanet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ library DarkForestPlanet {
event ArtifactActivated(address player, uint256 artifactId, uint256 loc);
event ArtifactDeactivated(address player, uint256 artifactId, uint256 loc);
event PlanetUpgraded(address player, uint256 loc, uint256 branch, uint256 toBranchLevel);
event PlanetDestroyed(address player, uint256 loc);

function isPopCapBoost(uint256 _location) public pure returns (bool) {
bytes memory _b = abi.encodePacked(_location);
Expand Down Expand Up @@ -376,6 +377,15 @@ library DarkForestPlanet {
);
}

// this will only allow players to initialize in the middle ring of the universe.
if(s().gameConstants.SHRINK > 0) {
uint256 radius = s().worldRadius;
uint256 upperQuartile = (radius * s().gameConstants.DISC_UPPER_BOUND) / 100;
uint256 lowerQuartile = (radius * s().gameConstants.DISC_LOWER_BOUND) / 100;
require(_radius >= lowerQuartile, "Init radius is too low");
require(_radius <= upperQuartile, "Init radius is too high");
}

require(
_perlin >= s().gameConstants.INIT_PERLIN_MIN,
"Init not allowed in perlin value less than INIT_PERLIN_MIN"
Expand All @@ -392,11 +402,11 @@ library DarkForestPlanet {
DarkForestUtils.getActiveArtifact(args.oldLoc);
DarkForestTypes.Artifact memory activeArtifactTo =
DarkForestUtils.getActiveArtifact(args.newLoc);
require(
!s().planetsExtendedInfo[args.newLoc].destroyed &&
!s().planetsExtendedInfo[args.oldLoc].destroyed,
"planet is destroyed"
);

// Do nothing if planet is destroyed. Ensures that moves will still refresh planet.
if(s().planetsExtendedInfo[args.newLoc].destroyed || s().planetsExtendedInfo[args.oldLoc].destroyed) {
return;
}
require(
s().planets[args.oldLoc].owner == msg.sender,
"Only owner account can perform operation on planets"
Expand Down Expand Up @@ -535,9 +545,16 @@ library DarkForestPlanet {
uint256[12] memory artifactIdsToAddToPlanet
) = getRefreshedPlanet(location, block.timestamp);

DarkForestTypes.PlanetExtendedInfo memory prevPlanet = s().planetsExtendedInfo[location];

s().planets[location] = planet;
s().planetsExtendedInfo[location] = planetInfo;

// Only emit PlanetDestroyed when actual destruction occurred.
if(planetInfo.destroyed && !prevPlanet.destroyed) {
emit PlanetDestroyed(planet.owner, location);
}

DarkForestTypes.PlanetEventMetadata[] storage events = s().planetEvents[location];

for (uint256 toRemoveIdx = 0; toRemoveIdx < 12; toRemoveIdx++) {
Expand Down
21 changes: 21 additions & 0 deletions contracts/DarkForestTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ library DarkForestTypes {
// Game config
// This struct is ~1 properties from maximum struct size
struct GameConstants {
uint256 DESTROY_THRESHOLD;
uint256 INITIAL_WORLD_RADIUS;
uint256 MAX_NATURAL_PLANET_LEVEL;
uint256 TIME_FACTOR_HUNDREDTHS; // speedup/slowdown game
uint256 PERLIN_THRESHOLD_1;
Expand All @@ -110,6 +112,12 @@ library DarkForestTypes {
uint256 LOCATION_REVEAL_COOLDOWN;
uint8[5][10][4] PLANET_TYPE_WEIGHTS; // spaceType (enum 0-3) -> planetLevel (0-7) -> planetType (enum 0-4)
uint256[6] ARTIFACT_POINT_VALUES;
uint256 SHRINK_START;
uint256 ROUND_END;
uint256 MIN_RADIUS;
uint256 SHRINK;
uint256 DISC_LOWER_BOUND;
uint256 DISC_UPPER_BOUND;
}

struct DFInitArgs {
Expand All @@ -128,6 +136,7 @@ library DarkForestTypes {
bool PERLIN_MIRROR_Y;
uint256 PERLIN_LENGTH_SCALE; // must be a power of two up to 8192
// Game config
uint256 DESTROY_THRESHOLD;
uint256 MAX_NATURAL_PLANET_LEVEL;
uint256 TIME_FACTOR_HUNDREDTHS; // speedup/slowdown game
uint256 PERLIN_THRESHOLD_1;
Expand All @@ -142,6 +151,12 @@ library DarkForestTypes {
uint8[5][10][4] PLANET_TYPE_WEIGHTS; // spaceType (enum 0-3) -> planetLevel (0-7) -> planetType (enum 0-4)
uint256[6] ARTIFACT_POINT_VALUES;
uint256 PHOTOID_ACTIVATION_DELAY;
uint256 SHRINK_START;
uint256 ROUND_END;
uint256 MIN_RADIUS;
uint256 SHRINK;
uint256 DISC_LOWER_BOUND;
uint256 DISC_UPPER_BOUND;
}

struct DFPInitPlanetArgs {
Expand Down Expand Up @@ -181,6 +196,12 @@ library DarkForestTypes {
ArrivalType arrivalType;
}

struct ApplyArrivalData {
uint256 newArtifactId;
Planet planet;
bool destroyed;
}

struct DFTCreateArtifactArgs {
uint256 tokenId;
address discoverer;
Expand Down
35 changes: 35 additions & 0 deletions contracts/DarkForestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,41 @@ library DarkForestUtils {
}

function _getRadius() public view returns (uint256) {
if(s().gameConstants.SHRINK > 0) {
return _shrinkRadius();
}
else {
return _growRadius();
}
}

function _shrinkRadius() internal view returns (uint256) {
uint256 radius = s().worldRadius;
uint256 shrinkFactor = s().gameConstants.SHRINK;
uint256 totalTime = s().gameConstants.ROUND_END - s().gameConstants.SHRINK_START;
// Only shrink after START_TIME has occurred. Allows for delaying of shrinking.
uint256 startTime = s().gameConstants.SHRINK_START > block.timestamp ? block.timestamp: s().gameConstants.SHRINK_START;
uint256 minRadius = s().gameConstants.MIN_RADIUS;
uint256 timeElapsed = block.timestamp - startTime;
// Clip timeElapsed to max totalTime
if(timeElapsed > totalTime) timeElapsed = totalTime;

// only shrink after initial time elapsed.
radius = (
s().gameConstants.INITIAL_WORLD_RADIUS *
(
(totalTime**shrinkFactor) -
(timeElapsed**shrinkFactor)
)
)
/ totalTime**shrinkFactor;

// set minimum
if (radius < minRadius) radius = minRadius;
return radius;
}

function _growRadius() internal view returns (uint256) {
uint256 nPlayers = s().playerIds.length;
uint256 target4RadiusConstant = s().TARGET4_RADIUS;
uint256 target4 = s().initializedPlanetCountByLevel[4] + 20 * nPlayers;
Expand Down
Loading