player * WOPR_addPlayer( game *aGame, char *name ) { player *aPlayer; aPlayer = allocStruct( player ); setName( aPlayer, name ); aPlayer->pswd = strdup( "none" ); aPlayer->addr = strdup( "none" ); aPlayer->realName = strdup( "none" ); aPlayer->pswdstate = 1; aPlayer->drive = 1; aPlayer->weapons = 1; aPlayer->shields = 1; aPlayer->cargo = 1; aPlayer->msize = aGame->galaxysize; aPlayer->flags = F_ANONYMOUS | F_AUTOUNLOAD | F_PRODTABLE | F_SORTGROUPS | F_GROUPFORECAST | F_PLANETFORECAST | F_SHIPTYPEFORECAST | F_ROUTESFORECAST | F_BATTLEPROTOCOL; /* Should check if the player exists already... */ addList( &( aGame->players ), aPlayer ); return aPlayer; }
void b_order( game *aGame, player *P, strlist **s ) { group *g; /* pointer to named group */ group *g2; /* pointer to new group */ int i; /* int value for number of ships */ char *ns; /* char value for number of ships */ DBUG_ENTER( "b_order" ); /* find the named group */ g = findgroup( P, getstr( 0 ) ); if ( !g ) { mistake( P, INFO, *s, "Group not recognized." ); DBUG_VOID_RETURN; } /* check to see if we're detaching from a fleet */ ns = getstr( 0 ); if ( noCaseStrncmp( ns, "fleet", 5 ) == 0 ) { if ( !epsilon( g->dist, 0.0, 0.0001 ) ) { mistake( P, WARNING, *s, "Fleet is in hyperspace." ); DBUG_VOID_RETURN; } g->thefleet = 0; DBUG_VOID_RETURN; } /* are there enough ships to detach? */ i = atoi( ns ); if ( i > g->ships ) { /* FS Dec 1998 */ mistake( P, INFO, *s, "Not enough ships in group." ); DBUG_VOID_RETURN; } /* this was an odd problem */ if ( i < 0 ) { /* KDW July 1999 */ mistake( P, WARNING, *s, "Can't have negative number of ships." ); DBUG_VOID_RETURN; } /* create a new group for what's being broken off and add it to * the list of groups the player owns */ g2 = allocStruct( group ); *g2 = *g; g2->ships = i; g->ships -= i; g2->thefleet = 0; g2->next = NULL; numberGroup( P, g2 ); g2->name = ( char * ) malloc( 8 ); sprintf( g2->name, "%d", g2->number ); addList( &P->groups, g2 ); DBUG_VOID_RETURN; }
void wopr_allies( game *aGame, char *playerName1, char *playerName2 ) { player *aPlayer1; player *aPlayer2; alliance *a; aPlayer1 = findElement( player, aGame->players, playerName1 ); aPlayer2 = findElement( player, aGame->players, playerName2 ); a = allocStruct( alliance ); a->who = aPlayer1; addList( &aPlayer2->allies, a ); a = allocStruct( alliance ); a->who = aPlayer2; addList( &aPlayer1->allies, a ); }
void x_order( game *aGame, player *P, strlist **s ) { group *g; group *g2; int i; int j; DBUG_ENTER( "x_order" ); g = findgroup( P, getstr( 0 ) ); if ( !g ) { mistake( P, ERROR, *s, "Group not recognized." ); DBUG_VOID_RETURN; } if ( g->dist > 0.0 ) { mistake( P, ERROR, *s, "Group is in hyperspace." ); DBUG_VOID_RETURN; } if ( g->loadtype == CG_COL && g->where->owner && g->where->owner != P ) { mistake( P, ERROR, *s, "Can't unload colonists onto an alien planet." ); DBUG_VOID_RETURN; } i = g->ships; if ( ( j = atoi( getstr( 0 ) ) ) != 0 ) { i = j; if ( i > g->ships ) { mistake( P, ERROR, *s, "Not enough ships, all available used." ); i = g->ships; } if ( i <= 0 ) { /* KDW July 1999 */ mistake( P, ERROR, *s, "You must specify more than 0 ships, all available used." ); i = g->ships; } if ( i != g->ships ) { g2 = allocStruct( group ); *g2 = *g; g2->next = NULL; numberGroup( P, g2 ); g2->name = ( char * ) malloc( 8 ); sprintf( g2->name, "%d", g2->number ); addList( &P->groups, g2 ); g->ships -= i; g2->ships = i; g = g2; } } unloadgroup( g, P, g->load ); g->where->mat += shipmass( g ) * g->ships; g->ships = 0; DBUG_VOID_RETURN; }
game * WOPR_createGame( ) { game *aGame; planet *aPlanet; aGame = allocStruct( game ); if ( aGame != NULL ) { setName( aGame, "wopr" ); aGame->turn = 0; aGame->galaxysize = 30.0; aPlanet = addplanet( aGame ); setName( aPlanet, "BattleGround" ); } else { return NULL; } return aGame; }
void fightphase(game *aGame, int phase) { planet *p; pdebug(DFULL, "fightphase\n"); plog(LFULL, "Checking for battles\n"); for (p = aGame->planets; p; p = p->next) { battle *b; if ((b = isBattle(aGame->players, p))) { batstat *batstats; batstat *aBatstat; participant *part; plog(LFULL, "Battle at %s\n", p->name); pdebug(DFULL2, "Battle at %s\n", p->name); b->phase = phase; batstats = NULL; for (part = b->participants; part; part = part->next) { aBatstat = allocStruct(batstat); aBatstat->who = part->who; aBatstat->groups = part->groups; addList(&batstats, aBatstat); } allocEnemies(batstats); doBattle(batstats, b->protocol, phase); addList(&(aGame->battles), b); updateGroups(b); for (aBatstat = batstats; aBatstat; aBatstat = aBatstat->next) { free(aBatstat->enemies); } freelist(batstats); } } }
Scene* Scene_create(GameEngine* engine, Scene* lastScene) { Scene* this = allocStruct(Scene); this->engine = engine; SDL_Point screenSize; SDL_RenderGetLogicalSize(engine->renderer, &screenSize.x, &screenSize.y); this->camera = Camera_create(screenSize.x, screenSize.y); this->walkableBounds.x = 0; this->walkableBounds.w = 0; this->walkableBounds.y = 295 * PHYSICS_SCALE; this->walkableBounds.h = 185 * PHYSICS_SCALE; this->entities = Vector_Create(); this->triggers = Vector_Create(); this->bodies = Vector_Create(); this->shapes = Vector_Create(); this->sky = Sky_create(engine->textureCache); withSprintf(path, "images/%s/bg.png", anyArgs("red"), { SDL_Texture* texture = TextureCache_getForUnconstantPath(engine->textureCache, path); this->background = Sprite_create(texture); });
battle * isBattle(player *players, planet *p) { player *side; participant *participants; battle *aBattle; pdebug(DFULL, "isBattle\n"); participants = NULL; aBattle = NULL; for (side = players; side; side = side->next) { group *aGroup; for (aGroup = side->groups; aGroup; aGroup = aGroup->next) { if (aGroup->location == p) { participant *aParticipant; aParticipant = allocStruct(participant); assert(aParticipant != NULL); aParticipant->who = side; aParticipant->groups = NULL; addList(&participants, aParticipant); break; } } } if (numberOfElements(participants) >= 2) { participant *aParticipant; for (aParticipant = participants; aParticipant; aParticipant = aParticipant->next) { group *aGroup; for (aGroup = aParticipant->who->groups; aGroup; aGroup = aGroup->next) { if (aGroup->location == p) { group *newGroup; newGroup = allocStruct(group); assert(newGroup != NULL); *newGroup = *aGroup; assert(newGroup->ships == aGroup->ships); newGroup->left = aGroup->ships; newGroup->next = NULL; newGroup->name = (char*)malloc(8); sprintf(newGroup->name, "%d", newGroup->number); addList(&(aParticipant->groups), newGroup); } } } if (mustBattle(participants)) { participant *aParticipant; aBattle = allocStruct(battle); assert(aBattle != NULL); aBattle->participants = participants; aBattle->where = p; aBattle->protocol = allocProtocol(); for (aParticipant = participants; aParticipant; aParticipant = aParticipant->next) { group *aGroup; for (aGroup = aParticipant->groups; aGroup; aGroup = aGroup->next) { aGroup->canshoot = alloc(sizeof(int) * (aGroup->ships)); aGroup->alive = alloc(sizeof(int) * (aGroup->ships)); assert(aGroup->canshoot); assert(aGroup->alive); } } } } if (aBattle == NULL) { participant *r, *r2; pdebug(DFULL2, "isBattle : freeing participants.\n"); r = participants; while (r) { freelist(r->groups); r2 = r->next; free(r); r = r2; } } return aBattle; }
int parse_group( game *aGame, player *aPlayer ) { group *aGroup; char *typeName; char *par; shiptype *aShiptype; aGroup = allocStruct( group ); par = getstr( lineBuffer ); if ( *par ) { aGroup->ships = atoi( par ); } else { fprintf( stderr, "Number of ships not specified\n" ); } typeName = getstr( NULL ); if ( !typeName[0] ) { fprintf( stderr, "No ship type name given.\n" ); return FALSE; } if ( ( aShiptype = findElement( shiptype, aPlayer->shiptypes, typeName ) ) == NULL ) { fprintf( stderr, "Ship type name not found\n" ); return FALSE; } aGroup->type = aShiptype; par = getstr( 0 ); if ( *par ) { aGroup->drive = atof( par ); } else { fprintf( stderr, "No drive tech specified\n" ); } par = getstr( 0 ); if ( *par ) { aGroup->weapons = atof( par ); } else { fprintf( stderr, "No drive tech specified\n" ); } par = getstr( 0 ); if ( *par ) { aGroup->shields = atof( par ); } else { fprintf( stderr, "No shield tech specified\n" ); } par = getstr( 0 ); if ( *par ) { aGroup->cargo = atof( par ); } else { fprintf( stderr, "No shield tech specified\n" ); } cktech( aGroup ); aGroup->loadtype = CG_EMPTY; aGroup->from = aGroup->where = findPlanet( aGame, "BattleGround" ); aGroup->dist = 0; aGroup->thefleet = NULL; aGroup->defense = 0.0; aGroup->attack = 0.0; aGroup->flags = 0; aGroup->location = findPlanet( aGame, "BattleGround" ); numberGroup( aPlayer, aGroup ); aGroup->name = ( char * ) malloc( 8 ); sprintf( aGroup->name, "%d", aGroup->number ); addList( &aPlayer->groups, aGroup ); return TRUE; }
int WOPR_parse_type( game *aGame, player *aPlayer ) { char *typeName; char *par; int underDefined; shiptype *aShiptype; typeName = getstr( lineBuffer ); if ( !typeName[0] ) { fprintf( stderr, "No ship type name given.\n" ); return FALSE; } if ( findElement( shiptype, aPlayer->shiptypes, typeName ) != NULL ) { fprintf( stderr, "Ship type name already in use.\n" ); return FALSE; } aShiptype = allocStruct( shiptype ); setName( aShiptype, typeName ); aShiptype->drive = atof( getstr( 0 ) ); if ( aShiptype->drive < 1 ) aShiptype->drive = 0; underDefined = FALSE; par = getstr( 0 ); if ( *par ) { aShiptype->attacks = atoi( par ); } else { underDefined = TRUE; aShiptype->attacks = 0; } par = getstr( 0 ); if ( *par ) { aShiptype->weapons = atof( par ); } else { underDefined = TRUE; aShiptype->weapons = 0.0; } if ( aShiptype->attacks && aShiptype->weapons < 1 ) { fprintf( stderr, "Warning: gun size should be greater or equal to 1.\n" ); aShiptype->attacks = 0; } if ( !aShiptype->attacks ) aShiptype->weapons = 0; par = getstr( 0 ); if ( *par ) { aShiptype->shields = atof( par ); } else { underDefined = TRUE; aShiptype->shields = 0.0; } if ( aShiptype->shields && aShiptype->shields < 1 ) { aShiptype->shields = 0; fprintf( stderr, "Warning: shield size should be zero, or greater or equal to 1.\n" ); } par = getstr( 0 ); if ( *par ) { aShiptype->cargo = atof( par ); } else { underDefined = TRUE; aShiptype->cargo = 0.0; } if ( aShiptype->cargo && aShiptype->cargo < 1 ) { aShiptype->cargo = 0; fprintf( stderr, "Warning: cargo size should be zero, or greater or equal to 1.\n" ); } if ( !aShiptype->drive && !aShiptype->attacks && !aShiptype->shields && !aShiptype->cargo ) { fprintf( stderr, "At least one component must be non-zero.\n" ); return FALSE; } if ( underDefined ) { fprintf( stderr, "Warning: A ship design requires 5 parameters.\n" ); } addList( &aPlayer->shiptypes, aShiptype ); return TRUE; }