DayZ 1.24
Loading...
Searching...
No Matches
PlayerStomach.c File Reference

Go to the source code of this file.

Classes

class  StomachItem
 

Functions

void PlayerStomach (PlayerBase player)
 
void ~PlayerStomach ()
 
float GetStomachVolume ()
 
void ClearContents ()
 
void SetAgentTransferFilter (int filter_agents)
 
int GetAgentTransferFilter ()
 
static void RegisterItem (string classname, int id)
 
static string GetClassnameFromID (int id)
 
static int GetIDFromClassname (string name)
 
static bool InitData ()
 
int GetStorageVersion ()
 
bool IsDigesting ()
 
int GetDigestingType ()
 
void Update (float delta_time)
 
void ProcessNutrients (float delta_time)
 
void DigestAgents (int agents, float quantity)
 
void PrintUpdate ()
 
void AddToStomach (string class_name, float amount, int food_stage=0, int agents=0)
 
void OnStoreSave (ParamsWriteContext ctx)
 
bool OnStoreLoad (ParamsReadContext ctx, int version)
 
int GetDebugObject (array< ref Param > object_out)
 

Variables

class StomachItem DIGESTING_WATER = 1
 
const int DIGESTING_ENERGY = 2
 
const int quantity_bit_offset = 16
 
const int id_bit_offset = 4
 
static int CHECKSUM
 
const float DIGESTION_POINTS = PlayerConstants.DIGESTION_SPEED
 
const int ACCEPTABLE_QUANTITY_MAX = 32768
 
const int ACCEPTABLE_FOODSTAGE_MAX = FoodStageType.COUNT - 1
 
static ref map< string, intm_NamesToIDs = new map<string, int>
 
static ref map< int, stringm_IDsToNames = new map<int, string>
 
static const bool m_InitData = PlayerStomach.InitData()
 
ref array< ref StomachItemm_StomachContents = new array<ref StomachItem>
 
int m_AgentTransferFilter
 
bool m_Digesting
 
int m_DigestingType
 
PlayerBase m_Player
 
float m_StomachVolume
 
const int STORAGE_VERSION = 106
 

Function Documentation

◆ AddToStomach()

void AddToStomach ( string class_name,
float amount,
int food_stage = 0,
int agents = 0 )

Definition at line 310 of file PlayerStomach.c.

311 {
313 return;
314 bool is_liquid;
315
317 if (profile)
318 is_liquid = true;
319 else
321
322 if (profile)
323 {
324 // sanity checks start
325 if (amount > ACCEPTABLE_QUANTITY_MAX || amount < 0)
326 amount = 0;
329 // sanity checks end
330
331 agents = agents | profile.GetAgents();
332 bool found = false;
333 for (int i = 0; i < m_StomachContents.Count(); i++)
334 {
336 if (stomach_item.GetClassName() == class_name)
337 {
338 if (is_liquid || stomach_item.GetFoodStage() == food_stage)
339 {
340 stomach_item.AddAmount(amount);
341 stomach_item.AddAgents(agents);
342 found = true;
343 }
344 }
345 }
346
347 if (!found)
349 }
350 }
FoodStageType
Definition FoodStage.c:2
class OptionSelectorMultistate extends OptionSelector class_name
const int ACCEPTABLE_QUANTITY_MAX
const int ACCEPTABLE_FOODSTAGE_MAX
ref array< ref StomachItem > m_StomachContents
static int GetIDFromClassname(string name)
static NutritionalProfile GetNutritionalProfile(ItemBase item, string classname="", int food_stage=0)
Definition Liquid.c:2
static NutritionalProfile GetNutritionalProfileByName(string class_name)
Definition Liquid.c:210

References ACCEPTABLE_FOODSTAGE_MAX, ACCEPTABLE_QUANTITY_MAX, class_name, GetIDFromClassname(), Edible_Base::GetNutritionalProfile(), Liquid::GetNutritionalProfileByName(), and m_StomachContents.

Referenced by OnStoreLoad().

◆ ClearContents()

void ClearContents ( )

Definition at line 138 of file PlayerStomach.c.

139 {
140 m_StomachContents.Clear();
141 m_StomachVolume = 0.0;
142 }
float m_StomachVolume

References m_StomachContents, and m_StomachVolume.

◆ DigestAgents()

void DigestAgents ( int agents,
float quantity )

Definition at line 276 of file PlayerStomach.c.

277 {
278 if (!agents)
279 return;
280 agents = agents & (~m_AgentTransferFilter);//filter against the agent filter mask
281 int highest_bit = Math.Log2(agents) + 1;
282 for (int i = 0; i < highest_bit; i++)
283 {
284 int agent = (1 << i)& agents;
285 if (agent)
286 m_Player.m_AgentPool.DigestAgent(agent, quantity);
287 }
288 }
PlayerBase m_Player
Definition EnMath.c:7
static proto float Log2(float x)
Returns the binary (base-2) logarithm of x.

References Math::Log2(), and m_Player.

Referenced by ProcessNutrients().

◆ GetAgentTransferFilter()

int GetAgentTransferFilter ( )

Definition at line 149 of file PlayerStomach.c.

150 {
152 }
int m_AgentTransferFilter

References m_AgentTransferFilter.

◆ GetClassnameFromID()

static string GetClassnameFromID ( int id)
static

Definition at line 164 of file PlayerStomach.c.

165 {
166 return m_IDsToNames.Get(id);
167 }
static ref map< int, string > m_IDsToNames

References m_IDsToNames.

Referenced by OnStoreLoad().

◆ GetDebugObject()

int GetDebugObject ( array< ref Param > object_out)

Definition at line 409 of file PlayerStomach.c.

410 {
411 int count = m_StomachContents.Count();
412 for (int i = 0; i < m_StomachContents.Count(); i++)
413 {
414 int id = PlayerStomach.GetIDFromClassname(m_StomachContents.Get(i).m_ClassName);
415 int food_stage = m_StomachContents.Get(i).m_FoodStage;
416 int agents = m_StomachContents.Get(i).m_Agents;
417 float amount = m_StomachContents.Get(i).m_Amount;
419 object_out.Insert(p4);
420 }
422 object_out.Insert(p1);
423 return count;
424
425 }
void PlayerStomach(PlayerBase player)

References m_StomachContents, m_StomachVolume, and PlayerStomach().

◆ GetDigestingType()

int GetDigestingType ( )

Definition at line 230 of file PlayerStomach.c.

231 {
232 return m_DigestingType;
233 }
int m_DigestingType

References m_DigestingType.

◆ GetIDFromClassname()

static int GetIDFromClassname ( string name)
static

Definition at line 169 of file PlayerStomach.c.

170 {
171 if (!m_NamesToIDs.Contains(name))
172 return -1;
173 return m_NamesToIDs.Get(name);
174 }
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
static ref map< string, int > m_NamesToIDs

References m_NamesToIDs, and name.

Referenced by AddToStomach().

◆ GetStomachVolume()

float GetStomachVolume ( )

Definition at line 133 of file PlayerStomach.c.

134 {
135 return m_StomachVolume;
136 }

References m_StomachVolume.

◆ GetStorageVersion()

int GetStorageVersion ( )

Definition at line 220 of file PlayerStomach.c.

221 {
222 return STORAGE_VERSION;
223 }
const int STORAGE_VERSION

References STORAGE_VERSION.

◆ InitData()

static bool InitData ( )
static

Definition at line 176 of file PlayerStomach.c.

177 {
179
180 all_paths.Insert("CfgVehicles");
181 all_paths.Insert("cfgLiquidDefinitions");
182
183 string config_path;
184 string child_name;
185 int scope;
186 string path;
188 for (int i = 0; i < all_paths.Count(); i++)
189 {
190 config_path = all_paths.Get(i);
191 int children_count = GetGame().ConfigGetChildrenCount(config_path);
192
193 for (int x = 0; x < children_count; x++)
194 {
195 GetGame().ConfigGetChildName(config_path, x, child_name);
196 path = config_path + " " + child_name;
197 scope = GetGame().ConfigGetInt(config_path + " " + child_name + " scope");
198 bool should_check = 1;
199 if (config_path == "CfgVehicles" && scope == 0)
200 should_check = 0;
201
202 if (should_check)
203 {
204 bool has_nutrition = GetGame().ConfigIsExisting(path + " Nutrition");
205 bool has_stages = GetGame().ConfigIsExisting(path + " Food");
206
208 {
209 //Print("child name:" + child_name);
210 RegisterItem(child_name, consumable_count);//consumable_count value serves as an unique ID for each item
212 }
213 }
214 }
215 }
216 //Print("consumable_count " + consumable_count);
217 return true;
218 }
Icon x
static void RegisterItem(string classname, int id)
proto native CGame GetGame()
array< string > TStringArray
Definition EnScript.c:666

References GetGame(), path, RegisterItem(), and x.

◆ IsDigesting()

bool IsDigesting ( )

Definition at line 225 of file PlayerStomach.c.

226 {
227 return (m_DigestingType != 0);
228 }

References m_DigestingType.

◆ OnStoreLoad()

bool OnStoreLoad ( ParamsReadContext ctx,
int version )

Definition at line 373 of file PlayerStomach.c.

374 {
375 int checksum, count;
376 if (!ctx.Read(checksum))
377 return false;
378
379 if (!ctx.Read(count))
380 return false;
381 for (int i = 0; i < count; i++)
382 {
383 int value, agents;
384 if (!ctx.Read(value))
385 return false;
386 if (!ctx.Read(agents))
387 return false;
388 if (checksum == CHECKSUM) //if checksum matches, add to stomach, otherwise throw the data away but go through the de-serialization to keep the stream intact
389 {
390 int amount = value >> quantity_bit_offset;//isolate amount bits
391 int id_mask = Math.Pow(2, quantity_bit_offset) - 1;
392 int id = (id_mask & value) >> id_bit_offset;//isolate id bits
393 int food_mask = Math.Pow(2, id_bit_offset) - 1;
394 int food_stage = value & food_mask;
395 //Print("LOAD value:" + value);
396 //Print("LOAD id:" + id);
397 //Print("LOAD id_bit_offset:" + id_bit_offset);
398 //Print("LOAD food_stage:" + food_stage);
399 string classname = GetClassnameFromID(id);
401 }
402 }
403 //Print("LOAD checksum:" + checksum);
404 if (checksum != CHECKSUM)
405 Print("Stomach checksum fail");
406 return true;
407 }
void AddToStomach(string class_name, float amount, int food_stage=0, int agents=0)
static int CHECKSUM
const int quantity_bit_offset
static string GetClassnameFromID(int id)
const int id_bit_offset
proto void Print(void var)
Prints content of variable to console/log.
static proto float Pow(float v, float power)
Return power of v ^ power.

References AddToStomach(), CHECKSUM, GetClassnameFromID(), id_bit_offset, Math::Pow(), Print(), and quantity_bit_offset.

◆ OnStoreSave()

void OnStoreSave ( ParamsWriteContext ctx)

Definition at line 352 of file PlayerStomach.c.

353 {
354 ctx.Write(PlayerStomach.CHECKSUM);
355 ctx.Write(m_StomachContents.Count());
357 for (int i = 0; i < m_StomachContents.Count(); i++)
358 {
360 int id = PlayerStomach.GetIDFromClassname(stomach_item.m_ClassName);
361 //Print("SAVE id:" + id);
362 //Print("SAVE id_bit_offset:" + id_bit_offset);
363
364 int write_result = stomach_item.m_FoodStage | (id << id_bit_offset);
366 ctx.Write(write_result);
367 ctx.Write(stomach_item.m_Agents);
368 //Print("SAVE write_result:" + write_result);
369 }
370 //Print("SAVE CHECKSUM:" + PlayerStomach.CHECKSUM);
371 }
Param3 int

References id_bit_offset, m_StomachContents, PlayerStomach(), and quantity_bit_offset.

◆ PlayerStomach()

void PlayerStomach ( PlayerBase player)

◆ PrintUpdate()

void PrintUpdate ( )

Definition at line 290 of file PlayerStomach.c.

291 {
292 Print("================================");
293 for (int i = 0; i < m_StomachContents.Count(); i++)
294 {
295 string itemname = m_StomachContents.Get(i).m_ClassName;
296 float amount = m_StomachContents.Get(i).GetAmount();
297 int food_stage = m_StomachContents.Get(i).GetFoodStage();
298 int agents = m_StomachContents.Get(i).m_Agents;
299 Print(">");
300 Print("itemname:" + itemname);
301 Print("amount:" + amount);
302 Print("food_stage:" + food_stage);
303 Print("agents:" + agents);
304 Print(">");
305 }
306 Print("m_StomachVolume:" + m_StomachVolume);
307 Print("================================");
308 }

References m_StomachContents, m_StomachVolume, and Print().

◆ ProcessNutrients()

void ProcessNutrients ( float delta_time)

Definition at line 241 of file PlayerStomach.c.

242 {
245 m_DigestingType = 0;
246 if (stomach_items_count == 0)
247 return;
248
250 m_StomachVolume = 0;//reset, it's accumulated with each pass
251 for (int i = 0; i < m_StomachContents.Count(); i ++)
252 {
253 item = m_StomachContents.Get(i);
255 int agents;
257 {
258 m_StomachContents.Remove(i);
259 i--;
260 }
262 m_Player.GetStatEnergy().Add(energy);
263 m_Player.GetStatWater().Add(water);
264
265 if (energy > 0)
267
268 if (water > 0)
270
272 }
273 }
const float DIGESTION_POINTS
class StomachItem DIGESTING_WATER
void DigestAgents(int agents, float quantity)
const int DIGESTING_ENERGY

References DigestAgents(), DIGESTING_ENERGY, DIGESTING_WATER, DIGESTION_POINTS, m_DigestingType, m_Player, m_StomachContents, and m_StomachVolume.

Referenced by Update().

◆ RegisterItem()

static void RegisterItem ( string classname,
int id )
static

Definition at line 154 of file PlayerStomach.c.

155 {
156 int hash = classname.Hash();
157 CHECKSUM = CHECKSUM ^ hash; //xor hash vs current checksum
158 // Print(classname);
159 // Print(CHECKSUM);
160 m_NamesToIDs.Insert(classname, id);
161 m_IDsToNames.Insert(id, classname);
162 }

References CHECKSUM, m_IDsToNames, and m_NamesToIDs.

Referenced by InitData().

◆ SetAgentTransferFilter()

void SetAgentTransferFilter ( int filter_agents)

Definition at line 144 of file PlayerStomach.c.

145 {
147 }

References m_AgentTransferFilter.

◆ Update()

void Update ( float delta_time)

Definition at line 236 of file PlayerStomach.c.

237 {
239 }
void ProcessNutrients(float delta_time)

References ProcessNutrients().

◆ ~PlayerStomach()

Definition at line 127 of file PlayerStomach.c.

128 {
129
130 }

Variable Documentation

◆ ACCEPTABLE_FOODSTAGE_MAX

const int ACCEPTABLE_FOODSTAGE_MAX = FoodStageType.COUNT - 1

Definition at line 109 of file PlayerStomach.c.

Referenced by AddToStomach().

◆ ACCEPTABLE_QUANTITY_MAX

const int ACCEPTABLE_QUANTITY_MAX = 32768

Definition at line 108 of file PlayerStomach.c.

Referenced by AddToStomach().

◆ CHECKSUM

int CHECKSUM
static

Definition at line 106 of file PlayerStomach.c.

Referenced by OnStoreLoad(), and RegisterItem().

◆ DIGESTING_ENERGY

const int DIGESTING_ENERGY = 2

Definition at line 102 of file PlayerStomach.c.

Referenced by ProcessNutrients().

◆ DIGESTING_WATER

class StomachItem DIGESTING_WATER = 1

Referenced by ProcessNutrients().

◆ DIGESTION_POINTS

Definition at line 107 of file PlayerStomach.c.

Referenced by ProcessNutrients().

◆ id_bit_offset

const int id_bit_offset = 4

Definition at line 105 of file PlayerStomach.c.

Referenced by OnStoreLoad(), and OnStoreSave().

◆ m_AgentTransferFilter

int m_AgentTransferFilter

Definition at line 114 of file PlayerStomach.c.

Referenced by GetAgentTransferFilter(), and SetAgentTransferFilter().

◆ m_Digesting

bool m_Digesting

Definition at line 115 of file PlayerStomach.c.

◆ m_DigestingType

int m_DigestingType

Definition at line 116 of file PlayerStomach.c.

Referenced by GetDigestingType(), IsDigesting(), and ProcessNutrients().

◆ m_IDsToNames

ref map<int, string> m_IDsToNames = new map<int, string>
static

Definition at line 111 of file PlayerStomach.c.

Referenced by GetClassnameFromID(), and RegisterItem().

◆ m_InitData

const bool m_InitData = PlayerStomach.InitData()
static

Definition at line 112 of file PlayerStomach.c.

◆ m_NamesToIDs

ref map<string, int> m_NamesToIDs = new map<string, int>
static

Definition at line 110 of file PlayerStomach.c.

Referenced by GetIDFromClassname(), and RegisterItem().

◆ m_Player

PlayerBase m_Player

Definition at line 117 of file PlayerStomach.c.

◆ m_StomachContents

◆ m_StomachVolume

float m_StomachVolume

◆ quantity_bit_offset

const int quantity_bit_offset = 16

Definition at line 104 of file PlayerStomach.c.

Referenced by OnStoreLoad(), and OnStoreSave().

◆ STORAGE_VERSION

const int STORAGE_VERSION = 106

Definition at line 119 of file PlayerStomach.c.