Ticket #118: variations.2.diff

File variations.2.diff, 12.0 KB (added by trompetin17, 9 years ago)
  • source/graphics/MapReader.cpp

    diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp
    index ed57c2c..68d052f 100644
    a b  
    1 /* Copyright (C) 2013 Wildfire Games.
     1/* Copyright (C) 2015 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    1919
    2020#include "MapReader.h"
    2121
     22#include <boost/algorithm/string/split.hpp>
     23#include <boost/algorithm/string/classification.hpp>
    2224#include "graphics/Camera.h"
    2325#include "graphics/CinemaTrack.h"
    2426#include "graphics/Entity.h"
     
    2830#include "graphics/Terrain.h"
    2931#include "graphics/TerrainTextureEntry.h"
    3032#include "graphics/TerrainTextureManager.h"
     33#include "graphics/Unit.h"
    3134#include "lib/timer.h"
    3235#include "lib/external_libraries/libsdl.h"
    3336#include "maths/MathUtil.h"
    CMapReader::CMapReader()  
    6063    // Maps that don't override the default probably want the old lighting model
    6164    //m_LightEnv.SetLightingModel("old");
    6265    //pPostproc->SetPostEffect(L"default");
    63    
     66
    6467}
    6568
    6669// LoadMap: try to load the map from given file; reinitialise the scene to new data if successful
    void CMapReader::LoadMap(const VfsPath& pathname, JSRuntime* rt, JS::HandleValu  
    112115    // delete all existing entities
    113116    if (pSimulation2)
    114117        pSimulation2->ResetState();
    115    
     118
    116119    // reset post effects
    117120    if (pPostproc)
    118121        pPostproc->SetPostEffect(L"default");
    int CMapReader::ApplyTerrainData()  
    296299
    297300                        mp.Tex = m_TerrainTextures[tileptr->m_Tex1Index];
    298301                        mp.Priority = tileptr->m_Priority;
    299    
     302
    300303                        tileptr++;
    301304                    }
    302305                }
    int CMapReader::ApplyData()  
    324327    {
    325328        // Default to global camera (with constraints)
    326329        pGameView->ResetCameraTarget(pGameView->GetCamera()->GetFocus());
    327    
     330
    328331        // TODO: Starting rotation?
    329332        CmpPtr<ICmpPlayer> cmpPlayer(*pSimContext, cmpPlayerManager->GetPlayerByID(m_PlayerID));
    330333        if (cmpPlayer && cmpPlayer->HasStartingCamera())
    void CMapSummaryReader::GetMapSettings(ScriptInterface& scriptInterface, JS::Mut  
    388391{
    389392    JSContext* cx = scriptInterface.GetContext();
    390393    JSAutoRequest rq(cx);
    391    
     394
    392395    scriptInterface.Eval("({})", ret);
    393396    if (m_ScriptSettings.empty())
    394397        return;
    private:  
    439442    int at_angle;
    440443    int at_uid;
    441444    int at_seed;
     445    int at_variation;
    442446
    443447    XMBElementList nodes; // children of root
    444448
    void CXMLReader::Init(const VfsPath& xml_filename)  
    490494    AT(angle);
    491495    AT(uid);
    492496    AT(seed);
     497    AT(variation);
    493498#undef AT
    494499#undef EL
    495500
    void CXMLReader::ReadEnvironment(XMBElement parent)  
    740745                    // graphics are disabled
    741746                    if (!m_MapReader.pWaterMan)
    742747                        continue;
    743                    
     748
    744749                    if (element_name == el_type)
    745750                    {
    746751                        if (waterelement.GetText() == "default")
    void CXMLReader::ReadCinema(XMBElement parent)  
    863868
    864869#undef EL
    865870#undef AT
    866    
     871
    867872    std::map<CStrW, CCinemaPath> pathList;
    868873    XERO_ITER_EL(parent, element)
    869874    {
    870875        int elementName = element.GetNodeName();
    871            
     876
    872877        if ( elementName == el_path )
    873878        {
    874879            XMBAttributeList attrs = element.GetAttributes();
    void CXMLReader::ReadCinema(XMBElement parent)  
    891896                        pathData.m_Growth = attrs.GetNamedItem(at_growth).ToInt();
    892897                        pathData.m_Switch = attrs.GetNamedItem(at_switch).ToInt();
    893898                }
    894                
     899
    895900                //Load node data used for spline
    896901                else if ( elementName == el_node )
    897902                {
    void CXMLReader::ReadCinema(XMBElement parent)  
    900905                    {
    901906                        elementName = nodeChild.GetNodeName();
    902907                        attrs = nodeChild.GetAttributes();
    903                        
     908
    904909                        //Fix?:  assumes that time is last element
    905910                        if ( elementName == el_position )
    906911                        {
    void CXMLReader::ReadCinema(XMBElement parent)  
    920925                            data.Distance = nodeChild.GetText().ToFloat();
    921926                        else
    922927                            debug_warn(L"Invalid cinematic element for node child");
    923                    
     928
    924929                        backwardSpline.AddNode(data.Position, data.Rotation, data.Distance);
    925930                    }
    926931                }
    927932                else
    928933                    debug_warn(L"Invalid cinematic element for path child");
    929                
    930                
     934
     935
    931936            }
    932937
    933938            //Construct cinema path with data gathered
    void CXMLReader::ReadCinema(XMBElement parent)  
    938943                debug_warn(L"Failure loading cinematics");
    939944                return;
    940945            }
    941                    
     946
    942947            for ( std::vector<SplineData>::const_reverse_iterator it = nodes.rbegin();
    943948                                                            it != nodes.rend(); ++it )
    944949            {
    945950                spline.AddNode(it->Position, it->Rotation, it->Distance);
    946951            }
    947                
     952
    948953            CCinemaPath path(pathData, spline);
    949             pathList[name] = path; 
     954            pathList[name] = path;
    950955        }
    951956        else
    952957            ENSURE("Invalid cinema child");
    int CXMLReader::ReadEntities(XMBElement parent, double end_time)  
    986991        CFixedVector3D Position;
    987992        CFixedVector3D Orientation;
    988993        long Seed = -1;
     994        std::set<CStr> actorSelections;
    989995
    990996        // Obstruction control groups.
    991997        entity_id_t ControlGroup = INVALID_ENTITY;
    int CXMLReader::ReadEntities(XMBElement parent, double end_time)  
    10351041            else if (element_name == el_actor)
    10361042            {
    10371043                XMBAttributeList attrs = setting.GetAttributes();
    1038                 CStr seedStr = attrs.GetNamedItem(at_seed);
    1039                 if (!seedStr.empty())
     1044                CStr variationStr = attrs.GetNamedItem(at_variation);
     1045
     1046                if (!variationStr.empty())
    10401047                {
    1041                     Seed = seedStr.ToLong();
    1042                     ENSURE(Seed >= 0);
     1048                    boost::algorithm::split(actorSelections, variationStr, boost::is_any_of("|"));
     1049                }
     1050                else
     1051                {
     1052                    CStr seedStr = attrs.GetNamedItem(at_seed);
     1053                    if (!seedStr.empty())
     1054                    {
     1055                        Seed = seedStr.ToLong();
     1056                        ENSURE(Seed >= 0);
     1057                    }
    10431058                }
    10441059            }
    10451060            else
    int CXMLReader::ReadEntities(XMBElement parent, double end_time)  
    10801095            CmpPtr<ICmpVisual> cmpVisual(sim, ent);
    10811096            if (cmpVisual)
    10821097            {
    1083                 if (Seed != -1)
     1098                if (!actorSelections.empty())
     1099                {
     1100                    CUnit* unit = cmpVisual->GetUnit();
     1101                    if (unit)
     1102                        unit->SetActorSelections(actorSelections);
     1103                }
     1104                else if (Seed != -1)
    10841105                    cmpVisual->SetActorSeed((u32)Seed);
    1085                 // TODO: variation/selection strings
    10861106            }
    10871107
    10881108            if (PlayerID == m_MapReader.m_PlayerID && (boost::algorithm::ends_with(TemplateName, L"civil_centre") || m_MapReader.m_StartingCameraTarget == INVALID_ENTITY))
    int CMapReader::GenerateMap()  
    12601280{
    12611281    JSContext* cx = pSimulation2->GetScriptInterface().GetContext();
    12621282    JSAutoRequest rq(cx);
    1263    
     1283
    12641284    if (!m_MapGen)
    12651285    {
    12661286        // Initialize map generator
    12671287        m_MapGen = new CMapGenerator();
    12681288
    12691289        VfsPath scriptPath;
    1270        
     1290
    12711291        if (m_ScriptFile.length())
    12721292            scriptPath = L"maps/random/"+m_ScriptFile;
    12731293
    12741294        // Stringify settings to pass across threads
    12751295        std::string scriptSettings = pSimulation2->GetScriptInterface().StringifyJSON(&m_ScriptSettings.get());
    1276        
     1296
    12771297        // Try to generate map
    12781298        m_MapGen->GenerateMap(scriptPath, scriptSettings);
    12791299    }
    int CMapReader::GenerateMap()  
    12931313        // Parse data into simulation context
    12941314        JS::RootedValue data(cx);
    12951315        pSimulation2->GetScriptInterface().ReadStructuredClone(results, &data);
    1296        
     1316
    12971317        if (data.isUndefined())
    12981318        {
    12991319            // RMS failed - return to main menu
    int CMapReader::GenerateMap()  
    13121332        // to allow more CPU for the map generator thread
    13131333        SDL_Delay(100);
    13141334    }
    1315    
     1335
    13161336    // return progress
    13171337    return progress;
    13181338};
    int CMapReader::ParseTerrain()  
    13231343    TIMER(L"ParseTerrain");
    13241344    JSContext* cx = pSimulation2->GetScriptInterface().GetContext();
    13251345    JSAutoRequest rq(cx);
    1326    
     1346
    13271347    // parse terrain from map data
    13281348    //  an error here should stop the loading process
    13291349#define GET_TERRAIN_PROPERTY(val, prop, out)\
    int CMapReader::ParseTerrain()  
    13761396        {
    13771397            size_t patchY = y / PATCH_SIZE;
    13781398            size_t offY = y % PATCH_SIZE;
    1379            
     1399
    13801400            STileDesc tile;
    13811401            tile.m_Tex1Index = tileIndex[y*size + x];
    13821402            tile.m_Tex2Index = 0xFFFF;
    int CMapReader::ParseEntities()  
    14111431
    14121432    size_t entity_idx = 0;
    14131433    size_t num_entities = entities.size();
    1414    
     1434
    14151435    Entity currEnt;
    14161436
    14171437    while (entity_idx < num_entities)
    int CMapReader::ParseEnvironment()  
    14931513
    14941514    GET_ENVIRONMENT_PROPERTY(envObj, SunElevation, m_LightEnv.m_Elevation)
    14951515    GET_ENVIRONMENT_PROPERTY(envObj, SunRotation, m_LightEnv.m_Rotation)
    1496    
     1516
    14971517    CColor terrainAmbientColor;
    14981518    GET_ENVIRONMENT_PROPERTY(envObj, TerrainAmbientColor, terrainAmbientColor)
    14991519    m_LightEnv.m_TerrainAmbientColor = RGBColor(terrainAmbientColor.r, terrainAmbientColor.g, terrainAmbientColor.b);
  • source/graphics/MapWriter.cpp

    diff --git a/source/graphics/MapWriter.cpp b/source/graphics/MapWriter.cpp
    index 9d59ac0..fa6281f 100644
    a b  
    1 /* Copyright (C) 2014 Wildfire Games.
     1/* Copyright (C) 2015 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    2727#include "Terrain.h"
    2828#include "TerrainTextureEntry.h"
    2929#include "TerrainTextureManager.h"
     30#include "Unit.h"
    3031
     32
     33#include <boost/algorithm/string/join.hpp>
    3134#include "maths/MathUtil.h"
    3235#include "maths/NUSpline.h"
    3336#include "ps/CLogger.h"
    void CMapWriter::EnumTerrainTextures(CTerrain *pTerrain,  
    103106{
    104107    // the list of all handles in use
    105108    std::vector<CTerrainTextureEntry*> entries;
    106    
     109
    107110    // resize tile array to required size
    108111    tiles.resize(SQR(pTerrain->GetVerticesPerSide()-1));
    109112    STileDesc* tileptr=&tiles[0];
    void CMapWriter::PackTerrain(CFilePacker& packer, CTerrain* pTerrain)  
    160163    // pack map size
    161164    const ssize_t mapsize = pTerrain->GetPatchesPerSide();
    162165    packer.PackSize(mapsize);
    163    
     166
    164167    // pack heightmap
    165     packer.PackRaw(pTerrain->GetHeightMap(),sizeof(u16)*SQR(pTerrain->GetVerticesPerSide()));   
     168    packer.PackRaw(pTerrain->GetHeightMap(),sizeof(u16)*SQR(pTerrain->GetVerticesPerSide()));
    166169
    167170    // the list of textures used by map
    168171    std::vector<CStr> terrainTextures;
    169172    // descriptions of each tile
    170173    std::vector<STileDesc> tiles;
    171    
     174
    172175    // build lists by scanning through the terrain
    173176    EnumTerrainTextures(pTerrain, terrainTextures, tiles);
    174    
     177
    175178    // pack texture names
    176179    const size_t numTextures = terrainTextures.size();
    177180    packer.PackSize(numTextures);
    178181    for (size_t i=0;i<numTextures;i++)
    179182        packer.PackString(terrainTextures[i]);
    180    
     183
    181184    // pack tile data
    182185    packer.PackRaw(&tiles[0],sizeof(STileDesc)*tiles.size());
    183186}
    void CMapWriter::WriteXML(const VfsPath& filename,  
    270273                    XML_Setting("WindAngle", pWaterMan->m_WindAngle);
    271274                }
    272275            }
    273            
     276
    274277            {
    275278                XML_Element("Postproc");
    276279                {
    void CMapWriter::WriteXML(const VfsPath& filename,  
    386389                CmpPtr<ICmpVisual> cmpVisual(sim, ent);
    387390                if (cmpVisual)
    388391                {
     392                    CUnit* unit = cmpVisual->GetUnit();
    389393                    u32 seed = cmpVisual->GetActorSeed();
    390                     if (seed != (u32)ent)
     394                    bool variationsAdded = false;
     395                    XML_Element("Actor");
     396                    if (unit)
    391397                    {
    392                         XML_Element("Actor");
    393                         XML_Attribute("seed", seed);
     398                        const std::set<CStr>& actorSelections = unit->GetActorSelections();
     399
     400                        if (!actorSelections.empty())
     401                        {
     402                            CStr actorSelectionsStr = boost::algorithm::join(unit->GetActorSelections(), "|");
     403                            XML_Attribute("variation", actorSelectionsStr.c_str());
     404                            variationsAdded = true;
     405                        }
    394406                    }
    395                     // TODO: variation/selection strings
     407                    if (!variationsAdded && seed != (u32)ent)
     408                        XML_Attribute("seed", seed);
    396409                }
    397410            }
    398411        }
    399412
    400413        const std::map<CStrW, CCinemaPath>& paths = pCinema->GetAllPaths();
    401414        std::map<CStrW, CCinemaPath>::const_iterator it = paths.begin();
    402        
     415
    403416        {
    404417            XML_Element("Paths");
    405418
    void CMapWriter::WriteXML(const VfsPath& filename,  
    407420            {
    408421                CStrW name = it->first;
    409422                float timescale = it->second.GetTimescale();
    410                
     423
    411424                XML_Element("Path");
    412425                XML_Attribute("name", name);
    413426                XML_Attribute("timescale", timescale);
    414427
    415428                const std::vector<SplineData>& nodes = it->second.GetAllNodes();
    416429                const CCinemaData* data = it->second.GetData();
    417                
     430
    418431                {
    419432                    XML_Element("Distortion");
    420433                    XML_Attribute("mode", data->m_Mode);
    void CMapWriter::WriteXML(const VfsPath& filename,  
    426439                for ( ssize_t j=nodes.size()-1; j >= 0; --j )
    427440                {
    428441                    XML_Element("Node");
    429                    
     442
    430443                    {
    431444                        XML_Element("Position");
    432445                        XML_Attribute("x", nodes[j].Position.X);