Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 623 624 [625] 626 627 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 909189 times)

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9360 on: April 16, 2016, 06:41:50 pm »

I'd recommend using a quadtree* arrangement. Sort into 4 "buckets" based on the highest bit, then in each of those buckets, sort into 4 more buckets based on the next highest bit. Obviously, this needs to be a sparse tree, either lock it off at some specific level, and do a linear comparision of things in a region, or have a dynamic scenario where buckets are added or deleted depending on whether there's anything down that branch.

* for 2D data. To store sorted 1D data, it's a binary tree (e.g., a heap. but heap balancing won't work in 2D), and for 3D it's an octree. Either way it's largely the same algorithm.
« Last Edit: April 16, 2016, 06:49:01 pm by Reelya »
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #9361 on: April 16, 2016, 07:18:22 pm »

You can't use a hash to store coordinates (or any other data for that matter) because different inputs can result in the same hash.  You can start with a value and get its hash, and thus compare if a hash and value are equivalent.  But you can't start with a hash and walk backwards to get the value that created it.
The point wasn't to only store the hash, it was to use it as comparison criteria for a tree structure. The object itself stores its own coordinate, as well as the coordinate's hash.

I'd recommend using a quadtree* arrangement. Sort into 4 "buckets" based on the highest bit, then in each of those buckets, sort into 4 more buckets based on the next highest bit. Obviously, this needs to be a sparse tree, either lock it off at some specific level, and do a linear comparision of things in a region, or have a dynamic scenario where buckets are added or deleted depending on whether there's anything down that branch.

* for 2D data. To store sorted 1D data, it's a binary tree (e.g., a heap. but heap balancing won't work in 2D), and for 3D it's an octree. Either way it's largely the same algorithm.
Wow, that's not something I thought of until now. I guess that would definitely prevent any collisions between coordinates that aren't actually equivalent. I'll need to think of how I could implement that.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Emma

  • Bay Watcher
  • Romace plots aren't actually that bad
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9362 on: April 16, 2016, 11:33:39 pm »

I've decided that it's time for me to learn either C or C++ as well as Python. Wish me luck! And if any of you have tips then I'd be grateful to receive them.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9363 on: April 17, 2016, 01:35:08 am »

I'm a C++ guy myself, so feel free to shoot any questions you have my way.

As for where to start, I'm thinking the best place to start is in how you arrange your .cpp and .h files. You should think of the .h files as a table of contents and the .cpp files hold the actual stuff. The .h files are an index of what resources are available, and each resource is defined inside a single .cpp module.

It helps to know how compilation actually works:

 What happens is that each .cpp file becomes one module, and any #include statements are removed, and replaced by the contents of the text file you #included. It's literal cut and paste, so there's no deeper semantics at work here. This can cause problems when you #include the same .h file in two .cpp files. Anything in the .h file is replicated, so if you have a variable "int money" in a .h file, then each .cpp file now has a separate variable called "int money", as far as the compiler is concerned. And since two global variables cannot have the same name, this causes a compiler error. Similar with functions bodies in a .h file, they are replicated for each .cpp file that references them. To get around this, you add extern to variable names in a .h file, and have the actual variable itself in a .cpp file. extern just means that it's an reference to a variable which exists in a different module.

extern int money;

For functions, you just make the function header in the .h file, then make the full thing somewhere else. This also speeds up compilation, because any change to a .cpp file only recompiles that one .cpp file, whereas changes to a .h file affect all .cpp files which reference it. So you want to localize anything you might work on.

Another thing is for book-keeping. Set up your header files like this:

#ifndef WHATEVA
#define WHATEVA
//
// actual .h stuff goes in the middle
//
#endif

WHATEVA should be different in each .h file. This tells the compiler to skip the code if it's already been included in that one .cpp module. e.g. if you had a vector library that was #included by many other .h files, then it might get added in two or more times for the same module, which also causes an error. The #define macros prevent that ever being an issue.
« Last Edit: April 17, 2016, 02:02:21 am by Reelya »
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9364 on: April 17, 2016, 01:58:01 am »

C or C++ as well as Python.
Cython all the way
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9365 on: April 17, 2016, 02:04:05 am »

What about Jython and Lython?

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9366 on: April 17, 2016, 02:07:15 am »

IronPython's a thing too

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9367 on: April 17, 2016, 02:30:25 am »

Most of those don't have a noticeable increase in performance AFAIK so I don't care about them
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9368 on: April 17, 2016, 02:36:54 am »

you can write kerbal space program mods in ironpython

i have actually done it

EDIT: wait no i didn't lol (it was Boo) but it is possible afaik

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9369 on: April 17, 2016, 02:41:36 am »

Oh that's cool, actually
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9370 on: April 18, 2016, 03:09:19 pm »

It helps to know how compilation actually works: ...
Technically that's linking.

Compiling, IIRC, is syntax checking and converting each file into assembly instructions. Then that's assembled into binary. Linking comes after that, copy-pasting the binaries.
« Last Edit: April 18, 2016, 03:12:20 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9371 on: April 18, 2016, 05:29:15 pm »

The #include part, which my post is mainly about, is the pre-processor, it's the input into the compiler. Keywords like extern do actually change what's spat out of the compiler stage, so that's the mere set-up for the later linking stage, it's not the linking itself. And the tips about inter-file dependencies, which cab speed up compilation, do actually speed up the compilation stage, they don't speed up the linking stage at all.

And the second part of the post:

#ifndef WHATEVA
#define WHATEVA
// header file here
#endif

... that's also the preprocessor, which is normally lumped in with the compiler, not linker.
« Last Edit: April 18, 2016, 05:36:20 pm by Reelya »
Logged

Emma

  • Bay Watcher
  • Romace plots aren't actually that bad
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9372 on: April 18, 2016, 06:53:01 pm »

This is probably going to sound stupid but if I'm creating a constant variable in C++ should I use:

const int example = 5

Or should I use:

#define example 5
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9373 on: April 18, 2016, 07:03:10 pm »

In C++ you'd want to use const in almost all cases.  #define is a relic from before C supported true constant variables, and C++ was originally derived from C.

C probably supports the const keyword these days too, but I'm not sure.

To expand on why, #define can do a lot of things you don't expect it to.  It largely behaves as a text replace in your code, so if you did this:

Code: [Select]
#define something 5

int somethingElse = 5;

You'd get a compilation error.  Why?  Because the compiler would see this:

Code: [Select]
int 5else = 5;

So it's annoying and bad for that reason.  My guess is that this is why traditionally programmers make #defined things in UPPER_CASE, but you can still have this occur if you're not careful.  Another thing is that if you use const modifiers on your variables, you can inspect them in debuggers as real variables.  The values will always be what you defined them as, but a lot of debuggers won't tell you what a #defined value is.
« Last Edit: April 18, 2016, 07:14:26 pm by Telgin »
Logged
Through pain, I find wisdom.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9374 on: April 18, 2016, 07:55:15 pm »

const also allows indirection, whereas a #define does not:

"header.h"
extern const int aValue;

So it's just better all around as your programs get more complex
Pages: 1 ... 623 624 [625] 626 627 ... 796