Skip to content

Commit b96b571

Browse files
committed
hungry creatures will now pick up food if they randomly happen to pass by a food source, though they do not actively seek food out
1 parent 9257f62 commit b96b571

File tree

14 files changed

+303
-6
lines changed

14 files changed

+303
-6
lines changed

AUTHORS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
Kurt M. Weber (kmw) <[email protected]>
2+
* Kurt is a Ph.D. student in Russian history at the University of California, Riverside, He discovered
3+
Moria on an old IBM PC/XT his father purchased at a yard sale in the late 90's, then over the next
4+
several years became an avid roguelike fan. Lavender Throne is a hobby for him when he needs a break
5+
from his real work.
26

37
PCG Random Number Generator (src/pcg_basic.c, src/pcg_basic.h):
48
* Melissa O'Neill <[email protected]>

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
for version 0.10
2+
* hungry creatures will now pick up food if they randomly happen to pass by a food source, though
3+
they do not actively seek food out (kmw, 2017-06-26)
4+
15
for version 0.9
26
* release (kmw, 2017-05-21)
37
* unowned items are saved and restored (kmw, 2017-05-21)

TODO

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
* When updating plant status every turn, any excess growth should be carried over, rather than just stopping
22
and resetting growth at the next stage to 0 (2017-03-27, kmw)
33
* Make savefiles portable--this will require paying attention to endianness, at the very least (2017-04-11,
4-
kmw)
4+
kmw)
5+
* Add creature food preferences, and update creature AI to respect them when eating (2017-05-28, kmw)
6+
* Creatures should prefer not to waste food by eating items that give more nutrition value than what they
7+
need, unless desperately hungry (2017-06-26, kmw)
8+
* Creatures should avoid eating corpses of former allies (2017-06-26, kmw)
9+
* Creatures should pick up unneeded food and carry it around for later (2017-06-26, kmw)
10+
* It should be possible to start eating, get some nutrition, then stop and save the rest for later
11+
(2017-06-26, kmw)
12+
* Hungry creatures should actively seek out food, rather than simply hoping they randomly pass by
13+
some (2017-06-26, kmw)

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ lvt_SOURCES = leveldisp.c levelgen.c main.c pcg_basic.c random.c map.c screen.c
44
util.c messages.c questions.c allocator.c creaturegen.c colors.c name.c faction.c \
55
creaturemove.c attack.c creaturelist.c item.c look.c itemmgmt.c flooritems.c \
66
inventory.c spawnitem.c throw.c plantgen.c plantlist.c plantmgmt.c plants.c \
7-
pickfruit.c eat.c seedlist.c seeds.c save.c readsave.c
7+
pickfruit.c eat.c seedlist.c seeds.c save.c readsave.c creatureeat.c
88
noinst_HEADERS = lvt.h level.h pcg_basic.h creature.h messages.h types.h move.h stringlookups.h \
99
colors.h item.h attack.h species.h creaturemgmt.h plant.h save.h

src/Makefile.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ am_lvt_OBJECTS = leveldisp.$(OBJEXT) levelgen.$(OBJEXT) main.$(OBJEXT) \
115115
throw.$(OBJEXT) plantgen.$(OBJEXT) plantlist.$(OBJEXT) \
116116
plantmgmt.$(OBJEXT) plants.$(OBJEXT) pickfruit.$(OBJEXT) \
117117
eat.$(OBJEXT) seedlist.$(OBJEXT) seeds.$(OBJEXT) \
118-
save.$(OBJEXT) readsave.$(OBJEXT)
118+
save.$(OBJEXT) readsave.$(OBJEXT) creatureeat.$(OBJEXT)
119119
lvt_OBJECTS = $(am_lvt_OBJECTS)
120120
lvt_LDADD = $(LDADD)
121121
AM_V_P = $(am__v_P_@AM_V@)
@@ -268,7 +268,7 @@ lvt_SOURCES = leveldisp.c levelgen.c main.c pcg_basic.c random.c map.c screen.c
268268
util.c messages.c questions.c allocator.c creaturegen.c colors.c name.c faction.c \
269269
creaturemove.c attack.c creaturelist.c item.c look.c itemmgmt.c flooritems.c \
270270
inventory.c spawnitem.c throw.c plantgen.c plantlist.c plantmgmt.c plants.c \
271-
pickfruit.c eat.c seedlist.c seeds.c save.c readsave.c
271+
pickfruit.c eat.c seedlist.c seeds.c save.c readsave.c creatureeat.c
272272

273273
noinst_HEADERS = lvt.h level.h pcg_basic.h creature.h messages.h types.h move.h stringlookups.h \
274274
colors.h item.h attack.h species.h creaturemgmt.h plant.h save.h
@@ -363,6 +363,7 @@ distclean-compile:
363363
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/attack.Po@am__quote@
364364
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chargen.Po@am__quote@
365365
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/colors.Po@am__quote@
366+
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/creatureeat.Po@am__quote@
366367
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/creaturegen.Po@am__quote@
367368
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/creatureinit.Po@am__quote@
368369
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/creaturelist.Po@am__quote@

src/creature.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ typedef struct creature{
119119
struct item *inventory[52];
120120
unsigned int weight;
121121
unsigned int nutrition;
122+
bool hungry;
122123
//creatureAggression aggression;
123124
struct {
124125
unsigned int speed;

src/creatureeat.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* This file is part of Lavender Throne.
2+
* Copyright 2016 by Kurt Weber
3+
*
4+
* Lavender Throne is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Lavender Throne is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Lavender Throne. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#define _CREATUREEAT_C
19+
20+
#include <stdbool.h>
21+
22+
#include "lvt.h"
23+
#include "creature.h"
24+
25+
bool hungerAction(creature *curCreature){
26+
item *foodItem = 0;
27+
coord3D itemLoc;
28+
29+
if (getCreatureNutrition(curCreature) <= (getCreatureWeight(curCreature) / 2)){
30+
setCreatureHungry(curCreature);
31+
}
32+
33+
if (isCreatureHungry(curCreature)){
34+
if (hasFoodInventory(curCreature)){
35+
foodItem = selectOptimalFoodInventory(curCreature);
36+
if (foodItem){
37+
eatItem(curCreature, foodItem);
38+
return true;
39+
}
40+
}
41+
42+
foodItem = checkAdjacentFood(getCreatureLocation(curCreature));
43+
if (foodItem){
44+
itemLoc = getItemLocation(foodItem);
45+
if (addCreatureInventoryItem(curCreature, foodItem)){
46+
removeContent(itemLoc.level, itemLoc.x, itemLoc.y, foodItem);
47+
return true;
48+
}
49+
}
50+
51+
foodItem = checkAdjacentFruitingBush(getCreatureLocation(curCreature));
52+
if (foodItem){
53+
if (addCreatureInventoryItem(curCreature, foodItem)){
54+
return true;
55+
}
56+
}
57+
}
58+
59+
return false;
60+
}

src/creaturemgmt.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <math.h>
2121
#include <stdbool.h>
22+
#include <stdlib.h>
2223
#include "creaturemgmt.h"
2324
#include "lvt.h"
2425
#include "creature.h"
@@ -545,6 +546,49 @@ void getCreatureInventory(creature *creature, item *inventory[52]){
545546
return;
546547
}
547548

549+
bool hasFoodInventory(creature *creature){
550+
unsigned int i = 0;
551+
itemClassId cId;
552+
553+
for (i = 0; i < 52; i++){
554+
if (creature->inventory[i]){
555+
cId = getItemClass(creature->inventory[i]);
556+
if ((cId == ITEM_TYPE_FRUIT) || (cId == ITEM_TYPE_CORPSE)){
557+
return true;
558+
}
559+
}
560+
}
561+
562+
return false;
563+
}
564+
565+
item *selectOptimalFoodInventory(creature *creature){
566+
unsigned int nutDiff;
567+
item *bestNutDiffItem = 0;
568+
itemClassId cId;
569+
unsigned int i = 0;
570+
int curNutDiff = 0;
571+
int bestNutDiff = 0;
572+
573+
nutDiff = getCreatureWeight(creature) - getCreatureNutrition(creature);
574+
bestNutDiff = nutDiff;
575+
576+
for (i = 0; i < 52; i++){
577+
if (creature->inventory[i]){
578+
cId = getItemClass(creature->inventory[i]);
579+
if ((cId == ITEM_TYPE_FRUIT) || (cId == ITEM_TYPE_CORPSE)){
580+
curNutDiff = nutDiff - getItemNutrition(creature->inventory[i]);
581+
if (abs(curNutDiff) < abs(bestNutDiff)){
582+
bestNutDiff = curNutDiff;
583+
bestNutDiffItem = creature->inventory[i];
584+
}
585+
}
586+
}
587+
}
588+
589+
return bestNutDiffItem;
590+
}
591+
548592
void unwieldWeapon(creature *creature){
549593
item *weapon;
550594

@@ -725,6 +769,22 @@ unsigned int getCreatureArmorClass(creature *creature){
725769
return ac;
726770
}
727771

772+
void setCreatureHungry(creature *creature){
773+
creature->hungry = true;
774+
775+
return;
776+
}
777+
778+
void unsetCreatureHungry(creature *creature){
779+
creature->hungry = false;
780+
781+
return;
782+
}
783+
784+
bool isCreatureHungry(creature *creature){
785+
return creature->hungry;
786+
}
787+
728788
void setCreatureNutrition (creature *creature, unsigned int nutrition){
729789
unsigned int weight;
730790

src/creaturemove.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ void moveCreatures(){
3939
curCreature = curCreatureNode->creature;
4040
incrementCreatureSpeedCounter(curCreature, getCreatureSpeed(curCreature));
4141
while (hasAction(curCreature)){
42-
doMoveCreature(curCreature);
42+
if (!hungerAction(curCreature)){
43+
doMoveCreature(curCreature);
44+
}
4345
}
4446

4547
// we have to do this before we update the life cycle, because if the creature dies as a result of

src/itemmgmt.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ itemClassId getItemClass(item *item){
112112
return item->itemClass;
113113
}
114114

115+
unsigned int getItemNutrition(item *item){
116+
itemClassId cId;
117+
118+
cId = getItemClass(item);
119+
120+
if (cId == ITEM_TYPE_FRUIT){
121+
return getFruitNutrition(item);
122+
}
123+
124+
if (cId == ITEM_TYPE_CORPSE){
125+
return getCorpseNutrition(item);
126+
}
127+
128+
return 0;
129+
}
130+
131+
132+
115133
unsigned int getEffectiveArmor(item *item){
116134
unsigned int ac;
117135

src/levelgen.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ void placeDoors(level level){
379379

380380
doorEligible = findDoorEligible(level);
381381

382+
if (!doorEligible){
383+
free(doorEligible);
384+
return;
385+
}
386+
382387
while (doorEligible[i].x){
383388
if (uniformRandomRangeInt(&levelGenRNG, 1, 1000) < doorLikelihood){
384389
if (uniformRandomRangeInt(&levelGenRNG, 1, 1000) < hiddenDoorLikelihood){

src/lvt.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ void initializeColors();
7575
#else
7676
#endif
7777

78+
#ifndef _CREATUREEAT_C
79+
bool hungerAction(creature *curCreature);
80+
#endif
81+
7882
#ifndef _CREATUREGEN_C
7983
creatureList *generateStartingCreatures();
8084
#else
@@ -162,6 +166,11 @@ void regenerateHitPoints(creature *creature);
162166
void setCreatureNutrition (creature *creature, unsigned int nutrition);
163167
unsigned int getCreatureNutrition(creature *creature);
164168
bool updateCreatureNutrition(creature *creature);
169+
void setCreatureHungry(creature *creature);
170+
void unsetCreatureHungry(creature *creature);
171+
bool isCreatureHungry(creature *creature);
172+
bool hasFoodInventory(creature *creature);
173+
item *selectOptimalFoodInventory(creature *creature);
165174
#else
166175
bool updateCreatureLifeCycleNotMatured(creature *creature);
167176
bool updateCreatureLifeCycleMatured(creature *creature);
@@ -193,6 +202,7 @@ void changeCreatureLocation(creature *creature, coord3D newPos);
193202

194203
#ifndef _EAT_C
195204
void doEat();
205+
void eatItem(creature *creature, item *foodItem);
196206
#else
197207
void eatItem(creature *creature, item *foodItem);
198208
#endif
@@ -297,6 +307,11 @@ unsigned int getSeedDormancy(item *seed);
297307
bool isSeed(item *item);
298308
void setItemOwner(item *item, creature *owner);
299309
creature *getItemOwner(item *item);
310+
unsigned int getItemNutrition(item *item);
311+
#else
312+
itemClassId getItemClass(item *item);
313+
unsigned int getFruitNutrition(item *fruit);
314+
unsigned int getCorpseNutrition(item *corpse);
300315
#endif
301316

302317
#ifndef _LEVELDISP_C
@@ -396,6 +411,7 @@ char *generateName();
396411

397412
#ifndef _PICKFRUIT_C
398413
void doPickFruit();
414+
item *pickFruitFromPlant(plant *plant);
399415
#else
400416
item *pickFruitFromPlant(plant *plant);
401417
#endif
@@ -590,6 +606,8 @@ char inventoryIndexToLetter(char c);
590606
bool isInventoryLetter(char c);
591607
int inventoryLetterToIndex(char c);
592608
void directionToUnitMatrix(moveDirection dir, moveMatrix *matrix);
609+
item *checkAdjacentFood(coord3D location);
610+
item *checkAdjacentFruitingBush(coord3D location);
593611
#else
594612
#endif
595613

src/move.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#define KEY_DOWNLEFT KEY_END
2626
#define KEY_DOWNRIGHT KEY_NPAGE
2727

28-
2928
typedef enum moveOutcome{
3029
MOVE_SUCCESS,
3130
MOVE_FAILED_WALL,

0 commit comments

Comments
 (0)