Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 40 41 [42] 43 44 ... 796

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

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #615 on: January 16, 2012, 08:32:44 pm »

The day I figured out how pointers and references worked and realized the implications and possibilities, formerly unfathomable vistas of knowledge exploded within my brain like an atom bomb. It was like touching the programming equivalent of the Monolith or something.
That happened literally a month ago for me.
It's like d-b-d, man. D-B-FUCKING-D.


I actually still have a not-so-old project where I ran into the issue of mobs contained in a map class not being able to read data about the map class itself. This lead into the incredibly clunky solution of certain functions in the mob class having to have the map object passed in as an argument, despite it containing the mob class.
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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #616 on: January 16, 2012, 08:34:18 pm »

Could of used delegates man... I mean it would have been an even more clunky POS, but at least it would have looked so mustik and mayjerk that it would have been justified.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #617 on: January 16, 2012, 08:54:33 pm »

Delegates are just closures right? Those are fun to work with:


Code: [Select]
(defn draw-map [g game-map player]
   (.setColor g Color/WHITE)
   (shadowcast
     #(let [x (+ %1 (:x @player))
            y (+ %2 (:y @player))]
       (draw-tile g (@game-map x y) (+ %1 (/ console-tiles-x 2))
                                    (+ %2 (/ console-tiles-y 2) (- 1))))
    #(let [x (+ %1 (:x @player))
           y (+ %2 (:y @player))]
       (= :wall (:type (@game-map x y))))
   (:sight-range @player)))


That's a piece of clojure code. Shadowcast is a function that does recursive shadow casting. It accepts 3 inputs: A function that is applied to each x/y coordinate in view, a function that tells it which x/y coordinates are blocked, and a value that tells it how far the player/creature/whatever can see. #(...) is clojure shorthand for make a function out of what's in between the parenthesis. %1...%n are the first... the nth input parameters respectively. Let introduces new local variables, which we need since the x and y provided by shadowcast are relative to the start position, so we need to translate them to map coordinates.


Now as you can see, we construct 2 functions that refer to variables present in this function, via (:x @player), (:y @player) and (@game-map x y). But those same variables are not present in the shadowcast function. So we've captured information about the current environment and passed it on to another environment (the shadowcast function). I could've put anything in there though. While testing I used #(println %1 %2), which just prints the provided x and y coordinates to the output. The shadowcast function only needs to do the shadowcasting, it doesn't need to know what we're doing with the coordinates, that's not it's job. I could use the same code to simulate light from a torch or the effect of an explosion.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #618 on: January 16, 2012, 09:06:45 pm »

Pretty fucking sexy... I can imagine putting on my X-ray specs and passing in a different function rather than messy if blocks and all that bullshit.
I like it.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #619 on: January 16, 2012, 10:40:34 pm »

Well, my first attempt at an incredibly barebones AI walking scheme didn't work very well.  I duplicated some effort from a function I forgot I already wrote, the "collision detection" is pretty haphazard, and I need to rewrite part of my screen-drawing scheme so that I won't have to refresh the console every turn cycle.  All of these are fixable I'm sure, but these last couple days, I feel like the mental explosion that was propelling me for two weeks is starting to run out of steam.  The enthusiasm is still there, I just can't seem to wrap my head around stuff as well as I did some days ago.  I keep wanting to go off and play all these games I own.

No wait, just thought of how to do the screen-drawing part.  Back on rails.  Not that this isn't a great excuse for me to learn how to use Curses or Libtcod or whatever.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #620 on: January 16, 2012, 10:44:55 pm »

As a wise man never said, it is better to reinvent the wheel so you know how it works before using somebody else's wheel in your work.

Well, I said it now, so I guess a wise man did say it. :P

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #621 on: January 16, 2012, 10:57:28 pm »

As a wise man never said, it is better to reinvent the wheel so you know how it works before using somebody else's wheel in your work.

Well, I said it now, so I guess a wise man did say it. :P

I'm pretty sure my professors have been saying it for years.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #622 on: January 16, 2012, 11:04:32 pm »

thatsthejoke.jpg

Anyway, basically, it's best to figure out how to do something by attempting to do it yourself first, and then seeing if other people have done it better. If nobody ever reinvented the wheel, it would still be a crudely-cut slab of stone.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #623 on: January 16, 2012, 11:16:54 pm »

Anyway, basically, it's best to figure out how to do something by attempting to do it yourself first, and then seeing if other people have done it better. If nobody ever reinvented the wheel, it would still be a crudely-cut slab of stone.

I used to have a problem with this attitude, until I remembered that programming is a fundamentally individual process.  You have to learn on your own how things work, and two people attempting to produce the same function will not do so the same way.  If you have to learn how the wheel works by reinventing it yourself in order to understand how the bigger stuff works.

Not that it isn't really really frustrating for someone who doesn't know how to make code to be told that.


Anyway, I did indeed solve my display and most of the collision detection problems.  Now I just need to build collision detection for the player-location.  That'll be fun.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Willfor

  • Bay Watcher
  • The great magmaman adventurer. I do it for hugs.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #624 on: January 16, 2012, 11:21:59 pm »

The plus side is that every time you're solving a problem, you're building up copy/paste code that can go into your future projects. And while you have to be careful about copy/pasting code -- and knowing when to use it -- it's something that makes solving those same problems in future iterations of your work much easier. I've been hobby programming for a couple of years, and I've built up a bit of a personal library of code that I use on almost every other flight of fancy I decide to pursue.
Logged
In the wells of livestock vans with shells and garden sands /
Iron mixed with oxygen as per the laws of chemistry and chance /
A shape was roughly human, it was only roughly human /
Apparition eyes / Apparition eyes / Knock, apparition, knock / Eyes, apparition eyes /

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #625 on: January 16, 2012, 11:30:51 pm »

I'm sort of thinking of going one up, forgetting libtcod, and starting my own roguelike library for c#... Libtcod can be damn annoying at times.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #626 on: January 16, 2012, 11:49:52 pm »

There's actually a varient of Wilfor's method that's even better - if you build well encapsulated code (and continue working in the same language), you don't even need to worry about the technicalities of copy paste - you can just re-use the same object.

Wrap it up into a nice gem or library if you keep finding yourself reusing it, you know?

Mind you, sometimes it's nice to have a "solved" problem simply for reference when solving a harder one. Keeping your old code around can be pretty valuable.
Logged

Vactor

  • Bay Watcher
  • ^^ DF 1.0 ^^
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #627 on: January 16, 2012, 11:52:29 pm »

I added in some under the hood stuff to my program, each object can now have a resource generation and a resource absorption script, as well as a selected object that it receives emissions from.  This will set up what I need to have the resource absorption amount a function of the emission level, as well as distance of an emitting object.  I should probably try to figure out a way to have an arbitrary number of emitting objects being absorbed by one, as it is right now it only works the other way around.

Bonus points to anyone who can identify what this is a rendering of:

Spoiler (click to show/hide)
Logged
Wreck of Theseus: My 2D Roguelite Mech Platformer
http://www.bay12forums.com/smf/index.php?topic=141525.0

My AT-ST spore creature http://www.youtube.com/watch?v=0btwvL9CNlA

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #628 on: January 16, 2012, 11:54:39 pm »

I suppose I could learn the curses API and help make a multi-language library by contributing a C/C++ API to said library.

@Vactor: I feel like I should know what that is.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #629 on: January 17, 2012, 02:14:56 am »

Alright, I've just written a program that can generate islands, which produces results like this:
Spoiler (click to show/hide)
Thoughts?
Logged
Pages: 1 ... 40 41 [42] 43 44 ... 796