]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - svvm_cmds.c
Clarify behaviour of random() builtin
[xonotic/darkplaces.git] / svvm_cmds.c
index 5147a324fcf9e5e1adf53d3342b8c1e8fe76fd25..ed5bb5aa27ab90acb476e31515287e503c6957ec 100644 (file)
@@ -229,6 +229,7 @@ const char *vm_sv_extensions[] = {
 "ZQ_PAUSE",
 "DP_RM_CLIPGROUP",
 "DP_QC_FS_SEARCH_PACKFILE",
+"DP_QC_FINDBOX",
 NULL
 //"EXT_CSQC" // not ready yet
 };
@@ -254,7 +255,7 @@ static void VM_SV_setorigin(prvm_prog_t *prog)
                VM_Warning(prog, "setorigin: can not modify world entity\n");
                return;
        }
-       if (e->priv.server->free)
+       if (e->free)
        {
                VM_Warning(prog, "setorigin: can not modify free entity\n");
                return;
@@ -305,7 +306,7 @@ static void VM_SV_setsize(prvm_prog_t *prog)
                VM_Warning(prog, "setsize: can not modify world entity\n");
                return;
        }
-       if (e->priv.server->free)
+       if (e->free)
        {
                VM_Warning(prog, "setsize: can not modify free entity\n");
                return;
@@ -338,7 +339,7 @@ static void VM_SV_setmodel(prvm_prog_t *prog)
                VM_Warning(prog, "setmodel: can not modify world entity\n");
                return;
        }
-       if (e->priv.server->free)
+       if (e->free)
        {
                VM_Warning(prog, "setmodel: can not modify free entity\n");
                return;
@@ -806,7 +807,7 @@ static int VM_SV_newcheckclient(prvm_prog_t *prog, int check)
                // look up the client's edict
                ent = PRVM_EDICT_NUM(i);
                // check if it is to be ignored, but never ignore the one we started on (prevent infinite loop)
-               if (i != check && (ent->priv.server->free || PRVM_serveredictfloat(ent, health) <= 0 || ((int)PRVM_serveredictfloat(ent, flags) & FL_NOTARGET)))
+               if (i != check && (ent->free || PRVM_serveredictfloat(ent, health) <= 0 || ((int)PRVM_serveredictfloat(ent, flags) & FL_NOTARGET)))
                        continue;
                // found a valid client (possibly the same one again)
                break;
@@ -853,7 +854,7 @@ static void VM_SV_checkclient(prvm_prog_t *prog)
 
        // return check if it might be visible
        ent = PRVM_EDICT_NUM(sv.lastcheck);
-       if (ent->priv.server->free || PRVM_serveredictfloat(ent, health) <= 0)
+       if (ent->free || PRVM_serveredictfloat(ent, health) <= 0)
        {
                VM_RETURN_EDICT(prog->edicts);
                return;
@@ -901,7 +902,7 @@ static void VM_SV_checkpvs(prvm_prog_t *prog)
        VectorCopy(PRVM_G_VECTOR(OFS_PARM0), viewpos);
        viewee = PRVM_G_EDICT(OFS_PARM1);
 
-       if(viewee->priv.server->free)
+       if(viewee->free)
        {
                VM_Warning(prog, "checkpvs: can not check free entity\n");
                PRVM_G_FLOAT(OFS_RETURN) = 4;
@@ -1005,7 +1006,7 @@ static void VM_SV_findradius(prvm_prog_t *prog)
        else
                chainfield = prog->fieldoffsets.chain;
        if (chainfield < 0)
-               prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
+               prog->error_cmd("VM_SV_findradius: %s doesnt have the specified chain field !", prog->name);
 
        chain = (prvm_edict_t *)prog->edicts;
 
@@ -1056,6 +1057,50 @@ static void VM_SV_findradius(prvm_prog_t *prog)
        VM_RETURN_EDICT(chain);
 }
 
+/*
+=================
+VM_SV_findbox
+
+Returns a chain of entities that are touching a box (a simpler findradius); supports DP_QC_FINDCHAIN_TOFIELD
+
+findbox (mins, maxs)
+=================
+*/
+static void VM_SV_findbox(prvm_prog_t *prog)
+{
+       prvm_edict_t *chain;
+       int i, numtouchedicts;
+       static prvm_edict_t *touchedicts[MAX_EDICTS];
+       int chainfield;
+
+       VM_SAFEPARMCOUNTRANGE(2, 3, VM_SV_findbox);
+
+       if(prog->argc == 3)
+               chainfield = PRVM_G_INT(OFS_PARM2);
+       else
+               chainfield = prog->fieldoffsets.chain;
+       if (chainfield < 0)
+               prog->error_cmd("VM_SV_findbox: %s doesnt have the specified chain field !", prog->name);
+
+       chain = (prvm_edict_t *)prog->edicts;
+
+       numtouchedicts = SV_EntitiesInBox(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), MAX_EDICTS, touchedicts);
+       if (numtouchedicts > MAX_EDICTS)
+       {
+               // this never happens
+               Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
+               numtouchedicts = MAX_EDICTS;
+       }
+       for (i = 0; i < numtouchedicts; ++i)
+       {
+               prog->xfunction->builtinsprofile++;
+               PRVM_EDICTFIELDEDICT(touchedicts[i], chainfield) = PRVM_EDICT_TO_PROG(chain);
+               chain = touchedicts[i];
+       }
+
+       VM_RETURN_EDICT(chain);
+}
+
 static void VM_SV_precache_sound(prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_SV_precache_sound);
@@ -1096,7 +1141,7 @@ static void VM_SV_walkmove(prvm_prog_t *prog)
                VM_Warning(prog, "walkmove: can not modify world entity\n");
                return;
        }
-       if (ent->priv.server->free)
+       if (ent->free)
        {
                VM_Warning(prog, "walkmove: can not modify free entity\n");
                return;
@@ -1151,14 +1196,19 @@ static void VM_SV_droptofloor(prvm_prog_t *prog)
                VM_Warning(prog, "droptofloor: can not modify world entity\n");
                return;
        }
-       if (ent->priv.server->free)
+       if (ent->free)
        {
                VM_Warning(prog, "droptofloor: can not modify free entity\n");
                return;
        }
 
        VectorCopy (PRVM_serveredictvector(ent, origin), end);
-       end[2] -= 256;
+       if (sv.worldmodel->brush.isq3bsp)
+               end[2] -= 4096;
+       else if (sv.worldmodel->brush.isq2bsp)
+               end[2] -= 128;
+       else
+               end[2] -= 256; // Quake, QuakeWorld
 
        if (sv_gameplayfix_droptofloorstartsolid_nudgetocorrect.integer)
                SV_NudgeOutOfSolid(ent);
@@ -1307,7 +1357,7 @@ static void VM_SV_aim(prvm_prog_t *prog)
                VM_Warning(prog, "aim: can not use world entity\n");
                return;
        }
-       if (ent->priv.server->free)
+       if (ent->free)
        {
                VM_Warning(prog, "aim: can not use free entity\n");
                return;
@@ -1535,7 +1585,7 @@ static void VM_SV_makestatic(prvm_prog_t *prog)
                VM_Warning(prog, "makestatic: can not modify world entity\n");
                return;
        }
-       if (ent->priv.server->free)
+       if (ent->free)
        {
                VM_Warning(prog, "makestatic: can not modify free entity\n");
                return;
@@ -1768,7 +1818,7 @@ static void VM_SV_copyentity(prvm_prog_t *prog)
                VM_Warning(prog, "copyentity: can not read world entity\n");
                return;
        }
-       if (in->priv.server->free)
+       if (in->free)
        {
                VM_Warning(prog, "copyentity: can not read free entity\n");
                return;
@@ -1779,14 +1829,13 @@ static void VM_SV_copyentity(prvm_prog_t *prog)
                VM_Warning(prog, "copyentity: can not modify world entity\n");
                return;
        }
-       if (out->priv.server->free)
+       if (out->free)
        {
                VM_Warning(prog, "copyentity: can not modify free entity\n");
                return;
        }
        memcpy(out->fields.fp, in->fields.fp, prog->entityfields * sizeof(prvm_vec_t));
-       if (VectorCompare(PRVM_serveredictvector(out, absmin), PRVM_serveredictvector(out, absmax)))
-               return;
+
        SV_LinkEdict(out);
 }
 
@@ -2359,7 +2408,7 @@ static void VM_SV_clientcommand(prvm_prog_t *prog)
 
        temp_client = host_client;
        host_client = svs.clients + i;
-       Cmd_ExecuteString(&cmd_serverfromclient, PRVM_G_STRING(OFS_PARM1), src_client, true);
+       Cmd_ExecuteString(cmd_serverfromclient, PRVM_G_STRING(OFS_PARM1), src_client, true);
        host_client = temp_client;
 }
 
@@ -2378,7 +2427,7 @@ static void VM_SV_setattachment(prvm_prog_t *prog)
                VM_Warning(prog, "setattachment: can not modify world entity\n");
                return;
        }
-       if (e->priv.server->free)
+       if (e->free)
        {
                VM_Warning(prog, "setattachment: can not modify free entity\n");
                return;
@@ -2492,7 +2541,7 @@ static int SV_GetTagMatrix (prvm_prog_t *prog, matrix4x4_t *out, prvm_edict_t *e
 
        if (ent == prog->edicts)
                return 1;
-       if (ent->priv.server->free)
+       if (ent->free)
                return 2;
 
        modelindex = (int)PRVM_serveredictfloat(ent, modelindex);
@@ -2561,7 +2610,7 @@ static void VM_SV_gettagindex(prvm_prog_t *prog)
                VM_Warning(prog, "VM_SV_gettagindex(entity #%i): can't affect world entity\n", PRVM_NUM_FOR_EDICT(ent));
                return;
        }
-       if (ent->priv.server->free)
+       if (ent->free)
        {
                VM_Warning(prog, "VM_SV_gettagindex(entity #%i): can't affect free entity\n", PRVM_NUM_FOR_EDICT(ent));
                return;
@@ -2657,7 +2706,7 @@ static void VM_SV_dropclient(prvm_prog_t *prog)
        }
        oldhostclient = host_client;
        host_client = svs.clients + clientnum;
-       SV_DropClient(false);
+       SV_DropClient(false, "Client dropped");
        host_client = oldhostclient;
 }
 
@@ -2730,7 +2779,7 @@ static void VM_SV_setmodelindex(prvm_prog_t *prog)
                VM_Warning(prog, "setmodelindex: can not modify world entity\n");
                return;
        }
-       if (e->priv.server->free)
+       if (e->free)
        {
                VM_Warning(prog, "setmodelindex: can not modify free entity\n");
                return;
@@ -2853,34 +2902,14 @@ static void VM_SV_pointparticles(prvm_prog_t *prog)
 qbool SV_VM_ConsoleCommand (const char *text)
 {
        prvm_prog_t *prog = SVVM_prog;
-       int restorevm_tempstringsbuf_cursize;
-       int save_self;
-       qbool r = false;
-
-       if(!sv.active || !prog || !prog->loaded)
-               return false;
-
-       if (PRVM_serverfunction(ConsoleCmd))
-       {
-               save_self = PRVM_serverglobaledict(self);
-               PRVM_serverglobalfloat(time) = sv.time;
-               restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
-               PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(sv.world.prog->edicts);
-               PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, text);
-               prog->ExecuteProgram(prog, PRVM_serverfunction(ConsoleCmd), "QC function ConsoleCmd is missing");
-               prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
-               PRVM_serverglobaledict(self) = save_self;
-               r = (int) PRVM_G_FLOAT(OFS_RETURN) != 0;
-       }
-       return r;
+       return PRVM_ConsoleCommand(prog, text, &prog->funcoffsets.ConsoleCmd, true, PRVM_EDICT_TO_PROG(sv.world.prog->edicts), sv.time,  !(!sv.active || !prog || !prog->loaded), "QC function ConsoleCmd is missing"); 
 }
 
 // #352 void(string cmdname) registercommand (EXT_CSQC)
 static void VM_SV_registercommand (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_SV_registercmd);
-       if(!Cmd_Exists(&cmd_server, PRVM_G_STRING(OFS_PARM0)))
-               Cmd_AddCommand(CF_SERVER, PRVM_G_STRING(OFS_PARM0), NULL, "console command created by QuakeC");
+       Cmd_AddCommand(CF_SERVER, PRVM_G_STRING(OFS_PARM0), NULL, "console command created by QuakeC");
 }
 
 //PF_setpause,    // void(float pause) setpause        = #531;
@@ -3779,7 +3808,7 @@ NULL,                                                     // #562
 NULL,                                                  // #563
 NULL,                                                  // #564
 NULL,                                                  // #565
-NULL,                                                  // #566
+VM_SV_findbox,                                 // #566 entity(vector mins, vector maxs) findbox = #566; (DP_QC_FINDBOX)
 NULL,                                                  // #567
 NULL,                                                  // #568
 NULL,                                                  // #569