|
Posted by metlslime on 2007/08/08 04:57:56 |
This is a counterpart to the "Mapping Help" thread. If you need help with QuakeC coding, or questions about how to do some engine modification, this is the place for you! We've got a few coders here on the forum and hopefully someone knows the answer. |
|
|
OK, As Suspected
#1585 posted by Preach on 2014/09/13 21:35:09
Well, I think I've thought of a not-terrible way to fix up the missing data when a game is saved in this particular case. Must write all this up into a blog post or something though.
Blog Post
#1586 posted by Preach on 2014/09/28 18:16:19
Just in time to make it so that there are blog posts for this month, a new coding article. This week: the Factory Pattern.
http://tomeofpreach.wordpress.com/2014/09/28/the-factory-pattern/
Use it to spawn clones of entities, reset your map cleanly between rounds and ...prevent map hacks? What blasphemy!
#1587 posted by JneeraZ on 2014/09/29 14:08:13
Preach got me interested in Python so I spent time learning how it works and converted my OBJ2MAP C# utility to it. Python is probably going to be my utility language going forward - so nice to work with!
https://dl.dropboxusercontent.com/u/161473/SeptQuake/PythonVictory.jpg
Python Fucking Rules
#1588 posted by Lunaran on 2014/09/29 23:34:40
#1589 posted by RickyT33 on 2014/10/02 12:42:58
Peak Ballmer
CSQC: Talking To The Server
#1591 posted by Qmaster on 2014/10/06 03:31:52
It can sometimes feel convoluted to communicate with CSQC and the server, sending entity data back and forth. I'm going to summarize some things here to help people out, but also so that I can help myself learn about them. When you can teach something, you understand it and have to learn more about it than those you teach.
There are currently three methods that I'm aware of to send data to CSQC:
Console variables, AddStats, and CSQC_Ent_Update
There are currently these methods that I'm aware of to send data from CSQC to the server:
Console variables, SV_ParseClientCommand
Console variables seems fairly straightforward. Advantages: Able to be keymapped, Able to be accessed by the player via the console or config file, simple to code, two way street
Disadvantage: Able to be accessed by the player (cheats!), Only useful for a single variable (floats, strings, vectors)
AddStats is basically the same as a console variable but only visible to CSQC, and automatically updated whenever the variable in server QC is changed. This is great for ammo and health since these are simple float values.
Advantages: Simple to code, auto-updating
Disadvantages: Only useful for a single variable, One-way sending of data to CSQC (CSQC can't change it, only read it)
SV_ParseClientCommand is a beautiful thing. This function is what is called anytime someone types in text into the console and hits [ENTER]. CSQC can call it by using a localcmd ("cmd ",yourvariable.here,yourvariable.here2,"n"); You can actually override all cheats by having void (string cmd) SV_ParseClientCommand = {/*clientcommand(self, cmd);*/};, (note the /**/ commenting out the important part, if you forgot to add this line at the end of your client command function, you may be wondering why all your cheats aren't working...oops! learned that the hard way)
Advantages: Powerful and can be used to send multiple data variables at once from CSQC to server QC
Disadvantages: A bit complicated to set up, one-way from CSQC to server QC.
Finally, CSQC_Ent_Update is the most confusing one of all, but potentially the most powerful way to send data to CSQC. Updating an entity requires the server entity to have float() .SendEntity function that is called by the engine everytime that entity's .Version field is changed. Typically, like this: entity.Version++; The SendEntity function needs to WriteByte (or String or whatever) for each variable it sends. This needs to match each ReadByte (or Readwhatever) in the CSQC_Ent_Update function.
Advantages: Powerful, can send multiple variables at once to CSQC (practically sending an entire entity to CSQC)
Disadvantages: Complicated, confusing, easy to break or make buggy.
The most confusing thing for me is that CSQC_Ent_Update is called...um...shoot. I dunno when it's called. Immediately when the entity's .Version is modified in server QC, i.e. mid-frame? at the end of the server frame does the engine check all entity's versions and then call CSQC_Ent_Updates sequentially? Is Darkplaces's implementation different than FTEQW's?
What is the order anyways? In one frame, is server code carried out first, then CSQC code? Or CSQC first?
Who knows...CSQC isn't that popular so the all the basic concepts for it communicates with QC isn't defined anywherer in so explicit an article. There are snippets here and there but no one place has all the data. Hopefully this brief summary can help someone.
#1592 posted by Spike on 2014/10/06 13:18:26
A couple of things to note.
1: .Version is outdated, you shouldn't use it, instead set self.SendFlags |= 1; or whatever and the engine will tell you what flags were set since the client last received a copy of the entity.
2: CSQC_Ent_Update is called 'whenever'. There is no specific time documented because an update can be lost or queued. Network latency is also a factor. If you depend upon ordering then you loose. If the entity gets removed in ssqc before the message was sent (due to packetloss triggering constant resends) then it can be lost entirely, otherwise packetloss will merely trigger a resend. Not every version is guarenteed to be sent, the server sends when it knows there's something pending and when it feels like it. It has lower latency than reliable messages (as it doesn't depend upon previous reliables to be acked first), but does not guarentee anything but the most recent update (like stats).
3: it is possible to send messages directly to csqc. either via temp entities (dp), or via svc 83 (fte, must be multicast). Beware of illegible server messages, you can try sv_csqcdebug in fte to diagnose these, assuming they're caused by QC.
4: fte has a sendevent builtin that can send csqc->ssqc messages.
5: deltalisten(fte) or getentity(dp or fte) can be used to read various bits of an entity without needing special sending/parsing.
6: using cvars to send stuff to csqc is horrible, and is impossible for csqc->ssqc (no server exploits please).
7: csqc can directly parse centerprints(CSQC_Parse_CenterPrint), stuffcmds(CSQC_Parse_StuffCmd), or sprints/bprints(CSQC_Parse_Print). its the equivelent to SV_ParseClientCommand, but the the other way around.
Brilliant Stuff Spike!
#1593 posted by Qmaster on 2014/10/07 00:24:30
Now if someone had time to update: http://quakewiki.org/wiki/EXT_CSQC#Sending_Data_to_the_Client
:) Maybe when I'm finished with my project in a couple months I'll update that page.
Returning Null...
#1594 posted by necros on 2014/11/01 21:52:33
in c++
So, in java, you might do something like this:
class Foo
{
��int a;
��int b;
��Foo(int x)
��{
����a = x;
����b = x;
��}
}
Foo getFoo(boolean z)
{
��if (z)
����return Foo(5);
��else
����return null;
}
So you'd get a pointer to a new Foo object or not depending on the argument z.��This is nice.
But what about in c++?��The Foo class is tiny, and it would be best to return it by value.��But if we wanted to return null, this isn't possible anymore.
Is the only solution to that really this:
bool getFoo(boolean z, Foo& f)
{
��if (z)
��{
����f = Foo(5)
����return true;
��}
��else
��{
����return false;
��}
}
#1595 posted by necros on 2014/11/01 21:53:10
granted, you could return pointers to new objects on the heap, but then you've got to delete them after which is a pain.
Necros
I shall preface this with a quote by Tony Hoare:
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
Don't abuse null as a special return value indicating that nothing could be returned. It's really bad style (I do it all the time though because it's easy - go figure).
There are several ways to avoid null in this scenario:
1. Provide a method that checks whether a result will be available before you invoke your get method. There is a name for this idiom, but I can't remember it right now. It's basically like this:
bool canFoo(int x);
Foo getFoo(int x);
In getFoo, write an assertion that canFoo is true to detect bugs in debug mode.
However, this has the disadvantage that any logic that determines whether a return value is available may have to be executed twice: Once in canFoo and once in getFoo when the value is computed.
2) Create a default instance of Foo that is always returned when no value is available. This instance might even be able to behave in a sensible way, maybe displaying an error message when some of its methods are called.
3) Return a std::pair<boolean, Foo> where the first element indicates whether a result was available or not. This is basically the same as returning NULL from the method when you use pointers, but it makes the calling code a bit uglier because the syntax to access the elements of std::pair is a bit awkward. I sometimes write my own wrappers instead of std::pair for this.
Personally, I will use of these options in the order I presented them, so 3 is a catch-all that I'll use if 1 and 2 are not feasible. In either case, you put the burden of checking whether a result exists on the caller, but that's no worse than using NULL.
Side note: Some languages such as LUA allow you to return more than one value from a function. I consider this the best solution because you can write something along the lines of
bool valid, Foo foo = bar.getFoo(5);
if (valid) { ... }
#1597 posted by necros on 2014/11/02 01:20:22
I notice you didn't have my own suggestion in your list above... Is there something wrong with passing a reference that you will store the actual result in to the function? I figure, as long as you're careful with your const declarations, there shouldn't be any danger of accidentally mangling anything important.
I Don't Like Out Parameters
And I try to avoid them where I can. That's why. Also if you know that your Foo need not be changed, you should make it const, but you can't with your approach because getFoo cannot take a const reference.
#1599 posted by necros on 2014/11/02 17:46:17
Ah ok, I would use it more for short live temporary things anyway.
Something you'd create on the stack before calling the method, then read what you got back if the method returned true (and ignore it if it was false).
using a pair seems like the least hacky and most efficient way of doing it otherwise.
method #2 means you either need dummy values which you need to know mean the object is garbage or you need to include a boolean value into the class that you can check to see if the data is good or not, but that boolean may not always be relevant.
method #1 is good, but you might be doing a check twice, or some steps needed to check in canFoo() might be used to do the work in getFoo() so at the least you can be repeating some steps.
Anyway, thanks for your input. I am starting to really warm to this language!
Constness
I am a total const nazi. Every variable / parameter that can be const, should be. The same goes for member functions. This is the best feature that C++ has over Java, in my opinion. I would really suggest that you become a const nazi too, because it leads to cleaner code if you give some thought about whether a function can have side effects or not, and functions without side effects are much safer!
You're right about option #2 - it's seldomly applicable, but when it is, it works very well and leads to cleaner code because the caller doesn't have to worry about whether the return value is valid or not.
#1601 posted by Joel B on 2014/11/02 18:35:18
I appreciate languages like Python and Erlang that allow you to naturally return arbitrary tuples from functions. Or that are dynamically typed so that you can just return a special error token from a function that would normally return some other type.
I'd probably go with solution 3, since another gotcha with method #1 is that there's a race window between checking and fetching the data (if the data is mutable).
Only If There Are Concurrent Threads.
But yeah, that's true.
Random Weirdness
#1603 posted by necros on 2014/11/02 22:06:04
Interesting bit...
if you forward declare a class, you can use a pointer.
if you don't include the header for that class, when you call delete on the object, it silently fails to run the destructor.
that had me stumped for a while...
Clang Has A Compiler Warning For This.
Be sure to enable all compiler warnings you can. It will safe you a lot of headaches.
Monster_friend
#1605 posted by madfox on 2014/11/13 00:38:12
I made a zombie friend, that does escort the player and, as long as the player doesn't shoot it, only attacks other.
One thing that strikes me is, that when the zombie has killed a monster, it won't return to the player to follow.
It just stand with the killed corps, and as long here are no enemies, it stays there.
How do I make it follow the player again after killing?
zomby_friend
This Would Be My Approach
#1606 posted by ijed on 2014/11/13 12:52:27
But it will probably require some messing about to get it to work. You could drop this in a repeating function you call in the run and stand frames.
if (self.enemy.health < 0)
{
self.movetarget = "";
};
Basically, if my enemy is dead then set my movetarget (walk destination) to null, which will mean the first entity in the list - the player.
Won't follow other players in coop.
OK
#1607 posted by madfox on 2014/11/14 01:33:38
Out of the blue to ask this perculiar thing without explaining the changed qc.
In ai.qc in Found_Target I placed this part:
/*
Zomby_friend starts fight
*/
// ------------------------------------------------
float() FindMonster =
// ------------------------------------------------
{
local entity beast;
if (self.attack_state != ESCORTING)
return FALSE;
if (self.enemy)
return FALSE;
beast = findradius(self.origin, 1500);
while(beast)
{
if ( (beast.flags & FL_MONSTER) && visible(beast) && beast != self && beast.health > 0)
self.enemy = beast;
beast = beast.chain;
}
if (!self.enemy)
return FALSE;
FoundTarget();
return TRUE;
};
So I think it's the best place, now only puzzling.
Big Ol' Parms Post
#1608 posted by Preach on 2014/12/28 02:32:08
Been a while since I've had a chance to write something up to completion, here's a write-up on the parms mechanism, after the posts in the Mapping Help thread on that topic.
http://tomeofpreach.wordpress.com/2014/12/28/sending-values-between-levels/
To necros: I think that since the article manages to compress the player data into 3 parms values, it should be feasible to use cvars to smuggle that info - scratch1-3 would be enough. So as long as you accept you aren't getting anything like half-life's transition zones that can take other entities with them, a hub system should be feasible.
I guess the other practical things are to use some of the space in the third parameter as an index to select which spawn point the player should transfer to, and working out exactly how to send the command. Probably the best way would be as a single semicolon separated string, setting the scratch variables, then a "map" command in case we haven't visited this map yet, then the "load" command to attempt to load the save game for the map in progress. Probably overlooking something but it sounds solid enough...
#1609 posted by necros on 2014/12/28 04:13:39
oh wow, that's crazy... you are a mad genius. :)
the savegame as hub system does have some other flaws though, for example, say a fiend leaps at you as you go through a hub save/load transition. when you return to the map, that fiend will still be floating in mid air to hit you. :(
I suppose you could designate transition zones as off limits so any monster in the zone when the a hub savegame is loaded would be move away or something.
ehhh, maybe it's better to use that scripting extension that lets you read/write to text files...
|
|
You must be logged in to post in this thread.
|
Website copyright © 2002-2024 John Fitzgibbons. All posts are copyright their respective authors.
|
|