Skip to content
This repository was archived by the owner on Apr 26, 2025. It is now read-only.
This repository was archived by the owner on Apr 26, 2025. It is now read-only.

Remove code duplication in HandleAI #98

@VedVid

Description

@VedVid

For example, now it is:

		case RangedDumbAI:
			if c.Equipment[SlotWeaponPrimary] != nil {
				// Use primary ranged weapon.
				if c.DistanceTo(cs[0].X, cs[0].Y) >= FOVLength-1 {
					// TODO:
					// For now, every ranged skill has range equal to FOVLength-1
					// but it should change in future.
					c.MoveTowards(b, cs[0].X, cs[0].Y, ai)
				} else {
					// DumbAI will not check if target is valid
					vec, err := NewVector(c.X, c.Y, cs[0].X, cs[0].Y)
					if err != nil {
						fmt.Println(err)
					}
					_ = ComputeVector(vec)
					_, _, target, _ := ValidateVector(vec, b, cs, o)
					if target != nil {
						c.AttackTarget(target)
					}
				}
	} else if c.Equipment[SlotWeaponSecondary] != nil {
		// Use secondary ranged weapon.
				if c.DistanceTo(cs[0].X, cs[0].Y) >= FOVLength-1 {
					// TODO:
					// For now, every ranged skill has range equal to FOVLength-1
					// but it should change in future.
					c.MoveTowards(b, cs[0].X, cs[0].Y, ai)
				} else {
					// DumbAI will not check if target is valid
					vec, err := NewVector(c.X, c.Y, cs[0].X, cs[0].Y)
					if err != nil {
						fmt.Println(err)
					}
					_ = ComputeVector(vec)
					_, _, target, _ := ValidateVector(vec, b, cs, o)
					if target != nil {
						c.AttackTarget(target)
					}
				}
			} else {
				// Use melee attack.
				if c.AITriggered == true {
					if c.DistanceTo(cs[0].X, cs[0].Y) > 1 {
						c.MoveTowards(b, cs[0].X, cs[0].Y, ai)
					} else {
						c.AttackTarget(cs[0])
					}
				} else {
					dx := RandRange(-1, 1)
					dy := RandRange(-1, 1)
					c.Move(dx, dy, b)
				}
			}

it could looks like that:

    if primaryWeapon != nil {  
        UsePrimaryWeapon(Creature)  
    } else if secondaryWeapon != nil {  
        UseSecondaryWeapon(Creature)  
    } else {  
        UseMeleeWeapon(Creature)
    }  

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions