Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3

Author Topic: How safe are traps?  (Read 4255 times)

Starver

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #15 on: September 20, 2017, 07:01:33 pm »

(Hmm, didn't post the first time. Trying again.)

All you need to do is to arrange that every referer that has a pointer to an object gets a record stored in object.referers[] at the time of linking (an easy-to-add part of the linking method, if you insist on doing it that way) then when you're actioning the deletion of the object you have an array of back-references to follow to set their pointers to nil, a special "n/a" object or whatever method you're going to use to protect yourself from link-loss. Just remember also to splice out/neutralise the back-link whenever you remove anything with a forward-reference (or change said FR), so as not to get the same error arising in reverse. But that can be built raw into the associated handling methods.

It's not rocket-science (once you realise which objects you use are liable to such linkings, if you don't apply it as a globally inherited quality) and maybe this is what the shared_ptr thing does (though I suspect that it actually adds overhead only to delay garbage collection until every potential referer has itself been garbage-collected), so maybe use it. Or maybe that just dumbs things down so that even the most rational and wise programmer starts to forget the point of back-pointers through mnemonic abbation.

(I put it much better the first time, I think. Sorry.)
« Last Edit: September 20, 2017, 07:04:25 pm by Starver »
Logged

bloop_bleep

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #16 on: September 20, 2017, 07:05:07 pm »

Yes, that's exactly what shared_ptr does, but it encapsulates all that in a single class that acts like a pointer, so it's easier to use than programming it all from scratch.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Starver

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #17 on: September 20, 2017, 07:36:33 pm »

Yes, that's exactly what shared_ptr does, but it encapsulates all that in a single class that acts like a pointer, so it's easier to use than programming it all from scratch.
Except that programming it from scratch gives you a better understanding. (Or maybe that's just in my own personal programming paradise..)

Indeeed, had a look at its details, and...
Quote
shared_ptr objects can only share ownership by copying their value: If two shared_ptr are constructed (or made) from the same (non-shared_ptr) pointer, they will both be owning the pointer without sharing it, causing potential access problems when one of them releases it (deleting its managed object) and leaving the other pointing to an invalid location.
...which sounds like it doesn't work at all like I'd have long-handedly implemented it, and so maybe I'll just stick to my own manual methods for the foreseeable future.  ;)
Logged

cyberTripping

  • Bay Watcher
  • JPEG Artifactoid
    • View Profile
Re: How safe are traps?
« Reply #18 on: September 20, 2017, 10:13:42 pm »

I guess some followup questions since I can't find specifics on the wiki and need to get creative with defenses:

Will enemies try to dodge cage traps? For instance, could I make a bridge of cage traps so that invaders would attempt to dodge, and instead fall into a pit of spikes several layers down? Or are all the easier dodge-bridge setups now fucked thanks to wear-associated risks?
Logged
Everything's in the news today

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: How safe are traps?
« Reply #19 on: September 20, 2017, 10:24:07 pm »

Nah. Cage traps always capture what they trigger on. Non-trapavoid creatures, dodging dwarves, webbed fbs...

For dodge-strip setup, you could perhaps continue old mode if you managed to get enough training weapons named (through sparring, for instance). Alternatively, you could use ranged dwarves.

I think all other options (besides catapults, I guess), such as minecart persuasion from above, might threaten to hit/interrupt your dwarves if they happen to be there so yeah.

bloop_bleep

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #20 on: September 20, 2017, 10:25:55 pm »

I guess some followup questions since I can't find specifics on the wiki and need to get creative with defenses:

Will enemies try to dodge cage traps? For instance, could I make a bridge of cage traps so that invaders would attempt to dodge, and instead fall into a pit of spikes several layers down? Or are all the easier dodge-bridge setups now fucked thanks to wear-associated risks?
Cage traps can't be dodged. If a creature can be caged it will be caged.

Yes, that's exactly what shared_ptr does, but it encapsulates all that in a single class that acts like a pointer, so it's easier to use than programming it all from scratch.
Except that programming it from scratch gives you a better understanding. (Or maybe that's just in my own personal programming paradise..)

Indeeed, had a look at its details, and...
Quote
shared_ptr objects can only share ownership by copying their value: If two shared_ptr are constructed (or made) from the same (non-shared_ptr) pointer, they will both be owning the pointer without sharing it, causing potential access problems when one of them releases it (deleting its managed object) and leaving the other pointing to an invalid location.
...which sounds like it doesn't work at all like I'd have long-handedly implemented it, and so maybe I'll just stick to my own manual methods for the foreseeable future.  ;)
I read your post again and understand it more. The problem with your solution is it'll still cause crashes, just now it's because you're accessing a null pointer instead of a deallocated pointer. The reason is because when the object representing the trap component is destroyed, other functions are still using it. The solution would be to hold off destroying the object until all references to it are gone, which is what shared_ptr does.

shared_ptr is a wrapper around normal pointers and is designed to behave (externally) like them. shared_ptr has an internal (static-scope) map from the raw pointers to the number of references to them -- when a shared_ptr is created from a pointer, the correspondent number is increased by one, and when a shared_ptr is destroyed, the number is decreased by one. When the number of references reach zero the pointer is deallocated.

Fakeedit: Ninja'd!
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Starver

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #21 on: September 21, 2017, 04:25:48 am »

I read your post again and understand it more. The problem with your solution is it'll still cause crashes, just now it's because you're accessing a null pointer instead of a deallocated pointer.
Some variation on "if (referer.pointer == nul) then { cleanup() && return}" would be the optional way of handling if you nullify. (Also suggested changing the pointer to a global 'safe object' that itself safely absorbs all potentially failing attempts. Perhaps you also arrange to send a SIG back to the callers regiatered in referers[], if you're in a multithreading situation where that's important.)

Quote
The reason is because when the object representing the trap component is destroyed, other functions are still using it.
If the garbage collection won't go to the memory model, the memory model must go to the garbage collection! (Go to == check, naturally, but I've never let a half-decent paraphrase get in the way of accuracy.  ;))  I'd seriously write it all from the ground up in assembler if the compiler doesn't insert basic sanity checks in realloc and free operations. It sounded like the problem was more random pointers spread all around the code (not in current use, in the one-or-maybe-two-threaded standard DF operation, though potentially to be checked any arbitrarily small number of op-cycles later as faux-multitasking switches onto the temperature-check code or whatever. So have everything liable to be temperature checked know from where (likely the master 3d grid array, as with other global checks) such requests are to come from, and at the moment of wear-check loss (perhaps from an all_weapons array, fed to the wearing down function) make sure all other references are neutered in a future-check-friendly way.

For the parallel rendering process (does that even need to pointer onto individual trap components?), fire off a signal to alert it to a change (it won't be the first thing it needs to be signalled about!).

Quote
The solution would be to hold off destroying the object until all references to it are gone, which is what shared_ptr does.

shared_ptr is a wrapper around normal pointers and is designed to behave (externally) like them. shared_ptr has an internal (static-scope) map from the raw pointers to the number of references to them -- when a shared_ptr is created from a pointer, the correspondent number is increased by one, and when a shared_ptr is destroyed, the number is decreased by one. When the number of references reach zero the pointer is deallocated.
I still say that a backpointer array (itself an intrinsic count, but with added functionality to justify the memory cost) would work best. Slightly heavier on memory, but so long as you know it is there, and use it meticulously, it solves so many problems in an exactly user-defined way.

But this is just how I'd do it. I'm a low-level type of coder, and at the same time I have no idea of the entire and fully pre-knotted complexity already invested in DF. It could be that reimplementing things my way would be effectively a whole Development Arc in itself, more comparable to the mythical full multithreading implementation than even the 64bit advances... Still, throwing it out there. You can probably freely criticise some stupid coding error/misstep/assumption of my own when I eventually identifiably unleash my own personal project(s) upon the world. I have several of those in the offing, unencumbered by the team-collaboration and peer review reports that my more professional outputs have gone through... ;)
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #22 on: September 21, 2017, 05:02:50 am »

C doesn't have either garbage collection or any basic safety anywhere. All handling of anything has to be built on top of the high level assembly language (e.g. by using libraries that add some safety). If you want safety built in you should use a high level language that provides checks of usage of null pointers, accessing of arrays outside of their bounds, etc.

This is all irrelevant in this case, however, since DF uses C and, as far as I can see through DFHack, pointers in the structures are naked (i.e. just pointers, not some management objects that takes care of their pointer).

If you're using naked pointers and a cycle approach like DF does, you may consider marking the object as being destroyed to let other operations do what they need to do and disconnect from the object so it can be safely destroyed at the end of the next full cycle. As per the normal C rules, it only works if you remember to add the appropriate handling everywhere it needs to be added.
Logged

bloop_bleep

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #23 on: September 21, 2017, 10:15:12 am »

Oh, I see.

I thought DF was written in C++, IDK why.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

PatrikLundell

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #24 on: September 21, 2017, 12:00:24 pm »

Well, yes, DF is actually written in C++ (which originally was C with a bunch of macros made available. Later those macros were replaced by actual language constructs (allowing for classes, inheritance, polymorphism, etc.)), but that doesn't change the fact that the core is C with its inherent lack of any safety net (which makes the code fast, as no checks are made).

No real programming languages contain garbage collection, with the exception of LISP, as far as I know. Garbage collection is common in script languages (like Java) though.
Logged

bloop_bleep

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #25 on: September 21, 2017, 01:05:48 pm »

Well, yes, DF is actually written in C++ (which originally was C with a bunch of macros made available. Later those macros were replaced by actual language constructs (allowing for classes, inheritance, polymorphism, etc.)), but that doesn't change the fact that the core is C with its inherent lack of any safety net (which makes the code fast, as no checks are made).

No real programming languages contain garbage collection, with the exception of LISP, as far as I know. Garbage collection is common in script languages (like Java) though.
C++ has all sorts of memory management utilities in its standard library (<memory> header, specifically.) C++ doesn't have garbage collection by default, but it does provide tools that programmers can use to implement it themselves. I can understand the difficulty of rewriting existing code to use a new construct, I was just pointing out how this problem would be solved nowadays.

Sorry to OP for the huge derail.
« Last Edit: September 21, 2017, 01:08:38 pm by bloop_bleep »
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: How safe are traps?
« Reply #26 on: September 22, 2017, 11:18:43 am »

No real programming languages contain garbage collection, with the exception of LISP, as far as I know. Garbage collection is common in script languages (like Java) though.
What do you mean Java is a "script language"? C# is basically C++ with garbage collection.
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)?

bloop_bleep

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #27 on: September 22, 2017, 12:44:17 pm »

No real programming languages contain garbage collection, with the exception of LISP, as far as I know. Garbage collection is common in script languages (like Java) though.
What do you mean Java is a "script language"? C# is basically C++ with garbage collection.
Java is compiled to object code, which then is run by the JVM. It's not direct source code -> executable, so it's considered a scripting language.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

PatrikLundell

  • Bay Watcher
    • View Profile
Re: How safe are traps?
« Reply #28 on: September 22, 2017, 05:02:53 pm »

Java is either interpreted directly and/or transformed into byte code (it can do this transformation as it's parsing the script or can do it beforehand, and can probably skip the transformation as well), as bloop_bleep said. Byte code is still essentially interpreted, although rather than parsing text, it's into symbols that are a little more compact than text and thus is slightly less slow, but it doesn't compile it into machine code, as compiled languages are.
I have to admit I'm rather hazy on C#, but I had the impression it was essentially Microsoft's attempt to create its own script competitor to Java.
Logged

anewaname

  • Bay Watcher
  • The mattock... My choice for problem solving.
    • View Profile
Re: How safe are traps?
« Reply #29 on: September 23, 2017, 04:02:14 am »

I have to admit I'm rather hazy on C#, but I had the impression it was essentially Microsoft's attempt to create its own script competitor to Java.
C# is just one of the languages in the set of .NET languages that use the .NET middleware. Whatever it looks like on the outside, under the hood it is goes .NET.

This might not be the right forum group to bring Microsoft's motives into the topic. It reminds me of this post  :p

Logged
Quote from: dragdeler
There is something to be said about, if the stakes are as high, maybe reconsider your certitudes. One has to be aggressively allistic to feel entitled to be able to trust. But it won't happen to me, my bit doesn't count etc etc... Just saying, after my recent experiences I couldn't trust the public if I wanted to. People got their risk assessment neurons rotten and replaced with game theory. Folks walk around like fat turkeys taunting the world to slaughter them.
Pages: 1 [2] 3