用java写星际争霸的ai (2)

我们第一战设定为编写一个2BB的战术,如果成功的话1V1电脑应该没有问题,尤其是TVZ。

首先我们要11农民的时候造一个兵营

		//If we have at least 11 SCVs and we have not built barracks before
		if ((bwapi.getSelf().getSupplyUsed() == 22)){
			//Check if we have enough minerals for a barrack
			if (bwapi.getSelf().getMinerals() >= 150){
				int worker = getNearestUnit(UnitTypes.Terran_SCV.ordinal(), homePositionX, homePositionY);
				if (worker != -1) {
					// if we found him, try to select appropriate build tile position for a barrack, near home. We will change it to choke point later
					Point buildTile = getBuildTile(worker, UnitTypes.Terran_Barracks.ordinal(), homePositionX, homePositionY);
					//if we found a good build position
					if (buildTile.x != -1) {
						firstbb = 0;
						bwapi.build(worker, buildTile.x, buildTile.y, UnitTypes.Terran_Barracks.ordinal());					
					}
				}
			}
		}

这里模仿的是造房子的代码。为什么是22不是11呢?困扰了我一会,不过作为资深虫族玩家立马反应过来了,zergling的人口是一半也就是1,所以正常游戏中的1人口其实是2人口。


2BB的流程是13农民以后第二个兵营,并且在兵营里造兵,这里我们模仿的是基地造农民处的代码

		//Let's produce marines at barracks
		for (Unit unit : bwapi.getMyUnits()) {
			// if this unit is a command center (Terran_Command_Center)
			if (unit.getTypeID() == UnitTypes.Terran_Barracks.ordinal()) {
				// if it's training queue is empty
				if (unit.getTrainingQueueSize() == 0) {
					// check if we have enough minerals train one marine (Terran_Marine)
					if ((bwapi.getSelf().getMinerals() >= 50) ) 
						bwapi.train(unit.getID(), UnitTypes.Terran_Marine.ordinal());
				}
			}
		}		


我们要在差不多16人口的时候放下refinery,然后academy采气,这两段代码和建造兵营的一样。

现在我们要保持有三个SCV采GAS。

下篇再说


你可能感兴趣的:(Starcraft,AI)