diff --git a/source/actor.cpp b/source/actor.cpp index c36688b..755b5f4 100644 --- a/source/actor.cpp +++ b/source/actor.cpp @@ -75,14 +75,20 @@ namespace actor return act; } + actor_t *duplicate(actor_t *act) + { + actor_t *new_act = createEmptyActor(); + actor::templates::copy(new_act, act); + new_act->surface = draw::loadSurface(new_act->bmp); + new_act->prev = new_act->next = new_act->above = new_act->below = nullptr; + return new_act; + } + actor_t *createFromTemplate(const char *name) { actor_t *templ = actor::templates::getByName(name); if (!templ) return nullptr; - - actor_t *act = createEmptyActor(); - actor::templates::copy(act, templ); - return act; + return duplicate(templ); } actor_t *createFromFile(char **buffer) @@ -163,6 +169,39 @@ namespace actor char tmp[255]; + // Li donem al actor un nom únic + void setUniqueName(actor_t *act) + { + // Recorrem tots els actors + actor_t *other = first; + while (other != nullptr) + { + // Si no som eixe actor... + if (other != act) + { + // ... però tenim el mateix nom... + if (strcmp(act->name, other->name)==0) + { + // Si el nom actual no acaba en dos digits, li afegim "01" al final + int size = strlen(act->name); + if (act->name[size-1] < 48 || act->name[size-1] > 57 || act->name[size-2] < 48 || act->name[size-2] > 57 ) + { + strcat(act->name, "01"); + } else { + // Si ja acaba en dos digits, agafem el numero, li sumem 1, i li'l tornem a ficar + int num = (act->name[size-1]-48) + (act->name[size-2]-48)*10; + num++; + act->name[size-1] = (num % 10)+48; + act->name[size-2] = int(num / 10)+48; + } + // I tornem a començar des del principi amb el nou nom + other = first; + } + } + other = other->next; + } + } + const char *numToOrient(uint8_t value) { tmp[0]=0; @@ -1030,12 +1069,21 @@ namespace actor dest->movement = source->movement; } - void add(actor_t *actor) + void add(actor_t *act) { - actor_t new_template; - copy(&new_template, actor); - templates.push_back(new_template); + // Si ja hi ha una plantilla amb eixe nom... + if (actor::templates::getByName(act->name)) + { + // ... la actualitzem amb les dades del actor seleccionat + actor_t *existing_template = actor::templates::getByName(act->name); + copy(existing_template, act); + } else { + // ... i sinó, afegim el actor seleccionat a la llista de plantilles + actor_t new_template; + copy(&new_template, act); + templates.push_back(new_template); + } + save(); } } - -} \ No newline at end of file +} diff --git a/source/actor.h b/source/actor.h index 89ce291..458f797 100644 --- a/source/actor.h +++ b/source/actor.h @@ -90,10 +90,14 @@ namespace actor // Torna un nou actor actor_t *create(std::string name, vec3_t p, vec3_t s, std::string bmp, SDL_Rect r, SDL_Point o); + actor_t *duplicate(actor_t *act); + actor_t *createFromTemplate(const char *name); actor_t *createFromFile(char **buffer); + void setUniqueName(actor_t *act); + void saveToFile(FILE *f, actor_t *act); void setDirty(actor_t *act, const bool force=false); @@ -129,6 +133,6 @@ namespace actor actor_t *get(const int index); actor_t *getByName(const char *name); void copy(actor_t *dest, actor_t *source); - void add(actor_t *actor); + void add(actor_t *act); } } \ No newline at end of file