shithub: rott

Download patch

ref: 4be0dbd606248cf4dc6add7a92d4de2361034892
parent: 78bf67af9a8d7da4f70eddba499d257856ba6364
author: LTCHIPS <ltchips994@gmail.com>
date: Mon Oct 29 16:22:22 EDT 2018

rewrote queue functions, fixed zomrott error that was caused by a null actor state after loading a save

--- a/src/queue.c
+++ b/src/queue.c
@@ -1,102 +1,94 @@
-#include <stdio.h>
+//Copyright (C) 2017-2018  Steven LeVesque
+
 #include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
 #include "queue.h"
+#include <string.h>
 
-//Fetched from https://codereview.stackexchange.com/questions/141238/implementing-a-generic-queue-in-c
 
-
-void queueInit(Queue *q, size_t memSize)
+void InitQueue(Queue* queue,size_t sizeOfItem)
 {
-   q->sizeOfQueue = 0;
-   q->memSize = memSize;
-   q->head = q->tail = NULL;
+    queue->NumOfItems = 0;
+    queue->SizeOfItem = sizeOfItem;
+    queue->Head = NULL;
+    queue->Tail = queue->Head;
+
 }
 
-int enqueue(Queue *q, const void *data)
+void Enqueue(Queue* queue, const void  * item)
 {
-    node *newNode = (node *)malloc(sizeof(node));
-
-    if(newNode == NULL)
+    Node * newNode = (Node *) malloc(sizeof(Node));
+    
+    newNode->next = NULL;
+    
+    newNode->data =  malloc(queue->SizeOfItem);
+    
+    memcpy(newNode->data, item, queue->SizeOfItem);
+    
+    if (queue->NumOfItems == 0)
     {
-        return -1;
+        queue->Head = newNode;
+        queue->Tail = newNode;
     }
-
-    newNode->data = malloc(q->memSize);
-
-    if(newNode->data == NULL)
+    else if (queue->Head != NULL)
     {
-        free(newNode);
-        return -1;
+        newNode->next = queue->Head;
+        if (queue->Tail == queue->Head)
+            queue->Tail = newNode->next;
+        queue->Head = newNode;
     }
+    
+    queue->NumOfItems++;
+    
+}
 
-    newNode->next = NULL;
-
-    memcpy(newNode->data, data, q->memSize);
-
-    if(q->sizeOfQueue == 0)
+void Dequeue(Queue* queue)
+{
+    if (queue->NumOfItems == 0)
     {
-        q->head = q->tail = newNode;
+        return;
     }
-    else
+    else if (queue->NumOfItems == 1)
     {
-        q->tail->next = newNode;
-        q->tail = newNode;
+        
+        free(queue->Head->data);
+        free(queue->Head);
+        queue->Head = NULL;
+        queue->Tail = NULL;
+        
+        queue->NumOfItems--;
     }
-
-    q->sizeOfQueue++;
-    return 0;
-}
-
-void dequeue(Queue *q, void *data)
-{
-    if(q->sizeOfQueue > 0)
+    else 
     {
-        node *temp = q->head;
-        memcpy(data, temp->data, q->memSize);
+        
+        Node * tempNode = queue->Head;
+        
+        queue->Head = queue->Head->next;
+        
+        free(tempNode->data);
+        free(tempNode);
+        
+        queue->NumOfItems--;
+        
+    }    
 
-        if(q->sizeOfQueue > 1)
-        {
-            q->head = q->head->next;
-        }
-        else
-        {
-            q->head = NULL;
-            q->tail = NULL;
-        }
-
-        q->sizeOfQueue--;
-        free(temp->data);
-        free(temp);
-    }
 }
 
-void queuePeek(Queue *q, void *data)
+void ClearQueue(Queue* queue)
 {
-    if(q->sizeOfQueue > 0)
+    Node * currNode = queue->Head;
+    
+    Node * tempNode = NULL;
+    while(queue->NumOfItems > 0)
     {
-       node *temp = q->head;
-       memcpy(data, temp->data, q->memSize);
+        tempNode = currNode;
+        currNode = tempNode->next;
+        //free(tempNode->data);
+        free(tempNode);
+        queue->NumOfItems--;
+        
     }
-}
+    queue->Head = NULL;
+    queue->Tail = NULL;
 
-void clearQueue(Queue *q)
-{
-  node *temp;
-
-  while(q->sizeOfQueue > 0)
-  {
-      temp = q->head;
-      q->head = temp->next;
-      free(temp->data);
-      free(temp);
-      q->sizeOfQueue--;
-  }
-
-  q->head = q->tail = NULL;
 }
-
-int getQueueSize(Queue *q)
-{
-    return q->sizeOfQueue;
-}
\ No newline at end of file
--- a/src/queue.h
+++ b/src/queue.h
@@ -1,28 +1,35 @@
-#ifndef QUEUE_H_INCLUDED
-#define QUEUE_H_INCLUDED
+//Copyright (C) 2017-2018  Steven LeVesque
 
-//Fetched from https://codereview.stackexchange.com/questions/141238/implementing-a-generic-queue-in-c
+#ifndef QUEUE_H
+#define QUEUE_H
 
-
-typedef struct Node
+typedef struct Node 
 {
-  void *data;
-  struct Node *next;
-}node;
+    void * data;
+    struct Node * next;
 
-typedef struct QueueList
+} Node;
+
+typedef struct Queue 
 {
-    int sizeOfQueue;
-    size_t memSize;
-    node *head;
-    node *tail;
-}Queue;
+    int NumOfItems;
+    size_t SizeOfItem;
+    Node * Head;
+    Node * Tail;
 
-void queueInit(Queue *q, size_t memSize);
-int enqueue(Queue *, const void *);
-void dequeue(Queue *, void *);
-void queuePeek(Queue *, void *);
-void clearQueue(Queue *);
-int getQueueSize(Queue *);
+} Queue;
 
-#endif /* QUEUE_H_INCLUDED */
\ No newline at end of file
+void InitQueue(Queue*,size_t);
+
+void Enqueue(Queue*, const void  *);
+
+void Dequeue(Queue*);
+
+void ClearQueue(Queue*);
+
+//void PrintQueue(Queue*);
+
+
+
+#endif /* QUEUE_H */
+
--- a/src/rt_actor.c
+++ b/src/rt_actor.c
@@ -3627,30 +3627,30 @@
     switch(ob->obclass)
     {
         case lowguardobj:
-            enqueue(enemiesToRes[0], ob);
+            Enqueue(enemiesToRes[0], ob);
             break;
         case highguardobj:
-            enqueue(enemiesToRes[1], ob);
+            Enqueue(enemiesToRes[1], ob);
             break;
 
         case strikeguardobj:
-            enqueue(enemiesToRes[2], ob);
+            Enqueue(enemiesToRes[2], ob);
             break;
         case blitzguardobj:
-            enqueue(enemiesToRes[3], ob);
+            Enqueue(enemiesToRes[3], ob);
             break;
         case triadenforcerobj:
-            enqueue(enemiesToRes[4], ob);
+            Enqueue(enemiesToRes[4], ob);
             break;
     #if (SHAREWARE == 0)
         case overpatrolobj:
-            enqueue(enemiesToRes[5], ob);
+            Enqueue(enemiesToRes[5], ob);
             break;
         case deathmonkobj:
-            enqueue(enemiesToRes[6], ob);
+            Enqueue(enemiesToRes[6], ob);
             break;
         case dfiremonkobj:
-            enqueue(enemiesToRes[7], ob);
+            Enqueue(enemiesToRes[7], ob);
             break;
     #endif
         default:
@@ -3665,7 +3665,7 @@
     int x = 0;
     for (x = 0; x < 8; x++)
     {
-        clearQueue(enemiesToRes[x]);
+        ClearQueue(enemiesToRes[x]);
     }
 }
 
@@ -3736,16 +3736,17 @@
     
     for (index = 0; index < 8; index++)
     {
-        if (enemiesToRes[index]->sizeOfQueue == 0)
+        if (enemiesToRes[index]->NumOfItems == 0)
         {
             continue;
         }
-        actor = enemiesToRes[index]->head->data;
+        actor = enemiesToRes[index]->Head->data;
+        
         if (currTime >= actor->resurrectAtTime)
         {
             SD_PlaySoundRTP(SD_PLAYERSPAWNSND, actor->x, actor->y);
             SpawnDuringGameWithState (actor->obclass,actor->tilex,actor->tiley,actor->dir, 1, actor->state);
-            dequeue(enemiesToRes[index], actor);
+            Dequeue(enemiesToRes[index]);
             gamestate.killcount--;
         }
     }
--- a/src/rt_game.c
+++ b/src/rt_game.c
@@ -4973,18 +4973,18 @@
         for (z = 0; z < 8; z++)
         {
             size = sizeof(int);
-            SafeWrite(savehandle,&enemiesToRes[z]->sizeOfQueue, size);
-            if (enemiesToRes[z]->sizeOfQueue == 0)
+            SafeWrite(savehandle,&enemiesToRes[z]->NumOfItems, size);
+            if (enemiesToRes[z]->NumOfItems == 0)
             {
                 continue;
             }
             
             int x;
-            node * thingToSave = enemiesToRes[z]->head;
+            Node * thingToSave = enemiesToRes[z]->Head;
             size = sizeof(objtype);
-            for (x = 0; x < enemiesToRes[z]->sizeOfQueue; x++)
+            for (x = 0; x < enemiesToRes[z]->NumOfItems; x++)
             {
-                SafeWrite(savehandle, (objtype *) thingToSave->data, size);
+                SafeWrite(savehandle, thingToSave->data, size);
                 thingToSave = thingToSave->next;
             }
         }
@@ -5389,7 +5389,7 @@
             origQueueSize = 0;
                 
             enemyQueue = malloc(sizeof(Queue));
-            queueInit(enemyQueue, sizeof(objtype));
+            InitQueue(enemyQueue, sizeof(objtype));
                 
             memcpy(&origQueueSize, bufptr, size);
             bufptr+=size;
@@ -5407,8 +5407,11 @@
                 objtype * item = (objtype *) malloc(sizeof(objtype));
             
                 memcpy(item, bufptr, size);
+                
+                //fix for crash that occurs after something tries to respawn after the game is loaded
+                SetReverseDeathState(item);
             
-                enqueue(enemyQueue, item);
+                Enqueue(enemyQueue, item);
             
                 bufptr+=size;
             
--- a/src/rt_ted.c
+++ b/src/rt_ted.c
@@ -5313,7 +5313,7 @@
             //clearQueue(enemiesToRes[x]);
         //}
         Queue * enemyQueue = malloc(sizeof(Queue));
-        queueInit(enemyQueue, sizeof(objtype));
+        InitQueue(enemyQueue, sizeof(objtype));
         enemiesToRes[x] = enemyQueue;
     }
 }