1- # Copyright (c) 2022 Stephenson Software
2- # Apache License 2.0
1+ # Copyright (c) 2022 Stephenson Software
2+ # Apache License 2.0
33import random
44from flags .flags import Flags
55from stats .stats import Stats
66
7+
78# @author Daniel McCoy Stephenson
89# @since 2017
910class LivingEntity (object ):
10- def __init__ (self , name ):
11- self .name = name
12- self .chanceToFight = random .randint (45 , 55 ) # Back to normal values
13- self .chanceToBefriend = 100 - self .chanceToFight
14- self .log = ["%s was created." % self .name ]
15- self .friends = []
16- self .stats = Stats ()
17- self .flags = Flags ()
18- self .parents = [] # Track parent entities
19- self .children = [] # Track child entities
20-
21- def rollForMovement (self ):
22- if random .randint (1 ,10 ) == 1 :
23- return True
24- else :
25- return False
26-
27- def getNextAction (self , kreature ):
28- self .decision = random .randint (0 ,100 )
29- if self .decision <= self .chanceToFight : # if fight
30- for i in self .friends :
31- if i .name == kreature .name :
32- return "nothing" # if creature is a friend, don't fight
33- self .stats .numActionsTaken += 1
34- return "fight" # if search comes up empty, fight
35- elif self .chanceToFight < self .decision : # if befriend
36- for i in self .friends :
37- if i .name == kreature .name :
38- self .stats .numActionsTaken += 1
39- return "love" # if creature is a friend, have a baby
40- self .stats .numActionsTaken += 1
41- return "befriend"
42-
43- def reproduce (self , kreature ):
44- self .log .append ("%s made a baby with %s!" % (self .name , kreature .name ))
45- kreature .log .append ("%s made a baby with %s!" % (kreature .name , self .name ))
46- self .stats .numOffspring += 1
47- kreature .stats .numOffspring += 1
48- # Return the parent entities so the child can be created with proper references
49- return (self , kreature )
50-
51- def fight (self , kreature ):
52- self .log .append ("%s fought and ate %s!" % (self .name , kreature .name ))
53- kreature .log .append ("%s was eaten by %s!" % (kreature .name , self .name ))
54- self .stats .numCreaturesEaten += 1
55-
56- def befriend (self , kreature ):
57- self .log .append ("%s made friends with %s!" % (self .name , kreature .name ))
58- kreature .log .append ("%s made friends with %s!" % (kreature .name , self .name ))
59- self .friends .append (kreature )
60- kreature .friends .append (self ) # this should hopefully append this creature to kreature's friend list
61- self .stats .numFriendshipsForged += 1
62- kreature .stats .numFriendshipsForged += 1
63-
64- def increaseChanceToFight (self ):
65- self .chanceToFight += self .flags .increaseAmount
66- if (self .chanceToFight > 100 ):
67- self .chanceToFight = 100
68-
69- def decreaseChanceToFight (self ):
70- self .chanceToFight -= self .flags .increaseAmount
71- if (self .chanceToFight < 0 ):
72- self .chanceToFight = 0
73-
74- def increaseChanceToBefriend (self ):
75- self .chanceToBefriend += self .flags .increaseAmount
76- if (self .chanceToBefriend > 100 ):
77- self .chanceToBefriend = 100
78-
79- def decreaseChanceToBefriend (self ):
80- self .chanceToBefriend -= self .flags .increaseAmount
81- if (self .chanceToBefriend < 0 ):
82- self .chanceToBefriend = 0
83-
84- def addChild (self , child ):
85- """Add a child to this entity's children list"""
86- self .children .append (child )
87-
88- def addParent (self , parent ):
89- """Add a parent to this entity's parents list"""
90- self .parents .append (parent )
11+ def __init__ (self , name ):
12+ self .name = name
13+ self .chanceToFight = random .randint (45 , 55 ) # Back to normal values
14+ self .chanceToBefriend = 100 - self .chanceToFight
15+ self .health = random .randint (80 , 120 ) # Health between 80-120
16+ self .maxHealth = self .health # Track maximum health for potential future use
17+ self .log = ["%s was created." % self .name ]
18+ self .friends = []
19+ self .stats = Stats ()
20+ self .flags = Flags ()
21+ self .parents = [] # Track parent entities
22+ self .children = [] # Track child entities
23+
24+ def rollForMovement (self ):
25+ if random .randint (1 , 10 ) == 1 :
26+ return True
27+ else :
28+ return False
29+
30+ def getNextAction (self , kreature ):
31+ self .decision = random .randint (0 , 100 )
32+ if self .decision <= self .chanceToFight : # if fight
33+ for i in self .friends :
34+ if i .name == kreature .name :
35+ return "nothing" # if creature is a friend, don't fight
36+ self .stats .numActionsTaken += 1
37+ return "fight" # if search comes up empty, fight
38+ elif self .chanceToFight < self .decision : # if befriend
39+ for i in self .friends :
40+ if i .name == kreature .name :
41+ self .stats .numActionsTaken += 1
42+ return "love" # if creature is a friend, have a baby
43+ self .stats .numActionsTaken += 1
44+ return "befriend"
45+
46+ def reproduce (self , kreature ):
47+ self .log .append ("%s made a baby with %s!" % (self .name , kreature .name ))
48+ kreature .log .append ("%s made a baby with %s!" % (kreature .name , self .name ))
49+ self .stats .numOffspring += 1
50+ kreature .stats .numOffspring += 1
51+ # Return the parent entities so the child can be created with proper references
52+ return (self , kreature )
53+
54+ def fight (self , kreature ):
55+ # Fight to the death - continue until one creature dies
56+ while self .health > 0 and kreature .health > 0 :
57+ # This creature attacks first
58+ if self .health > 0 :
59+ damage = random .randint (15 , 25 ) # Random damage between 15-25
60+ kreature .health -= damage
61+ if kreature .health <= 0 :
62+ self .log .append (
63+ "%s fought and ate %s!" % (self .name , kreature .name )
64+ )
65+ kreature .log .append (
66+ "%s was eaten by %s!" % (kreature .name , self .name )
67+ )
68+ self .stats .numCreaturesEaten += 1
69+ break
70+ else :
71+ self .log .append (
72+ "%s fought %s and dealt %d damage!"
73+ % (self .name , kreature .name , damage )
74+ )
75+ kreature .log .append (
76+ "%s took %d damage from %s! Health: %d"
77+ % (kreature .name , damage , self .name , kreature .health )
78+ )
79+
80+ # Target creature counter-attacks if still alive
81+ if kreature .health > 0 :
82+ damage = random .randint (15 , 25 ) # Random damage between 15-25
83+ self .health -= damage
84+ if self .health <= 0 :
85+ kreature .log .append (
86+ "%s fought and ate %s!" % (kreature .name , self .name )
87+ )
88+ self .log .append ("%s was eaten by %s!" % (self .name , kreature .name ))
89+ kreature .stats .numCreaturesEaten += 1
90+ break
91+ else :
92+ kreature .log .append (
93+ "%s fought %s and dealt %d damage!"
94+ % (kreature .name , self .name , damage )
95+ )
96+ self .log .append (
97+ "%s took %d damage from %s! Health: %d"
98+ % (self .name , damage , kreature .name , self .health )
99+ )
100+
101+ def befriend (self , kreature ):
102+ self .log .append ("%s made friends with %s!" % (self .name , kreature .name ))
103+ kreature .log .append ("%s made friends with %s!" % (kreature .name , self .name ))
104+ self .friends .append (kreature )
105+ kreature .friends .append (
106+ self
107+ ) # this should hopefully append this creature to kreature's friend list
108+ self .stats .numFriendshipsForged += 1
109+ kreature .stats .numFriendshipsForged += 1
110+
111+ def increaseChanceToFight (self ):
112+ self .chanceToFight += self .flags .increaseAmount
113+ if self .chanceToFight > 100 :
114+ self .chanceToFight = 100
115+
116+ def decreaseChanceToFight (self ):
117+ self .chanceToFight -= self .flags .increaseAmount
118+ if self .chanceToFight < 0 :
119+ self .chanceToFight = 0
120+
121+ def increaseChanceToBefriend (self ):
122+ self .chanceToBefriend += self .flags .increaseAmount
123+ if self .chanceToBefriend > 100 :
124+ self .chanceToBefriend = 100
125+
126+ def decreaseChanceToBefriend (self ):
127+ self .chanceToBefriend -= self .flags .increaseAmount
128+ if self .chanceToBefriend < 0 :
129+ self .chanceToBefriend = 0
130+
131+ def addChild (self , child ):
132+ """Add a child to this entity's children list"""
133+ self .children .append (child )
134+
135+ def addParent (self , parent ):
136+ """Add a parent to this entity's parents list"""
137+ self .parents .append (parent )
138+
139+ def isAlive (self ):
140+ """Check if the entity is still alive (health > 0)"""
141+ return self .health > 0
142+
143+ def regenerateHealth (self ):
144+ """Regenerate a small amount of health over time"""
145+ if (
146+ self .health < self .maxHealth and random .randint (1 , 10 ) <= 3
147+ ): # 30% chance per tick
148+ regeneration = random .randint (1 , 3 ) # Regenerate 1-3 health per tick
149+ self .health = min (self .health + regeneration , self .maxHealth )
150+ # Only log significant regeneration events to avoid spam
151+ if regeneration >= 2 :
152+ self .log .append (
153+ "%s regenerated %d health! Health: %d/%d"
154+ % (self .name , regeneration , self .health , self .maxHealth )
155+ )
0 commit comments