Пример #1
0
/**
 * Initializes the game world with the attributes provided.
 **/
void CWorld::RegisterInit(const CStrW& mapFile, int playerID)
{
	// Load the map, if one was specified
	if (mapFile.length())
	{
		VfsPath mapfilename(VfsPath("maps/scenarios") / (mapFile + L".pmp"));
		CMapReader* reader = 0;

		try
		{
			reader = new CMapReader;
			CTriggerManager* pTriggerManager = NULL;
			reader->LoadMap(mapfilename, m_Terrain,
				CRenderer::IsInitialised() ? g_Renderer.GetWaterManager() : NULL,
				CRenderer::IsInitialised() ? g_Renderer.GetSkyManager() : NULL,
				&g_LightEnv, m_pGame->GetView(),
				m_pGame->GetView() ? m_pGame->GetView()->GetCinema() : NULL,
				pTriggerManager, CRenderer::IsInitialised() ? &g_Renderer.GetPostprocManager() : NULL,
				m_pGame->GetSimulation2(), &m_pGame->GetSimulation2()->GetSimContext(), playerID, false);
				// fails immediately, or registers for delay loading
		}
		catch (PSERROR_File& err)
		{
			delete reader;
			LOGERROR(L"Failed to load scenario %ls: %hs", mapfilename.string().c_str(), err.what());
			throw PSERROR_Game_World_MapLoadFailed("Failed to load scenario.\nCheck application log for details.");
		}
	}
}
Пример #2
0
    bool Station::updateData(bool force){
        bool result = false;
        std::string command;
        /* To do */
        /* Check connection and if force true update connection */
        force = false;
        /* Weather Forecast */
        if (Downloader::downloadData(this->fileName()+".orig", this->forecastURL(), this->cookie())) {
            result = true;
        }else{
            std::cerr<<"ERROR downloading  "<<this->forecastURL()<<std::endl;
            result = false;
        }
        if ((result) && (this->detailURL() != "") && (Downloader::downloadData(this->fileName()+".detail.orig", this->detailURL(), this->cookie()))){
            command = this->converter()+ " " +  this->fileName() + ".orig " + this->fileName()+" " + this->fileName()+".detail.orig";
            std::cerr<<" EXEC "<<command<<std::endl;
            if (system(command.c_str()) == 0)
                result = true;
            else
               result = false;
        }else{
            command = this->converter()+ " " +  this->fileName() + ".orig " + this->fileName();
            std::cerr<<" EXEC "<<command<<std::endl;
            if (system(command.c_str()) == 0)
                result = true;
            else
               result = false;
        }
        /* Map */
        if (this->mapURL() != ""){
            std::string mapfilename(Core::AbstractConfig::getCachePath());
            char map_url[4096];
            char map_url_additional[4096];
            struct stat attrib;
            mapfilename += this->sourceName().c_str();
            mapfilename += "_";
            mapfilename += _id->c_str();
            mapfilename += "_map_";
            mapfilename += "%i.png";
            snprintf(map_url, sizeof(map_url)-1, mapfilename.c_str(), 0);
            std::cerr<<map_url<<std::endl;
            /* Check modification time of the last file and download map if it more than 3 hours */
            if ((stat(map_url, &attrib) == 0) &&
                (time(NULL) - attrib.st_mtime > 3600*2.5)&&
                (attrib.st_size > 0)){
                for (int i=4; i>=0; i--){
                    snprintf(map_url, sizeof(map_url)-1, mapfilename.c_str(), i);
                    std::cerr<<map_url<<std::endl;
                    if (stat(map_url, &attrib) == 0){
                        snprintf(map_url_additional, sizeof(map_url_additional)-1,
                                 mapfilename.c_str(), i+1);
                        rename(map_url, map_url_additional);
                    }
                }
            }

            /* snprintf(map_url, sizeof(map_url)-1, mapfilename.c_str(), 0); */
            std::cerr<<map_url<<std::endl;
            if ((stat(map_url, &attrib) != 0)||
                (attrib.st_size == 0) ||
                (stat(map_url, &attrib) == 0) &&
                (time(NULL) - attrib.st_mtime > 3600*2.5)){
                std::cerr<<map_url<<" "<<attrib.st_mtime<< " "<<time(NULL)<< std::endl;
                Downloader::downloadData(map_url, this->mapURL(), "");
            }
            
            
        }

        return result;
    }
Пример #3
0
void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationCommand>& commands)
{
    PROFILE3("sim update");
    PROFILE2_ATTR("turn %d", (int)m_TurnNumber);

    fixed turnLengthFixed = fixed::FromInt(turnLength) / 1000;

    /*
     * In serialization test mode, we save the original (primary) simulation state before each turn update.
     * We run the update, then load the saved state into a secondary context.
     * We serialize that again and compare to the original serialization (to check that
     * serialize->deserialize->serialize is equivalent to serialize).
     * Then we run the update on the secondary context, and check that its new serialized
     * state matches the primary context after the update (to check that the simulation doesn't depend
     * on anything that's not serialized).
     */

    const bool serializationTestDebugDump = false; // set true to save human-readable state dumps before an error is detected, for debugging (but slow)
    const bool serializationTestHash = true; // set true to save and compare hash of state

    SerializationTestState primaryStateBefore;
    if (m_EnableSerializationTest)
    {
        ENSURE(m_ComponentManager.SerializeState(primaryStateBefore.state));
        if (serializationTestDebugDump)
            ENSURE(m_ComponentManager.DumpDebugState(primaryStateBefore.debug, false));
        if (serializationTestHash)
            ENSURE(m_ComponentManager.ComputeStateHash(primaryStateBefore.hash, false));
    }


    UpdateComponents(m_SimContext, turnLengthFixed, commands);


    if (m_EnableSerializationTest)
    {
        // Initialise the secondary simulation
        CTerrain secondaryTerrain;
        CSimContext secondaryContext;
        secondaryContext.m_Terrain = &secondaryTerrain;
        CComponentManager secondaryComponentManager(secondaryContext);
        secondaryComponentManager.LoadComponentTypes();
        ENSURE(LoadDefaultScripts(secondaryComponentManager, NULL));
        ResetComponentState(secondaryComponentManager, false, false);

        // Load the map into the secondary simulation

        LDR_BeginRegistering();
        CMapReader* mapReader = new CMapReader; // automatically deletes itself

        // TODO: this duplicates CWorld::RegisterInit and could probably be cleaned up a bit
        std::string mapType;
        m_ComponentManager.GetScriptInterface().GetProperty(m_InitAttributes.get(), "mapType", mapType);
        if (mapType == "scenario")
        {
            // Load scenario attributes
            std::wstring mapFile;
            m_ComponentManager.GetScriptInterface().GetProperty(m_InitAttributes.get(), "map", mapFile);

            VfsPath mapfilename(VfsPath("maps/scenarios") / (mapFile + L".pmp"));
            mapReader->LoadMap(mapfilename, &secondaryTerrain, NULL, NULL, NULL, NULL, NULL, NULL,
                               NULL, &secondaryContext, INVALID_PLAYER, true); // throws exception on failure
        }
        else
        {
            // TODO: support random map scripts
            debug_warn(L"Serialization test mode only supports scenarios");
        }

        LDR_EndRegistering();
        ENSURE(LDR_NonprogressiveLoad() == INFO::OK);

        ENSURE(secondaryComponentManager.DeserializeState(primaryStateBefore.state));

        SerializationTestState secondaryStateBefore;
        ENSURE(secondaryComponentManager.SerializeState(secondaryStateBefore.state));
        if (serializationTestDebugDump)
            ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateBefore.debug, false));
        if (serializationTestHash)
            ENSURE(secondaryComponentManager.ComputeStateHash(secondaryStateBefore.hash, false));

        if (primaryStateBefore.state.str() != secondaryStateBefore.state.str() ||
                primaryStateBefore.hash != secondaryStateBefore.hash)
        {
            ReportSerializationFailure(&primaryStateBefore, NULL, &secondaryStateBefore, NULL);
        }

        SerializationTestState primaryStateAfter;
        ENSURE(m_ComponentManager.SerializeState(primaryStateAfter.state));
        if (serializationTestHash)
            ENSURE(m_ComponentManager.ComputeStateHash(primaryStateAfter.hash, false));

        UpdateComponents(secondaryContext, turnLengthFixed,
                         CloneCommandsFromOtherContext(m_ComponentManager.GetScriptInterface(), secondaryComponentManager.GetScriptInterface(), commands));

        SerializationTestState secondaryStateAfter;
        ENSURE(secondaryComponentManager.SerializeState(secondaryStateAfter.state));
        if (serializationTestHash)
            ENSURE(secondaryComponentManager.ComputeStateHash(secondaryStateAfter.hash, false));

        if (primaryStateAfter.state.str() != secondaryStateAfter.state.str() ||
                primaryStateAfter.hash != secondaryStateAfter.hash)
        {
            // Only do the (slow) dumping now we know we're going to need to report it
            ENSURE(m_ComponentManager.DumpDebugState(primaryStateAfter.debug, false));
            ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateAfter.debug, false));

            ReportSerializationFailure(&primaryStateBefore, &primaryStateAfter, &secondaryStateBefore, &secondaryStateAfter);
        }
    }

//	if (m_TurnNumber == 0)
//		m_ComponentManager.GetScriptInterface().DumpHeap();

    // Run the GC occasionally
    // (TODO: we ought to schedule this for a frame where we're not
    // running the sim update, to spread the load)
    if (m_TurnNumber % 10 == 0)
        m_ComponentManager.GetScriptInterface().MaybeGC();

    if (m_EnableOOSLog)
        DumpState();

    // Start computing AI for the next turn
    CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY);
    if (cmpAIManager)
        cmpAIManager->StartComputation();

    ++m_TurnNumber;
}