Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 365 366 [367] 368 369 ... 796

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

freeformschooler

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5490 on: February 26, 2014, 03:54:03 pm »

Alright, here is a diagram:
Spoiler: diagram (click to show/hide)
The triangle on the right is obtained because rayDirX and rayDirY form a vector which specifies the direction of the ray, which is how we know that it has the same angles as the triangle on the left (which is supposed to be representative of the deltaDistX triangle in your diagram). Now in the triangle on the right, we get that tan(a)=rayDirX/rayDirY (from the definition of the tan function). Since the two triangles have the same angles, we can also get tan(a)=1/B from the triangle on the left. Putting these two equations together gives us rayDirX/rayDirY=1/B, which can be solved for B to give B=rayDirY/rayDirX which is what the formula from that page gives us. Is there anything that I have failed to make clear here?

Thanks SO much that is super helpful. It makes a lot more sense now.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5491 on: February 27, 2014, 08:49:03 am »

Does anyone know how to use D3j, then? ;__;
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5492 on: February 27, 2014, 10:05:38 am »

Can someone look at my Mutant Generator program and help me improve it. As is, it works as intended, but it relies on extremely long lists of if statements and I was wondering if there would be an alternative way to choose random outcomes that would actually save time and look nicer (I realize I could use maps or arrays or whatever, but I don't think that would actually spare me from the tedium). Also general advice on my technique would be nice. I think the code is readable enough with my function and variable names, but I hardly commented it at all.

http://pastebin.com/y3kEy95F
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5493 on: February 27, 2014, 10:39:09 am »

Using arrays really is the only way to reduce your tedium :p
I'd make it have arrays of tuples (<prob> and <result>) and store the sum of all the <prob>s, then generate a random number between 0 and <sum>, then subtract the <prob> each time <sum> > <prob>.
Eg,

mutants = [(1, "taller"), (4, "faster"), (2, "healing"), (3, "nimble")]
sum = 10  # insert_sum_function_here
randomnumber = random(0, sum)
result = ""
for m in mutants:
    if randomnumber > mutants[m][0]:
        sum -= mutants[m][0]
    else:
        result = mutants[m][1]
        break


So when the randomnumber is 5,

(1, "taller")   -> randomnumber = 4
(4, "faster") -> return "faster"


Using variants of the above algorithm, and using arrays for the pick_animal() function will greatly clean up the code.

animals = ["pack wolf", "willsauce", "human", "bear", "rabbit"]
def pick_animal():
   i = random(0, len(animals))
   return animals


BTW, I think you can do 96 <= x <= 99.


ps: I don't remember the Python random number syntax.
« Last Edit: February 27, 2014, 10:42:15 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5494 on: February 27, 2014, 10:56:34 am »

You could save a lot of these if cases by using arrays or dicts. For simple cases, like the animal switch, you could do stuff like this:

Code: [Select]
animals = ['Owl', 'Aliigator', 'Badger', 'Doggy', 'Bear', 'Camel', 'Cat', 'Chicken', 'Chimpanzee', 'Cow', 'Butterfly', 'Donkey', 'Elephant', 'Elk', 'Fox', 'Giraffe', 'Goat', 'Gorilla', 'Groundhog', 'Hippo', 'Horse', 'Jaguar', 'Lion', 'Llama', 'Moose', 'Panda', 'Penguin', 'Pig', 'Platypus', 'Rabbit', 'Raven', 'Reindeer', 'Rhino', 'Sea Lamprey', 'Shark', 'Sheep', 'Swordfish', 'Carp', 'Sturgeon', 'Tiger', 'Turkey', 'Vulture', 'Walrus', 'Wolf', 'Buffalo', 'Raccoon', 'Rat', 'Spider', 'Mosquito', 'Tortoise', 'Buffalo', 'Coyote', 'Lizard', 'Komodo Dragon', 'Dolphin', 'Flamingo']
def pick_animal():
    if (random.uniform(0, 1) < 0.02):
        return input('What is your favorite animal?')
    return animals[random.randint(0, len(animals)-1)]

For weighted random things (such as the gen_mental), you could either do the same as above (and have some entries appear multiple times), or you can do the following:

Code: [Select]
mentals = {
    'Absorbtion': 1,
    'Beguiling': 2,
    'Confusion': 1,
    'Death Field Generation': 1,
    'Density Control (Others)': 1,
    'Devolution': 1,
    'Directional Sense': 1,
    'Empathy': 1,
    'Fear Generation': 2,
    'Force Field Generation': 1,
    'Gamma Eye': 2,
    'Genius Capability': 3,
    'Heightened Brain Talent': 2,
    'Heightened Intelligence': 1,
    'Illusion Generation': 1,
    'Intuition': 2,
    'Levitation': 2,
    'Life Leech': 5,
    'Light Manipulation': 1,
    'Magnetic Control': 1,
    'Make it Up': 1,
    'Mass Mind': 1,
    'Mental Blast': 2,
    'Mental Control': 2,
    'Mental Control of Body': 3,
    'Mental Invisibility': 1,
    'Mental Multiplier': 1,
    'Mental Paralysis': 2,
    'Mental Shield': 2,
    'Molecular Disruption': 1,
    'Molecular Sense': 1,
    'Pick One Mental Mutation': 6,
    'Pick Two Mental Mutations': 3,
    'Planar Opening': 1,
    'Plant Control': 3,
    'Precognition': 1,
    'Psychometry': 1,
    'Pyro/Cryokinesis': 2,
    'Reflection': 2,
    'Repelling Force': 1,
    'Repulsion Field': 1,
    'Stunning Force': 2,
    'Summoning': 3,
    'Symbiotic Attachment': 1,
    'Telekinetic Arm': 1,
    'Telekinetic Flight': 1,
    'Telepathy': 1,
    'Teleport Object': 2,
    'Teleportation': 1,
    'Temporal Fugue': 1,
    'Thou Hath Telekinesis': 3,
    'Thought Imitation': 1,
    'Time Distortion': 2,
    'Time Manipulation': 1,
    'Time Phasing': 1,
    'Total Healing': 1,
    'Weather Manipulation': 1,
    'Will Force': 1,
}
sumMentalWeights = sum(mentals.values())

def gen_mental(x):
    target = random.uniform(0, sumMentalWeights)
    for attr, weight in mentals.items():
        target -= weight
        if (target < 0):
            return attr

EDIT: Ninja'd, but my code is cleaner :P
Logged

lemon10

  • Bay Watcher
  • Citrus Master
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5495 on: February 27, 2014, 01:17:30 pm »

For gen_ability(), I would probably use:
ability=d6a + d6b + d6c + d6d - min(d6a, d6b, d6c, dcd)

Instead of all the if's and such you did use.
Logged
And with a mighty leap, the evil Conservative flies through the window, escaping our heroes once again!
Because the solution to not being able to control your dakka is MOAR DAKKA.

That's it. We've finally crossed over and become the nation of Da Orky Boyz.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5496 on: February 27, 2014, 02:05:13 pm »

Sacrificing a little bit of readability:

ability = reduce(operator.add, sorted([random.randint(1, 6) for i in range(4)])[1:])

I'm using Python 2.7. If you're using 3, from functools import reduce. The good thing about this is that it can easily be separated out into several statements if desired.

get_modifier looks like a modification of D&D attribute modifiers. You could probably replace it with a combination of %3 and floor or ceiling. replace it with (10-score)/3, which can handle 0, negatives (probably less than useful), and arbitrarily large scores.
« Last Edit: February 27, 2014, 02:21:57 pm by Mephisto »
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5497 on: February 27, 2014, 03:29:56 pm »

The system for attributes (along with everything else) comes from Third Edition Gamma World, which has specific rules for stupidly large or high scores.

Thanks a lot, guys.
Logged

Singularity125

  • Bay Watcher
  • [GAMING INTENSIFIES]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5498 on: February 28, 2014, 11:39:28 am »

Over the span of a week or so, I finally read through this thread. Although there seems to be no real mention of Boo here. Disappointed... I'm thinking of trying to make a roguelike using Boo. Perhaps once I figure it out I can do tutorials on it? It's relatively niche and not well known, but I think there's support for it in Unity, so there's that. :P
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5499 on: February 28, 2014, 12:35:47 pm »

There's a Roguelike Development Megathread in OG, you could try searching for that. It mentions Boo in the OP, I'm fairly sure.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Singularity125

  • Bay Watcher
  • [GAMING INTENSIFIES]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5500 on: February 28, 2014, 02:49:43 pm »

Yeah, I know of that thread, but it hadn't been posted in in a while. The search tells me november of 2013. And Boo itself only has three mentions in the whole thread, all of them from 2009. No real tutorials either. Even if there were it's a language in active development and has definitely changed since then.

*shrug* I'll just strike out on my own and see if I can't figure something out. And then, if anyone is interested, make a tutorial about it.
Logged

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5501 on: February 28, 2014, 03:42:22 pm »

I find myself in need of a good, free XML editor. I have a file format I need to reverse engineer, and there are too many tags for me to make sense of unaided. Suggestions?
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

gnome42

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5502 on: February 28, 2014, 04:07:40 pm »

Over the span of a week or so, I finally read through this thread. Although there seems to be no real mention of Boo here. Disappointed... I'm thinking of trying to make a roguelike using Boo. Perhaps once I figure it out I can do tutorials on it? It's relatively niche and not well known, but I think there's support for it in Unity, so there's that. :P

<old man mode>  In my day we had to carefully carve out the holes on a blank, unmarked punch card with a spoon sharpened on the concrete floors we slept on.  You and your new fangled IDEs  Get off my lawn.</old man mode>

Enough tongue-in-cheek ranting.  But there is a point behind it. 
Context sensitive editors, IDEs, things that go beep when you type something with the 'a' and 's' switched are great.  Having the editor point out when you have managed to treat an integer like a pointer and it is really a bad idea to do this, can save you from the sillier mistakes ( which everyone will make anyway, it is just a case of when it is caught )
But you will never really learn a language that way.  Or the more interesting things you can get away with. 
Syntax and color highlighting are nice.  ( heck I use them when available ), but they will make it harder to really learn what you are doing.
They are a tool. 
Unfortunately, they tend to also become a crutch.

Gnome.

lamenting that colleges seem to have stopped producing Computer Science majors, and seem to be only producing code grinders.
Logged

gnome42

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5503 on: February 28, 2014, 04:16:42 pm »

I find myself in need of a good, free XML editor. I have a file format I need to reverse engineer, and there are too many tags for me to make sense of unaided. Suggestions?

Google and the search phrase "XML reverse engineering"?
many of the links refer to generating the XSD, but that is essentially what you are trying to do.
http://www.dotkam.com/2008/05/28/generate-xsd-from-xml/    seems somewhat close.
https://www.myeclipseide.com/PNphpBB2-viewtopic-t-28559.html   talks about an eclipse plugin for doing so.

The fun way of course would be to beat the snot out of the source of one of the numerous free XML parser libraries to dump the tag/sub-tag relationships from any given chunk of XML text in a nice format that you like. 
Or write one. 
In Befunge.




gnome
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5504 on: February 28, 2014, 04:58:30 pm »


The field is full of older generations lamenting the behavior of the newer generations. Frankly, it's stupid. Insulting everyone who doesn't do things like you want isn't a great way to go about it.

It's an oft-repeated "joke", but you've never written Java without an IDE, have you? I have, and I would prefer an IDE that takes care of the lines and lines of boilerplate than have to remember all that shit myself.
Logged
Pages: 1 ... 365 366 [367] 368 369 ... 796