Development > Coding

C: appending arrays

<< < (2/3) > >>

Hoehrer:
i think i've found a solution ... if i use a struct that keeps track of the total numbers of strings in it i think this will be _alot_ easier. What do you think?

Example code for that struct:

--- Code: ---typedef struct research_requirements_s
{
char requires[MAX_TECHLINKS][MAX_VAR];
int numEntries;
} research_requirements_t;
--- End code ---


Werner

oxyXen:
Hi


--- Quote ---What would that change? I would still need to handle the appending of arrays... don't I? But i might not understand what you mean exactly. I'll look into this as well.
--- End quote ---


char is limited to a size of 1 byte, which means, that you can either identify "only" 255 techs by a character, or you can make combinations of character, which will of course increase the possibilities, but if one tech has x chars in its id, and another y chars, than there's no justification for implementing a function that can handle it.
With ints you have thousands of unique ids and everyone takes exactly sizeof(int) space in the memory.


--- Quote ---R_GetFirstRequired should return "oxygen" and "xentronium"
--- End quote ---

What if xentronium requires xenophysics?

This whole research thing seems really to be not easy  :?

here is my idea of the structure object:

--- Code: ---int id
char* name
int numRequired
techstruct* required[numRequired]
bool achieved
int timeToResearch

--- End code ---


Now you can surf to your tech needed:

--- Code: ---void ResearchSomething(techstruct* t)
{
   for (int i=0; i<t->numRequired; i++)
   {
      if (!t->required[i]->achieved))
      {
         ResearchSomething(t->required[i]);
         return;
      } else
      {
         com_printf("Research this: %s", t->name+'\0');
      }
   }
}

--- End code ---


Now i'll try to post something sourceforge.

Good luck

ox

oxyXen:

--- Quote from: "Hoehrer" ---i think i've found a solution ... if i use a struct that keeps track of the total numbers of strings in it i think this will be _alot_ easier. What do you think?

Example code for that struct:

--- Code: ---typedef struct research_requirements_s
{
char requires[MAX_TECHLINKS][MAX_VAR];
int numEntries;
} research_requirements_t;
--- End code ---


Werner
--- End quote ---


Heh, i don't know yet. What exactly does MAX_TECHLINKS do?

ox

Hoehrer:

--- Quote from: "oxyXen" ---
--- Quote from: "Hoehrer" ---i think i've found a solution ... if i use a struct that keeps track of the total numbers of strings in it i think this will be _alot_ easier. What do you think?

Example code for that struct:

--- Code: ---typedef struct research_requirements_s
{
char requires[MAX_TECHLINKS][MAX_VAR];
int numEntries;
} research_requirements_t;
--- End code ---


Werner
--- End quote ---


Heh, i don't know yet. What exactly does MAX_TECHLINKS do?

ox
--- End quote ---


That's just the maximal number (c-define) of strings that are allowed.
MAX_TECHLINKS ... number of available array entries (entry=string)
MAX_VAR ... numer of characters in a string
numEntries ... lists the number of the _used_ entries and is set when writing strings into the array.

I'm using this struct already in the code. The fact that this is now handles in a struct helps alot in the functions i'm not writing. So no need to worry too much about it..... thanks fo all your help so far anyway. :D

Werner

Hoehrer:
Sorry, i missed your previous post, but as i wrote in my previous post the problem is more or less solved now.


--- Quote from: "oxyXen" ---
--- Quote ---What would that change? I would still need to handle the appending of arrays... don't I? But i might not understand what you mean exactly. I'll look into this as well.
--- End quote ---


char is limited to a size of 1 byte, which means, that you can either identify "only" 255 techs by a character, or you can make combinations of character, which will of course increase the possibilities, but if one tech has x chars in its id, and another y chars, than there's no justification for implementing a function that can handle it.
With ints you have thousands of unique ids and everyone takes exactly sizeof(int) space in the memory.
--- End quote ---

I'm currently not identifying the techs by a char ... i'm using a full string that is ~256 chars long ;) this provides _alot_ more unique possibilities and is more readable as well. Space-requirement is an issue though, but that is solved as soon as i've finished the integration of the new struct in the getfirstrequired function. -> previous post.


--- Quote ---
--- Quote ---R_GetFirstRequired should return "oxygen" and "xentronium"
--- End quote ---

What if xentronium requires xenophysics?
--- End quote ---

Then it should return "oxygen" and "xenophysics" instead (always the one that is nearer to "initial" and has not yet been researched)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version