Skip to content

Commit

Permalink
Craving protocol and server-side implemenation done. Compiles, but no…
Browse files Browse the repository at this point in the history
…t tested yet.
  • Loading branch information
jasonrohrer committed Jun 24, 2020
1 parent 8f136a9 commit 0b36e3e
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 1 deletion.
123 changes: 123 additions & 0 deletions server/cravings.cpp
@@ -0,0 +1,123 @@

#include "minorGems/util/SimpleVector.h"
#include "minorGems/util/SettingsManager.h"
#include "minorGems/util/random/JenkinsRandomSource.h"


#include "../gameSource/objectBank.h"
#include "../gameSource/transitionBank.h"



typedef struct CravingList {
int lineageEveID;
SimpleVector<int> cravedFoodIDs;
} CravingList;


static SimpleVector<CravingList> list;



// returns NULL if not found
static CravingList *getListForLineage( int inLineageEveID ) {
for( int i=0; i<list.size(); i++ ) {
CravingList *l = list.getElement( i );
if( l->lineageEveID == inLineageEveID ) {
return l;
}
}
return NULL;
}



static JenkinsRandomSource randSource;


static int getRandomFood( int inPlayerGenerationNumber,
int inFoodToAvoid = -1 ) {

double e = SettingsManager::getDoubleSetting( "cravingPoolExponent", 1.4 );

int maxDepth = lrint( pow( inPlayerGenerationNumber, e ) );

if( maxDepth < 1 ) {
maxDepth = 1;
}


SimpleVector<int> *allFoods = getAllPossibleFoodIDs();

SimpleVector<int> possibleFoods;

for( int i=0; i<allFoods->size(); i++ ) {
int id = allFoods->getElementDirect( i );

if( id != inFoodToAvoid ) {

int d = getObjectDepth( id );

if( d <= maxDepth ) {
possibleFoods.push_back( id );
}
}
}

if( possibleFoods.size() > 0 ) {
int pick =
randSource.getRandomBoundedInt( 0, possibleFoods.size() - 1 );

return possibleFoods.getElementDirect( pick );
}
else {
// no possible foods at or below d depth

// return first food in main list
if( allFoods->size() > 0 ) {
return allFoods->getElementDirect( 0 );
}
else {
return -1;
}
}
}




int getCravedFood( int inLineageEveID, int inPlayerGenerationNumber,
int inLastCravedID ) {

CravingList *l = getListForLineage( inLineageEveID );

if( l == NULL ) {
// push a new empty list
CravingList newL;
newL.lineageEveID = inLineageEveID;
list.push_back( newL );

l = getListForLineage( inLineageEveID );
}

int listSize = l->cravedFoodIDs.size();
for( int i=0; i < listSize; i++ ) {
int foodID = l->cravedFoodIDs.getElementDirect( i );

if( foodID == inLastCravedID && i < listSize - 1 ) {
return l->cravedFoodIDs.getElementDirect( i + 1 );
}
}

// got here, we went off end of list without finding our last food
// add a new one and return that.

// avoid repeating last food we craved
int newFoodID = getRandomFood( inPlayerGenerationNumber, inLastCravedID );

l->cravedFoodIDs.push_back( newFoodID );

return newFoodID;
}


6 changes: 6 additions & 0 deletions server/cravings.h
@@ -0,0 +1,6 @@




int getCravedFood( int inLineageEveID, int inPlayerGenerationNumber,
int inLastCravedID = -1 );
11 changes: 11 additions & 0 deletions server/protocol.txt
Expand Up @@ -228,6 +228,8 @@ HOMELAND (HL)

FLIP (FL)

CRAVING (CR)

FRAME (FM)

PONG
Expand Down Expand Up @@ -985,6 +987,15 @@ requests (clients should still auto-flip players based on movement).



CR
food_id bonus
#

Tells player about which food they're currently craving, and how much their
YUM multiplier will increase when they eat it.




FM
#
Expand Down
73 changes: 72 additions & 1 deletion server/server.cpp
Expand Up @@ -57,6 +57,7 @@
#include "arcReport.h"
#include "curseDB.h"
#include "specialBiomes.h"
#include "cravings.h"


#include "minorGems/util/random/JenkinsRandomSource.h"
Expand Down Expand Up @@ -181,6 +182,8 @@ static int canYumChainBreak = 0;
// -1 for no cap
static int yumBonusCap = -1;

static double minAgeForCravings = 10;


static double posseSizeSpeedMultipliers[4] = { 0.75, 1.25, 1.5, 2.0 };

Expand Down Expand Up @@ -1080,6 +1083,10 @@ typedef struct LiveObject {
double lastGateVisitorNoticeTime;
double lastNewBabyNoticeTime;

int cravingFoodID;
int cravingFoodYumIncrement;
char cravingKnown;

} LiveObject;


Expand Down Expand Up @@ -6925,7 +6932,10 @@ static char isYummy( LiveObject *inPlayer, int inObjectID ) {
// we're NOT replacing o with the yumParent object
// because o isn't used beyond this point
}


if( inObjectID == inPlayer->cravingFoodID ) {
return true;
}

for( int i=0; i<inPlayer->yummyFoodChain.size(); i++ ) {
if( inObjectID == inPlayer->yummyFoodChain.getElementDirect(i) ) {
Expand Down Expand Up @@ -6979,6 +6989,25 @@ static void updateYum( LiveObject *inPlayer, int inFoodEatenID,
}

inPlayer->yummyFoodChain.push_back( eatenID );

if( eatenID == inPlayer->cravingFoodID ) {

for( int i=0; i< inPlayer->cravingFoodYumIncrement; i++ ) {
// add extra copies to YUM chain as a bonus
inPlayer->yummyFoodChain.push_back( eatenID );
}

// craving satisfied, go on to next thing in list
inPlayer->cravingFoodID =
getCravedFood( inPlayer->lineageEveID,
inPlayer->parentChainLength,
inPlayer->cravingFoodID );
// reset generational bonus counter
inPlayer->cravingFoodYumIncrement = 1;

// flag them for getting a new craving message
inPlayer->cravingKnown = false;
}
}


Expand Down Expand Up @@ -8286,6 +8315,10 @@ int processLoggedInPlayer( int inAllowOrForceReconnect,
canYumChainBreak = SettingsManager::getIntSetting( "canYumChainBreak", 0 );

yumBonusCap = SettingsManager::getIntSetting( "yumBonusCap", -1 );


minAgeForCravings = SettingsManager::getDoubleSetting( "minAgeForCravings",
10 );


numConnections ++;
Expand All @@ -8304,6 +8337,10 @@ int processLoggedInPlayer( int inAllowOrForceReconnect,
newObject.lastGateVisitorNoticeTime = 0;
newObject.lastNewBabyNoticeTime = 0;

newObject.cravingFoodID = -1;
newObject.cravingFoodYumIncrement = 0;
newObject.cravingKnown = false;

newObject.id = nextID;
nextID++;

Expand Down Expand Up @@ -8938,6 +8975,12 @@ int processLoggedInPlayer( int inAllowOrForceReconnect,
newObject.lineageEveID = newObject.id;

newObject.lifeStartTimeSeconds -= 14 * ( 1.0 / getAgeRate() );

// she starts off craving a food right away
newObject.cravingFoodID = getCravedFood( newObject.lineageEveID,
newObject.parentChainLength );
// initilize increment
newObject.cravingFoodYumIncrement = 1;


// when placing eve, pick a race that is not currently
Expand Down Expand Up @@ -9717,6 +9760,13 @@ int processLoggedInPlayer( int inAllowOrForceReconnect,
// mother
newObject.lineage->push_back( newObject.parentID );


// inherit mother's craving at time of birth
newObject.cravingFoodID = parent->cravingFoodID;

// increment for next generation
newObject.cravingFoodYumIncrement = parent->cravingFoodYumIncrement + 1;


// inherit last heard monument, if any, from parent

Expand Down Expand Up @@ -15955,6 +16005,19 @@ static void setRefuseFoodEmote( LiveObject *hitPlayer ) {



static void sendCraving( LiveObject *inPlayer ) {
char *message = autoSprintf( "CR\n%d %d\n#",
inPlayer->cravingFoodID,
inPlayer->cravingFoodYumIncrement );
sendMessageToPlayer( inPlayer, message, strlen( message ) );
delete [] message;

inPlayer->cravingKnown = true;
}




int main() {

if( checkReadOnly() ) {
Expand Down Expand Up @@ -23481,6 +23544,14 @@ int main() {
nextPlayer->emotUnfreezeETA = 0;
}

if( ! nextPlayer->error &&
! nextPlayer->cravingKnown &&
computeAge( nextPlayer ) >= minAgeForCravings ) {

sendCraving( nextPlayer );
}



if( nextPlayer->dying && ! nextPlayer->error &&
curTime >= nextPlayer->dyingETA ) {
Expand Down
1 change: 1 addition & 0 deletions server/settings/cravingPoolExponent.ini
@@ -0,0 +1 @@
1.4
1 change: 1 addition & 0 deletions server/settings/minAgeForCravings.ini
@@ -0,0 +1 @@
10

0 comments on commit 0b36e3e

Please sign in to comment.