Optimize
This commit is contained in:
@@ -14,15 +14,18 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#include "gamescc.h"
|
#include "gamescc.h"
|
||||||
|
#include "tetris.c" // replace with game code
|
||||||
#include "tetris.h"
|
|
||||||
|
|
||||||
static int random_tetromino(void)
|
|
||||||
{
|
|
||||||
return rand() % NUM_TETROMINOS;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef STANDALONE
|
#ifndef STANDALONE
|
||||||
|
int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector<uint8_t> payload);
|
||||||
|
int32_t games_playerdata_validate(int64_t *cashoutp,uint256 &playertxid,struct CCcontract_info *cp,std::vector<uint8_t> playerdata,uint256 gametxid,CPubKey pk);
|
||||||
|
int32_t games_replay2(uint8_t *newdata,uint64_t seed,char *keystrokes,int32_t num,struct games_player *player,int32_t sleepmillis);
|
||||||
|
void games_packitemstr(char *packitemstr,struct games_packitem *item);
|
||||||
|
int64_t games_cashout(struct games_player *P);
|
||||||
|
char *games_extractgame(int32_t makefiles,char *str,int32_t *numkeysp,std::vector<uint8_t> &newdata,uint64_t &seed,uint256 &playertxid,struct CCcontract_info *cp,uint256 gametxid,char *gamesaddr);
|
||||||
|
|
||||||
|
#include "tetris.cpp" // replace with game specific functions
|
||||||
|
|
||||||
/*
|
/*
|
||||||
./c cclib rng 17 \"[%229433dc3749aece1bd568f374a45da3b0bc6856990d7da3cd175399577940a775%22,250]\"
|
./c cclib rng 17 \"[%229433dc3749aece1bd568f374a45da3b0bc6856990d7da3cd175399577940a775%22,250]\"
|
||||||
{
|
{
|
||||||
@@ -55,12 +58,6 @@ static int random_tetromino(void)
|
|||||||
./c cclib events 17 \"[%226d%22,%229433dc3749aece1bd568f374a45da3b0bc6856990d7da3cd175399577940a775%22,1]\"
|
./c cclib events 17 \"[%226d%22,%229433dc3749aece1bd568f374a45da3b0bc6856990d7da3cd175399577940a775%22,1]\"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector<uint8_t> payload);
|
|
||||||
int32_t games_playerdata_validate(int64_t *cashoutp,uint256 &playertxid,struct CCcontract_info *cp,std::vector<uint8_t> playerdata,uint256 gametxid,CPubKey pk);
|
|
||||||
int32_t games_replay2(uint8_t *newdata,uint64_t seed,char *keystrokes,int32_t num,struct games_player *player,int32_t sleepmillis);
|
|
||||||
void games_packitemstr(char *packitemstr,struct games_packitem *item);
|
|
||||||
int64_t games_cashout(struct games_player *P);
|
|
||||||
|
|
||||||
|
|
||||||
CScript games_newgameopret(int64_t buyin,int32_t maxplayers)
|
CScript games_newgameopret(int64_t buyin,int32_t maxplayers)
|
||||||
{
|
{
|
||||||
@@ -1193,91 +1190,6 @@ UniValue games_keystrokes(uint64_t txfee,struct CCcontract_info *cp,cJSON *param
|
|||||||
} else return(cclib_error(result,"couldnt reparse params"));
|
} else return(cclib_error(result,"couldnt reparse params"));
|
||||||
}
|
}
|
||||||
|
|
||||||
char *games_extractgame(int32_t makefiles,char *str,int32_t *numkeysp,std::vector<uint8_t> &newdata,uint64_t &seed,uint256 &playertxid,struct CCcontract_info *cp,uint256 gametxid,char *gamesaddr)
|
|
||||||
{
|
|
||||||
CPubKey gamespk; int32_t i,num,retval,maxplayers,gameheight,batonht,batonvout,numplayers,regslot,numkeys,err; std::string symbol,pname; CTransaction gametx; int64_t buyin,batonvalue; char fname[64],*keystrokes = 0; std::vector<uint8_t> playerdata; uint256 batontxid; FILE *fp; uint8_t newplayer[10000]; struct games_player P,endP;
|
|
||||||
gamespk = GetUnspendable(cp,0);
|
|
||||||
*numkeysp = 0;
|
|
||||||
seed = 0;
|
|
||||||
num = numkeys = 0;
|
|
||||||
playertxid = zeroid;
|
|
||||||
str[0] = 0;
|
|
||||||
if ( (err= games_isvalidgame(cp,gameheight,gametx,buyin,maxplayers,gametxid,0)) == 0 )
|
|
||||||
{
|
|
||||||
if ( (retval= games_findbaton(cp,playertxid,&keystrokes,numkeys,regslot,playerdata,batontxid,batonvout,batonvalue,batonht,gametxid,gametx,maxplayers,gamesaddr,numplayers,symbol,pname)) == 0 )
|
|
||||||
{
|
|
||||||
UniValue obj;
|
|
||||||
seed = games_gamefields(obj,maxplayers,buyin,gametxid,gamesaddr);
|
|
||||||
//fprintf(stderr,"(%s) found baton %s numkeys.%d seed.%llu playerdata.%d playertxid.%s\n",pname.size()!=0?pname.c_str():Games_pname.c_str(),batontxid.ToString().c_str(),numkeys,(long long)seed,(int32_t)playerdata.size(),playertxid.GetHex().c_str());
|
|
||||||
memset(&P,0,sizeof(P));
|
|
||||||
if ( playerdata.size() > 0 )
|
|
||||||
{
|
|
||||||
for (i=0; i<playerdata.size(); i++)
|
|
||||||
((uint8_t *)&P)[i] = playerdata[i];
|
|
||||||
}
|
|
||||||
if ( keystrokes != 0 && numkeys != 0 )
|
|
||||||
{
|
|
||||||
if ( makefiles != 0 )
|
|
||||||
{
|
|
||||||
sprintf(fname,"%s.%llu.0",GAMENAME,(long long)seed);
|
|
||||||
if ( (fp= fopen(fname,"wb")) != 0 )
|
|
||||||
{
|
|
||||||
if ( fwrite(keystrokes,1,numkeys,fp) != numkeys )
|
|
||||||
fprintf(stderr,"error writing %s\n",fname);
|
|
||||||
fclose(fp);
|
|
||||||
}
|
|
||||||
sprintf(fname,"%s.%llu.player",GAMENAME,(long long)seed);
|
|
||||||
if ( (fp= fopen(fname,"wb")) != 0 )
|
|
||||||
{
|
|
||||||
if ( fwrite(&playerdata[0],1,(int32_t)playerdata.size(),fp) != playerdata.size() )
|
|
||||||
fprintf(stderr,"error writing %s\n",fname);
|
|
||||||
fclose(fp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//fprintf(stderr,"call replay2\n");
|
|
||||||
num = games_replay2(newplayer,seed,keystrokes,numkeys,playerdata.size()==0?0:&P,0);
|
|
||||||
newdata.resize(num);
|
|
||||||
for (i=0; i<num; i++)
|
|
||||||
{
|
|
||||||
newdata[i] = newplayer[i];
|
|
||||||
((uint8_t *)&endP)[i] = newplayer[i];
|
|
||||||
}
|
|
||||||
//fprintf(stderr,"back replay2 gold.%d\n",endP.gold);
|
|
||||||
if ( endP.gold <= 0 || endP.hitpoints <= 0 || (endP.strength&0xffff) <= 0 || endP.level <= 0 || endP.experience <= 0 || endP.dungeonlevel <= 0 )
|
|
||||||
{
|
|
||||||
sprintf(str,"zero value character was killed -> no playerdata\n");
|
|
||||||
newdata.resize(0);
|
|
||||||
*numkeysp = numkeys;
|
|
||||||
return(keystrokes);
|
|
||||||
/* P.gold = (P.gold * 8) / 10;
|
|
||||||
if ( keystrokes != 0 )
|
|
||||||
{
|
|
||||||
free(keystrokes);
|
|
||||||
keystrokes = 0;
|
|
||||||
*numkeysp = 0;
|
|
||||||
return(keystrokes);
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sprintf(str,"$$$gold.%d hp.%d strength.%d/%d level.%d exp.%d dl.%d",endP.gold,endP.hitpoints,endP.strength&0xffff,endP.strength>>16,endP.level,endP.experience,endP.dungeonlevel);
|
|
||||||
//fprintf(stderr,"%s\n",str);
|
|
||||||
*numkeysp = numkeys;
|
|
||||||
return(keystrokes);
|
|
||||||
}
|
|
||||||
} else num = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stderr,"extractgame: couldnt find baton keystrokes.%p retval.%d\n",keystrokes,retval);
|
|
||||||
if ( keystrokes != 0 )
|
|
||||||
free(keystrokes), keystrokes = 0;
|
|
||||||
}
|
|
||||||
} else fprintf(stderr,"extractgame: invalid game\n");
|
|
||||||
//fprintf(stderr,"extract %s\n",gametxid.GetHex().c_str());
|
|
||||||
return(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
UniValue games_extract(uint64_t txfee,struct CCcontract_info *cp,cJSON *params)
|
UniValue games_extract(uint64_t txfee,struct CCcontract_info *cp,cJSON *params)
|
||||||
{
|
{
|
||||||
UniValue result(UniValue::VOBJ); CPubKey pk,gamespk; int32_t i,n,numkeys,flag = 0; uint64_t seed; char str[512],gamesaddr[64],*pubstr,*hexstr,*keystrokes = 0; std::vector<uint8_t> newdata; uint256 gametxid,playertxid; FILE *fp; uint8_t pub33[33];
|
UniValue result(UniValue::VOBJ); CPubKey pk,gamespk; int32_t i,n,numkeys,flag = 0; uint64_t seed; char str[512],gamesaddr[64],*pubstr,*hexstr,*keystrokes = 0; std::vector<uint8_t> newdata; uint256 gametxid,playertxid; FILE *fp; uint8_t pub33[33];
|
||||||
@@ -1556,11 +1468,5 @@ UniValue games_setname(uint64_t txfee,struct CCcontract_info *cp,cJSON *params)
|
|||||||
return(result);
|
return(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "tetris.cpp"
|
|
||||||
|
|
||||||
#else // STANDALONE
|
|
||||||
|
|
||||||
#include "tetris.c"
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
|
||||||
#include "tetris.h"
|
#include "tetris.h"
|
||||||
#include "dapps/dappstd.c"
|
|
||||||
|
|
||||||
|
static int random_tetromino(void)
|
||||||
|
{
|
||||||
|
return rand() % NUM_TETROMINOS;
|
||||||
|
}
|
||||||
|
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
/** https://github.com/brenns10/tetris
|
/** https://github.com/brenns10/tetris
|
||||||
@@ -622,10 +625,12 @@ void init_colors(void)
|
|||||||
init_pair(TC_CELLZ, COLOR_RED, COLOR_BLACK);
|
init_pair(TC_CELLZ, COLOR_RED, COLOR_BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
/*
|
/*
|
||||||
Main tetris game!
|
Main tetris game!
|
||||||
*/
|
*/
|
||||||
#ifdef STANDALONE
|
#include "dapps/dappstd.c"
|
||||||
|
|
||||||
char *clonestr(char *str)
|
char *clonestr(char *str)
|
||||||
{
|
{
|
||||||
char *clone; int32_t len;
|
char *clone; int32_t len;
|
||||||
@@ -756,3 +761,5 @@ int32_t games_replay(uint64_t seed,int32_t sleeptime)
|
|||||||
{
|
{
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -128,6 +128,91 @@ int32_t games_playerdata_validate(int64_t *cashoutp,uint256 &playertxid,struct C
|
|||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *games_extractgame(int32_t makefiles,char *str,int32_t *numkeysp,std::vector<uint8_t> &newdata,uint64_t &seed,uint256 &playertxid,struct CCcontract_info *cp,uint256 gametxid,char *gamesaddr)
|
||||||
|
{
|
||||||
|
CPubKey gamespk; int32_t i,num,retval,maxplayers,gameheight,batonht,batonvout,numplayers,regslot,numkeys,err; std::string symbol,pname; CTransaction gametx; int64_t buyin,batonvalue; char fname[64],*keystrokes = 0; std::vector<uint8_t> playerdata; uint256 batontxid; FILE *fp; uint8_t newplayer[10000]; struct games_player P,endP;
|
||||||
|
gamespk = GetUnspendable(cp,0);
|
||||||
|
*numkeysp = 0;
|
||||||
|
seed = 0;
|
||||||
|
num = numkeys = 0;
|
||||||
|
playertxid = zeroid;
|
||||||
|
str[0] = 0;
|
||||||
|
if ( (err= games_isvalidgame(cp,gameheight,gametx,buyin,maxplayers,gametxid,0)) == 0 )
|
||||||
|
{
|
||||||
|
if ( (retval= games_findbaton(cp,playertxid,&keystrokes,numkeys,regslot,playerdata,batontxid,batonvout,batonvalue,batonht,gametxid,gametx,maxplayers,gamesaddr,numplayers,symbol,pname)) == 0 )
|
||||||
|
{
|
||||||
|
UniValue obj;
|
||||||
|
seed = games_gamefields(obj,maxplayers,buyin,gametxid,gamesaddr);
|
||||||
|
//fprintf(stderr,"(%s) found baton %s numkeys.%d seed.%llu playerdata.%d playertxid.%s\n",pname.size()!=0?pname.c_str():Games_pname.c_str(),batontxid.ToString().c_str(),numkeys,(long long)seed,(int32_t)playerdata.size(),playertxid.GetHex().c_str());
|
||||||
|
memset(&P,0,sizeof(P));
|
||||||
|
if ( playerdata.size() > 0 )
|
||||||
|
{
|
||||||
|
for (i=0; i<playerdata.size(); i++)
|
||||||
|
((uint8_t *)&P)[i] = playerdata[i];
|
||||||
|
}
|
||||||
|
if ( keystrokes != 0 && numkeys != 0 )
|
||||||
|
{
|
||||||
|
if ( makefiles != 0 )
|
||||||
|
{
|
||||||
|
sprintf(fname,"%s.%llu.0",GAMENAME,(long long)seed);
|
||||||
|
if ( (fp= fopen(fname,"wb")) != 0 )
|
||||||
|
{
|
||||||
|
if ( fwrite(keystrokes,1,numkeys,fp) != numkeys )
|
||||||
|
fprintf(stderr,"error writing %s\n",fname);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
sprintf(fname,"%s.%llu.player",GAMENAME,(long long)seed);
|
||||||
|
if ( (fp= fopen(fname,"wb")) != 0 )
|
||||||
|
{
|
||||||
|
if ( fwrite(&playerdata[0],1,(int32_t)playerdata.size(),fp) != playerdata.size() )
|
||||||
|
fprintf(stderr,"error writing %s\n",fname);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//fprintf(stderr,"call replay2\n");
|
||||||
|
num = games_replay2(newplayer,seed,keystrokes,numkeys,playerdata.size()==0?0:&P,0);
|
||||||
|
newdata.resize(num);
|
||||||
|
for (i=0; i<num; i++)
|
||||||
|
{
|
||||||
|
newdata[i] = newplayer[i];
|
||||||
|
((uint8_t *)&endP)[i] = newplayer[i];
|
||||||
|
}
|
||||||
|
//fprintf(stderr,"back replay2 gold.%d\n",endP.gold);
|
||||||
|
if ( endP.gold <= 0 || endP.hitpoints <= 0 || (endP.strength&0xffff) <= 0 || endP.level <= 0 || endP.experience <= 0 || endP.dungeonlevel <= 0 )
|
||||||
|
{
|
||||||
|
sprintf(str,"zero value character was killed -> no playerdata\n");
|
||||||
|
newdata.resize(0);
|
||||||
|
*numkeysp = numkeys;
|
||||||
|
return(keystrokes);
|
||||||
|
/* P.gold = (P.gold * 8) / 10;
|
||||||
|
if ( keystrokes != 0 )
|
||||||
|
{
|
||||||
|
free(keystrokes);
|
||||||
|
keystrokes = 0;
|
||||||
|
*numkeysp = 0;
|
||||||
|
return(keystrokes);
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sprintf(str,"$$$gold.%d hp.%d strength.%d/%d level.%d exp.%d dl.%d",endP.gold,endP.hitpoints,endP.strength&0xffff,endP.strength>>16,endP.level,endP.experience,endP.dungeonlevel);
|
||||||
|
//fprintf(stderr,"%s\n",str);
|
||||||
|
*numkeysp = numkeys;
|
||||||
|
return(keystrokes);
|
||||||
|
}
|
||||||
|
} else num = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr,"extractgame: couldnt find baton keystrokes.%p retval.%d\n",keystrokes,retval);
|
||||||
|
if ( keystrokes != 0 )
|
||||||
|
free(keystrokes), keystrokes = 0;
|
||||||
|
}
|
||||||
|
} else fprintf(stderr,"extractgame: invalid game\n");
|
||||||
|
//fprintf(stderr,"extract %s\n",gametxid.GetHex().c_str());
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
int32_t games_replay2(uint8_t *newdata,uint64_t seed,char *keystrokes,int32_t num,struct games_player *player,int32_t sleepmillis) // replay in daemon
|
int32_t games_replay2(uint8_t *newdata,uint64_t seed,char *keystrokes,int32_t num,struct games_player *player,int32_t sleepmillis) // replay in daemon
|
||||||
{
|
{
|
||||||
return(-1);
|
return(-1);
|
||||||
|
|||||||
@@ -1,51 +1,7 @@
|
|||||||
/******************************************************************************
|
|
||||||
* Copyright © 2014-2019 The SuperNET Developers. *
|
|
||||||
* *
|
|
||||||
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
|
|
||||||
* the top-level directory of this distribution for the individual copyright *
|
|
||||||
* holder information and the developer policies on copyright and licensing. *
|
|
||||||
* *
|
|
||||||
* Unless otherwise agreed in a custom licensing agreement, no part of the *
|
|
||||||
* SuperNET software, including this file may be copied, modified, propagated *
|
|
||||||
* or distributed except according to the terms contained in the LICENSE file *
|
|
||||||
* *
|
|
||||||
* Removal or modification of this copyright notice is prohibited. *
|
|
||||||
* *
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#ifndef H_TETRIS_H
|
#ifndef H_TETRIS_H
|
||||||
#define H_TETRIS_H
|
#define H_TETRIS_H
|
||||||
|
|
||||||
#define GAMENAME "tetris"
|
|
||||||
#define GAMEMAIN tetris
|
|
||||||
#define CHAINNAME "GTEST"
|
|
||||||
|
|
||||||
#define MAXPACK 23
|
|
||||||
struct games_packitem
|
|
||||||
{
|
|
||||||
int32_t type,launch,count,which,hplus,dplus,arm,flags,group;
|
|
||||||
char damage[8],hurldmg[8];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct games_player
|
|
||||||
{
|
|
||||||
int32_t gold,hitpoints,strength,level,experience,packsize,dungeonlevel,amulet;
|
|
||||||
struct games_packitem gamespack[MAXPACK];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct games_state
|
|
||||||
{
|
|
||||||
uint64_t seed;
|
|
||||||
char *keystrokes,*keystrokeshex;
|
|
||||||
uint32_t needflush,replaydone;
|
|
||||||
int32_t numkeys,ind,num,guiflag,counter,sleeptime,playersize,restoring,lastnum;
|
|
||||||
FILE *logfp;
|
|
||||||
struct games_player P;
|
|
||||||
char buffered[10000];
|
|
||||||
uint8_t playerdata[10000];
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
/** https://github.com/brenns10/tetris
|
/** https://github.com/brenns10/tetris
|
||||||
@file main.c
|
@file main.c
|
||||||
@@ -193,5 +149,49 @@ bool tg_check(tetris_game *obj, int row, int col);
|
|||||||
bool tg_tick(tetris_game *obj, tetris_move move);
|
bool tg_tick(tetris_game *obj, tetris_move move);
|
||||||
void tg_print(tetris_game *obj, FILE *f);
|
void tg_print(tetris_game *obj, FILE *f);
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Copyright © 2014-2019 The SuperNET Developers. *
|
||||||
|
* *
|
||||||
|
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
|
||||||
|
* the top-level directory of this distribution for the individual copyright *
|
||||||
|
* holder information and the developer policies on copyright and licensing. *
|
||||||
|
* *
|
||||||
|
* Unless otherwise agreed in a custom licensing agreement, no part of the *
|
||||||
|
* SuperNET software, including this file may be copied, modified, propagated *
|
||||||
|
* or distributed except according to the terms contained in the LICENSE file *
|
||||||
|
* *
|
||||||
|
* Removal or modification of this copyright notice is prohibited. *
|
||||||
|
* *
|
||||||
|
******************************************************************************/
|
||||||
|
#define GAMENAME "tetris"
|
||||||
|
#define GAMEMAIN tetris
|
||||||
|
#define CHAINNAME "GTEST"
|
||||||
|
|
||||||
|
#define MAXPACK 23
|
||||||
|
struct games_packitem
|
||||||
|
{
|
||||||
|
int32_t type,launch,count,which,hplus,dplus,arm,flags,group;
|
||||||
|
char damage[8],hurldmg[8];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct games_player
|
||||||
|
{
|
||||||
|
int32_t gold,hitpoints,strength,level,experience,packsize,dungeonlevel,amulet;
|
||||||
|
struct games_packitem gamespack[MAXPACK];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct games_state
|
||||||
|
{
|
||||||
|
uint64_t seed;
|
||||||
|
char *keystrokes,*keystrokeshex;
|
||||||
|
uint32_t needflush,replaydone;
|
||||||
|
int32_t numkeys,ind,num,guiflag,counter,sleeptime,playersize,restoring,lastnum;
|
||||||
|
FILE *logfp;
|
||||||
|
struct games_player P;
|
||||||
|
char buffered[10000];
|
||||||
|
uint8_t playerdata[10000];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user