Author Topic: Tracking targets  (Read 41123 times)

Slash

  • Creator of Roguetemple
  • Administrator
  • Rogueliker
  • *****
  • Posts: 1203
  • Karma: +4/-1
    • View Profile
    • Slashie.net
    • Email
Re: Tracking targets
« Reply #30 on: January 02, 2011, 05:19:54 AM »
The Observer pattern is the obvious answer here.

Also, nobody *owns* a pattern... maybe someone must read a bit before replying :)

MrMorley

  • Newcomer
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • Jason's Development Blog
    • Email
Re: Tracking targets
« Reply #31 on: January 02, 2011, 01:15:43 PM »
I don't use boost or smart pointers. I don't like how vague they are. I like to know what I get. Also, I'm puzzled when someone is talking about patterns. So what if some idea or part of the source code is a "pattern"? They don't own these ideas, whoever they are. I don't even care.

Vague?

Boost is basically just the new content for the next version the standard template library (lots of the classes like smart pointers, hash maps and multithreading are being added to the STL for C++0x) and then some.

The smart pointer is pretty well defined. It counts the references to a pointer, and deletes it when no references are left. It's simple to implement your own instead, if you'd rather remove your perceived vagueness. Unless you take issue with templates, in which case....uhm, good luck with that?

And patterns are the ideas. Nobody owns them, just like nobody owns the concept of "chair" but you would still 90% of the time use the basic design of chair when making something to sit on. It tends to be useful to define what works, and definitions mean names.

"Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice." - Christopher Alexander
« Last Edit: January 02, 2011, 01:22:32 PM by MrMorley »
Jason's Development Blog
Nerds who program party hard

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Tracking targets
« Reply #32 on: January 02, 2011, 01:34:19 PM »
Templates are the thing that sucks most in C++ and what I know about the next version of C++ it's going to fail.

scaught

  • Newcomer
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Re: Tracking targets
« Reply #33 on: January 03, 2011, 12:41:31 AM »
Templates are the thing that sucks most in C++ and what I know about the next version of C++ it's going to fail.
I can prove your poorly formed opinion wrong in two snippets of code:

Code: [Select]
#define max( a, b ) ( a > b ? a : b )
Code: [Select]
template< typename T > T max( T const a, T const b ) { return a > b ? a : b; }

Snippet B is vastly superior to Snippet A in every possible way.  This is an unarguable fact.

Templates are a way to increase productivity and decrease bugs when used correctly.  People often misunderstand what code a template could generate and paint themselves into a corner of bloat and poor performance.  C++0x extends the tools available and makes even more available (decltype, lambdas, auto, variadic templates, etc).  (Shame about concepts not being ratified, tho -- that would've been a really nice nice-to-have)  It is as likely to "fail" (how do you even measure that?) as C++98 was.  Or C++ in general.  Or even C.  (each had/has its detractors, claiming it would never catch on.  Hell, the head of the CS dept of the university I attended almost 20 years ago was convinced Modula-2 was going to be 'the next big language'.)

By your bizarre disparaging comments about boost, smart pointers, patterns, etc, it's painfully obvious you're self taught.  You've reached a point in your self-education where you're accomplishing things and think you somehow now know best.  You are wrong.  You need to keep up the learning, even if you continue self-teaching.  Work with other people.  Drop the attitude and drop the assumptions.  Go start learning again.  You'll be a better programmer for it.

willraman

  • Newcomer
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Tracking targets
« Reply #34 on: January 03, 2011, 06:41:34 PM »
In response to the OP. I have a map of vectors that act as containers for ground items. The items exist in a container, always. Either an item container on the character, or a container on the ground.

When the AI node needs an item, the item of interest ID is stored in memory of the character, and the AI state determines what it wants to do with that item and finally there is a an X,Y coordinate pair of where it needs to go. If it needs to, it can quickly open the container to check for the item to make sure it is still at the location or completely forget about the item if it's AI state changes (like it was attacked or decided it is really tired).

Not sure if this is the most efficient means. Yet it works great for when I drop several items in adjacent map squares and each item is considered a stack. As an example in Legacy of a Warlord one of the army members eat his fill, drops the item and then the next guy in line walks on top of the container, picks up the item and eats his fill... then the next guy seeing that all the food was eaten, goes back into food hunt mode shoots out some LOS rays and sees the square next to him has food and walks over to it to repeats the community eating process. Then all his hungry buddies seeing that the item is gone, do the same thing (go into hunt mode), target the item on the tile that was next to the one their original item was at or stop eating and go attack a goblin that wandered to close.

Will

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Tracking targets
« Reply #35 on: January 03, 2011, 10:03:12 PM »
Snippet B is vastly superior to Snippet A in every possible way.  This is an unarguable fact.

Sadly it's something that you can't use in this context. You see, I never use macros and I try as hard as possible to avoid templates.

Quote
Templates are a way to increase productivity and decrease bugs when used correctly.

Isn't everything? Java was supposed to do that and then python.

Slash

  • Creator of Roguetemple
  • Administrator
  • Rogueliker
  • *****
  • Posts: 1203
  • Karma: +4/-1
    • View Profile
    • Slashie.net
    • Email
Re: Tracking targets
« Reply #36 on: January 04, 2011, 12:24:58 AM »
instead of diving into a pointless language flamewar, we could go back to the original subject...

So yeah, have each item store a reference to the npcs who are currently looking at it. When doing the LOS for each NPC (as simple or complex as you want it to be) and finding an item, add such npc to the item's "lookers list". Clean the lists for each item after a "turn" ends (or when it's time for the item to "act", you'll need to add it into your queue, or just an abstract actor in charge of cleaning up all the item's lists). If the item is destroyed, notify all those looking at it..

Should work good enough if not perfect

MrMorley

  • Newcomer
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • Jason's Development Blog
    • Email
Re: Tracking targets
« Reply #37 on: January 04, 2011, 05:16:00 AM »
Aside from making the comment that C# and Java both have generics after it became apparent some level of metaprogramming is required in a strictly typed language...at least for lower level things like linked lists, associated arrays, smart pointers etc.

Uhm yeah, you've basically been given all the options.

The viewed keeps a list, or the viewer keeps a list. If of pointers, both raise segment fault issues without some kind of memory management. If of ids, you have to have implemented some kind of id-obj association system. Also if an id is freed and reallocated the viewed/viewer could be fooled depending on the system.

Alternatively some kind of event system (trigger "obj_disassociated" event and let the event dispatch system handle all which want to know of said event). Best/easiest implemented if you are already using one (and definitely easiest if it's a flexible one).
« Last Edit: January 04, 2011, 05:19:08 AM by MrMorley »
Jason's Development Blog
Nerds who program party hard

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Tracking targets
« Reply #38 on: January 04, 2011, 10:26:44 AM »
instead of diving into a pointless language flamewar

It's not always a flamewar. I find it good to discuss about programming. Over-templatizing is a problem indeed and that's what they try to do in the next version of C++ but I can't help thinking that there is a pressure to follow "modern" programming languages like Python. However nothing has proved Python superior and the same thing happened for Java that was supposed to "fix" all the "problems" C++ had. I think some stuff of STL is nice, but only because basic C++ doesn't have something like std::string. I think templates are quite ok when used as a library. But I like strict type based programming, nothing wrong with that. I've learned to work with types so I don't often get confused with them as beginners do.

MrMorley

  • Newcomer
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • Jason's Development Blog
    • Email
Re: Tracking targets
« Reply #39 on: January 04, 2011, 05:45:53 PM »
I agree that using templates pointlessly is stupid. So is using functional programming pointlessly, using polymorphism pointlessly, you get the idea. However, for low level concepts such as associative arrays, smart pointers, linked lists and so-forth that's a situation where templates are useful and the alternative solutions (such as reimplementing a new linked list class every time you need one with minor changes) are just pointless reinvention of the wheel. Templates pretty much exist is to stop the potential horrors of void pointers...

As for the features C++0x adds, well aside from variadic templates and template aliases they don't all seem that template orientated. Type inference, Lambda functions, constructor delegation, the nullptr constant, strongly typed enumerations, unrestricted unions, a standardised multithreading API and thread local storage, Regular expressions...holy heck I wants it now ;_;

As for the "java didn't remove C++"...well, there is not such thing as a superior language. Programming languages are tools and most have a place somewhere in the world of software where they are better suited than other languages. There is no be-all-and-end-all language.
« Last Edit: January 04, 2011, 08:07:37 PM by MrMorley »
Jason's Development Blog
Nerds who program party hard

willraman

  • Newcomer
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Tracking targets
« Reply #40 on: January 04, 2011, 10:07:33 PM »
What I am trying to understand is why the need to have observed item call back to an observer? Wouldn't the observer have to then tell the item, "Hey I'm not interested in you any more..." if the AI found an item that was closer or better or the AI changed it's mind when it was attacked?

I'm trying to wrap my mind around a scenario of why the observer is better then what I currently use. That is a list that stores the items and returns a not_found (NULL) flag if the item was removed from the list when the AI checks each turn if it should continue moving towards the Item's location.

Will

MrMorley

  • Newcomer
  • Posts: 24
  • Karma: +0/-0
    • View Profile
    • Jason's Development Blog
    • Email
Re: Tracking targets
« Reply #41 on: January 05, 2011, 08:54:09 PM »
If I understand your situation...

Say you have n items removed in a turn, and m items in total being watched in that turn. You are doing m-n needless checks. The observer pattern only alerts items when they need to be alerted, thus you are doing only as much as needed.
Jason's Development Blog
Nerds who program party hard

roguedjack

  • Rogueliker
  • ***
  • Posts: 70
  • Karma: +0/-0
    • View Profile
    • Rogue Survivor blog
    • Email
Re: Tracking targets
« Reply #42 on: January 07, 2011, 10:57:15 AM »
Krice.

Patterns are like standard algorithms.
Patterns help solving DESIGN/PROGRAMMING problems the same way standard algorithms help sove TASKS problems.
Patterns are good because they have been proven to work in practice and you can communicate your ideas more easily with other programmers with just saying the name of the pattern.
Patterns are just common ways to solve problems. Since quite a lot of people ended up using very similar solutions they have been formalized and given names like "Command pattern", "Observer pattern"...
Just like people used some similar data structures that are formalized and given names like "Tree", "List", "Graph"...
Nowadays patterns are part of your average software designer culture. Like "types", "variables", "quick-sort"....

Directing you to a pattern is a way of offering you a solution without writing a little 10 pages essay to explain you the idea in detail and how it is a candidate solution to your problem.
You wouldn't explain the list structure to other programmers over and over again. You just say "you can use a list to store your character items" and point them to the "list" idiom. Same for patterns.

Same with smart pointers. If you keep lots of references with pointers between your objects, smart pointers can help you avoiding crashing the program with invalid pointers. You can do without of course.

If you knew the Observer pattern for instance you wouldn't have to ask your initial question. It is exactly offering a viable, simple and clear solution for your problem.
Just like someone who know the "A-star" algorithm don't have to ask questions like "how can my AI find a path?".

You are perfectly entitled to refuse using or looking at these common solutions. Just know you are probably overlooking a set of tools that can help you fixing your problems.
Most programmers are using them not because they look nice or are fancy but because they work.
Just be aware that a lot of other people have gone through the same problems as you and ended up finding good solutions and are sharing them : Patterns, Algorithms, Data structures, Other languages...

Some solutions are just superior to others, others are superior under some conditions. Most of the time someone else has already solved a problem better than you could ever do ("you" means you, me, everyone).
You either:
a) accept that and trust other programmers experiences.
or
b) try for own thing and see the results.

Most people start doing b) thinking "uh. that new thing is complicated, useless, stupid, overdoing-it. the stuff I already use is fine."
...and after months or years of experience...
"hey, those guys were right! the best solution i found is just like those guys said in the first place! and sometimes they found an even better soltion than mine! duh! i lost a lot of time, but at least I learned! next time i'll listen and havea look at that new thing first!"

Ok. Hope my little easy didn't bore/enraged you. Good luck anyway with whatever solution you choose.
« Last Edit: January 07, 2011, 11:00:11 AM by roguedjack »

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Tracking targets
« Reply #43 on: January 09, 2011, 11:34:51 AM »
Smart pointers are too smart. I like to struggle with old school pointers the proper way.

Bear

  • Rogueliker
  • ***
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: Tracking targets
« Reply #44 on: January 09, 2011, 05:46:06 PM »
Smart pointers are too smart. I like to struggle with old school pointers the proper way.

Heh.  Hey, if that's part of your artistic vision, go for it.  It's like me writing in C because I like the old-fashioned hardcore-ness of C.