diff --git a/t1/] b/t1/] new file mode 100644 index 0000000000000000000000000000000000000000..d1bb855a6614d0e4c5551c1d8411de7ad7617976 --- /dev/null +++ b/t1/] @@ -0,0 +1,232 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <math.h> +#include "libconjunto.h" +#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 +#define FIM 3 + +typedef struct { + int id; + int paciencia; + int idade; + int exp; + conjunto_t *habilidades_do_heroi; +} heroi_t; + +typedef struct { + int id; + int lotacao_max; + conjunto_t *publico; + fila_t *fila; + int posx; + int posy; +} local_t; + +typedef struct { + int tempo_atual; + int tamanho_mundo; /* o mundo é um quadrado */ + int fim_do_mundo; + int n_missoes; + conjunto_t *cj_habilidades; + int n_herois; + int n_locais; + heroi_t *herois; + local_t *locais; +} mundo_t; + +/* retorna um inteiro aleatório entre a e b (inclusive) */ +int aleat(int a, int b) { + return rand() % b + 1 + a; +} + +/* 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); +} + +int vazia_lef(lef_t *l) { + return l->Primeiro == 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; + + 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; + + 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; +} + +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; +} + +mundo_t *cria_mundo(lef_t *lista_de_eventos) { + mundo_t *m; + if ( !(m = malloc(sizeof(mundo_t))) ) + MEM_ERROR_EXIT; + + m->tempo_atual = 0; + m->tamanho_mundo = 20000; + m->fim_do_mundo = 34944; + m->n_missoes = m->fim_do_mundo / 100; + + 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; + + /* cria vetor de heróis e preenche */ + 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); + + /* cria vetor de locais e preenche */ + 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); + + /* para cada herói, cria evento de chegada e insere na lef */ + /* evento_t *evento_chegada_heroi; */ + for (i = 0; i < m->n_herois; i++) { + /* evento_chegada_heroi = cria_evento(aleat(0, 96*7), CHEGADA, m->herois[i].id, aleat(0, n_locais-1)); */ + /* TODO: testar se funciona alocar o evento estaticamente desse jeito */ + evento_t evento_chegada_heroi = { + aleat(0, 96*7), + CHEGADA, + m->herois[i].id, + aleat(0, m->n_locais-1) + }; + if ( !(adiciona_ordem_lef(lista_de_eventos, &evento_chegada_heroi)) ) + MEM_ERROR_EXIT; + } + + /* para cada missao, cria evento e insere na lef */ + for (i = 0; i < m->n_missoes; i++) { + /* evento_chegada_heroi = cria_evento(aleat(0, 96*7), CHEGADA, m->herois[i].id, aleat(0, n_locais-1)); */ + /* TODO: testar se funciona alocar o evento estaticamente desse jeito */ + evento_t evento_missao = { + aleat(0, m->fim_do_mundo), + MISSAO, + i, + 0 + }; + if ( !(adiciona_ordem_lef(lista_de_eventos, &evento_missao)) ) + MEM_ERROR_EXIT; + } + + + /* cria evento de fim e insere na lef */ + /* TODO: testar se funciona alocar o evento estaticamente desse jeito */ + evento_t fim = { + m->fim_do_mundo, + FIM, + 0, + 0, + }; + if ( !(adiciona_ordem_lef(lista_de_eventos, &fim)) ) + MEM_ERROR_EXIT; + + return m; +} + +/* lef_t *inicializa_lista_de_eventos() */ + +int main() { + srand(time(0)); + + lef_t *lista_de_eventos; + if ( !(lista_de_eventos = cria_lef()) ) + MEM_ERROR_EXIT; + + mundo_t *mundo; + mundo = cria_mundo(lista_de_eventos); + 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); + + int i = 0; + nodo_lef_t *exemplo = lista_de_eventos->Primeiro; + printf("eventos alocados na lef:\n"); + while (exemplo) { + printf("tempo do evento %d: %d\n", i, exemplo->evento->tempo); + i++; + exemplo = exemplo->prox; + } + + /* ciclo */ + /* retira o primeiro evento da lista_de_eventos; */ + evento_t *evento_atual; + evento_atual = obtem_primeiro_lef(lista_de_eventos); + /* atualiza o estado do sistema; */ + switch (evento_atual->tipo) { + case CHEGADA: + break; + case SAIDA: + break; + case MISSAO: + break; + case FIM: + break; + } + /* agenda os novos eventos na lista_de_eventos; */ + + lista_de_eventos = destroi_lef(lista_de_eventos); + + return 0; +} diff --git a/t1/liblef.c b/t1/liblef.c index b1a35827f13eda0319e8b1dd3ba0fdd0ce3c4f80..aaaf468035e828cccdb7e128e39d78d8f58a9dd3 100644 --- a/t1/liblef.c +++ b/t1/liblef.c @@ -63,7 +63,7 @@ int adiciona_ordem_lef(lef_t *l, evento_t *evento) { novo_nodo->prox = l->Primeiro; novo_nodo->evento = novo_evento; - if (novo_nodo->prox == NULL) { + if (l->Primeiro == NULL || l->Primeiro->evento->tempo > novo_nodo->evento->tempo) { l->Primeiro = novo_nodo; return 1; } diff --git a/t1/liblef.o b/t1/liblef.o index a96efd52d0df3a735ec4284cd0f4344d5ba8f4e2..250aecd3e0c86b486ac523edce0624a72bd8197a 100644 Binary files a/t1/liblef.o and b/t1/liblef.o differ diff --git a/t1/mundo b/t1/mundo index b53a829938eb3e6f24afc750ad1d4fd7670b46f5..8033d2a53664c1b967e4cb8ede56c7e5e249b5c4 100755 Binary files a/t1/mundo and b/t1/mundo differ diff --git a/t1/mundo.c b/t1/mundo.c index 96bf90ed890228fed09b564ee4bcc5f24180694d..33d245b05b45ac6c015c18411a2e567ec00cf18a 100644 --- a/t1/mundo.c +++ b/t1/mundo.c @@ -37,6 +37,8 @@ typedef struct { typedef struct { int tempo_atual; int tamanho_mundo; /* o mundo é um quadrado */ + int fim_do_mundo; + int n_missoes; conjunto_t *cj_habilidades; int n_herois; int n_locais; @@ -57,6 +59,10 @@ double distancia(local_t loc1, local_t loc2) { return sqrt((double)quadrado_da_distancia); } +int vazia_lef(lef_t *l) { + return l->Primeiro == NULL; +} + heroi_t inicializa_heroi(int id, conjunto_t *conjunto_de_habilidades) { heroi_t h; @@ -89,13 +95,28 @@ local_t inicializa_local(int id, int tamanho_mundo) { return l; } -mundo_t *cria_mundo() { +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; +} + +mundo_t *cria_mundo(lef_t *lista_de_eventos) { mundo_t *m; if ( !(m = malloc(sizeof(mundo_t))) ) MEM_ERROR_EXIT; m->tempo_atual = 0; m->tamanho_mundo = 20000; + m->fim_do_mundo = 34944; + m->n_missoes = m->fim_do_mundo / 100; const int n_habilidades = 10; if ( !(m->cj_habilidades = cria_cjt(n_habilidades)) ) @@ -107,63 +128,121 @@ mundo_t *cria_mundo() { m->n_herois = n_habilidades * 5; m->n_locais = m->n_herois / 6; + /* cria vetor de heróis e preenche */ 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); + /* cria vetor de locais e preenche */ 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; -} + /* para cada herói, cria evento de chegada e insere na lef */ + /* evento_t *evento_chegada_heroi; */ + for (i = 0; i < m->n_herois; i++) { + /* evento_chegada_heroi = cria_evento(aleat(0, 96*7), CHEGADA, m->herois[i].id, aleat(0, n_locais-1)); */ + /* TODO: testar se funciona alocar o evento estaticamente desse jeito */ + evento_t evento_chegada_heroi = { + aleat(0, 96*7), + CHEGADA, + m->herois[i].id, + aleat(0, m->n_locais-1) + }; + if ( !(adiciona_ordem_lef(lista_de_eventos, &evento_chegada_heroi)) ) + MEM_ERROR_EXIT; + } + + /* para cada missao, cria evento e insere na lef */ + for (i = 0; i < m->n_missoes; i++) { + /* evento_chegada_heroi = cria_evento(aleat(0, 96*7), CHEGADA, m->herois[i].id, aleat(0, n_locais-1)); */ + /* TODO: testar se funciona alocar o evento estaticamente desse jeito */ + evento_t evento_missao = { + aleat(0, m->fim_do_mundo), + MISSAO, + i, + 0 + }; + if ( !(adiciona_ordem_lef(lista_de_eventos, &evento_missao)) ) + MEM_ERROR_EXIT; + } + -evento_t *cria_evento(int tempo, int tipo, int dado1, int dado2) { - evento_t *e; - if ( !(e = malloc(sizeof(evento_t))) ) + /* cria evento de fim e insere na lef */ + /* TODO: testar se funciona alocar o evento estaticamente desse jeito */ + evento_t fim = { + m->fim_do_mundo, + FIM, + 0, + 0, + }; + if ( !(adiciona_ordem_lef(lista_de_eventos, &fim)) ) 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) { - + return m; } /* lef_t *inicializa_lista_de_eventos() */ int main() { srand(time(0)); - const int fim_do_mundo = 34944; - const int n_missoes = fim_do_mundo / 100; + + lef_t *lista_de_eventos; + if ( !(lista_de_eventos = cria_lef()) ) + MEM_ERROR_EXIT; mundo_t *mundo; - mundo = cria_mundo(); + mundo = cria_mundo(lista_de_eventos); 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; + + int i = 0; + nodo_lef_t *exemplo = lista_de_eventos->Primeiro; + printf("eventos alocados na lef:\n"); + while (exemplo) { + printf("tempo do evento %d: %d\n", i, exemplo->evento->tempo); + printf(" tipo do evento %d: ", i); + switch (exemplo->evento->tipo) { + case CHEGADA: + printf("CHEGADA\n"); + break; + case SAIDA: + printf("SAIDA\n"); + break; + case MISSAO: + printf("MISSAO\n"); + break; + case FIM: + printf("FIM\n"); + break; + default: + printf("deu alguma coisa muito louca aqui\n"); + break; + } + i++; + exemplo = exemplo->prox; + } /* ciclo */ /* retira o primeiro evento da lista_de_eventos; */ + evento_t *evento_atual; + evento_atual = obtem_primeiro_lef(lista_de_eventos); /* atualiza o estado do sistema; */ + switch (evento_atual->tipo) { + case CHEGADA: + break; + case SAIDA: + break; + case MISSAO: + break; + case FIM: + break; + } /* agenda os novos eventos na lista_de_eventos; */ - /* (mundo->tempo_atual)++; */ lista_de_eventos = destroi_lef(lista_de_eventos); diff --git a/t1/mundo.o b/t1/mundo.o index df506fe03d7908648f8b18fbbe43f97b7dfe3f7c..5460770afb1c88e350aa9e31063bc84ba152c2a1 100644 Binary files a/t1/mundo.o and b/t1/mundo.o differ