void UnitInterface::spawnPlayerUnits(const iXY &location,
                                     Uint16 player_id,
                                     const PlayerUnitConfig &unit_config)
{
    iXY next_loc;
    UnitBase *unit;
    unsigned long unit_type_index;
    unsigned long unit_spawn_count;
    unsigned long unit_spawn_index;

    NetMessageEncoder encoder;

    unit_placement_matrix.reset( location );

    for ( unit_type_index = 0; unit_type_index < UnitProfileInterface::getNumUnitTypes(); unit_type_index++ ) {

        unit_spawn_count = unit_config.getSpawnUnitCount( unit_type_index );
        for ( unit_spawn_index = 0; unit_spawn_index < unit_spawn_count; unit_spawn_index++ ) {
            unit_placement_matrix.getNextEmptyLoc( &next_loc );
            unit = createUnit(unit_type_index, next_loc, player_id);

            assert(unit != 0);
            UnitRemoteCreate create_mesg(unit->player->getID(), unit->id,
                    next_loc.x, next_loc.y, unit->unit_state.unit_type);
            encoder.encodeMessage(&create_mesg, sizeof(create_mesg));
        } // ** for unit_spawn_index
    } // ** for unit_type_index

    encoder.sendEncodedMessage();
}
Ejemplo n.º 2
0
    bool sendNextUnits()
    {
        NetMessageEncoder encoder;
        UnitInterface::Units::const_iterator i;
        iXY unit_map_loc;
        UnitBase* unit;

        do
        {
            i = UnitInterface::getUnits().lower_bound(sync_count);
            if ( i == UnitInterface::getUnits().end() || i->first >= sync_end )
            {
                sync_count = sync_end;
                sync_done = sync_total;
                break;
            }

            unit = i->second;
            MapInterface::pointXYtoMapXY(unit->unit_state.location, unit_map_loc);
            UnitRemoteCreate urc(unit->player_id,
                                 unit->id,
                                 unit_map_loc.x,
                                 unit_map_loc.y,
                                 unit->unit_state.unit_type);

            if ( ! encoder.encodeMessage(&urc, sizeof(urc)) )
            {
                break;
            }

            sync_done += 1;
            sync_count = i->first + 1;

        } while ( sync_count < sync_end );

        connect_client->sendMessage(encoder.getEncodedMessage(),
                                    encoder.getEncodedLen());

        if ( sync_count < sync_end )
        {
            if ( sendunitpercent_timer.count() )
            {
                ConnectProcessStateMessage state_mesg;
                state_mesg.setMessageEnum(_connect_state_message_sync_units_percent);
                state_mesg.setPercentComplete((sync_done*100)/sync_total);
                connect_client->sendMessage( &state_mesg, sizeof(state_mesg));
                sendunitpercent_timer.reset();
            }
        }

        return sync_count == sync_end;
    }
void UnitInterface::spawnPlayerUnits(const iXY &location,
                                     PlayerID player_id,
                                     const PlayerUnitConfig &unit_config)
{
    iXY next_loc;
    UnitBase *unit;
    unsigned long unit_type_index;
    unsigned long unit_spawn_count;
    unsigned long unit_spawn_index;

    NetMessageEncoder encoder;

    unit_placement_matrix.reset( location );

    for ( unit_type_index = 0; unit_type_index < UnitProfileInterface::getNumUnitTypes(); unit_type_index++ )
    {

        unit_spawn_count = unit_config.getSpawnUnitCount( unit_type_index );
        for ( unit_spawn_index = 0; unit_spawn_index < unit_spawn_count; unit_spawn_index++ )
        {
            unit_placement_matrix.getNextEmptyLoc( &next_loc );
            unit = createUnit(unit_type_index, next_loc, player_id);
            UnitRemoteCreate create_mesg(unit->player->getID(), unit->id,
                    next_loc.x, next_loc.y, unit->unit_state.unit_type);

            if ( !encoder.encodeMessage(&create_mesg, sizeof(create_mesg)) )
            {
                LOGGER.info("UnitInterface encoder full, sending and resetting");
                SERVER->broadcastMessage(encoder.getEncodedMessage(),
                                         encoder.getEncodedLen());
                encoder.resetEncoder();
                encoder.encodeMessage(&create_mesg, sizeof(create_mesg));
            }
        } // ** for unit_spawn_index
    } // ** for unit_type_index

    if ( ! encoder.isEmpty() )
    {
        LOGGER.info("UnitInterface sending remaining");
        SERVER->broadcastMessage(encoder.getEncodedMessage(),
                                 encoder.getEncodedLen());
    }
}
Ejemplo n.º 4
0
    bool sendNextPlayers()
    {
        NetMessageEncoder encoder;
        PlayerStateSync pss;

        do
        {
            PlayerInterface::getPlayer(sync_count)->getNetworkPlayerState(pss.player_state);
            if ( ! encoder.encodeMessage(&pss, sizeof(pss)) )
            {
                break;
            }
        } while ( ++sync_count < sync_end );

        connect_client->sendMessage(encoder.getEncodedMessage(),
                                    encoder.getEncodedLen());

        sendStatePercent();

        connect_client->sendRemaining();

        return sync_count == sync_end;
    }
void
ObjectiveInterface::syncObjectives( ClientSocket * client )
{
    NetMessageEncoder encoder;
    ObjectiveSyncMesg msg;

    for ( int i = 0; i < num_objectives; ++i )
    {
        msg.set(i);
        objective_list[i]->getSyncData( msg.sync_data );

        if ( ! encoder.encodeMessage(&msg, sizeof(msg)) )
        {
            client->sendMessage(encoder.getEncodedMessage(),
                                encoder.getEncodedLen());
            encoder.resetEncoder();
            encoder.encodeMessage(&msg, sizeof(msg)); // this time shuold be good
        }
    }

    client->sendMessage(encoder.getEncodedMessage(),
                        encoder.getEncodedLen());
}