如何创建模拟器并模拟像罗马帝国时代的崛起这样的即时战略游戏?

时间:2012-05-10 07:31:57

标签: simulation

我有兴趣模拟罗马帝国时代的崛起,收集食物,木材等的过程。这样的模拟器将有助于实现经济增长,并将找到建造建筑物和收集资源的优化顺序。请注意,模拟器只会检查经济过程,而不是战斗。是否有任何易于使用并进行此类模拟的免费软件?问题是如何使用模拟器模拟过程(而不是如何创建实时游戏)。

1 个答案:

答案 0 :(得分:3)

Peladao问了一个RTS引擎的例子。我在这里写一个示例代码,并对其进行评论。

伪代码:

    loop:        
    "decide what want to build";
    "try to build that"; (a verification is made if resources are enough)
    "generate resources";
    goto loop;

真实代码(javascript)

1)没有意思。定义稍后使用的函数。

var rand = function(){
    return Math.random()*100;
};

var random = (function(){   
    return {
        betwen:function(min,max){
            return Math.random()*(max-min)+min;
        },
        element:function(datarray){ 
            var len = datarray.length;
            if(!len) return;
            var r = parseInt(this.betwen(0,len),10);
            return datarray[r];
        }   
    };
})();

var scroll = (function(){        
    var index = 0;
    var maximo = 10;

    return {
    cortarViejas:function(){
        var edadCorte = index - maximo;

        $(".logline").each(function(){
            if($(this).data("nace")<edadCorte)
                $(this).remove();           
        }); 
    },
    add:function(txt){
        var line = $("<div>");      
        line.addClass("logline");
        line.data("nace",index++);
        line.html(txt);

        $(line).insertAfter('#marca');

        this.cortarViejas();
    },
    error:function(txt){
        this.add("ERROR: "+txt);
    },
    battle:function(txt){
        this.add("Battle: "+ txt);
    },
    oceaniaDice:function(txt){
        this.add("[Oceania broadcast] "+ txt);
    },
    debugOceania:function(txt){
        this.add("[Oceania debug] "+ txt);
    }
    };

})();

2)继承人有趣的部分。大洋洲是一个产生资源的帝国,并将这些资源投入坦克或士兵中

var oceania = (function(){        

    var tanks = 0;
    var soldiers = 0;
    var build = "";//build strategy 
    var dead = 0;

    var prices = {"tank":5,"soldier":1};

    var hateLines = [ "Eastasia will be destroyed","We hate Eurasia","Our troops are doing good",
        "We have always ben at war with Eastasia","Under the spreading chestnut tree.I sold you and you sold me"];

    var metal = 0;
    var powerplants = 1;
    var factory = 3;    

    return {
    info:function(){
        scroll.debugOceania("Build strategy:"+ build);      
        scroll.debugOceania("Metal:"+ metal);       
        scroll.debugOceania("Army: soldier "+soldiers + ", tanks "+tanks );     
        scroll.debugOceania("Dead:"+ dead);     
    },
    militarTick:function(stuff){

        if(tanks>10 && soldiers > 20){
            tanks -= 10;
            soldiers -= 20;
            scroll.battle("Some units losts! 10 tanks, 20 soldiers");       
            dead += 10+ 20;
        }           
    },

3)在这里,帝国执行他建立[东西]的愿望

    buy:function(stuff){    
        if(!prices[stuff]) {
            scroll.error("buying what?:"+stuff);
            return;
        }
        if(prices[stuff]>metal) {
            scroll.debugOceania(stuff + " is too expensive");
            return;
        }
        metal -= prices[stuff];                 

        switch(stuff){
            case "tank":                            
                tanks++;    
                //scroll.debugOceania("Oceania create tank");               
                break;
            case "soldier":         
                soldiers++; 
                //scroll.debugOceania("Oceania create soldier");                
                break;      
        }       
    },
    buildTick:function(){
        switch(build){
            case "tanks":
                this.buy("tank");
                break;
            case "soldiers":
                this.buy("soldier");
                break;
        }
    },
3)在这里,帝国根据坦克/士兵比例,士兵数量,坦克数量或任何其他战略层面的问题来决定他的愿望是什么。

    strategyTick:function(){        
        var likeSoldiers=0,likeTanks=0;

        build = "nothing";

        //You have something, build something!
        if( metal> 10) //we have metal, lets buy soldiers!
            likeSoldier = 10;   

        //minimum this  
        if ( tanks < 10)
            likeTanks += 10;
        if ( soldiers< 20)
            likeSoldiers += 10;

        //we want 5 soldiers for 1 tank
        if ( soldiers < tanks * 5)
            likeSoldiers += 10;

        //if we have at least 40 soldiers, we would love to have more tanks!
        if ( soldiers > 40) 
            likeTanks += 10;

        //if we have at least 40 tanks, we would love to have moresoldiers!
        if ( tanks > 40) 
            likeSoldiers += 10;

        //we want soldiers more than anything else, lets build soldiers!
        if(likeSoldiers > likeTanks ){      
            build = "soldiers";
        }else 
        //we want tanks more than anything else, lets build soldiers!
        if(  likeTanks > 5) {
            build = "tanks";
        }               
    },

4)此行之后的所有内容都可以忽略。

    produccionTick:function(){
        var t;
        var energy = powerplants * 3;
        var factorycount = factory;
        var metalInitial = metal;

        for(t=0;t<energy,factorycount>0;t++,factorycount--){            
            metal = metal + 1;      
        }           
        //var produce = metal - metalInitial;
        //scroll.debugOceania("Oceania create "+ produce + " metal. Total now:"+metal);         
    },
    propagandaTick:function(){
        if(rand()<5) {
            var hate = random.element(hateLines);
            scroll.oceaniaDice(hate);
        }   
    },
    tick:function(){
        this.propagandaTick();
        this.produccionTick();
        this.strategyTick();
        this.buildTick();
        this.militarTick();
    }   
    };

})();



$(function(){
    $("#cmd").keyup(function(event){                    
        if (event.which == 13) {
            var command = $(this).val();

            $(this).val("");
            scroll.add(command);

            $("#cmd").focus();

            event.preventDefault();
        }  
    });


    $("#reload").click(function(){  
        document.location = "game.htm?r="+Math.random();
    });

    oceania.tick();

    $(document).ready(function(){
        setInterval(function(){
            oceania.tick();
        },300);
    });     
});