From 70206b3bceebaaf24f56b6e85723f91d00f0a21a Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Sun, 5 Nov 2006 18:35:19 +0000 Subject: [PATCH] *** empty log message *** git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@473 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/bot/bot.cpp | 8 ++- source/src/bot/botmanager.cpp | 30 ++++-------- source/src/client.cpp | 8 ++- source/src/clientextras.cpp | 21 +++----- source/src/clients2c.cpp | 42 ++++++++-------- source/src/command.cpp | 2 +- source/src/cube.h | 3 +- source/src/editing.cpp | 6 +-- source/src/entities.cpp | 2 +- source/src/main.cpp | 7 --- source/src/protos.h | 13 +++-- source/src/rendercubes.cpp | 43 +++++----------- source/src/rendergl.cpp | 15 +++--- source/src/server.cpp | 92 +++++++++++++++-------------------- source/src/serverbrowser.cpp | 16 +++--- source/src/serverms.cpp | 6 +-- source/src/serverutil.cpp | 82 +++++++++++++++++++++++-------- source/src/tools.cpp | 2 +- source/src/tools.h | 16 +++--- source/src/world.cpp | 4 +- source/src/worldio.cpp | 2 +- source/src/worldlight.cpp | 8 ++- 22 files changed, 210 insertions(+), 218 deletions(-) diff --git a/source/src/bot/bot.cpp b/source/src/bot/bot.cpp index c218317f..88af1361 100644 --- a/source/src/bot/bot.cpp +++ b/source/src/bot/bot.cpp @@ -311,8 +311,7 @@ void CBot::SendBotInfo() m_pMyEnt->state)<<5); ENetPacket *packet = enet_packet_create (NULL, MAXTRANS, 0); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_BOTUPDATE); putint(p, BotManager.GetBotIndex(m_pMyEnt)); @@ -340,9 +339,8 @@ void CBot::SendBotInfo() }; - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); - incomingdemodata(start, p-start, true); + enet_packet_resize(packet, p.length()); + incomingdemodata(p.buf, p.length(), true); if(clienthost) { enet_host_broadcast(clienthost, 0, packet); diff --git a/source/src/bot/botmanager.cpp b/source/src/bot/botmanager.cpp index 098704b1..3228ef61 100644 --- a/source/src/bot/botmanager.cpp +++ b/source/src/bot/botmanager.cpp @@ -1021,16 +1021,14 @@ void addbot(char *arg1, char *arg2, char *arg3) } ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_BOTCOMMAND); putint(p, int(COMMAND_ADDBOT)); // Bot command type putint(p, 1); // Bot count putint(p, skill); // Bot skill sendstring(team, p); // Bot team sendstring(name, p); // Bot name - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); } } @@ -1071,16 +1069,14 @@ void addnbot(char *arg1, char *arg2, char *arg3) } ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_BOTCOMMAND); putint(p, int(COMMAND_ADDBOT)); // Bot command type putint(p, i); // Bot count putint(p, skill); // Bot skill sendstring(team, p); // Bot team sendstring(name, p); // Bot name - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); } } @@ -1166,16 +1162,14 @@ void kickbot(const char *szName) else if (clienthost) // Ask server to destroy bot(with voting) { ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_BOTCOMMAND); putint(p, int(COMMAND_KICKBOT)); // Bot command type putint(p, 1); // 0==kick all bots, 1==kick one specific bot char tmp[20]; strcpy(tmp, szName); sendstring(tmp, p); // Bot name - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); } } @@ -1202,13 +1196,11 @@ void kickallbots(void) { ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_BOTCOMMAND); putint(p, int(COMMAND_KICKBOT)); // Bot command type putint(p, 0); // 0==kick all bots, 1==kick one specific bot - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); } } @@ -1294,13 +1286,11 @@ void botskill(char *bot, char *skill) else if (clienthost) // ask server to change botskill { ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_BOTCOMMAND); putint(p, int(COMMAND_BOTSKILL)); // Bot command type putint(p, SkillNr); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); } } diff --git a/source/src/client.cpp b/source/src/client.cpp index d70c3084..e2f8f6ed 100644 --- a/source/src/client.cpp +++ b/source/src/client.cpp @@ -222,8 +222,7 @@ void c2sinfo(dynent *d) // send update to the server if(clientnum<0) return; // we haven't had a welcome message from the server yet if(lastmillis-lastupdate<40) return; // don't update faster than 25fps ENetPacket *packet = enet_packet_create (NULL, MAXTRANS, 0); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, MAXTRANS); bool serveriteminitdone = false; if(toserverpwd) { @@ -310,9 +309,8 @@ void c2sinfo(dynent *d) // send update to the server lastping = lastmillis; }; }; - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); - incomingdemodata(start, p-start, true); + enet_packet_resize(packet, p.length()); + incomingdemodata(p.buf, p.length(), true); if(clienthost) { enet_host_broadcast(clienthost, 0, packet); enet_host_flush(clienthost); } else localclienttoserver(packet); lastupdate = lastmillis; diff --git a/source/src/clientextras.cpp b/source/src/clientextras.cpp index 5e8edd71..1aa7dfb0 100644 --- a/source/src/clientextras.cpp +++ b/source/src/clientextras.cpp @@ -178,23 +178,20 @@ void sendmap(char *mapname) uchar *mapdata = readmap(mapname, &mapsize); if(!mapdata) return; ENetPacket *packet = enet_packet_create(NULL, MAXTRANS + mapsize, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_SENDMAP); sendstring(mapname, p); putint(p, mapsize); - if(65535 - (p - start) < mapsize) + if(65535 - p.length() < mapsize) { conoutf("map %s is too large to send", (int)mapname); - free(mapdata); + delete[] mapdata; enet_packet_destroy(packet); return; }; - memcpy(p, mapdata, mapsize); - p += mapsize; - free(mapdata); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + p.put(mapdata, mapsize); + delete[] mapdata; + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); conoutf("sending map %s to server...", mapname); s_sprintfd(msg)("[map %s uploaded to server, \"getmap\" to receive it]", mapname); @@ -204,11 +201,9 @@ void sendmap(char *mapname) void getmap() { ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, MAXTRANS); putint(p, SV_RECVMAP); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); sendpackettoserv(packet); conoutf("requesting map from server..."); } diff --git a/source/src/clients2c.cpp b/source/src/clients2c.cpp index 7eb32fad..0cc6baaa 100644 --- a/source/src/clients2c.cpp +++ b/source/src/clients2c.cpp @@ -26,7 +26,7 @@ void changemap(char *name) // request map change, server ma }; // Added by Rick -void botcommand(uchar *&p, char *text) +void botcommand(ucharbuf &p, char *text) { int type = getint(p); switch(EBotCommands(type)) @@ -34,12 +34,12 @@ void botcommand(uchar *&p, char *text) case COMMAND_ADDBOT: getint(p); getint(p); - sgetstr(); - sgetstr(); + getstring(text, p); + getstring(text, p); break; case COMMAND_KICKBOT: if (getint(p)==1) // Kick a specific bot - sgetstr(); + getstring(text, p); break; case COMMAND_BOTSKILL: getint(p); @@ -81,11 +81,10 @@ extern void trydisconnect(); void localservertoclient(uchar *buf, int len) // processes any updates from the server { - if(ENET_NET_TO_HOST_16(*(ushort *)buf)!=len) neterr("packet length"); incomingdemodata(buf, len); - - uchar *end = buf+len; - uchar *p = buf+2; + + ucharbuf p(buf, len); + char text[MAXTRANS]; int cn = -1, type; dynent *d = NULL; @@ -93,7 +92,7 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th bool c2si = false, killedbybot=false; bool gib=false; - while(pname, &text); break; case SV_MAPCHANGE: - sgetstr(); + getstring(text, p); changemapserv(text, getint(p)); mapchanged = true; break; @@ -187,7 +186,7 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th case SV_INITC2S: // another client either connected or changed name/team { - sgetstr(); + getstring(text, p); if(d->name[0]) // already connected { if(strcmp(d->name, text)) @@ -205,7 +204,7 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th // End add by Rick }; s_strcpy(d->name, text); - sgetstr(); + getstring(text, p); s_strcpy(d->team, text); d->skin = getint(p); d->lifesequence = getint(p); @@ -404,11 +403,16 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th case SV_RECVMAP: { - sgetstr(); + getstring(text, p); conoutf("received map \"%s\" from server, reloading..", &text); int mapsize = getint(p); - writemap(text, mapsize, p); - p += mapsize; + if(p.remaining() < mapsize) + { + p.forceoverread(); + break; + }; + writemap(text, mapsize, &p.buf[p.len]); + p.len += mapsize; changemapserv(text, gamemode); break; }; @@ -421,7 +425,7 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th }; case SV_SERVMSG: - sgetstr(); + getstring(text, p); conoutf("%s", text); break; @@ -511,7 +515,7 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th dynent *b = getbot(getint(p)); if (!b) break; - sgetstr(); + getstring(text, p); if(b->name[0]) // already connected { if(strcmp(b->name, text)) @@ -523,7 +527,7 @@ void localservertoclient(uchar *buf, int len) // processes any updates from th conoutf("connected: %s", &text); }; s_strcpy(b->name, text); - sgetstr(); + getstring(text, p); s_strcpy(b->team, text); b->lifesequence = getint(p); b->bIsBot = true; diff --git a/source/src/command.cpp b/source/src/command.cpp index c545b7b8..5630f9b1 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -266,7 +266,7 @@ bool execfile(char *cfgfile) char *buf = loadfile(path(s), NULL); if(!buf) return false; execute(buf); - free(buf); + delete[] buf; return true; }; diff --git a/source/src/cube.h b/source/src/cube.h index d9c77250..78ead911 100644 --- a/source/src/cube.h +++ b/source/src/cube.h @@ -341,7 +341,7 @@ enum { M_NONE = 0, M_SEARCH, M_HOME, M_ATTACKING, M_PAIN, M_SLEEP, M_AIMING, M_N #define MAXTRANS 5000 // max amount of data to swallow in 1 go #define CUBE_SERVER_PORT 28763 #define CUBE_SERVINFO_PORT 28764 -#define PROTOCOL_VERSION 123 // bump when protocol changes +#define PROTOCOL_VERSION 1123 // bump when protocol changes #define WEAPONCHANGE_TIME 400 @@ -446,7 +446,6 @@ extern bool demoplayback; // Added by Rick (compares 2 vectors) #define vis(v1,v2) ((v1.x==v2.x) && (v1.y==v2.y) && (v1.z==v2.z)) // End add by Rick -#define sgetstr() { char *t = text; int tlen=0; do { *t = getint(p); } while(*t++ && ++tlenxs*undos[i]->ys*sizeof(sqr); - if(t>maxremain) free(undos.remove(i)); + if(t>maxremain) delete undos.remove(i); }; }; @@ -206,7 +206,7 @@ void editundo() if(undos.empty()) { conoutf("nothing more to undo"); return; }; block *p = undos.pop(); blockpaste(*p); - free(p); + freeblock(p); }; block *copybuf = NULL; @@ -214,7 +214,7 @@ block *copybuf = NULL; void copy() { EDITSELMP; - if(copybuf) free(copybuf); + freeblock(copybuf); copybuf = blockcopy(sel); }; diff --git a/source/src/entities.cpp b/source/src/entities.cpp index 2a9cbe59..00179218 100644 --- a/source/src/entities.cpp +++ b/source/src/entities.cpp @@ -241,7 +241,7 @@ void checkitems() }; }; -void putitems(uchar *&p) // puts items in network stream and also spawns them locally +void putitems(ucharbuf &p) // puts items in network stream and also spawns them locally { loopv(ents) if((ents[i].type>=I_CLIPS && ents[i].type<=I_AKIMBO) || ents[i].type==CARROT) { diff --git a/source/src/main.cpp b/source/src/main.cpp index 67ebe2ec..4d297089 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -34,13 +34,6 @@ void fatal(char *s, char *o) // failure exit cleanup(msg); }; -void *alloc(int s) // for some big chunks... most other allocs use the memory pool -{ - void *b = calloc(1,s); - if(!b) fatal("out of memory!"); - return b; -}; - int scr_w = 640; int scr_h = 480; diff --git a/source/src/protos.h b/source/src/protos.h index fca5e0d7..c3773f2b 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -127,6 +127,7 @@ extern void dodynlight(vec &vold, vec &v, int reach, int strength, dynent *owner extern void cleardlights(); extern block *blockcopy(block &b); extern void blockpaste(block &b); +extern void freeblock(block *&b); // worldrender extern void render_world(float vx, float vy, float vh, float changelod, int yaw, int pitch, float widef, int w, int h); @@ -138,7 +139,6 @@ extern int isoccluded(float vx, float vy, float cx, float cy, float csize); // main extern void fatal(char *s, char *o = ""); -extern void *alloc(int s); extern void keyrepeat(bool on); extern void show_out_of_renderloop_progress(float bar1, const char *text1, float bar2 = 0, const char *text2 = NULL); @@ -233,9 +233,12 @@ extern void localconnect(); extern void localdisconnect(); extern void localclienttoserver(struct _ENetPacket *); extern void serverslice(int seconds, unsigned int timeout); -extern void putint(uchar *&p, int n); -extern int getint(uchar *&p); -extern void sendstring(char *t, uchar *&p); +extern void putint(ucharbuf &p, int n); +extern int getint(ucharbuf &p); +extern void putuint(ucharbuf &p, int n); +extern int getuint(ucharbuf &p); +extern void sendstring(const char *t, ucharbuf &p); +extern void getstring(char *t, ucharbuf &p, int len = MAXTRANS); extern void startintermission(); extern void restoreserverstate(vector &ents); extern uchar *retrieveservers(uchar *buf, int buflen); @@ -271,7 +274,7 @@ extern bool intersect(dynent *d, vec &from, vec &to, vec *end = NULL); // entities extern void renderents(); -extern void putitems(uchar *&p); +extern void putitems(ucharbuf &p); extern void realpickup(int n, dynent *d); extern void flagaction(int team, int action); extern void renderentities(); diff --git a/source/src/rendercubes.cpp b/source/src/rendercubes.cpp index 54c54c8a..25e5fb3f 100644 --- a/source/src/rendercubes.cpp +++ b/source/src/rendercubes.cpp @@ -2,9 +2,7 @@ #include "cube.h" -vertex *verts = NULL; -int curvert; -int curmaxverts = 10000; +vector verts; void setarraypointers() { @@ -13,21 +11,11 @@ void setarraypointers() glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), &verts[0].u); }; -void reallocv() -{ - verts = (vertex *)realloc(verts, (curmaxverts *= 2)*sizeof(vertex)); - curmaxverts -= 10; - if(!verts) fatal("no vertex memory!"); - setarraypointers(); -}; - // generating the actual vertices is done dynamically every frame and sits at the // leaves of all these functions, and are part of the cpu bottleneck on really slow // machines, hence the macros. -#define vertcheck() { if(curvert>=curmaxverts) reallocv(); } - -#define vertf(v1, v2, v3, ls, t1, t2) { vertex &v = verts[curvert++]; \ +#define vertf(v1, v2, v3, ls, t1, t2) { vertex &v = verts.add(); \ v.u = t1; v.v = t2; \ v.x = v1; v.y = v2; v.z = v3; \ v.r = ls->r; v.g = ls->g; v.b = ls->b; v.a = 255; }; @@ -47,7 +35,7 @@ void mipstats(int a, int b, int c) { if(showm) conoutf("1x1/2x2/4x4: %d / %d / % COMMAND(showmip, ARG_NONE); -#define stripend() { if(floorstrip || deltastrip) { addstrip(ogltex, firstindex, curvert-firstindex); floorstrip = deltastrip = false; }; }; +#define stripend() { if(floorstrip || deltastrip) { addstrip(ogltex, firstindex, verts.length()-firstindex); floorstrip = deltastrip = false; }; }; void finishstrips() { stripend(); }; sqr sbright, sdark; @@ -55,7 +43,6 @@ VAR(lighterror,1,8,100); void render_flat(int wtex, int x, int y, int size, int h, sqr *l1, sqr *l2, sqr *l3, sqr *l4, bool isceil) // floor/ceil quads { - vertcheck(); if(showm) { l3 = l1 = &sbright; l4 = l2 = &sdark; }; int sx, sy; @@ -72,7 +59,7 @@ void render_flat(int wtex, int x, int y, int size, int h, sqr *l1, sqr *l2, sqr if(first) // start strip here { stripend(); - firstindex = curvert; + firstindex = verts.length(); ogltex = gltex; oh = h; ox = x; @@ -101,16 +88,16 @@ void render_flat(int wtex, int x, int y, int size, int h, sqr *l1, sqr *l2, sqr && abs(ol3g-l3->g)g)b)b)ftex, sx, sy); @@ -201,7 +186,7 @@ void render_2tris(sqr *h, sqr *s, int x1, int y1, int x2, int y2, int x3, int y3 vertf((float)x1, h->floor, (float)y1, l1, xf*x1, yf*y1); vertf((float)x2, h->floor, (float)y2, l2, xf*x2, yf*y2); vertf((float)x3, h->floor, (float)y3, l3, xf*x3, yf*y3); - addstrip(gltex, curvert-3, 3); + addstrip(gltex, verts.length()-3, 3); gltex = lookuptexture(h->ctex, sx, sy); xf = TEXTURESCALE/sx; @@ -210,7 +195,7 @@ void render_2tris(sqr *h, sqr *s, int x1, int y1, int x2, int y2, int x3, int y3 vertf((float)x3, h->ceil, (float)y3, l3, xf*x3, yf*y3); vertf((float)x2, h->ceil, (float)y2, l2, xf*x2, yf*y2); vertf((float)x1, h->ceil, (float)y1, l1, xf*x1, yf*y1); - addstrip(gltex, curvert-3, 3); + addstrip(gltex, verts.length()-3, 3); nquads++; }; @@ -232,7 +217,6 @@ void render_tris(int x, int y, int size, bool topleft, void render_square(int wtex, float floor1, float floor2, float ceil1, float ceil2, int x1, int y1, int x2, int y2, int size, sqr *l1, sqr *l2, bool flip) // wall quads { stripend(); - vertcheck(); if(showm) { l1 = &sbright; l2 = &sdark; }; int sx, sy; @@ -258,7 +242,7 @@ void render_square(int wtex, float floor1, float floor2, float ceil1, float ceil }; nquads++; - addstrip(gltex, curvert-4, 4); + addstrip(gltex, verts.length()-4, 4); }; int wx1, wy1, wx2, wy2; @@ -268,7 +252,6 @@ VARF(waterlevel, -128, -128, 127, if(!noteditmode()) hdr.waterlevel = waterlevel inline void vertw(int v1, float v2, int v3, sqr *c, float t1, float t2, float t) { - vertcheck(); vertf((float)v1, v2-(float)sin(v1*v3*0.1+t)*0.2f, (float)v3, c, t1, t2); }; @@ -317,7 +300,8 @@ int renderwater(float hf) int n = (wy2-wy1-1)/watersubdiv; nquads += n; n = (n+2)*2; - glDrawArrays(GL_TRIANGLE_STRIP, curvert -= n, n); + glDrawArrays(GL_TRIANGLE_STRIP, verts.length()-n, n); + verts.setsizenodelete(verts.length()-n); }; glDisable(GL_BLEND); @@ -348,7 +332,6 @@ void addwaterquad(int x, int y, int size) // update bounding rect that con void resetcubes() { - if(!verts) reallocv(); floorstrip = deltastrip = false; wx1 = -1; nquads = 0; diff --git a/source/src/rendergl.cpp b/source/src/rendergl.cpp index f3528601..40cc9984 100644 --- a/source/src/rendergl.cpp +++ b/source/src/rendergl.cpp @@ -10,8 +10,6 @@ #define GL_RGB_SCALE_EXT GL_RGB_SCALE_ARB #endif -extern int curvert; - bool hasoverbright = false; void purgetextures(); @@ -83,16 +81,16 @@ bool installtex(int tnum, char *texname, int &xs, int &ys, bool clamp, bool high xs = s->w; ys = s->h; while(xs>glmaxtexsize || ys>glmaxtexsize) { xs /= 2; ys /= 2; }; - void *scaledimg = s->pixels; + uchar *scaledimg = (uchar *)s->pixels; if(xs!=s->w) { conoutf("warning: quality loss: scaling %s", texname); // for voodoo cards under linux - scaledimg = alloc(xs*ys*3); + scaledimg = new uchar[xs*ys*3]; gluScaleImage(GL_RGB, s->w, s->h, GL_UNSIGNED_BYTE, s->pixels, xs, ys, GL_UNSIGNED_BYTE, scaledimg); }; int mode = s->format->BitsPerPixel==24 ? GL_RGB : GL_RGBA; if(gluBuild2DMipmaps(GL_TEXTURE_2D, mode, xs, ys, mode, GL_UNSIGNED_BYTE, scaledimg)) fatal("could not build mipmaps"); - if(xs!=s->w) free(scaledimg); + if(xs!=s->w) delete[] scaledimg; SDL_FreeSurface(s); return true; }; @@ -350,8 +348,9 @@ void gl_drawframe(int w, int h, float changelod, float curfps) resetcubes(); - curvert = 0; - strips.setsize(0); + extern vector verts; + verts.setsizenodelete(0); + strips.setsizenodelete(0); render_world(player1->o.x, player1->o.y, player1->o.z, changelod, (int)player1->yaw, (int)player1->pitch, (float)fov, w, h); @@ -414,7 +413,7 @@ void gl_drawframe(int w, int h, float changelod, float curfps) glDisable(GL_TEXTURE_2D); - gl_drawhud(w, h, (int)curfps, nquads, curvert, underwater); + gl_drawhud(w, h, (int)curfps, nquads, verts.length(), underwater); glEnable(GL_CULL_FACE); glEnable(GL_FOG); diff --git a/source/src/server.cpp b/source/src/server.cpp index 8f432346..b266c0f1 100644 --- a/source/src/server.cpp +++ b/source/src/server.cpp @@ -112,12 +112,10 @@ void send(int n, ENetPacket *packet) void send2(bool rel, int cn, int a, int b) { ENetPacket *packet = enet_packet_create(NULL, 32, rel ? ENET_PACKET_FLAG_RELIABLE : 0); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, a); putint(p, b); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); if(cn<0) process(packet, -1); else send(cn, packet); if(packet->referenceCount==0) enet_packet_destroy(packet); @@ -137,16 +135,14 @@ void sendflaginfo(int flag, int action, int cn = -1) { ctfflag &f = ctfflags[flag]; ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_FLAGINFO); putint(p, flag); putint(p, f.state); putint(p, action); if(f.state==CTFF_STOLEN || action==SV_FLAGRETURN) putint(p, f.thief_cn); else if(f.state==CTFF_DROPPED) loopi(3) putint(p, f.pos[i]); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet,p-start); + enet_packet_resize(packet, p.length()); if(cn<0) multicast(packet, -1); else send(cn, packet); if(packet->referenceCount==0) enet_packet_destroy(packet); @@ -167,12 +163,10 @@ void ctfreset(bool send=true) void sendservmsg(char *msg, int client=-1) { ENetPacket *packet = enet_packet_create(NULL, _MAXDEFSTR+10, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_SERVMSG); sendstring(msg, p); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); if(client==-1) multicast(packet, -1); else send(client, packet); if(packet->referenceCount==0) enet_packet_destroy(packet); }; @@ -457,7 +451,7 @@ bool botskillvote(int skill, int sender) return true; }; -void botcommand(uchar *&p, char *text, int sender) +void botcommand(ucharbuf &p, char *text, int sender) { if(!valid_client(sender)) return; int type = getint(p); @@ -468,9 +462,9 @@ void botcommand(uchar *&p, char *text, int sender) string name, team; int count = getint(p); int skill = getint(p); - sgetstr(); + getstring(text, p); strcpy(team, text); - sgetstr(); + getstring(text, p); strcpy(name, text); if (addbotvote(count, team, skill, name, sender)) { @@ -487,7 +481,7 @@ void botcommand(uchar *&p, char *text, int sender) int specific = getint(p); if (specific) - sgetstr(); + getstring(text, p); if (kickbotvote(specific, text, sender)) { @@ -515,13 +509,11 @@ void botcommand(uchar *&p, char *text, int sender) void changemap() { ENetPacket *packet = enet_packet_create(NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_MAPCHANGE); sendstring(smapname, p); putint(p, mode); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet,p-start); + enet_packet_resize(packet, p.length()); multicast(packet, -1); if(packet->referenceCount==0) enet_packet_destroy(packet); }; @@ -595,16 +587,14 @@ void sendmapinfo(int c) if(smapname[0]) { ENetPacket * packet = enet_packet_create (NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_MAPCHANGE); sendstring(smapname, p); putint(p, mode); putint(p, SV_ITEMLIST); loopv(sents) if(sents[i].spawned) putint(p, i); putint(p, -1); - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); send(c, packet); }; }; @@ -624,23 +614,16 @@ void sendmapinfo(int c) int tmp_pos[3]; -void process(ENetPacket * packet, int sender) // sender may be -1 +void process(ENetPacket *packet, int sender) // sender may be -1 { - if(ENET_NET_TO_HOST_16(*(ushort *)packet->data)!=packet->dataLength) - { - disconnect_client(sender, "packet length"); - return; - }; - - uchar *end = packet->data+packet->dataLength; - uchar *p = packet->data+2; + ucharbuf p(packet->data, packet->dataLength); char text[MAXTRANS]; int cn = -1, type; if(serverpassword[0] && sender>=0 && sender<=clients.length() && !clients[sender].isauthed) { int msg = getint(p); - sgetstr(); + getstring(text, p); if(msg != SV_PWD || !text[0] || strcmp(text, serverpassword)) disconnect_client(sender, "wrong password"); else { @@ -650,7 +633,7 @@ void process(ENetPacket * packet, int sender) // sender may be -1 return; }; - while(pend) { if(sender>=0) + if(p.overread()) { if(sender>=0) disconnect_client(sender, "end of packet"); return; }; multicast(packet, sender); }; void send_welcome(int n) { - ENetPacket * packet = enet_packet_create (NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ENetPacket *packet = enet_packet_create (NULL, MAXTRANS, ENET_PACKET_FLAG_RELIABLE); + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_INITS2C); putint(p, n); putint(p, PROTOCOL_VERSION); @@ -894,8 +881,7 @@ void send_welcome(int n) putint(p, -1); }; }; - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + enet_packet_resize(packet, p.length()); send(n, packet); if(smapname[0] && m_ctf_s) loopi(2) sendflaginfo(i, -1, n); // EDIT: AH }; diff --git a/source/src/serverbrowser.cpp b/source/src/serverbrowser.cpp index 418199fe..8d4b160a 100644 --- a/source/src/serverbrowser.cpp +++ b/source/src/serverbrowser.cpp @@ -224,15 +224,14 @@ void pingservers() { ENetBuffer buf; uchar ping[MAXTRANS]; - uchar *p; loopv(servers) { serverinfo &si = servers[i]; if(si.address.host == ENET_HOST_ANY) continue; - p = ping; + ucharbuf p(ping, MAXTRANS); putint(p, lastmillis); buf.data = ping; - buf.dataLength = p - ping; + buf.dataLength = p.length(); enet_socket_send(pingsock, &si.address, &buf, 1); }; lastinfo = lastmillis; @@ -263,28 +262,29 @@ void checkpings() enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE; ENetBuffer buf; ENetAddress addr; - uchar ping[MAXTRANS], *p; + uchar ping[MAXTRANS]; char text[MAXTRANS]; buf.data = ping; buf.dataLength = sizeof(ping); while(enet_socket_wait(pingsock, &events, 0) >= 0 && events) { - if(enet_socket_receive(pingsock, &addr, &buf, 1) <= 0) return; + int len = enet_socket_receive(pingsock, &addr, &buf, 1); + if(len <= 0) return; loopv(servers) { serverinfo &si = servers[i]; if(addr.host == si.address.host) { - p = ping; + ucharbuf p(ping, len); si.ping = lastmillis - getint(p); si.protocol = getint(p); if(si.protocol!=PROTOCOL_VERSION) si.ping = 9998; si.mode = getint(p); si.numplayers = getint(p); si.minremain = getint(p); - sgetstr(); + getstring(text, p); s_strcpy(si.map, text); - sgetstr(); + getstring(text, p); s_strcpy(si.sdesc, text); break; }; diff --git a/source/src/serverms.cpp b/source/src/serverms.cpp index 3581a2d6..721e2d03 100644 --- a/source/src/serverms.cpp +++ b/source/src/serverms.cpp @@ -100,7 +100,7 @@ void serverms(int mode, int numplayers, int minremain, char *smapname, int secon // reply all server info requests ENetBuffer buf; ENetAddress addr; - uchar pong[MAXTRANS], *p; + uchar pong[MAXTRANS]; int len; enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE; buf.data = pong; @@ -109,7 +109,7 @@ void serverms(int mode, int numplayers, int minremain, char *smapname, int secon buf.dataLength = sizeof(pong); len = enet_socket_receive(pongsock, &addr, &buf, 1); if(len < 0) return; - p = &pong[len]; + ucharbuf p(&pong[len], MAXTRANS-len); putint(p, PROTOCOL_VERSION); putint(p, mode); putint(p, numplayers); @@ -119,7 +119,7 @@ void serverms(int mode, int numplayers, int minremain, char *smapname, int secon s_strcat(mname, smapname); sendstring(mname, p); sendstring(serverdesc, p); - buf.dataLength = p - pong; + buf.dataLength = len + p.length(); enet_socket_send(pongsock, &addr, &buf, 1); }; }; diff --git a/source/src/serverutil.cpp b/source/src/serverutil.cpp index 2c1f20c1..2269ad91 100644 --- a/source/src/serverutil.cpp +++ b/source/src/serverutil.cpp @@ -4,28 +4,76 @@ // all network traffic is in 32bit ints, which are then compressed using the following simple scheme (assumes that most values are small). -void putint(uchar *&p, int n) +void putint(ucharbuf &p, int n) { - if(n<128 && n>-127) { *p++ = n; } - else if(n<0x8000 && n>=-0x8000) { *p++ = 0x80; *p++ = n; *p++ = n>>8; } - else { *p++ = 0x81; *p++ = n; *p++ = n>>8; *p++ = n>>16; *p++ = n>>24; }; + if(n<128 && n>-127) p.put(n); + else if(n<0x8000 && n>=-0x8000) { p.put(0x80); p.put(n); p.put(n>>8); } + else { p.put(0x81); p.put(n); p.put(n>>8); p.put(n>>16); p.put(n>>24); }; }; -int getint(uchar *&p) +int getint(ucharbuf &p) { - int c = *((char *)p); - p++; - if(c==-128) { int n = *p++; n |= *((char *)p)<<8; p++; return n;} - else if(c==-127) { int n = *p++; n |= *p++<<8; n |= *p++<<16; return n|(*p++<<24); } + int c = (char)p.get(); + if(c==-128) { int n = p.get(); n |= char(p.get())<<8; return n; } + else if(c==-127) { int n = p.get(); n |= p.get()<<8; n |= p.get()<<16; return n|(p.get()<<24); } else return c; }; -void sendstring(char *t, uchar *&p) +// much smaller encoding for unsigned integers up to 28 bits, but can handle signed +void putuint(ucharbuf &p, int n) +{ + if(n < 0 || n >= (1<<21)) + { + p.put(0x80 | (n & 0x7F)); + p.put(0x80 | ((n >> 7) & 0x7F)); + p.put(0x80 | ((n >> 14) & 0x7F)); + p.put(n >> 21); + } + else if(n < (1<<7)) p.put(n); + else if(n < (1<<14)) + { + p.put(0x80 | (n & 0x7F)); + p.put(n >> 7); + } + else + { + p.put(0x80 | (n & 0x7F)); + p.put(0x80 | ((n >> 7) & 0x7F)); + p.put(n >> 14); + }; +}; + +int getuint(ucharbuf &p) +{ + int n = p.get(); + if(n & 0x80) + { + n += (p.get() << 7) - 0x80; + if(n & (1<<14)) n += (p.get() << 14) - (1<<14); + if(n & (1<<21)) n += (p.get() << 21) - (1<<21); + if(n & (1<<28)) n |= 0xF0000000; + }; + return n; +}; + +void sendstring(const char *t, ucharbuf &p) { while(*t) putint(p, *t++); putint(p, 0); }; +void getstring(char *text, ucharbuf &p, int len) +{ + char *t = text; + do + { + if(t>=&text[len]) { text[len-1] = 0; return; }; + if(!p.remaining()) { *t = 0; return; }; + *t = getint(p); + } + while(*t++); +}; + const char *modenames[] = { "team deathmatch", "coopedit", "deathmatch", "survior", @@ -90,8 +138,8 @@ void sendmaps(int n, string mapname, int mapsize, uchar *mapdata) if(mapsize <= 0 || mapsize > 256*256) return; s_strcpy(copyname, mapname); copysize = mapsize; - if(copydata) free(copydata); - copydata = (uchar *)alloc(mapsize); + DELETEA(copydata); + copydata = new uchar[mapsize]; memcpy(copydata, mapdata, mapsize); } @@ -99,15 +147,12 @@ ENetPacket *recvmap(int n) { if(!copydata) return NULL; ENetPacket *packet = enet_packet_create(NULL, MAXTRANS + copysize, ENET_PACKET_FLAG_RELIABLE); - uchar *start = packet->data; - uchar *p = start+2; + ucharbuf p(packet->data, packet->dataLength); putint(p, SV_RECVMAP); sendstring(copyname, p); putint(p, copysize); - memcpy(p, copydata, copysize); - p += copysize; - *(ushort *)start = ENET_HOST_TO_NET_16(p-start); - enet_packet_resize(packet, p-start); + p.put(copydata, copysize); + enet_packet_resize(packet, p.length()); return packet; } @@ -115,7 +160,6 @@ ENetPacket *recvmap(int n) void localservertoclient(uchar *buf, int len) {}; void fatal(char *s, char *o) { cleanupserver(); printf("servererror: %s\n", s); exit(1); }; -void *alloc(int s) { void *b = calloc(1,s); if(!b) fatal("no memory!"); return b; }; int main(int argc, char* argv[]) // EDIT: AH { diff --git a/source/src/tools.cpp b/source/src/tools.cpp index 038600c4..d7e9d7a6 100644 --- a/source/src/tools.cpp +++ b/source/src/tools.cpp @@ -27,7 +27,7 @@ char *loadfile(char *fn, int *size) fclose(f); if(size_t(len)!=rlen) { - free(buf); + delete[] buf; return NULL; }; if(size!=NULL) *size = len; diff --git a/source/src/tools.h b/source/src/tools.h index 89c9aa60..37eca6c3 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -233,16 +233,6 @@ template struct vector loopi(ulen) if(buf[i]==o) remove(i--); }; - void replacewithlast(const T &o) - { - if(!ulen) return; - loopi(ulen-1) if(buf[i]==o) - { - buf[i] = buf[ulen-1]; - }; - ulen--; - }; - T &insert(int i, const T &e) { add(T()); @@ -439,6 +429,12 @@ struct databuf int remaining() const { return maxlen-len; }; bool overread() const { return flags&OVERREAD; }; bool overwrote() const { return flags&OVERWROTE; }; + + void forceoverread() + { + len = maxlen; + flags |= OVERREAD; + }; }; typedef databuf charbuf; diff --git a/source/src/world.cpp b/source/src/world.cpp index ebb4106d..00fee0b3 100644 --- a/source/src/world.cpp +++ b/source/src/world.cpp @@ -309,7 +309,7 @@ void setupworld(int factor) ssize = 1<<(sfactor = factor); cubicsize = ssize*ssize; mipsize = cubicsize*134/100; - sqr *w = world = (sqr *)alloc(mipsize*sizeof(sqr)); + sqr *w = world = new sqr[mipsize]; loopi(LARGEST_FACTOR*2) { wmip[i] = w; w += cubicsize>>(i*2); }; }; @@ -371,7 +371,7 @@ void empty_world(int factor, bool force) // main empty world creation routine startmap("maps/unnamed"); if(oldworld) { - free(oldworld); + delete[] oldworld; toggleedit(); execfile("config/default_map_settings.cfg"); execute("fullbright 1"); diff --git a/source/src/worldio.cpp b/source/src/worldio.cpp index dbe0f99f..a54632c9 100644 --- a/source/src/worldio.cpp +++ b/source/src/worldio.cpp @@ -283,7 +283,7 @@ void load_world(char *mname) // still supports all map formats that have } }; }; - free(world); + delete[] world; setupworld(hdr.sfactor); c2skeepalive(); char texuse[256]; diff --git a/source/src/worldlight.cpp b/source/src/worldlight.cpp index 026fc9a7..8e42ea88 100644 --- a/source/src/worldlight.cpp +++ b/source/src/worldlight.cpp @@ -167,7 +167,7 @@ void cleardlights() { block *backup = dlights.pop(); blockpaste(*backup); - free(backup); + freeblock(backup); }; }; @@ -196,7 +196,7 @@ void dodynlight(vec &vold, vec &v, int reach, int strength, dynent *owner) block *blockcopy(block &s) { - block *b = (block *)alloc(sizeof(block)+s.xs*s.ys*sizeof(sqr)); + block *b = (block *)new uchar[sizeof(block)+s.xs*s.ys*sizeof(sqr)]; *b = s; sqr *q = (sqr *)(b+1); for(int x = s.x; x