Ejemplo n.º 1
0
func (self *State) executeConflicts(l common.Logger) {
	execution := []func(){}
	for _, node := range self.Nodes {
		total := 0
		for _, units := range node.Units {
			total += units
		}
		for playerId, units := range node.Units {
			enemies := total - units
			if units > 0 && enemies > 0 {
				newSum := common.Max(0, common.Min(units-1, int(float64(units)-(float64(enemies)/5.0))))
				playerIdCpy := playerId
				nodeCpy := node
				if newSum < units {
					oldSum := units
					execution = append(execution, func() {
						nodeCpy.Units[playerIdCpy] = newSum
						self.Changes[nodeCpy.Id] = append(self.Changes[nodeCpy.Id], Change{
							Units:    newSum - oldSum,
							PlayerId: playerIdCpy,
							Reason:   ChangeReason("Conflict"),
						})
					})
				}
			}
		}
	}
	for _, exec := range execution {
		exec()
	}
}
Ejemplo n.º 2
0
func (self *State) executeGrowth(c common.Logger) {
	execution := []func(){}
	for _, node := range self.Nodes {
		total := 0
		for _, units := range node.Units {
			total += units
		}
		players := make([]PlayerId, 0, len(node.Units))
		for playerId, units := range node.Units {
			if units > 0 {
				players = append(players, playerId)
			}
		}
		if len(players) == 1 {
			playerId := players[0]
			units := node.Units[playerId]
			if total > 0 && total < node.Size {
				nodeCpy := node
				newSum := common.Min(node.Size, int(1+float64(units)*(1.0+(growthFactor*(float64(node.Size-total)/float64(node.Size))))))
				if newSum > units {
					execution = append(execution, func() {
						nodeCpy.Units[playerId] = newSum
						self.Changes[nodeCpy.Id] = append(self.Changes[nodeCpy.Id], Change{
							Units:    newSum - units,
							PlayerId: playerId,
							Reason:   ChangeReason("Growth"),
						})
					})
				}
			}
		} else if len(players) > 1 {
			if total > node.Size {
				for playerId, units := range node.Units {
					if units > 0 {
						playerIdCpy := playerId
						nodeCpy := node
						newSum := common.Max(0, int(float64(units)/(1.0+(starvationFactor*(float64(units)/float64(node.Size)))))-1)
						if newSum < units {
							oldSum := units
							execution = append(execution, func() {
								nodeCpy.Units[playerIdCpy] = newSum
								self.Changes[nodeCpy.Id] = append(self.Changes[nodeCpy.Id], Change{
									Units:    newSum - oldSum,
									PlayerId: playerIdCpy,
									Reason:   ChangeReason("Starvation"),
								})
							})
						}
					}
				}
			}
		}
	}
	for _, exec := range execution {
		exec()
	}
}