From f5a587e7c1c05fa40a14fccc821085a841d4a6d6 Mon Sep 17 00:00:00 2001
From: terminx <terminx@1a8010ca-5511-0410-912e-c29ae57300e0>
Date: Sun, 24 Aug 2008 03:19:40 +0000
Subject: [PATCH] git-svn-id: https://svn.eduke32.com/eduke32@1012
 1a8010ca-5511-0410-912e-c29ae57300e0

---
 polymer/eduke32/source/actors.c   | 14 +++----
 polymer/eduke32/source/funct.h    |  2 +-
 polymer/eduke32/source/game.c     | 69 ++++++++++++++++++-------------
 polymer/eduke32/source/gameexec.c | 48 +++++++++++++--------
 polymer/eduke32/source/gamevars.c |  4 +-
 5 files changed, 81 insertions(+), 56 deletions(-)

diff --git a/polymer/eduke32/source/actors.c b/polymer/eduke32/source/actors.c
index 69f0a531c..e8fead31d 100644
--- a/polymer/eduke32/source/actors.c
+++ b/polymer/eduke32/source/actors.c
@@ -7566,8 +7566,6 @@ BOLT:
 
 void moveobjects(void)
 {
-    int k = 0;
-
     movefta();              //ST 2
     moveweapons();          //ST 4
     movetransports();       //ST 9
@@ -7583,20 +7581,20 @@ void moveobjects(void)
 
     if (apScriptGameEvent[EVENT_GAME])
     {
-        int i, p, j;
+        int i, p, j, k = MAXSTATUS-1, pl;
 
-        for (;k<MAXSTATUS;k++)
+        do
         {
             i = headspritestat[k];
-            while (i >= 0)
+            if (i < 0) continue;
+            do
             {
-                int pl;
                 j = nextspritestat[i];
                 pl=findplayer(&sprite[i],&p);
                 OnEvent(EVENT_GAME,i, pl, p);
                 i = j;
-            }
-        }
+            } while (i >= 0);
+        } while (--k);
     }
 
     doanimations();
diff --git a/polymer/eduke32/source/funct.h b/polymer/eduke32/source/funct.h
index b0d4d18dc..5f51d44e1 100644
--- a/polymer/eduke32/source/funct.h
+++ b/polymer/eduke32/source/funct.h
@@ -225,7 +225,7 @@ extern int ReadGameVars(int fil);
 
 extern int GetGameVarID(int id, int iActor, int iPlayer);
 extern void SetGameVarID(int id, int lValue, int iActor, int iPlayer);
-extern void SetGameArrayID(int id,int index, int lValue);
+// extern void SetGameArrayID(int id,int index, int lValue);
 
 extern int AddGameVar(const char *pszLabel, int lValue, unsigned int dwFlags);
 extern int AddGameArray(const char *pszLabel, int asize);
diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c
index ebf2dcf11..98eb9c3c1 100644
--- a/polymer/eduke32/source/game.c
+++ b/polymer/eduke32/source/game.c
@@ -198,7 +198,7 @@ static int getatoken(scriptfile *sf, tokenlist *tl, int ntokens)
     tok = scriptfile_gettoken(sf);
     if (!tok) return T_EOF;
 
-    for (i=0;i<ntokens;i++)
+    for (i=ntokens-1;i>=0;i--)
     {
         if (!Bstrcasecmp(tok, tl[i].text))
             return tl[i].tokenid;
@@ -480,7 +480,7 @@ static void allowtimetocorrecterrorswhenquitting(void)
 
     ready2send = 0;
 
-    for (j=0;j<8;j++)
+    for (j=7;j>=0;j--)
     {
         oldtotalclock = totalclock;
 
@@ -633,7 +633,7 @@ void getpackets(void)
 
             for (i=connecthead;i>=0;i=connectpoint2[i])
                 if (i != myconnectindex)
-                    for (j=1;j<movesperpacket;j++)
+                    for (j=movesperpacket-1;j>=1;j--)
                     {
                         copybufbyte(&nsyn[i],&inputfifo[g_player[i].movefifoend&(MOVEFIFOSIZ-1)][i],sizeof(input_t));
                         g_player[i].movefifoend++;
@@ -670,7 +670,7 @@ void getpackets(void)
                 g_player[other].syncvalhead++;
             }
 
-            for (i=1;i<movesperpacket;i++)
+            for (i=movesperpacket-1;i>=1;i--)
             {
                 copybufbyte(&nsyn[other],&inputfifo[g_player[other].movefifoend&(MOVEFIFOSIZ-1)][other],sizeof(input_t));
                 g_player[other].movefifoend++;
@@ -714,7 +714,7 @@ void getpackets(void)
             if (k&2048) nsyn[other].extbits = ((nsyn[other].extbits&0x00ffffff)|((int)packbuf[j++])<<24);
             g_player[other].movefifoend++;
 
-            for (i=1;i<movesperpacket;i++)
+            for (i=movesperpacket-1;i>=1;i--)
             {
                 copybufbyte(&nsyn[other],&inputfifo[g_player[other].movefifoend&(MOVEFIFOSIZ-1)][other],sizeof(input_t));
                 g_player[other].movefifoend++;
@@ -966,7 +966,7 @@ void getpackets(void)
                     adduserquote(tempbuf);
                     Bsprintf(tempbuf,"PRESS F1 TO VOTE YES, F2 TO VOTE NO");
                     adduserquote(tempbuf);
-                    for (i=0;i<MAXPLAYERS;i++)
+                    for (i=MAXPLAYERS-1;i>=0;i--)
                     {
                         g_player[i].vote = 0;
                         g_player[i].gotvote = 0;
@@ -979,13 +979,13 @@ void getpackets(void)
                     {
                         voting = -1;
                         i = 0;
-                        for (j=0;j<MAXPLAYERS;j++)
+                        for (j=MAXPLAYERS-1;j>=0;j--)
                             i += g_player[j].gotvote;
 
                         if (i != numplayers)
                             Bsprintf(tempbuf,"%s^00 HAS CANCELED THE VOTE",g_player[(unsigned char)packbuf[2]].user_name);
                         else Bsprintf(tempbuf,"VOTE FAILED");
-                        for (i=0;i<MAXPLAYERS;i++)
+                        for (i=MAXPLAYERS-1;i>=0;i--)
                         {
                             g_player[i].vote = 0;
                             g_player[i].gotvote = 0;
@@ -1353,7 +1353,7 @@ static void caches(void)
     short i,k;
 
     k = 0;
-    for (i=0;i<cacnum;i++)
+    for (i=cacnum-1;i>=0;i--)
         if ((*cac[i].lock) >= 200)
         {
             Bsprintf(tempbuf,"Locked- %d: Leng:%d, Lock:%d",i,cac[i].leng,*cac[i].lock);
@@ -1363,7 +1363,7 @@ static void caches(void)
 
     k += 6;
 
-    for (i=1;i<11;i++)
+    for (i=10;i>=0;i--)
         if (lumplockbyte[i] >= 200)
         {
             Bsprintf(tempbuf,"RTS Locked %d:",i);
@@ -1809,7 +1809,7 @@ static void digitalnumber(int x,int y,int n,char s,char cs)
     Bsnprintf(b,10,"%d",n);
     i = Bstrlen(b);
 
-    for (k=0;k<i;k++)
+    for (k=i-1;k>=0;k--)
     {
         p = DIGITALNUM+*(b+k)-'0';
         j += tilesizx[p]+1;
@@ -1835,7 +1835,7 @@ void txdigitalnumberz(int starttile, int x,int y,int n,int s,int pal,int cs,int
     Bsnprintf(b,10,"%d",n);
     i = Bstrlen(b);
 
-    for (k=0;k<i;k++)
+    for (k=i-1;k>=0;k--)
     {
         p = starttile+*(b+k)-'0';
         j += (tilesizx[p]+1)*z/65536;
@@ -1867,7 +1867,7 @@ static void altdigitalnumber(int x,int y,int n,char s,char cs)
     Bsnprintf(b,10,"%d",n);
     i = Bstrlen(b);
 
-    for (k=0;k<i;k++)
+    for (k=i-1;k>=0;k--)
     {
         p = althud_numbertile+*(b+k)-'0';
         j += tilesizx[p]+1;
@@ -2908,7 +2908,7 @@ static int strget_(int small,int x,int y,char *t,int dalen,int c)
         char b[91],ii;
         for (ii=0;ii<inputloc;ii++)
             b[(unsigned char)ii] = '*';
-        b[(unsigned char)ii] = 0;
+        b[(unsigned char)inputloc] = 0;
         if (g_player[myconnectindex].ps->gm&MODE_TYPE)
             x = mpgametext(y,b,c,2+8+16);
         else x = gametext(x,y,b,c,2+8+16);
@@ -3111,7 +3111,7 @@ static void moveclouds(void)
 
         cloudtotalclock = totalclock+6;
 
-        for (i=0;i<numclouds;i++)
+        for (i=numclouds-1;i>=0;i--)
         {
             cloudx[i] += (sintable[(g_player[screenpeek].ps->ang+512)&2047]>>9);
             cloudy[i] += (sintable[g_player[screenpeek].ps->ang&2047]>>9);
@@ -3139,7 +3139,7 @@ static void drawoverheadmap(int cposx, int cposy, int czoom, short cang)
     yvect2 = mulscale16(yvect,yxaspect);
 
     //Draw red lines
-    for (i=0;i<numsectors;i++)
+    for (i=numsectors-1;i>=0;i--)
     {
         if (!(show2dsector[i>>3]&(1<<(i&7)))) continue;
 
@@ -3185,7 +3185,7 @@ static void drawoverheadmap(int cposx, int cposy, int czoom, short cang)
 
     //Draw sprites
     k = g_player[screenpeek].ps->i;
-    for (i=0;i<numsectors;i++)
+    for (i=numsectors-1;i>=0;i--)
     {
         if (!(show2dsector[i>>3]&(1<<(i&7)))) continue;
         for (j=headspritesect[i];j>=0;j=nextspritesect[j])
@@ -3331,7 +3331,7 @@ static void drawoverheadmap(int cposx, int cposy, int czoom, short cang)
     }
 
     //Draw white lines
-    for (i=0;i<numsectors;i++)
+    for (i=numsectors-1;i>=0;i--)
     {
         if (!(show2dsector[i>>3]&(1<<(i&7)))) continue;
 
@@ -3479,7 +3479,7 @@ void SetCrosshairColor(int r, int g, int b)
         ptr++;
         ii--;
     }
-    for (i = 0; i < 256; i++)
+    for (i = 255; i >= 0; i--)
         tempbuf[i] = i;
     makepalookup(CROSSHAIR_PAL,tempbuf,crosshair_colors.r>>2, crosshair_colors.g>>2, crosshair_colors.b>>2,1);
 
@@ -4425,7 +4425,7 @@ void displayrooms(int snum,int smoothratio)
         {
             dst = 0x7fffffff;
             i = 0;
-            for (k=0;k<mirrorcnt;k++)
+            for (k=mirrorcnt-1;k>=0;k--)
             {
                 j = klabs(wall[mirrorwall[k]].x-ud.camerax);
                 j += klabs(wall[mirrorwall[k]].y-ud.cameray);
@@ -6357,7 +6357,7 @@ int spawn(int j, int pn)
                         sector[sect].hitag = i;
                     }
 
-                    for (j = 0;j < MAXSPRITES;j++)
+                    for (j = MAXSPRITES-1;j>=0;j--)
                     {
                         if (sprite[j].statnum < MAXSTATUS)
                             if (sprite[j].picnum == SECTOREFFECTOR &&
@@ -6372,7 +6372,7 @@ int spawn(int j, int pn)
                                 break;
                             }
                     }
-                    if (j == MAXSPRITES)
+                    if (j == -1)
                     {
                         Bsprintf(tempbuf,"Found lonely Sector Effector (lotag 0) at (%d,%d)\n",sp->x,sp->y);
                         gameexit(tempbuf);
@@ -6609,7 +6609,9 @@ void animatesprites(int x,int y,int a,int smoothratio)
     spritetype *s,*t;
     int switchpic;
 
-    for (j=0;j < spritesortcnt; j++)
+    if (!spritesortcnt) return;
+
+    for (j=spritesortcnt-1;j>=0; j--)
     {
         t = &tsprite[j];
         i = t->owner;
@@ -6703,7 +6705,7 @@ void animatesprites(int x,int y,int a,int smoothratio)
         t->shade = l;
     }
 
-    for (j=0;j < spritesortcnt; j++) //Between drawrooms() and drawmasks()
+    for (j=spritesortcnt-1;j>=0; j--) //Between drawrooms() and drawmasks()
     {
         //is the perfect time to animate sprites
         t = &tsprite[j];
@@ -7430,14 +7432,25 @@ PALONLY:
         if (sector[t->sectnum].floorpicnum == MIRROR)
             t->xrepeat = t->yrepeat = 0;
     }
-    for (j=spritesortcnt-1;j>=0; j--)
+
+    j = spritesortcnt-1;
+    do
     {
         if (display_mirror) tsprite[j].statnum = TSPR_MIRROR;
-        if (tsprite[j].owner > 0 && tsprite[j].owner < MAXSPRITES && spriteext[tsprite[j].owner].flags & SPREXT_TSPRACCESS) 	         OnEvent(EVENT_ANIMATESPRITES, j, myconnectindex, -1);
+        if (spriteext[tsprite[j].owner].flags & SPREXT_TSPRACCESS && tsprite[j].owner < MAXSPRITES && tsprite[j].owner > 0)
         { 	 
-            OnEvent(EVENT_ANIMATESPRITES,tsprite[j].owner, myconnectindex, -1); 	 
-            spriteext[tsprite[j].owner].tspr = NULL; 	 
+            OnEvent(EVENT_ANIMATESPRITES,tsprite[j].owner, myconnectindex, -1);
+            spriteext[tsprite[j].owner].tspr = NULL;
         }
+    } while (--j > 0);
+
+    if (j < 0) return;
+
+    if (display_mirror) tsprite[j].statnum = TSPR_MIRROR;
+    if (spriteext[tsprite[j].owner].flags & SPREXT_TSPRACCESS && tsprite[j].owner < MAXSPRITES && tsprite[j].owner > 0)
+    { 	 
+        OnEvent(EVENT_ANIMATESPRITES,tsprite[j].owner, myconnectindex, -1);
+        spriteext[tsprite[j].owner].tspr = NULL;
     }
 }
 #ifdef _MSC_VER
diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c
index 4890e0556..827b66cf9 100644
--- a/polymer/eduke32/source/gameexec.c
+++ b/polymer/eduke32/source/gameexec.c
@@ -7005,10 +7005,12 @@ static int parse(void)
             int lType=*insptr++, lMaxDist=*insptr++, lVarID=*insptr++;
             int lFound=-1, lTemp, j, k;
 
-            for (k=0;k<MAXSTATUS;k++)
+            k = MAXSTATUS-1;
+            do
             {
                 j=headspritestat[(tw==CON_FINDNEARACTOR||tw==CON_FINDNEARACTOR3D)?1:k];    // all sprites
-                while (j>=0)
+                if (j == -1) continue;
+                do
                 {
                     if (sprite[j].picnum == lType && j != g_i)
                     {
@@ -7023,10 +7025,10 @@ static int parse(void)
                         }
                     }
                     j = nextspritestat[j];
-                }
+                } while (j>=0);
                 if ((tw==CON_FINDNEARACTOR||tw==CON_FINDNEARACTOR3D) || j == MAXSPRITES)
                     break;
-            }
+            } while (k--);
             SetGameVarID(lVarID, lFound, g_i, g_p);
             break;
         }
@@ -7045,10 +7047,12 @@ static int parse(void)
             int lType=*insptr++, lMaxDist=GetGameVarID(*insptr++, g_i, g_p), lVarID=*insptr++;
             int lFound=-1, lTemp, j, k;
 
-            for (k=MAXSTATUS-1;k>=0;k--)
+            k=MAXSTATUS-1;
+            do
             {
                 j=headspritestat[(tw==CON_FINDNEARACTORVAR||tw==CON_FINDNEARACTOR3DVAR)?1:k];    // all sprites
-                while (j>=0)
+                if (j == -1) continue;
+                do
                 {
                     if (sprite[j].picnum == lType && j != g_i)
                     {
@@ -7063,10 +7067,10 @@ static int parse(void)
                         }
                     }
                     j = nextspritestat[j];
-                }
+                } while (j >= 0);
                 if ((tw==CON_FINDNEARACTORVAR||tw==CON_FINDNEARACTOR3DVAR) || j == MAXSPRITES)
                     break;
-            }
+            } while (k--);
             SetGameVarID(lVarID, lFound, g_i, g_p);
             break;
         }
@@ -7083,10 +7087,12 @@ static int parse(void)
             int lType=*insptr++, lMaxDist=GetGameVarID(*insptr++, g_i, g_p), lMaxZDist=GetGameVarID(*insptr++, g_i, g_p);
             int lVarID=*insptr++, lFound=-1, lTemp, lTemp2, j, k;
 
-            for (k=MAXSTATUS-1;k>=0;k--)
+            k=MAXSTATUS-1;
+            do
             {
                 j=headspritestat[tw==CON_FINDNEARACTORZVAR?1:k];    // all sprites
-                while (j>=0)
+                if (j == -1) continue;
+                do
                 {
                     if (sprite[j].picnum == lType && j != g_i)
                     {
@@ -7103,10 +7109,10 @@ static int parse(void)
                         }
                     }
                     j = nextspritestat[j];
-                }
+                } while (j>=0);
                 if (tw==CON_FINDNEARACTORZVAR || j == MAXSPRITES)
                     break;
-            }
+            } while (k--);
             SetGameVarID(lVarID, lFound, g_i, g_p);
 
             break;
@@ -7124,10 +7130,12 @@ static int parse(void)
             int lType=*insptr++, lMaxDist=*insptr++, lMaxZDist=*insptr++, lVarID=*insptr++;
             int lTemp, lTemp2, lFound=-1, j, k;
 
-            for (k=MAXSTATUS-1;k>=0;k--)
+            k=MAXSTATUS-1;
+            do
             {
                 j=headspritestat[tw==CON_FINDNEARACTORZ?1:k];    // all sprites
-                while (j>=0)
+                if (j == -1) continue;
+                do
                 {
                     if (sprite[j].picnum == lType && j != g_i)
                     {
@@ -7144,10 +7152,10 @@ static int parse(void)
                         }
                     }
                     j = nextspritestat[j];
-                }
+                } while (j>=0);
                 if (tw==CON_FINDNEARACTORZ || j == MAXSPRITES)
                     break;
-            }
+            } while (k--);
             SetGameVarID(lVarID, lFound, g_i, g_p);
             break;
         }
@@ -7397,7 +7405,13 @@ static int parse(void)
             int index = GetGameVarID(*insptr++, g_i, g_p);
             int value = GetGameVarID(*insptr++, g_i, g_p);
 
-            SetGameArrayID(j,index,value);
+//            SetGameArrayID(j,index,value);
+            if (j<0 || j >= iGameArrayCount || index >= aGameArrays[j].size || index < 0)
+            {
+                OSD_Printf(OSD_ERROR "SetGameVarID(): tried to set invalid array ID (%d) or index out of bounds from sprite %d (%d), player %d\n",j,g_i,sprite[g_i].picnum,g_p);
+                return 0;
+            }
+            aGameArrays[j].plValues[index]=value;
             break;
         }
 
diff --git a/polymer/eduke32/source/gamevars.c b/polymer/eduke32/source/gamevars.c
index cfd107182..c86828bed 100644
--- a/polymer/eduke32/source/gamevars.c
+++ b/polymer/eduke32/source/gamevars.c
@@ -661,7 +661,7 @@ int GetGameVarID(int id, int iActor, int iPlayer)
         return(m * aGameVars[id].lValue);
     }
 }
-
+/*
 void SetGameArrayID(int id,int index, int lValue)
 {
     if (id<0 || id >= iGameArrayCount || !((index < aGameArrays[id].size)&&(index>=0)))
@@ -671,7 +671,7 @@ void SetGameArrayID(int id,int index, int lValue)
     }
     aGameArrays[id].plValues[index]=lValue;
 }
-
+*/
 void SetGameVarID(int id, int lValue, int iActor, int iPlayer)
 {
     if (id<0 || id >= iGameVarCount)