Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 509 510 [511] 512 513 ... 796

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

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7650 on: August 11, 2015, 06:01:12 pm »

I finally got that done. I did most of the replacing with my own, uhm, program (which went through every token once and asked me whether to replace it with what) and then I needed to correct some errors I had made while replacing. That correction I did with regexxer.
The regexp I used for that is the following:
Code: [Select]
((?<=^)|(?<=\s))\Qword-here\E((?=\s)|(?=$))

The program I wrote depends on the poslin code (for regex definitions for the tokens in poslin), cl-ppcre, split-sequence and fset.
Code: [Select]
;;;; poslin-replacer.lisp

(in-package #:poslin-replacer)

(defparameter *poslin-regexes*
  poslin::*parse-order*)

(defun first-tokens (string)
  (mapcar (lambda (regex)
            (cons regex (multiple-value-list (scan regex string))))
          *poslin-regexes*))

(defun first-token (string)
  (let ((min-start (length string))
        (first-token nil))
    (loop for token in (first-tokens string)
       do
         (let ((start (second token)))
           (when (and start (< start min-start))
             (setf min-start start
                   first-token token))))
    (apply #'values
           (rest first-token))))

(defun get-next-token (processed to-process)
  (multiple-value-bind (start end)
      (first-token to-process)
    (if start
        (values (subseq to-process start end)
                (concatenate 'string
                             processed (subseq to-process 0 start))
                (subseq to-process end))
        (values nil processed ""))))

(defun lines (string)
  (split-sequence #\Newline string))

(defun unlines (list-of-strings)
  (if (rest list-of-strings)
      (concatenate 'string
                   (first list-of-strings)
                   #(#\Newline)
                   (unlines (rest list-of-strings)))
      (first list-of-strings)))

(defun cleanup-line (string)
  (let ((length (length string)))
    (if (or (and (>= length 2)
                 (string= " $" (subseq string (- length 2))))
            (and (= length 1)
                 (string= "$" string)))
        ;; this is a bug – imagine a line ending in `$ $`
        (subseq string 0 (- length 2))
        string)))

(defun get-replacement ()
  (let ((string ""))
    (do ((curr (read-line *standard-input*)
               (read-line *standard-input*)))
        ((not (or (scan 'poslin::faulty-string
                        (poslin::cut-matches
                         'poslin::infinite-string
                         (poslin::cut-matches 'poslin::long-string
                                              (concatenate 'string
                                                           string curr
                                                           #(#\Newline)))))
                  (scan 'poslin::infinite-string
                        (concatenate 'string
                                     curr #(#\Newline)))))
         (unlines (mapcar #'cleanup-line
                          (lines (concatenate 'string
                                              string curr)))))
      (setf string
            (concatenate 'string
                         string curr #(#\Newline))))))

(defun replacement-cycle (string)
  (let ((tokens-done (empty-map)))
    (multiple-value-bind (current-token processed to-process)
        (get-next-token "" string)
      (let ((current-token current-token)
            (processed processed)
            (to-process to-process))
        (loop
           (progn
             (if current-token
                 (multiple-value-bind (replacement found?)
                     (@ tokens-done current-token)
                   (if replacement
                       (setf processed
                             (concatenate 'string
                                          processed replacement))
                       (if found?
                           (setf processed
                                 (concatenate 'string
                                              processed
                                              current-token))
                           (if (y-or-n-p "Do you want to replace ~
  `~A`?"
                                         current-token)
                               (let ((replacement (get-replacement)))
                                 (setf processed
                                       (concatenate 'string
                                                    processed
                                                    replacement)
                                       tokens-done
                                       (with tokens-done current-token
                                             replacement)))
                               (setf processed
                                     (concatenate 'string
                                                  processed
                                                  current-token)
                                     tokens-done
                                     (with tokens-done current-token
                                           nil))))))
                 (return processed))
             (multiple-value-bind (nc np nt)
                 (get-next-token processed to-process)
               (setf current-token nc
                     processed np
                     to-process nt))))))))

(defun read-file (file)
  (let ((eof (gensym "eof")))
    (with-open-file (f file)
      (do* ((curr (read-line f nil eof)
                  (read-line f nil eof))
            (collected curr (concatenate 'string
                                         collected #(#\Newline)
                                         (if (eq curr eof)
                                             ""
                                             curr))))
           ((eq curr eof)
            collected)))))

(defun write-file (file object)
  (with-open-file (f file
                     :direction :output)
    (format f "~A"
            object)))

(defun run-replacer (in-file out-file)
  (write-file out-file (replacement-cycle (read-file in-file))))
It allows to replace any token with any block of poslin code. To enter multiple lines as replacement, use ` $` at the end of a line.
This actually contains a bug. The following input
Code: [Select]
$ $
foo $
Should be translated to a replacement that looks exactly like this input, but it will rather wait for the next line and then be translated to this:
Code: [Select]
$
foo
followed by whatever the user inputs afterwards. This specific example would even lead to a read error when the modified file is loaded into poslin.

This is easily avoidable, but I still need to fix this sometime.
Logged
Taste my Paci-Fist

Sheb

  • Bay Watcher
  • You Are An Avatar
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7651 on: August 13, 2015, 05:21:17 am »

So in Python, I have a list of object that have several attributes. Is there an easy way to look up if one of the object's attributes has a given  value other than loading all the attributes themselves to a list and then looking if the attribute is in that list?
Logged

Quote from: Paul-Henry Spaak
Europe consists only of small countries, some of which know it and some of which don’t yet.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7652 on: August 13, 2015, 05:37:12 am »

Something like

getattr(OBJECT, 'attributename')

?

What's wrong with just checking for OBJECT.attrname == something?
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

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7653 on: August 13, 2015, 05:50:19 am »

So in Python, I have a list of object that have several attributes. Is there an easy way to look up if one of the object's attributes has a given  value other than loading all the attributes themselves to a list and then looking if the attribute is in that list?

If they're already in a list, why not do a for-loop? My Python is rusty, but maybe something like this:

Code: [Select]
for x in range(0, len(yourlist)-1)
    if yourlist[x].value == givenValue:
        return True

If there's a practical reason you can't do a for-loop and must instead check only specific items in the list, you'd probably be better off converting to a dictionary.
« Last Edit: August 13, 2015, 05:53:24 am by itisnotlogical »
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7654 on: August 13, 2015, 07:19:53 am »

So in Python, I have a list of object that have several attributes. Is there an easy way to look up if one of the object's attributes has a given  value other than loading all the attributes themselves to a list and then looking if the attribute is in that list?

filter!

Dumb example follows:

Code: [Select]
>>> foo = ['test', 'test2']
>>> filter(lambda x: len(x) == 4, foo)
['thing']

Instead of doing "len(whatever)", you would check the relevant attribute. There is also a version of filter in itertools if you want to play with that. The Python 3 version is also a little different, returning an iterator rather than the result.
« Last Edit: August 13, 2015, 07:22:05 am by Mephisto »
Logged

SealyStar

  • Bay Watcher
  • Gargoyles! Psychics!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7655 on: August 13, 2015, 10:38:04 pm »

For the past week or so, I've been trying to make a basic 2D game engine in Java hate the programmer not the language please. It was going smoothly until today I ran into a huge brick wall: I can't figure out to make sprites render "in front" (from a graphical perspective, on top) of others based on their relative position.

Naturally, this shouldn't be difficult; it's essentially as old as modern gaming itself (what with games from the 80s programmed in assembly pulling it off) and has been so common since then that not even the least experienced programmer would consider it a "trick" anymore. Yet I'm evidently too closed-minded to think of a means to accomplish this.
Logged
I assume it was about cod tendies and an austerity-caused crunch in the supply of good boy points.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7656 on: August 13, 2015, 11:21:24 pm »

Wouldn't you have to draw the stuff under first, then the ones on top later?
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

SealyStar

  • Bay Watcher
  • Gargoyles! Psychics!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7657 on: August 13, 2015, 11:34:02 pm »

Wouldn't you have to draw the stuff under first, then the ones on top later?
Yes. Maybe I should rephrase:

Given that in a game sprites should be able to move, the order to draw them in can and will change; therefore, to create the layered effect the program must be able to somehow determine the vertical position of each sprite and render those further down the screen later, and I can't think of an efficient way of sorting them by vertical position considering they would have to reordered essentially every frame.
Logged
I assume it was about cod tendies and an austerity-caused crunch in the supply of good boy points.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7658 on: August 13, 2015, 11:39:30 pm »

keep things in distinct layers, where you know that one layer is in front of another, but don't worry about the order within the layer.
Logged

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #7659 on: August 13, 2015, 11:49:09 pm »

keep things in distinct layers, where you know that one layer is in front of another, but don't worry about the order within the layer.
^^This.

Background Scenery (can be several layers of this sometimes; parallax is a beautiful thing) > Actors (characters, enemies, projectiles) > Foreground Scenery (including platforms and, for example, shrubs that you want to "hide" your actors with)
Logged

SealyStar

  • Bay Watcher
  • Gargoyles! Psychics!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7660 on: August 13, 2015, 11:49:38 pm »

keep things in distinct layers, where you know that one layer is in front of another, but don't worry about the order within the layer.
...
So, I'm probably at fault here; either I'm not explaining right or I'm misinterpreting these responses, but I'll try to be clear as possible and reduce it to the meat of the problem:

I want sprites to be able to move "up and down" (or "front and back") and I'm looking for a means of sorting them by y-position that's scaleable without turning into a computing nightmare (unlike, say, running a loop and reordering an array/arraylist/whatever of sprites every tick).

I figure there must be another way because, as I said, this technique has been used since literally the '80s, when I'm fairly sure the NES, for example, couldn't have handled it.

Layers would work for a sidescroller where the background is always background, foreground is always foreground, and actual playable area is always actual playable area. But I'm trying for a 3/4-ish view, where, for example, actors and level elements pass in front of or behind each other.
« Last Edit: August 13, 2015, 11:52:13 pm by SealyStar »
Logged
I assume it was about cod tendies and an austerity-caused crunch in the supply of good boy points.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7661 on: August 14, 2015, 12:50:00 am »

Actually, with depth calculations it's actually much more efficient to draw the closest things first, then only draw things that are further away afterwards, but you only draw pixels that haven't been touched before. This ensures only 1 write per pixel per frame. If you order the draw calls from closest to furthest without failure you only need a 1-bit z-buffer (did I write this pixel or not this frame?)
« Last Edit: August 14, 2015, 12:54:23 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 #7662 on: August 14, 2015, 01:35:15 am »

I want sprites to be able to move "up and down" (or "front and back") and I'm looking for a means of sorting them by y-position that's scaleable without turning into a computing nightmare (unlike, say, running a loop and reordering an array/arraylist/whatever of sprites every tick).
y co-ordinates?
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.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7663 on: August 14, 2015, 04:47:39 am »

Sealy, what method or library are you using to draw?
Logged

karhell

  • Bay Watcher
  • [IS_A_MOLPY]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7664 on: August 14, 2015, 04:56:05 am »

Actually, with depth calculations it's actually much more efficient to draw the closest things first, then only draw things that are further away afterwards, but you only draw pixels that haven't been touched before. This ensures only 1 write per pixel per frame. If you order the draw calls from closest to furthest without failure you only need a 1-bit z-buffer (did I write this pixel or not this frame?)
Not sure that works well with sprites, though...

The way I'd handle the problem, is :
- Give each sprite a z-index attribute, and two methods : zUp() and zDown()
- Create a HashMap of Sprite Lists, indexed by z-index (something like HashMap<Integer, List<Sprite>>)
- zUp() and zDown() update the sprite z-index and move it to the appropriate list (zMap.get(this.zIndex).remove(this);this.zIndex++/*(or --, of course)*/;zMap.get(this.zIndex).add(this))

Then when drawing, simply loop on your index array, drawing everything in each sub-array.

Something like that, anyway... My java is pretty rusty >.<
Logged
Pages: 1 ... 509 510 [511] 512 513 ... 796