Ejemplo n.º 1
0
func (p *Plan) FindParent(materials material.MaterialSlice) string {
	seed := time.Now().UnixNano()
	fmt.Println("Seed:", seed)
	rand.Seed(seed) // FIXME use own PRNG instance

	ingredients := make([]Ingredient, 0)

	for use, amount := range p.Requirements {
		possibleMaterials := materials.FindByUse(use)
		remainingAmount := amount
		for remainingAmount > 0 {
			randomAmount := rand.Int63n(int64(remainingAmount)) + 1
			randomIndex := rand.Int63n(int64(len(possibleMaterials)))
			randomGrade := rand.Int63n(5)

			mat := possibleMaterials[randomIndex]
			ingredient := Ingredient{mat, use, material.GradeIndexToGrade(randomGrade), uint8(randomAmount)}
			ingredients = append(ingredients, ingredient)

			remainingAmount -= uint8(randomAmount)
		}
	}

	recipe := &Recipe{p, ingredients}
	genes := recipe.genome(materials)
	fmt.Println("Genome:", genes)
	return genes
}
Ejemplo n.º 2
0
func (i Ingredient) genome(materials material.MaterialSlice) string {
	materialIndex := materials.Index(i.Material)
	gradeS := fmt.Sprintf("%d", i.Grade.Index())
	amountS := fmt.Sprintf("%d", i.Quantity)
	numLength := strconv.FormatInt(int64(len(strconv.FormatInt(int64(len(materials)), 10))), 10)
	materialIndexS := fmt.Sprintf("%0"+numLength+"d", materialIndex)

	useIndex := 0
	for k, use := range i.Material.Uses {
		if use == i.Use {
			useIndex = k
			break
		}
	}

	useIndexS := fmt.Sprintf("%02d", useIndex)

	return materialIndexS + gradeS + amountS + useIndexS
}