The ecology of altruistic motivations in triadic social environments