diff --git a/t1/libconjunto.o b/t1/libconjunto.o new file mode 100644 index 0000000000000000000000000000000000000000..e17c4f1925256fffe56844cb8ce222013cc543d0 Binary files /dev/null and b/t1/libconjunto.o differ diff --git a/t1/libfila.o b/t1/libfila.o new file mode 100644 index 0000000000000000000000000000000000000000..d4c9f0629970ea1cb13f0885a42846036940d90d Binary files /dev/null and b/t1/libfila.o differ diff --git a/t1/liblef.o b/t1/liblef.o new file mode 100644 index 0000000000000000000000000000000000000000..a96efd52d0df3a735ec4284cd0f4344d5ba8f4e2 Binary files /dev/null and b/t1/liblef.o differ diff --git a/t1/mundo b/t1/mundo new file mode 100755 index 0000000000000000000000000000000000000000..b53a829938eb3e6f24afc750ad1d4fd7670b46f5 Binary files /dev/null and b/t1/mundo differ diff --git a/t1/mundo.c b/t1/mundo.c index 61babebbc90ecb12a2df4b61ae89bddda0df875e..96bf90ed890228fed09b564ee4bcc5f24180694d 100644 --- a/t1/mundo.c +++ b/t1/mundo.c @@ -6,6 +6,12 @@ #include "libfila.h" #include "liblef.h" +#define MEM_ERROR_EXIT \ + do { \ + printf("Erro: não foi possÃvel alocar memória."); \ + exit(1); \ + } while(0) + #define CHEGADA 0 #define SAIDA 1 #define MISSAO 2 @@ -22,20 +28,20 @@ typedef struct { typedef struct { int id; int lotacao_max; - conjunto_t *herois_no_local; + conjunto_t *publico; fila_t *fila; - int localizacaox; - int localizacaoy; + int posx; + int posy; } local_t; typedef struct { int tempo_atual; - heroi_t *herois; - local_t *locais; - conjunto_t *habilidades; + int tamanho_mundo; /* o mundo é um quadrado */ + conjunto_t *cj_habilidades; int n_herois; int n_locais; - int n_tamanho_mundo; /* o mundo é um quadrado */ + heroi_t *herois; + local_t *locais; } mundo_t; /* retorna um inteiro aleatório entre a e b (inclusive) */ @@ -43,58 +49,123 @@ int aleat(int a, int b) { return rand() % b + 1 + a; } -/* retorna a distancia euclidiana entre duas coordenadas em double */ -double distancia(coordenada_t loc1, coordenada_t loc2) { - int soma = (loc2.x - loc1.x)*(loc2.x - loc1.x) + (loc2.y - loc1.y)*(loc2.y - loc1.y); - oreturn sqrt((double)soma); +/* retorna a distancia euclidiana entre dois locais em double */ +double distancia(local_t loc1, local_t loc2) { + int deltax = loc2.posx - loc1.posx; + int deltay = loc2.posy - loc1.posy; + int quadrado_da_distancia = deltax * deltax + deltay * deltay; + return sqrt((double)quadrado_da_distancia); } -heroi_t *inicializa_heroi(int id, conjunto_t *conjunto_de_habilidades) { - heroi_t *h; - if( !(h = malloc(sizeof(heroi_t))) ) - return NULL; - - if( !(h->habilidades_do_heroi = cria_subcjt_cjt(conjunto_de_habilidades, aleat(2, 5))) ) { - free(h); - return NULL; - } +heroi_t inicializa_heroi(int id, conjunto_t *conjunto_de_habilidades) { + heroi_t h; + + h.id = id; + h.paciencia = aleat(0, 100); + h.idade = aleat(18, 100); + h.exp = 0; - h->id = id; - h->paciencia = aleat(0, 100); - h->idade = aleat(18, 100); - h->exp = 0; + if ( !(h.habilidades_do_heroi = cria_subcjt_cjt(conjunto_de_habilidades, aleat(2, 5))) ) + MEM_ERROR_EXIT; return h; } -local_t *inicializa_local(int id, int tamanho_mundo) { - local_t *l; - if( !(l = malloc(sizeof(local_t))) ) - return NULL; +local_t inicializa_local(int id, int tamanho_mundo) { + local_t l; - l->localizacaox = aleat(0, tamanho_mundo-1); - l->localizacaoy = aleat(0, tamanho_mundo-1); - /* TODO: terminar essa função */ + l.id = id; + l.lotacao_max = aleat(5, 30); + + if ( !(l.publico = cria_cjt(l.lotacao_max)) ) + MEM_ERROR_EXIT; + + if ( !(l.fila = cria_fila()) ) + MEM_ERROR_EXIT; + + l.posx = aleat(0, tamanho_mundo-1); + l.posy = aleat(0, tamanho_mundo-1); + return l; } -mundo_t *inicializa_mundo() { +mundo_t *cria_mundo() { mundo_t *m; - if( !(m = malloc(sizeof(mundo_t))) ) - return NULL; - + if ( !(m = malloc(sizeof(mundo_t))) ) + MEM_ERROR_EXIT; + m->tempo_atual = 0; - if( !(m->habilidades = cria_cjt()) ) { - free(m); - return NULL; - } - /* TODO: terminar essa função */ + m->tamanho_mundo = 20000; + + const int n_habilidades = 10; + if ( !(m->cj_habilidades = cria_cjt(n_habilidades)) ) + MEM_ERROR_EXIT; + int i; + for (i = 0; i < n_habilidades; i++) + insere_cjt(m->cj_habilidades, i); + + m->n_herois = n_habilidades * 5; + m->n_locais = m->n_herois / 6; + + if ( !(m->herois = malloc(m->n_herois * sizeof(heroi_t))) ) + MEM_ERROR_EXIT; + for (i = 0; i < m->n_herois; i++) + m->herois[i] = inicializa_heroi(i, m->cj_habilidades); + + if ( !(m->locais = malloc(m->n_locais * sizeof(local_t))) ) + MEM_ERROR_EXIT; + for (i = 0; i < m->n_locais; i++) + m->locais[i] = inicializa_local(i, m->tamanho_mundo); return m; } +evento_t *cria_evento(int tempo, int tipo, int dado1, int dado2) { + evento_t *e; + if ( !(e = malloc(sizeof(evento_t))) ) + MEM_ERROR_EXIT; + + e->tempo = tempo; + e->tipo = tipo; + e->dado1 = dado1; + e->dado2 = dado2; + + return e; +} + +void trata_evento(evento_t evento, lef_t *lista_de_eventos) { + +} + +/* lef_t *inicializa_lista_de_eventos() */ + int main() { - srand(0); + srand(time(0)); + const int fim_do_mundo = 34944; + const int n_missoes = fim_do_mundo / 100; + + mundo_t *mundo; + mundo = cria_mundo(); + printf("inicializou o mundo!\n"); + printf("tamanho do mundo: %d\n", mundo->tamanho_mundo); + printf("conjunto de habilidades do mundo:\n"); + imprime_cjt(mundo->cj_habilidades); + printf("conjunto de habilidades dos herois do mundo:\n"); + int i; + for (i = 0; i < mundo->n_herois; i++) + imprime_cjt(mundo->herois[i].habilidades_do_heroi); + + lef_t *lista_de_eventos; + if ( !(lista_de_eventos = cria_lef()) ) + MEM_ERROR_EXIT; + + /* ciclo */ + /* retira o primeiro evento da lista_de_eventos; */ + /* atualiza o estado do sistema; */ + /* agenda os novos eventos na lista_de_eventos; */ + /* (mundo->tempo_atual)++; */ + + lista_de_eventos = destroi_lef(lista_de_eventos); return 0; } diff --git a/t1/mundo.o b/t1/mundo.o new file mode 100644 index 0000000000000000000000000000000000000000..df506fe03d7908648f8b18fbbe43f97b7dfe3f7c Binary files /dev/null and b/t1/mundo.o differ diff --git a/t1/teste.c b/t1/teste.c new file mode 100644 index 0000000000000000000000000000000000000000..9c63406d0376651bfa5f2d0c2b6ea43db0af1a89 --- /dev/null +++ b/t1/teste.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.h> + +int cria_inteiro(int valor) { + int i; + + i = valor; + + return i; +} + +int main() { + int tam; + printf("Tamanho: "); + scanf("%d", &tam); + + int *vetor; + if ( !(vetor = malloc(tam * sizeof(int))) ) { + printf("ERRO COMPRA MAIS RAM"); + return 1; + } + + int i; + for (i = 0; i < tam; i++) { + vetor[i] = cria_inteiro(i); + } + + for (i = 0; i < tam; i++) + printf("%d ", vetor[i]); + printf("\n"); + + free(vetor); + return 0; +}