Author Topic: Working on the AI but need some help with C++  (Read 3809 times)

0 Members and 1 Guest are viewing this topic.

Offline Firgeis

  • 24
Working on the AI but need some help with C++
Pretty self explanatory
Whats the code doing here?:

Assert(objp->type == OBJ_SHIP);

and here whats in the parenthesis?:

#define   AIF_FORMATION_WING   (1 << 0)

 
Working on the AI but need some help with C++
Asserts are double-checks to make sure that something that ought to be true really is. If for some reason it isn't the program should crash with a file, line number and in some cases the function that the assert failed in. Furthermore, in most cases Assert() is controlled by a define (e.g. #define DEBUGMODE) which, when not set, will make the assert function into an empty function, thereby saving processor time in a release version.

 

Offline phreak

  • Gun Phreak
  • 211
  • -1
Working on the AI but need some help with C++
and 1 << 0 is a binary shift to the left.  basically, the shift inserts 0s on the right of the 1, in this case 1 << 0 would be 1, 1 << 1 would be 2 (10 base 2)
Offically approved by Ebola Virus Man :wtf:
phreakscp - gtalk
phreak317#7583 - discord

 

Offline Firgeis

  • 24
Thanks alot for the help, still got some questions, in:

(objp->type == OBJ_SHIP); whats the -> for,? or is it a part of the name of the class/object/function

 

Offline neimad

  • 23
Quote
Originally posted by Firgeis
Thanks alot for the help, still got some questions, in:

(objp->type == OBJ_SHIP); whats the -> for,? or is it a part of the name of the class/object/function


"->" is used to gain access to members of an object/class in C.

In VB/Delphi it would look like this:

if objp.type = OBJ_SHIP then

 

Offline penguin

  • Eudyptes codus
  • 28
  • Still alive.
Quote
Originally posted by Firgeis
Thanks alot for the help, still got some questions, in:

(objp->type == OBJ_SHIP); whats the -> for,? or is it a part of the name of the class/object/function


Umm.  Not to be disrespectful, but you should get a decent book on C, or you'll be hopelessly lost wading through the 300K+ lines of code.  Kernigan and Ritchie wrote the classic "The C Programming Language," and that's a good place to start -- there are other, more recent ones that are probably just as good.

C, and even more so, C++, does not have the friendliest syntax in the world, and making changes without understanding what's happening can be very dangerous (especially with pointer variables, like "objp" in your example).

As has been mentioned already, -> gives you access to the elements of a pointer to a struct or class.  It combines the dereference operator ("*") with the element-of operator ("."), so "(*objp).type" is essentially the same as "objp->type"
your source code slave

 

Offline Firgeis

  • 24
You are probably right Peguin, however  i did a 1 year course of Turbo C (also im programmer of other laguagues, like VB), so everything looked familiar, but well... i dont know alot of object oriented programming in C++ so ill do what i can

 

Offline phreak

  • Gun Phreak
  • 211
  • -1
Quote
Originally posted by neimad


"->" is used to gain access to members of an object/class in C.

In VB/Delphi it would look like this:

if objp.type = OBJ_SHIP then


only if the struct/class/union is a pointer, otherwise its a dot

Code: [Select]


object *pobj;
object obj;

obj.var1=0;
pobj->var1=0;

Offically approved by Ebola Virus Man :wtf:
phreakscp - gtalk
phreak317#7583 - discord

 

Offline CP5670

  • Dr. Evil
  • Global Moderator
  • 212
Working on the AI but need some help with C++
The C/C++ syntax is not very good in my opinion; it requires way too many unnecessary keystrokes and has some really strange stuff assigned to certain operations. (for example, why the heck does = mean "set value to" and == mean "determine equality"?)The problem is that just about every language uses that same syntax, or a very slight variation of it. :p

 

Offline mikhael

  • Back to skool
  • 211
  • Fnord!
    • http://www.google.com/search?q=404error.com
Working on the AI but need some help with C++
Quote
Originally posted by CP5670
The C/C++ syntax is not very good in my opinion; it requires way too many unnecessary keystrokes and has some really strange stuff assigned to certain operations. (for example, why the heck does = mean "set value to" and == mean "determine equality"?)The problem is that just about every language uses that same syntax, or a very slight variation of it. :p


Its like that to avoid overloading the = operator. You could go ahead and overload it, but you set up an ambiguous situation as shown in the following psuedocode:
Code: [Select]

x = 1
y = 2
if x = y:
   doSomethingWhenTrue(x)
else
   doSomethingWhenFalse(x)


What is the value of X? Am I testing the value of X in the if, or am I testing th exit value of an assignment operation? In the first case, I'll execute the false code. In the latter case, i'll execute the true code.

Clarity is a good thing, especially when it only costs one keystroke.
[I am not really here. This post is entirely a figment of your imagination.]

 

Offline CP5670

  • Dr. Evil
  • Global Moderator
  • 212
Working on the AI but need some help with C++
Actually, that part is quite good and as you said, will allow for greater precision (always good ;)); I meant that the actual symbols should be something different. (= should be for testing equality, with >> or := for setting a value, as done in discrete math) The usage of semicolons also seems to be somewhat erratic; they appear after most commands to denote the end of the command, but they don't seem to be used at the ends of functions for some reason. It would also be nice if the braces were replaced with standard brackets, since the braces require a shifted keystroke.
« Last Edit: April 30, 2002, 01:21:33 am by 296 »

 

Offline CobaltStarr

  • <font color=
  • 28
Working on the AI but need some help with C++
standard square brackets "[" & "]" are used for array offsets

Code: [Select]

int array[MAX_ARRAY_SIZE]

array[0] = 256;
array[1] = 512;
...etc...


and if they were used for something else it could cause some abiguities...
HLP's... Ummm... Can I get back to you on that?
__________________

The ULTIMATE computer... The Universe!
"Even in the eternal slience of the void, we are never alone..." - For Setekh
"Reality is just something that doesn't disappear when you stop believing in it..." - Unknown
"Stupidity got us into this mess, why can't it get us out?" - Stolen from Maeglamor's signature

 

Offline neimad

  • 23
Quote
Originally posted by PhReAk
only if the struct/class/union is a pointer, otherwise its a dot


I wasn't far off. :)

I've never actually written or learned any C/C++ in my life, so I was basically just going from what I've seen posted on newsgroups for conversions between C/C++ and Delphi.

 

Offline mikhael

  • Back to skool
  • 211
  • Fnord!
    • http://www.google.com/search?q=404error.com
Working on the AI but need some help with C++
Quote
Originally posted by CP5670
Actually, that part is quite good and as you said, will allow for greater precision (always good ;)); I meant that the actual symbols should be something different. (= should be for testing equality, with >> or := for setting a value, as done in discrete math) The usage of semicolons also seems to be somewhat erratic; they appear after most commands to denote the end of the command, but they don't seem to be used at the ends of functions for some reason. It would also be nice if the braces were replaced with standard brackets, since the braces require a shifted keystroke.



== can be thought of as a back construct. Consider the cases of greater-than-or-equal or less-than-or-equal or not-equal, and 'equal' makes more sense:
Code: [Select]

== (equal)
<= (less than or equal)
>= (greater than or equal)
!= (not equal)


Further, == is easier to type than := or >>. >> is already a bitshifting operator in C and overloaded to a stream operator in C++ (programmers correct me if I'm wrong here).
[I am not really here. This post is entirely a figment of your imagination.]

 
Working on the AI but need some help with C++
Quote
Originally posted by mikhael


Further, == is easier to type than := or >>. >> is already a bitshifting operator in C and overloaded to a stream operator in C++ (programmers correct me if I'm wrong here).


Well, I don't know about you, but I consider the "=" sign to be a testing operator or a statement operator - not an "active" operator like "+" or "/".

Of course, that could stem from the fact that I'm a freakish math major (just starting out).  My first CSC course was in Pascal which used "=" as a boolean testing operator and ":=" as an assignment operator (which seemed quite natural to me.)

I never made the mistake of putting "=" when I meant ":=" - but I can't count the number of times in my Java course that I've had to fix bugs of If statements that return true 'cause I put

if (x = y)
  {
    //insert code here
  } //end if
"It is the year 2000, but where are the flying cars? I was promised flying cars! I don't see any flying cars. Why? Why? Why?" - [size=-2]Avery Brooks from an IBM commercial[/size]

 

Offline CP5670

  • Dr. Evil
  • Global Moderator
  • 212
Working on the AI but need some help with C++
Quote
Of course, that could stem from the fact that I'm a freakish math major (just starting out).


w00t; another math guy! :D What specific stuff are you into? (I'm a calculus nut, especially with the variational and fractional branches ;) :D)

I also tend to think of the equal sign as something that shows that two quantities are equal rather than setting a quantity. In math, equality is denoted by = while definition is shown using a ยบ. This isn't part of the standard character set though, so something else must be used for that (as TurboNed said, Pascal uses :=), but not the normal equal sign. ;)

 
Working on the AI but need some help with C++
Quote
Originally posted by CP5670


w00t; another math guy! :D What specific stuff are you into? (I'm a calculus nut, especially with the variational and fractional branches ;) :D)


Like I said - I'm just starting out.  First year Math Major - not really sure what branch I'm going to go into yet.

Okay, okay - let's get this back on topic.  (-:

  --TurboNed
"It is the year 2000, but where are the flying cars? I was promised flying cars! I don't see any flying cars. Why? Why? Why?" - [size=-2]Avery Brooks from an IBM commercial[/size]